Skip to content

Commit f470eac

Browse files
committed
Default cabal init application-dir to app, and library source-dir to src.
1 parent c5d4b7c commit f470eac

File tree

5 files changed

+88
-28
lines changed

5 files changed

+88
-28
lines changed

cabal-install/Distribution/Client/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,8 @@ commentSavedConfig = do
851851
IT.cabalVersion = toFlag IT.defaultCabalVersion,
852852
IT.language = toFlag Haskell2010,
853853
IT.license = NoFlag,
854-
IT.sourceDirs = Nothing,
855-
IT.applicationDirs = Nothing
854+
IT.sourceDirs = Just [IT.defaultSourceDir],
855+
IT.applicationDirs = Just [IT.defaultApplicationDir]
856856
},
857857
savedInstallFlags = defaultInstallFlags,
858858
savedClientInstallFlags= defaultClientInstallFlags,

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

Lines changed: 76 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ import Distribution.Types.LibraryName
5959
import Language.Haskell.Extension ( Language(..) )
6060

6161
import Distribution.Client.Init.Defaults
62-
( defaultCabalVersion, myLibModule )
62+
( defaultApplicationDir, defaultCabalVersion, myLibModule, defaultSourceDir )
6363
import Distribution.Client.Init.FileCreators
6464
( writeLicense, writeChangeLog, createDirectories, createLibHs, createMainHs
6565
, createTestSuiteIfEligible, writeCabalFile )
6666
import Distribution.Client.Init.Prompt
6767
( prompt, promptYesNo, promptStr, promptList, maybePrompt
68-
, promptListOptional, promptListOptional')
68+
, promptListOptional )
6969
import Distribution.Client.Init.Utils
7070
( eligibleForTestSuite, message )
7171
import Distribution.Client.Init.Types
@@ -482,56 +482,108 @@ getGenComments flags = do
482482
-- | Ask for the application root directory.
483483
getAppDir :: InitFlags -> IO InitFlags
484484
getAppDir flags = do
485-
appDirs <- return (applicationDirs flags)
486-
-- No application dir if this is a 'Library'.
487-
?>> if (packageType flags) == Flag Library then return (Just []) else return Nothing
488-
?>> fmap (:[]) `fmap` guessAppDir flags
489-
?>> fmap (>>= fmap ((:[]) . either id id)) (maybePrompt
490-
flags
491-
(promptListOptional'
492-
("Application " ++ mainFile ++ "directory")
493-
["src-exe", "app"] id))
494-
485+
appDirs <-
486+
return (applicationDirs flags)
487+
?>> noAppDirIfLibraryOnly
488+
?>> guessAppDir flags
489+
?>> promptUserForApplicationDir
490+
?>> setDefault
495491
return $ flags { applicationDirs = appDirs }
496492

497493
where
494+
-- If the packageType==Library, then there is no application dir.
495+
noAppDirIfLibraryOnly :: IO (Maybe [String])
496+
noAppDirIfLibraryOnly =
497+
if (packageType flags) == Flag Library
498+
then return (Just [])
499+
else return Nothing
500+
501+
-- Set the default application directory.
502+
setDefault :: IO (Maybe [String])
503+
setDefault = pure (Just [defaultApplicationDir])
504+
505+
-- Prompt the user for the application directory (defaulting to "app").
506+
-- Returns 'Nothing' if in non-interactive mode, otherwise will always
507+
-- return a 'Just' value ('Just []' if no separate application directory).
508+
promptUserForApplicationDir :: IO (Maybe [String])
509+
promptUserForApplicationDir = fmap (either (:[]) id) <$> maybePrompt
510+
flags
511+
(promptList
512+
("Application " ++ mainFile ++ "directory")
513+
[[defaultApplicationDir], ["src-exe"], []]
514+
(Just [defaultApplicationDir])
515+
showOption True)
516+
517+
showOption :: [String] -> String
518+
showOption [] = "(none)"
519+
showOption (x:_) = x
520+
521+
-- The name
522+
mainFile :: String
498523
mainFile = case mainIs flags of
499524
Flag mainPath -> "(" ++ mainPath ++ ") "
500525
_ -> ""
501526

