Commit f470eacb authored by Matt Renaud's avatar Matt Renaud

Default cabal init application-dir to app, and library source-dir to src.

parent c5d4b7ce
...@@ -851,8 +851,8 @@ commentSavedConfig = do ...@@ -851,8 +851,8 @@ commentSavedConfig = do
IT.cabalVersion = toFlag IT.defaultCabalVersion, IT.cabalVersion = toFlag IT.defaultCabalVersion,
IT.language = toFlag Haskell2010, IT.language = toFlag Haskell2010,
IT.license = NoFlag, IT.license = NoFlag,
IT.sourceDirs = Nothing, IT.sourceDirs = Just [IT.defaultSourceDir],
IT.applicationDirs = Nothing IT.applicationDirs = Just [IT.defaultApplicationDir]
}, },
savedInstallFlags = defaultInstallFlags, savedInstallFlags = defaultInstallFlags,
savedClientInstallFlags= defaultClientInstallFlags, savedClientInstallFlags= defaultClientInstallFlags,
......
...@@ -59,13 +59,13 @@ import Distribution.Types.LibraryName ...@@ -59,13 +59,13 @@ import Distribution.Types.LibraryName
import Language.Haskell.Extension ( Language(..) ) import Language.Haskell.Extension ( Language(..) )
import Distribution.Client.Init.Defaults import Distribution.Client.Init.Defaults
( defaultCabalVersion, myLibModule ) ( defaultApplicationDir, defaultCabalVersion, myLibModule, defaultSourceDir )
import Distribution.Client.Init.FileCreators import Distribution.Client.Init.FileCreators
( writeLicense, writeChangeLog, createDirectories, createLibHs, createMainHs ( writeLicense, writeChangeLog, createDirectories, createLibHs, createMainHs
, createTestSuiteIfEligible, writeCabalFile ) , createTestSuiteIfEligible, writeCabalFile )
import Distribution.Client.Init.Prompt import Distribution.Client.Init.Prompt
( prompt, promptYesNo, promptStr, promptList, maybePrompt ( prompt, promptYesNo, promptStr, promptList, maybePrompt
, promptListOptional, promptListOptional') , promptListOptional )
import Distribution.Client.Init.Utils import Distribution.Client.Init.Utils
( eligibleForTestSuite, message ) ( eligibleForTestSuite, message )
import Distribution.Client.Init.Types import Distribution.Client.Init.Types
...@@ -482,56 +482,108 @@ getGenComments flags = do ...@@ -482,56 +482,108 @@ getGenComments flags = do
-- | Ask for the application root directory. -- | Ask for the application root directory.
getAppDir :: InitFlags -> IO InitFlags getAppDir :: InitFlags -> IO InitFlags
getAppDir flags = do getAppDir flags = do
appDirs <- return (applicationDirs flags) appDirs <-
-- No application dir if this is a 'Library'. return (applicationDirs flags)
?>> if (packageType flags) == Flag Library then return (Just []) else return Nothing ?>> noAppDirIfLibraryOnly
?>> fmap (:[]) `fmap` guessAppDir flags ?>> guessAppDir flags
?>> fmap (>>= fmap ((:[]) . either id id)) (maybePrompt ?>> promptUserForApplicationDir
flags ?>> setDefault
(promptListOptional'
("Application " ++ mainFile ++ "directory")
["src-exe", "app"] id))
return $ flags { applicationDirs = appDirs } return $ flags { applicationDirs = appDirs }
where where
-- If the packageType==Library, then there is no application dir.
noAppDirIfLibraryOnly :: IO (Maybe [String])
noAppDirIfLibraryOnly =
if (packageType flags) == Flag Library
then return (Just [])
else return Nothing
-- Set the default application directory.
setDefault :: IO (Maybe [String])
setDefault = pure (Just [defaultApplicationDir])
-- Prompt the user for the application directory (defaulting to "app").
-- Returns 'Nothing' if in non-interactive mode, otherwise will always
-- return a 'Just' value ('Just []' if no separate application directory).
promptUserForApplicationDir :: IO (Maybe [String])
promptUserForApplicationDir = fmap (either (:[]) id) <$> maybePrompt
flags
(promptList
("Application " ++ mainFile ++ "directory")
[[defaultApplicationDir], ["src-exe"], []]
(Just [defaultApplicationDir])
showOption True)
showOption :: [String] -> String
showOption [] = "(none)"
showOption (x:_) = x
-- The name
mainFile :: String
mainFile = case mainIs flags of mainFile = case mainIs flags of
Flag mainPath -> "(" ++ mainPath ++ ") " Flag mainPath -> "(" ++ mainPath ++ ") "
_ -> "" _ -> ""
-- | Try to guess app directory. Could try harder; for the -- | Try to guess app directory. Could try harder; for the
-- moment just looks to see whether there is a directory called 'app'. -- moment just looks to see whether there is a directory called 'app'.
guessAppDir :: InitFlags -> IO (Maybe String) guessAppDir :: InitFlags -> IO (Maybe [String])
guessAppDir flags = do guessAppDir flags = do
dir <- maybe getCurrentDirectory return . flagToMaybe $ packageDir flags dir <- maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
appIsDir <- doesDirectoryExist (dir </> "app") appIsDir <- doesDirectoryExist (dir </> "app")
return $ if appIsDir return $ if appIsDir
then Just "app" then Just ["app"]
else Nothing else Nothing
-- | Ask for the source (library) root directory. -- | Ask for the source (library) root directory.
getSrcDir :: InitFlags -> IO InitFlags getSrcDir :: InitFlags -> IO InitFlags
getSrcDir flags = do getSrcDir flags = do
srcDirs <- return (sourceDirs flags) srcDirs <-
-- source dir if this is an 'Executable'. return (sourceDirs flags)
?>> if (packageType flags) == Flag Executable then return (Just []) else return Nothing ?>> noSourceDirIfExecutableOnly
?>> fmap (:[]) `fmap` guessSourceDir flags ?>> guessSourceDir flags
?>> fmap (>>= fmap ((:[]) . either id id)) (maybePrompt ?>> promptUserForSourceDir
flags ?>> setDefault
(promptListOptional' "Library source directory"
["src", "lib", "src-lib"] id))
return $ flags { sourceDirs = srcDirs } return $ flags { sourceDirs = srcDirs }
where
-- If the packageType==Executable, then there is no source dir.
noSourceDirIfExecutableOnly :: IO (Maybe [String])
noSourceDirIfExecutableOnly =
if (packageType flags) == Flag Executable
then return (Just [])
else return Nothing
-- Set the default source directory.
setDefault :: IO (Maybe [String])
setDefault = pure (Just [defaultSourceDir])
-- Prompt the user for the source directory (defaulting to "app").
-- Returns 'Nothing' if in non-interactive mode, otherwise will always
-- return a 'Just' value ('Just []' if no separate application directory).
promptUserForSourceDir :: IO (Maybe [String])
promptUserForSourceDir = fmap (either (:[]) id) <$> maybePrompt
flags
(promptList
("Library source directory")
[[defaultSourceDir], ["lib"], ["src-lib"], []]
(Just [defaultSourceDir])
showOption True)
showOption :: [String] -> String
showOption [] = "(none)"
showOption (x:_) = x
-- | Try to guess source directory. Could try harder; for the -- | Try to guess source directory. Could try harder; for the
-- moment just looks to see whether there is a directory called 'src'. -- moment just looks to see whether there is a directory called 'src'.
guessSourceDir :: InitFlags -> IO (Maybe String) guessSourceDir :: InitFlags -> IO (Maybe [String])
guessSourceDir flags = do guessSourceDir flags = do
dir <- dir <-
maybe getCurrentDirectory return . flagToMaybe $ packageDir flags maybe getCurrentDirectory return . flagToMaybe $ packageDir flags
srcIsDir <- doesDirectoryExist (dir </> "src") srcIsDir <- doesDirectoryExist (dir </> "src")
return $ if srcIsDir return $ if srcIsDir
then Just "src" then Just ["src"]
else Nothing else Nothing
-- | Check whether a potential source file is located in one of the -- | Check whether a potential source file is located in one of the
......
...@@ -13,7 +13,9 @@ ...@@ -13,7 +13,9 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
module Distribution.Client.Init.Defaults ( module Distribution.Client.Init.Defaults (
defaultCabalVersion defaultApplicationDir
, defaultSourceDir
, defaultCabalVersion
, myLibModule , myLibModule
) where ) where
...@@ -24,6 +26,12 @@ import qualified Distribution.ModuleName as ModuleName ...@@ -24,6 +26,12 @@ import qualified Distribution.ModuleName as ModuleName
import Distribution.CabalSpecVersion import Distribution.CabalSpecVersion
( CabalSpecVersion (..)) ( CabalSpecVersion (..))
defaultApplicationDir :: String
defaultApplicationDir = "app"
defaultSourceDir :: String
defaultSourceDir = "src"
defaultCabalVersion :: CabalSpecVersion defaultCabalVersion :: CabalSpecVersion
defaultCabalVersion = CabalSpecV2_4 defaultCabalVersion = CabalSpecV2_4
......
...@@ -20,7 +20,6 @@ module Distribution.Client.Init.Prompt ( ...@@ -20,7 +20,6 @@ module Distribution.Client.Init.Prompt (
, promptStr , promptStr
, promptList , promptList
, promptListOptional , promptListOptional
, promptListOptional'
, maybePrompt , maybePrompt
) where ) where
......
...@@ -8,6 +8,7 @@ description: { ...@@ -8,6 +8,7 @@ description: {
- Licenses are always asked using SPDX expression - Licenses are always asked using SPDX expression
- Fix an infinite loop when invalid license was passed on command line - Fix an infinite loop when invalid license was passed on command line
- `Setup.hs` is not written anymore - `Setup.hs` is not written anymore
- Default to --source-dir=src and --application-dir=app
TODO: complete the description TODO: complete the description
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment