Skip to content

Commit b910fcf

Browse files
authored
Merge pull request #6632 from m-renaud/m-renaud-cabal-init-comments
Comments and small refactorings in cabal init code.
2 parents 69030a4 + e49ae1d commit b910fcf

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

cabal-install/Distribution/Client/Init.hs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ initCabal verbosity packageDBs repoCtxt comp progdb initFlags = do
148148
-- Flag acquisition -----------------------------------------------------
149149
---------------------------------------------------------------------------
150150

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.
153153
extendFlags :: InstalledPackageIndex -> SourcePackageDb -> InitFlags -> IO InitFlags
154154
extendFlags pkgIx sourcePkgDb =
155155
getSimpleProject
@@ -188,14 +188,6 @@ maybeToFlag = maybe NoFlag Flag
188188
defaultCabalVersion :: Version
189189
defaultCabalVersion = mkVersion [1,10]
190190

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-
199191
-- | Ask if a simple project with sensible defaults should be created.
200192
getSimpleProject :: InitFlags -> IO InitFlags
201193
getSimpleProject flags = do
@@ -215,7 +207,11 @@ getSimpleProject flags = do
215207
flags { simpleProject = simpleProjFlag }
216208

217209

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).
219215
getCabalVersion :: InitFlags -> IO InitFlags
220216
getCabalVersion flags = do
221217
cabVer <- return (flagToMaybe $ cabalVersion flags)
@@ -227,6 +223,16 @@ getCabalVersion flags = do
227223

228224
return $ flags { cabalVersion = maybeToFlag cabVer }
229225

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+
230236

231237
-- | Get the package name: use the package directory (supplied, or the current
232238
-- directory by default) as a guess. It looks at the SourcePackageDb to avoid
@@ -269,7 +275,10 @@ getVersion flags = do
269275
?>> return v
270276
return $ flags { version = maybeToFlag v' }
271277

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.
273282
getLicense :: InitFlags -> IO InitFlags
274283
getLicense flags = do
275284
lic <- return (flagToMaybe $ license flags)
@@ -324,7 +333,7 @@ getAuthorInfo flags = do
324333
, email = maybeToFlag authorEmail'
325334
}
326335

327-
-- | Prompt for a homepage URL.
336+
-- | Prompt for a homepage URL for the package.
328337
getHomepage :: InitFlags -> IO InitFlags
329338
getHomepage flags = do
330339
hp <- queryHomepage
@@ -435,7 +444,7 @@ getGenTests flags = do
435444
(Just True))
436445
return $ flags { initializeTestSuite = maybeToFlag genTests }
437446

438-
-- | Ask for the test root directory.
447+
-- | Ask for the test suite root directory.
439448
getTestDir :: InitFlags -> IO InitFlags
440449
getTestDir flags = do
441450
dirs <- return (testDirs flags)
@@ -447,7 +456,7 @@ getTestDir flags = do
447456

448457
return $ flags { testDirs = dirs }
449458

450-
-- | Ask for the base language of the package.
459+
-- | Ask for the Haskell base language of the package.
451460
getLanguage :: InitFlags -> IO InitFlags
452461
getLanguage flags = do
453462
lang <- return (flagToMaybe $ language flags)
@@ -600,6 +609,8 @@ getModulesBuildToolsAndDeps pkgIx flags = do
600609
, otherExts = exts
601610
}
602611

612+
-- | Given a list of imported modules, retrieve the list of dependencies that
613+
-- provide those modules.
603614
importsToDeps :: InitFlags -> [ModuleName] -> InstalledPackageIndex -> IO [P.Dependency]
604615
importsToDeps flags mods pkgIx = do
605616

@@ -705,8 +716,9 @@ promptStr :: String -> Maybe String -> IO String
705716
promptStr = promptDefault' Just id
706717

707718
-- | 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
710722
promptYesNo =
711723
promptDefault' recogniseYesNo showYesNo
712724
where
@@ -746,6 +758,8 @@ mkDefPrompt pr def = pr ++ "?" ++ defStr def
746758
where defStr Nothing = " "
747759
defStr (Just s) = " [default: " ++ s ++ "] "
748760

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'.
749763
promptListOptional :: (Text t, Eq t)
750764
=> String -- ^ prompt
751765
-> [t] -- ^ choices
@@ -806,10 +820,18 @@ promptList' displayItem numChoices choices def other = do
806820
-- File generation ------------------------------------------------------
807821
---------------------------------------------------------------------------
808822

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.
809831
writeLicense :: InitFlags -> IO ()
810832
writeLicense flags = do
811833
message flags "\nGenerating LICENSE..."
812-
year <- show <$> getYear
834+
year <- show <$> getCurrentYear
813835
let authors = fromMaybe "???" . flagToMaybe . author $ flags
814836
let licenseFile =
815837
case license flags of
@@ -852,14 +874,16 @@ writeLicense flags = do
852874
Just licenseText -> writeFileSafe flags "LICENSE" licenseText
853875
Nothing -> message flags "Warning: unknown license type, you must put a copy in LICENSE yourself."
854876

