@@ -148,8 +148,8 @@ initCabal verbosity packageDBs repoCtxt comp progdb initFlags = do
148
148
-- Flag acquisition -----------------------------------------------------
149
149
---------------------------------------------------------------------------
150
150
151
- -- | Fill in more details by guessing, discovering, or prompting the
152
- -- user.
151
+ -- | Fill in more details in InitFlags by guessing, discovering, or prompting
152
+ -- the user.
153
153
extendFlags :: InstalledPackageIndex -> SourcePackageDb -> InitFlags -> IO InitFlags
154
154
extendFlags pkgIx sourcePkgDb =
155
155
getSimpleProject
@@ -188,14 +188,6 @@ maybeToFlag = maybe NoFlag Flag
188
188
defaultCabalVersion :: Version
189
189
defaultCabalVersion = mkVersion [1 ,10 ]
190
190
191
- displayCabalVersion :: Version -> String
192
- displayCabalVersion v = case versionNumbers v of
193
- [1 ,10 ] -> " 1.10 (legacy)"
194
- [2 ,0 ] -> " 2.0 (+ support for Backpack, internal sub-libs, '^>=' operator)"
195
- [2 ,2 ] -> " 2.2 (+ support for 'common', 'elif', redundant commas, SPDX)"
196
- [2 ,4 ] -> " 2.4 (+ support for '**' globbing)"
197
- _ -> display v
198
-
199
191
-- | Ask if a simple project with sensible defaults should be created.
200
192
getSimpleProject :: InitFlags -> IO InitFlags
201
193
getSimpleProject flags = do
@@ -215,7 +207,11 @@ getSimpleProject flags = do
215
207
flags { simpleProject = simpleProjFlag }
216
208
217
209
218
- -- | Ask which version of the cabal spec to use.
210
+ -- | Get the version of the cabal spec to use.
211
+ --
212
+ -- The spec version can be specified by the InitFlags cabalVersion field. If
213
+ -- none is specified then the user is prompted to pick from a list of
214
+ -- supported versions (see code below).
219
215
getCabalVersion :: InitFlags -> IO InitFlags
220
216
getCabalVersion flags = do
221
217
cabVer <- return (flagToMaybe $ cabalVersion flags)
@@ -227,6 +223,16 @@ getCabalVersion flags = do
227
223
228
224
return $ flags { cabalVersion = maybeToFlag cabVer }
229
225
226
+ where
227
+ displayCabalVersion :: Version -> String
228
+ displayCabalVersion v = case versionNumbers v of
229
+ [1 ,10 ] -> " 1.10 (legacy)"
230
+ [2 ,0 ] -> " 2.0 (+ support for Backpack, internal sub-libs, '^>=' operator)"
231
+ [2 ,2 ] -> " 2.2 (+ support for 'common', 'elif', redundant commas, SPDX)"
232
+ [2 ,4 ] -> " 2.4 (+ support for '**' globbing)"
233
+ _ -> display v
234
+
235
+
230
236
231
237
-- | Get the package name: use the package directory (supplied, or the current
232
238
-- directory by default) as a guess. It looks at the SourcePackageDb to avoid
@@ -269,7 +275,10 @@ getVersion flags = do
269
275
?>> return v
270
276
return $ flags { version = maybeToFlag v' }
271
277
272
- -- | Choose a license.
278
+ -- | Choose a license for the package.
279
+ --
280
+ -- The license can come from Initflags (license field), if it is not present
281
+ -- then prompt the user from a predefined list of licenses.
273
282
getLicense :: InitFlags -> IO InitFlags
274
283
getLicense flags = do
275
284
lic <- return (flagToMaybe $ license flags)
@@ -324,7 +333,7 @@ getAuthorInfo flags = do
324
333
, email = maybeToFlag authorEmail'
325
334
}
326
335
327
- -- | Prompt for a homepage URL.
336
+ -- | Prompt for a homepage URL for the package .
328
337
getHomepage :: InitFlags -> IO InitFlags
329
338
getHomepage flags = do
330
339
hp <- queryHomepage
@@ -435,7 +444,7 @@ getGenTests flags = do
435
444
(Just True ))
436
445
return $ flags { initializeTestSuite = maybeToFlag genTests }
437
446
438
- -- | Ask for the test root directory.
447
+ -- | Ask for the test suite root directory.
439
448
getTestDir :: InitFlags -> IO InitFlags
440
449
getTestDir flags = do
441
450
dirs <- return (testDirs flags)
@@ -447,7 +456,7 @@ getTestDir flags = do
447
456
448
457
return $ flags { testDirs = dirs }
449
458
450
- -- | Ask for the base language of the package.
459
+ -- | Ask for the Haskell base language of the package.
451
460
getLanguage :: InitFlags -> IO InitFlags
452
461
getLanguage flags = do
453
462
lang <- return (flagToMaybe $ language flags)
@@ -600,6 +609,8 @@ getModulesBuildToolsAndDeps pkgIx flags = do
600
609
, otherExts = exts
601
610
}
602
611
612
+ -- | Given a list of imported modules, retrieve the list of dependencies that
613
+ -- provide those modules.
603
614
importsToDeps :: InitFlags -> [ModuleName ] -> InstalledPackageIndex -> IO [P. Dependency ]
604
615
importsToDeps flags mods pkgIx = do
605
616
@@ -705,8 +716,9 @@ promptStr :: String -> Maybe String -> IO String
705
716
promptStr = promptDefault' Just id
706
717
707
718
-- | Create a yes/no prompt with optional default value.
708
- --
709
- promptYesNo :: String -> Maybe Bool -> IO Bool
719
+ promptYesNo :: String -- ^ prompt message
720
+ -> Maybe Bool -- ^ optional default value
721
+ -> IO Bool
710
722
promptYesNo =
711
723
promptDefault' recogniseYesNo showYesNo
712
724
where
@@ -746,6 +758,8 @@ mkDefPrompt pr def = pr ++ "?" ++ defStr def
746
758
where defStr Nothing = " "
747
759
defStr (Just s) = " [default: " ++ s ++ " ] "
748
760
761
+ -- | Create a prompt from a list of items, where no selected items is
762
+ -- valid and will be represented as a return value of 'Nothing'.
749
763
promptListOptional :: (Text t , Eq t )
750
764
=> String -- ^ prompt
751
765
-> [t ] -- ^ choices
@@ -806,10 +820,18 @@ promptList' displayItem numChoices choices def other = do
806
820
-- File generation ------------------------------------------------------
807
821
---------------------------------------------------------------------------
808
822
823
+ -- | Write the LICENSE file, as specified in the InitFlags license field.
824
+ --
825
+ -- For licences that contain the author's name(s), the values are taken
826
+ -- from the 'authors' field of 'InitFlags', and if not specified will
827
+ -- be the string "???".
828
+ --
829
+ -- If the license type is unknown no license file will be created and
830
+ -- a warning will be raised.
809
831
writeLicense :: InitFlags -> IO ()
810
832
writeLicense flags = do
811
833
message flags " \n Generating LICENSE..."
812
- year <- show <$> getYear
834
+ year <- show <$> getCurrentYear
813
835
let authors = fromMaybe " ???" . flagToMaybe . author $ flags
814
836
let licenseFile =
815
837
case license flags of
@@ -852,14 +874,16 @@ writeLicense flags = do
852
874
Just licenseText -> writeFileSafe flags " LICENSE" licenseText
853
875
Nothing -> message flags " Warning: unknown license type, you must put a copy in LICENSE yourself."
854
876
855
- getYear :: IO Integer
856
- getYear = do
877
+ -- | Returns the current calendar year.
878
+ getCurrentYear :: IO Integer
879
+ getCurrentYear = do
857
880
u <- getCurrentTime
858
881
z <- getCurrentTimeZone
859
882
let l = utcToLocalTime z u
860
883
(y, _, _) = toGregorian $ localDay l
861
884
return y
862
885
886
+ -- | Writes the changelog to the current directory.
863
887
writeChangeLog :: InitFlags -> IO ()
864
888
writeChangeLog flags = when ((defaultChangeLog `elem` ) $ fromMaybe [] (extraSrc flags)) $ do
865
889
message flags (" Generating " ++ defaultChangeLog ++ " ..." )
@@ -875,8 +899,9 @@ writeChangeLog flags = when ((defaultChangeLog `elem`) $ fromMaybe [] (extraSrc
875
899
pname = maybe " " display $ flagToMaybe $ packageName flags
876
900
pver = maybe " " display $ flagToMaybe $ version flags
877
901
878
-
879
-
902
+ -- | Creates and writes the initialized .cabal file.
903
+ --
904
+ -- Returns @False@ if no package name is specified, @True@ otherwise.
880
905
writeCabalFile :: InitFlags -> IO Bool
881
906
writeCabalFile flags@ (InitFlags {packageName = NoFlag }) = do
882
907
message flags " Error: no package name provided."
@@ -944,7 +969,7 @@ createMainHs flags =
944
969
Flag x -> x
945
970
NoFlag -> error " createMainHs: no mainIs"
946
971
947
- --- | Write a main file if it doesn't already exist.
972
+ -- | Write a main file if it doesn't already exist.
948
973
writeMainHs :: InitFlags -> FilePath -> IO ()
949
974
writeMainHs flags mainPath = do
950
975
dir <- maybe getCurrentDirectory return (flagToMaybe $ packageDir flags)
@@ -954,7 +979,7 @@ writeMainHs flags mainPath = do
954
979
message flags $ " Generating " ++ mainPath ++ " ..."
955
980
writeFileSafe flags mainFullPath (mainHs flags)
956
981
957
- -- | Check that a main file exists.
982
+ -- | Returns true if a main file exists.
958
983
hasMainHs :: InitFlags -> Bool
959
984
hasMainHs flags = case mainIs flags of
960
985
Flag _ -> (packageType flags == Flag Executable
@@ -991,6 +1016,7 @@ mainHs flags = (unlines . map prependPrefix) $ case packageType flags of
991
1016
Flag mainPath -> takeExtension mainPath == " .lhs"
992
1017
_ -> False
993
1018
1019
+ -- | The name of the test file to generate (if --tests is specified).
994
1020
testFile :: String
995
1021
testFile = " MyLibTest.hs"
996
1022
@@ -1003,7 +1029,7 @@ createTestHs flags =
1003
1029
Just (testPath: _) -> writeTestHs flags (testPath </> testFile)
1004
1030
_ -> writeMainHs flags testFile
1005
1031
1006
- --- | Write a test file.
1032
+ -- | Write a test file.
1007
1033
writeTestHs :: InitFlags -> FilePath -> IO ()
1008
1034
writeTestHs flags testPath = do
1009
1035
dir <- maybe getCurrentDirectory return (flagToMaybe $ packageDir flags)
@@ -1034,6 +1060,9 @@ moveExistingFile flags fileName =
1034
1060
message flags $ " Warning: " ++ fileName ++ " already exists, backing up old version in " ++ newName
1035
1061
copyFile fileName newName
1036
1062
1063
+
1064
+ -- | Given a file path find a new name for the file that does not
1065
+ -- already exist.
1037
1066
findNewName :: FilePath -> IO FilePath
1038
1067
findNewName oldName = findNewName' 0
1039
1068
where
0 commit comments