502527
-- | Try to guess app directory. Could try harder; for the
503528
-- moment just looks to see whether there is a directory called 'app'.
504-
guessAppDir :: InitFlags -> IO (Maybe String)
529+
guessAppDir :: InitFlags -> IO (Maybe [String])
505530
guessAppDir flags = do
506531
dir <- maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
507532
appIsDir <- doesDirectoryExist (dir </> "app")
508533
return $ if appIsDir
509-
then Just "app"
534+
then Just ["app"]
510535
else Nothing
511536

512537
-- | Ask for the source (library) root directory.
513538
getSrcDir :: InitFlags -> IO InitFlags
514539
getSrcDir flags = do
515-
srcDirs <- return (sourceDirs flags)
516-
-- source dir if this is an 'Executable'.
517-
?>> if (packageType flags) == Flag Executable then return (Just []) else return Nothing
518-
?>> fmap (:[]) `fmap` guessSourceDir flags
519-
?>> fmap (>>= fmap ((:[]) . either id id)) (maybePrompt
520-
flags
521-
(promptListOptional' "Library source directory"
522-
["src", "lib", "src-lib"] id))
540+
srcDirs <-
541+
return (sourceDirs flags)
542+
?>> noSourceDirIfExecutableOnly
543+
?>> guessSourceDir flags
544+
?>> promptUserForSourceDir
545+
?>> setDefault
523546

524547
return $ flags { sourceDirs = srcDirs }
525548

549+
where
550+
-- If the packageType==Executable, then there is no source dir.
551+
noSourceDirIfExecutableOnly :: IO (Maybe [String])
552+
noSourceDirIfExecutableOnly =
553+
if (packageType flags) == Flag Executable
554+
then return (Just [])
555+
else return Nothing
556+
557+
-- Set the default source directory.
558+
setDefault :: IO (Maybe [String])
559+
setDefault = pure (Just [defaultSourceDir])
560+
561+
-- Prompt the user for the source directory (defaulting to "app").
562+
-- Returns 'Nothing' if in non-interactive mode, otherwise will always
563+
-- return a 'Just' value ('Just []' if no separate application directory).
564+
promptUserForSourceDir :: IO (Maybe [String])
565+
promptUserForSourceDir = fmap (either (:[]) id) <$> maybePrompt
566+
flags
567+
(promptList
568+
("Library source directory")
569+
[[defaultSourceDir], ["lib"], ["src-lib"], []]
570+
(Just [defaultSourceDir])
571+
showOption True)
572+
573+
showOption :: [String] -> String
574+
showOption [] = "(none)"
575+
showOption (x:_) = x
576+
577+
526578
-- | Try to guess source directory. Could try harder; for the
527579
-- moment just looks to see whether there is a directory called 'src'.
528-
guessSourceDir :: InitFlags -> IO (Maybe String)
580+
guessSourceDir :: InitFlags -> IO (Maybe [String])
529581
guessSourceDir flags = do
530582
dir <-
531583
maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
532584
srcIsDir <- doesDirectoryExist (dir </> "src")
533585
return $ if srcIsDir
534-
then Just "src"
586+
then Just ["src"]
535587
else Nothing
536588

537589
-- | Check whether a potential source file is located in one of the

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
-----------------------------------------------------------------------------
1414

1515
module Distribution.Client.Init.Defaults (
16-
defaultCabalVersion
16+
defaultApplicationDir
17+
, defaultSourceDir
18+
, defaultCabalVersion
1719
, myLibModule
1820
) where
1921

@@ -24,6 +26,12 @@ import qualified Distribution.ModuleName as ModuleName
2426
import Distribution.CabalSpecVersion
2527
( CabalSpecVersion (..))
2628

29+
defaultApplicationDir :: String
30+
defaultApplicationDir = "app"
31+
32+
defaultSourceDir :: String
33+
defaultSourceDir = "src"
34+
2735
defaultCabalVersion :: CabalSpecVersion
2836
defaultCabalVersion = CabalSpecV2_4
2937

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Distribution.Client.Init.Prompt (
2020
, promptStr
2121
, promptList
2222
, promptListOptional
23-
, promptListOptional'
2423
, maybePrompt
2524
) where
2625

changelog.d/cabal-init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ description: {
88
- Licenses are always asked using SPDX expression
99
- Fix an infinite loop when invalid license was passed on command line
1010
- `Setup.hs` is not written anymore
11+
- Default to --source-dir=src and --application-dir=app
1112

1213
TODO: complete the description
1314
}

0 commit comments

Comments
 (0)