855-
getYear :: IO Integer
856-
getYear = do
877+
-- | Returns the current calendar year.
878+
getCurrentYear :: IO Integer
879+
getCurrentYear = do
857880
u <- getCurrentTime
858881
z <- getCurrentTimeZone
859882
let l = utcToLocalTime z u
860883
(y, _, _) = toGregorian $ localDay l
861884
return y
862885

886+
-- | Writes the changelog to the current directory.
863887
writeChangeLog :: InitFlags -> IO ()
864888
writeChangeLog flags = when ((defaultChangeLog `elem`) $ fromMaybe [] (extraSrc flags)) $ do
865889
message flags ("Generating "++ defaultChangeLog ++"...")
@@ -875,8 +899,9 @@ writeChangeLog flags = when ((defaultChangeLog `elem`) $ fromMaybe [] (extraSrc
875899
pname = maybe "" display $ flagToMaybe $ packageName flags
876900
pver = maybe "" display $ flagToMaybe $ version flags
877901

878-
879-
902+
-- | Creates and writes the initialized .cabal file.
903+
--
904+
-- Returns @False@ if no package name is specified, @True@ otherwise.
880905
writeCabalFile :: InitFlags -> IO Bool
881906
writeCabalFile flags@(InitFlags{packageName = NoFlag}) = do
882907
message flags "Error: no package name provided."
@@ -944,7 +969,7 @@ createMainHs flags =
944969
Flag x -> x
945970
NoFlag -> error "createMainHs: no mainIs"
946971

947-
--- | Write a main file if it doesn't already exist.
972+
-- | Write a main file if it doesn't already exist.
948973
writeMainHs :: InitFlags -> FilePath -> IO ()
949974
writeMainHs flags mainPath = do
950975
dir <- maybe getCurrentDirectory return (flagToMaybe $ packageDir flags)
@@ -954,7 +979,7 @@ writeMainHs flags mainPath = do
954979
message flags $ "Generating " ++ mainPath ++ "..."
955980
writeFileSafe flags mainFullPath (mainHs flags)
956981

957-
-- | Check that a main file exists.
982+
-- | Returns true if a main file exists.
958983
hasMainHs :: InitFlags -> Bool
959984
hasMainHs flags = case mainIs flags of
960985
Flag _ -> (packageType flags == Flag Executable
@@ -991,6 +1016,7 @@ mainHs flags = (unlines . map prependPrefix) $ case packageType flags of
9911016
Flag mainPath -> takeExtension mainPath == ".lhs"
9921017
_ -> False
9931018

1019+
-- | The name of the test file to generate (if --tests is specified).
9941020
testFile :: String
9951021
testFile = "MyLibTest.hs"
9961022

@@ -1003,7 +1029,7 @@ createTestHs flags =
10031029
Just (testPath:_) -> writeTestHs flags (testPath </> testFile)
10041030
_ -> writeMainHs flags testFile
10051031

1006-
--- | Write a test file.
1032+
-- | Write a test file.
10071033
writeTestHs :: InitFlags -> FilePath -> IO ()
10081034
writeTestHs flags testPath = do
10091035
dir <- maybe getCurrentDirectory return (flagToMaybe $ packageDir flags)
@@ -1034,6 +1060,9 @@ moveExistingFile flags fileName =
10341060
message flags $ "Warning: " ++ fileName ++ " already exists, backing up old version in " ++ newName
10351061
copyFile fileName newName
10361062

1063+
1064+
-- | Given a file path find a new name for the file that does not
1065+
-- already exist.
10371066
findNewName :: FilePath -> IO FilePath
10381067
findNewName oldName = findNewName' 0
10391068
where

cabal-install/Distribution/Client/Init/Licenses.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
{-|
2+
Module : Distribution.Client.Init.Licenses
3+
4+
Description : Factory functions for producing known license types.
5+
6+
License : BSD-like
7+
Maintainer : cabal-devel@haskell.org
8+
Stability : provisional
9+
Portability : portable
10+
-}
111
module Distribution.Client.Init.Licenses
212
( License
313
, bsd2

cabal-install/Distribution/Client/Init/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ instance Monoid InitFlags where
103103
instance Semigroup InitFlags where
104104
(<>) = gmappend
105105

106-
-- | Some common package categories.
106+
-- | Some common package categories (non-exhaustive list).
107107
data Category
108108
= Codec
109109
| Concurrency

0 commit comments

Comments
 (0)