Add “Ignore warning” option to cabal check
[cabal.git] / cabal-install / src / Distribution / Client / Init / Simple.hs
blob4d413085547f6d4eda29b9e0b0425374d88541df
1 module Distribution.Client.Init.Simple
2 ( -- * Project creation
3 createProject
5 -- * Gen targets
6 , genSimplePkgDesc
7 , genSimpleLibTarget
8 , genSimpleExeTarget
9 , genSimpleTestTarget
10 ) where
12 import qualified Data.List.NonEmpty as NEL
13 import qualified Data.Set as Set
14 import Distribution.Client.Init.Defaults
15 import Distribution.Client.Init.FlagExtractors
16 import Distribution.Client.Init.Types
17 import Distribution.Client.Init.Utils (currentDirPkgName, fixupDocFiles, mkPackageNameDep)
18 import Distribution.Client.Types.SourcePackageDb (SourcePackageDb (..))
19 import Distribution.Simple.Flag (Flag (..), flagElim, fromFlagOrDefault)
20 import Distribution.Simple.PackageIndex
21 import Distribution.Types.Dependency
22 import Distribution.Types.PackageName (unPackageName)
23 import Distribution.Verbosity
25 createProject
26 :: Interactive m
27 => Verbosity
28 -> InstalledPackageIndex
29 -> SourcePackageDb
30 -> InitFlags
31 -> m ProjectSettings
32 createProject v pkgIx _srcDb initFlags = do
33 pkgType <- packageTypePrompt initFlags
34 isMinimal <- getMinimal initFlags
35 doOverwrite <- getOverwrite initFlags
36 pkgDir <- getPackageDir initFlags
37 pkgDesc <- fixupDocFiles v =<< genSimplePkgDesc initFlags
39 let pkgName = _pkgName pkgDesc
40 cabalSpec = _pkgCabalVersion pkgDesc
41 mkOpts cs =
42 WriteOpts
43 doOverwrite
44 isMinimal
47 pkgDir
48 pkgType
49 pkgName
51 basedFlags <- addBaseDepToFlags pkgIx initFlags
53 case pkgType of
54 Library -> do
55 libTarget <- genSimpleLibTarget basedFlags
56 testTarget <- addLibDepToTest pkgName <$> genSimpleTestTarget basedFlags
57 return $
58 ProjectSettings
59 (mkOpts False cabalSpec)
60 pkgDesc
61 (Just libTarget)
62 Nothing
63 testTarget
64 Executable -> do
65 exeTarget <- genSimpleExeTarget basedFlags
66 return $
67 ProjectSettings
68 (mkOpts False cabalSpec)
69 pkgDesc
70 Nothing
71 (Just exeTarget)
72 Nothing
73 LibraryAndExecutable -> do
74 libTarget <- genSimpleLibTarget basedFlags
75 testTarget <- addLibDepToTest pkgName <$> genSimpleTestTarget basedFlags
76 exeTarget <- addLibDepToExe pkgName <$> genSimpleExeTarget basedFlags
77 return $
78 ProjectSettings
79 (mkOpts False cabalSpec)
80 pkgDesc
81 (Just libTarget)
82 (Just exeTarget)
83 testTarget
84 TestSuite -> do
85 testTarget <- genSimpleTestTarget basedFlags
86 return $
87 ProjectSettings
88 (mkOpts False cabalSpec)
89 pkgDesc
90 Nothing
91 Nothing
92 testTarget
93 where
94 -- Add package name as dependency of test suite
96 addLibDepToTest _ Nothing = Nothing
97 addLibDepToTest n (Just t) =
98 Just $
100 { _testDependencies = _testDependencies t ++ [mkPackageNameDep n]
103 -- Add package name as dependency of executable
105 addLibDepToExe n exe =
107 { _exeDependencies = _exeDependencies exe ++ [mkPackageNameDep n]
110 genSimplePkgDesc :: Interactive m => InitFlags -> m PkgDescription
111 genSimplePkgDesc flags = mkPkgDesc <$> currentDirPkgName
112 where
113 defaultExtraDoc = Just $ Set.singleton defaultChangelog
115 extractExtraDoc [] = defaultExtraDoc
116 extractExtraDoc fs = Just $ Set.fromList fs
118 mkPkgDesc pkgName =
119 PkgDescription
120 (fromFlagOrDefault defaultCabalVersion (cabalVersion flags))
121 pkgName
122 (fromFlagOrDefault defaultVersion (version flags))
123 (fromFlagOrDefault (defaultLicense $ getCabalVersionNoPrompt flags) (license flags))
124 (fromFlagOrDefault "" (author flags))
125 (fromFlagOrDefault "" (email flags))
126 (fromFlagOrDefault "" (homepage flags))
127 (fromFlagOrDefault "" (synopsis flags))
128 (fromFlagOrDefault "" (category flags))
129 (flagElim mempty Set.fromList (extraSrc flags))
130 (flagElim defaultExtraDoc extractExtraDoc (extraDoc flags))
132 genSimpleLibTarget :: Interactive m => InitFlags -> m LibTarget
133 genSimpleLibTarget flags = do
134 buildToolDeps <- getBuildTools flags
135 return $
136 LibTarget
137 { _libSourceDirs = fromFlagOrDefault [defaultSourceDir] $ sourceDirs flags
138 , _libLanguage = fromFlagOrDefault defaultLanguage $ language flags
139 , _libExposedModules =
140 flagElim (myLibModule NEL.:| []) extractMods $ exposedModules flags
141 , _libOtherModules = fromFlagOrDefault [] $ otherModules flags
142 , _libOtherExts = fromFlagOrDefault [] $ otherExts flags
143 , _libDependencies = fromFlagOrDefault [] $ dependencies flags
144 , _libBuildTools = buildToolDeps
146 where
147 extractMods [] = myLibModule NEL.:| []
148 extractMods as = NEL.fromList as
150 genSimpleExeTarget :: Interactive m => InitFlags -> m ExeTarget
151 genSimpleExeTarget flags = do
152 buildToolDeps <- getBuildTools flags
153 return $
154 ExeTarget
155 { _exeMainIs = flagElim defaultMainIs toHsFilePath $ mainIs flags
156 , _exeApplicationDirs =
157 fromFlagOrDefault [defaultApplicationDir] $ applicationDirs flags
158 , _exeLanguage = fromFlagOrDefault defaultLanguage $ language flags
159 , _exeOtherModules = fromFlagOrDefault [] $ otherModules flags
160 , _exeOtherExts = fromFlagOrDefault [] $ otherExts flags
161 , _exeDependencies = fromFlagOrDefault [] $ dependencies flags
162 , _exeBuildTools = buildToolDeps
165 genSimpleTestTarget :: Interactive m => InitFlags -> m (Maybe TestTarget)
166 genSimpleTestTarget flags = go =<< initializeTestSuitePrompt flags
167 where
168 go initialized
169 | not initialized = return Nothing
170 | otherwise = do
171 buildToolDeps <- getBuildTools flags
172 return $
173 Just $
174 TestTarget
175 { _testMainIs = flagElim defaultMainIs toHsFilePath $ mainIs flags
176 , _testDirs = fromFlagOrDefault [defaultTestDir] $ testDirs flags
177 , _testLanguage = fromFlagOrDefault defaultLanguage $ language flags
178 , _testOtherModules = fromFlagOrDefault [] $ otherModules flags
179 , _testOtherExts = fromFlagOrDefault [] $ otherExts flags
180 , _testDependencies = fromFlagOrDefault [] $ dependencies flags
181 , _testBuildTools = buildToolDeps
184 -- -------------------------------------------------------------------- --
185 -- Utils
187 -- | If deps are defined, and base is present, we skip the search for base.
188 -- otherwise, we look up @base@ and add it to the list.
189 addBaseDepToFlags :: Interactive m => InstalledPackageIndex -> InitFlags -> m InitFlags
190 addBaseDepToFlags pkgIx initFlags = case dependencies initFlags of
191 Flag as
192 | any ((==) "base" . unPackageName . depPkgName) as -> return initFlags
193 | otherwise -> do
194 based <- dependenciesPrompt pkgIx initFlags
195 return $
196 initFlags
197 { dependencies = Flag $ based ++ as
199 NoFlag -> do
200 based <- dependenciesPrompt pkgIx initFlags
201 return initFlags{dependencies = Flag based}