1 {-# LANGUAGE RecordWildCards #-}
3 -- | cabal-install CLI command: configure
4 module Distribution
.Client
.CmdConfigure
10 import Distribution
.Client
.Compat
.Prelude
13 import System
.Directory
14 import System
.FilePath
16 import Distribution
.Client
.ProjectConfig
17 ( readProjectLocalExtraConfig
18 , writeProjectLocalExtraConfig
20 import Distribution
.Client
.ProjectFlags
21 ( removeIgnoreProjectOption
23 import Distribution
.Client
.ProjectOrchestration
24 import Distribution
.Simple
.Flag
26 import Distribution
.Client
.NixStyleOptions
28 , defaultNixStyleFlags
31 import Distribution
.Client
.Setup
36 import Distribution
.Verbosity
40 import Distribution
.Simple
.Command
44 import Distribution
.Simple
.Utils
50 import Distribution
.Client
.DistDirLayout
53 import Distribution
.Client
.Errors
54 import Distribution
.Client
.HttpUtils
55 import Distribution
.Client
.ProjectConfig
.Types
56 import Distribution
.Client
.RebuildMonad
(runRebuild
)
57 import Distribution
.Types
.CondTree
60 import Distribution
.Utils
.NubList
64 configureCommand
:: CommandUI
(NixStyleFlags
())
67 { commandName
= "v2-configure"
68 , commandSynopsis
= "Add extra project configuration."
69 , commandUsage
= usageAlternatives
"v2-configure" ["[FLAGS]"]
70 , commandDescription
= Just
$ \_
->
72 "Adjust how the project is built by setting additional package flags "
73 ++ "and other flags.\n\n"
74 ++ "The configuration options are written to the 'cabal.project.local' "
75 ++ "file (or '$project_file.local', if '--project-file' is specified) "
76 ++ "which extends the configuration from the 'cabal.project' file "
77 ++ "(if any). This combination is used as the project configuration for "
78 ++ "all other commands (such as 'v2-build', 'v2-repl' etc) though it "
79 ++ "can be extended/overridden on a per-command basis.\n\n"
80 ++ "The v2-configure command also checks that the project configuration "
81 ++ "will work. In particular it checks that there is a consistent set of "
82 ++ "dependencies for the project as a whole.\n\n"
83 ++ "The 'cabal.project.local' file persists across 'v2-clean' but is "
84 ++ "overwritten on the next use of the 'v2-configure' command. The "
85 ++ "intention is that the 'cabal.project' file should be kept in source "
86 ++ "control but the 'cabal.project.local' should not.\n\n"
87 ++ "It is never necessary to use the 'v2-configure' command. It is "
88 ++ "merely a convenience in cases where you do not want to specify flags "
89 ++ "to 'v2-build' (and other commands) every time and yet do not want "
90 ++ "to alter the 'cabal.project' persistently."
91 , commandNotes
= Just
$ \pname
->
95 ++ " v2-configure --with-compiler ghc-7.10.3\n"
96 ++ " Adjust the project configuration to use the given compiler\n"
97 ++ " program and check the resulting configuration works.\n"
101 ++ " Reset the local configuration to empty. To check that the\n"
102 ++ " project configuration works, use 'cabal build'.\n"
103 , commandDefaultFlags
= defaultNixStyleFlags
()
105 removeIgnoreProjectOption
106 . nixStyleOptions
(const [])
109 -- | To a first approximation, the @configure@ just runs the first phase of
110 -- the @build@ command where we bring the install plan up to date (thus
111 -- checking that it's possible).
113 -- The only difference is that @configure@ also allows the user to specify
114 -- some extra config flags which we save in the file @cabal.project.local@.
116 -- For more details on how this works, see the module
117 -- "Distribution.Client.ProjectOrchestration"
118 configureAction
:: NixStyleFlags
() -> [String] -> GlobalFlags
-> IO ()
119 configureAction flags
@NixStyleFlags
{..} extraArgs globalFlags
= do
120 (baseCtx
, projConfig
) <- configureAction
' flags extraArgs globalFlags
122 if shouldNotWriteFile baseCtx
123 then notice v
"Config file not written due to flag(s)."
124 else writeProjectLocalExtraConfig
(distDirLayout baseCtx
) projConfig
126 v
= fromFlagOrDefault normal
(configVerbosity configFlags
)
128 configureAction
' :: NixStyleFlags
() -> [String] -> GlobalFlags
-> IO (ProjectBaseContext
, ProjectConfig
)
129 configureAction
' flags
@NixStyleFlags
{..} _extraArgs globalFlags
= do
130 -- TODO: deal with _extraArgs, since flags with wrong syntax end up there
132 baseCtx
<- establishProjectBaseContext v cliConfig OtherCommand
134 let localFile
= distProjectFile
(distDirLayout baseCtx
) "local"
135 -- If cabal.project.local already exists, and the flags allow, back up to cabal.project.local~
136 let backups
= fromFlagOrDefault
True $ configBackup configExFlags
137 appends
= fromFlagOrDefault
False $ configAppend configExFlags
138 backupFile
= localFile
<> "~"
140 if shouldNotWriteFile baseCtx
141 then return (baseCtx
, cliConfig
)
143 exists
<- doesFileExist localFile
144 when (exists
&& backups
) $ do
146 quote
(takeFileName localFile
)
147 <> " already exists, backing it up to "
148 <> quote
(takeFileName backupFile
)
150 copyFile localFile backupFile
152 -- If the flag @configAppend@ is set to true, append and do not overwrite
158 (fromNubList
. projectConfigProgPathExtra
$ projectConfigShared cliConfig
)
159 (flagToMaybe
. projectConfigHttpTransport
$ projectConfigBuildOnly cliConfig
)
160 (CondNode conf imps bs
) <-
161 runRebuild
(distProjectRootDirectory
. distDirLayout
$ baseCtx
) $
162 readProjectLocalExtraConfig v httpTransport
(distDirLayout baseCtx
)
163 when (not (null imps
&& null bs
)) $ dieWithException v UnableToPerformInplaceUpdate
164 return (baseCtx
, conf
<> cliConfig
)
165 else return (baseCtx
, cliConfig
)
167 v
= fromFlagOrDefault normal
(configVerbosity configFlags
)
169 commandLineFlagsToProjectConfig
172 mempty
-- ClientInstallFlags, not needed here
173 quote s
= "'" <> s
<> "'"
175 -- Config file should not be written when certain flags are present
176 shouldNotWriteFile
:: ProjectBaseContext
-> Bool
177 shouldNotWriteFile baseCtx
=
178 buildSettingDryRun
(buildSettings baseCtx
)
179 || buildSettingOnlyDownload
(buildSettings baseCtx
)