Commit 9e30cb92 authored by Chang Yang Jiao's avatar Chang Yang Jiao

Initial feature add: create both Library and Executable on cabal init

parent 1b1e9086
......@@ -60,7 +60,7 @@ import qualified Distribution.Package as P
import Language.Haskell.Extension ( Language(..) )
import Distribution.Client.Init.Types
( InitFlags(..), PackageType(..), Category(..) )
( InitFlags(..), BuildType(..), PackageType(..), Category(..) )
import Distribution.Client.Init.Licenses
( bsd2, bsd3, gplv2, gplv3, lgpl21, lgpl3, agplv3, apache20, mit, mpl20, isc )
import Distribution.Client.Init.Heuristics
......@@ -310,16 +310,16 @@ guessExtraSourceFiles flags = do
-- | Ask whether the project builds a library or executable.
getLibOrExec :: InitFlags -> IO InitFlags
getLibOrExec flags = do
isLib <- return (flagToMaybe $ packageType flags)
pkgType <- return (flagToMaybe $ packageType flags)
?>> maybePrompt flags (either (const Library) id `fmap`
promptList "What does the package build"
[Library, Executable]
[Library, Executable, LibraryAndExecutable]
Nothing display False)
?>> return (Just Library)
mainFile <- if isLib /= Just Executable then return Nothing else
mainFile <- if pkgType == Just Library then return Nothing else
getMainFile flags
return $ flags { packageType = maybeToFlag isLib
return $ flags { packageType = maybeToFlag pkgType
, mainIs = maybeToFlag mainFile
}
......@@ -742,6 +742,14 @@ createMainHs flags@InitFlags{ sourceDirs = _
, packageType = Flag Executable
, mainIs = Flag mainFile } =
writeMainHs flags mainFile
createMainHs flags@InitFlags{ sourceDirs = Just (srcPath:_)
, packageType = Flag LibraryAndExecutable
, mainIs = Flag mainFile } =
writeMainHs flags (srcPath </> mainFile)
createMainHs flags@InitFlags{ sourceDirs = _
, packageType = Flag LibraryAndExecutable
, mainIs = Flag mainFile } =
writeMainHs flags mainFile
createMainHs _ = return ()
-- | Write a main file if it doesn't already exist.
......@@ -870,30 +878,18 @@ generateCabalFile fileName c =
False
, case packageType c of
Flag Executable ->
text "\nexecutable" <+>
text (maybe "" display . flagToMaybe $ packageName c) $$
nest 2 (vcat
[ fieldS "main-is" (mainIs c) (Just ".hs or .lhs file containing the Main module.") True
, generateBuildInfo Executable c
])
Flag Library -> text "\nlibrary" $$ nest 2 (vcat
[ fieldS "exposed-modules" (listField (exposedModules c))
(Just "Modules exported by the library.")
True
, generateBuildInfo Library c
])
Flag Executable -> executableStanza
Flag Library -> libraryStanza
Flag LibraryAndExecutable -> libraryStanza $+$ executableStanza
_ -> empty
]
where
generateBuildInfo :: PackageType -> InitFlags -> Doc
generateBuildInfo pkgtype c' = vcat
generateBuildInfo :: BuildType -> InitFlags -> Doc
generateBuildInfo artType c' = vcat
[ fieldS "other-modules" (listField (otherModules c'))
(Just $ case pkgtype of
Library -> "Modules included in this library but not exported."
Executable -> "Modules included in this executable, other than Main.")
(Just $ case artType of
LibBuild -> "Modules included in this library but not exported."
ExecBuild -> "Modules included in this executable, other than Main.")
True
, fieldS "other-extensions" (listField (otherExts c'))
......@@ -964,6 +960,25 @@ generateCabalFile fileName c =
breakLine' [] = []
breakLine' cs = case span (==' ') cs of (w,cs') -> w : breakLine cs'
executableStanza :: Doc
executableStanza = text "\nexecutable" <+>
text (maybe "" display . flagToMaybe $ packageName c) $$
nest 2 (vcat
[ fieldS "main-is" (mainIs c) (Just ".hs or .lhs file containing the Main module.") True
, generateBuildInfo ExecBuild c
])
libraryStanza :: Doc
libraryStanza = text "\nlibrary" $$ nest 2 (vcat
[ fieldS "exposed-modules" (listField (exposedModules c))
(Just "Modules exported by the library.")
True
, generateBuildInfo LibBuild c
])
-- | Generate warnings for missing fields etc.
generateWarnings :: InitFlags -> IO ()
generateWarnings flags = do
......
......@@ -78,12 +78,14 @@ data InitFlags =
-- ones, which is why we want Maybe [foo] for collecting foo values,
-- not Flag [foo].
data PackageType = Library | Executable
data BuildType = LibBuild | ExecBuild
data PackageType = Library | Executable | LibraryAndExecutable
deriving (Show, Read, Eq)
instance Text PackageType where
disp = Disp.text . show
parse = Parse.choice $ map (fmap read . Parse.string . show) [Library, Executable] -- TODO: eradicateNoParse
parse = Parse.choice $ map (fmap read . Parse.string . show) [Library, Executable, LibraryAndExecutable] -- TODO: eradicateNoParse
instance Monoid InitFlags where
mempty = gmempty
......
......@@ -2012,6 +2012,12 @@ initCommand = CommandUI {
(\v flags -> flags { IT.packageType = v })
(noArg (Flag IT.Executable))
, option [] ["is-libandexec"]
"Build a library and an executable."
IT.packageType
(\v flags -> flags { IT.packageType = v })
(noArg (Flag IT.LibraryAndExecutable))
, option [] ["main-is"]
"Specify the main module."
IT.mainIs
......
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