@@ -5,28 +5,23 @@ module Distribution.Client.CmdConfigure (
55 configureCommand ,
66 configureAction ,
77 configureAction' ,
8- reconfigureCommand ,
9- reconfigureAction ,
10- reconfigureAction'
118 ) where
129
1310import Distribution.Client.Compat.Prelude
1411import Prelude ()
1512
1613import System.Directory
1714import System.FilePath
18- import qualified Data.Map as Map
1915
16+ import Distribution.Simple.Flag
2017import Distribution.Client.ProjectOrchestration
2118import Distribution.Client.ProjectConfig
2219 ( writeProjectLocalExtraConfig , readProjectLocalExtraConfig )
2320
2421import Distribution.Client.NixStyleOptions
2522 ( NixStyleFlags (.. ), nixStyleOptions , defaultNixStyleFlags )
2623import Distribution.Client.Setup
27- ( GlobalFlags , ConfigFlags (.. ) )
28- import Distribution.Simple.Flag
29- ( fromFlagOrDefault )
24+ ( GlobalFlags , ConfigFlags (.. ), ConfigExFlags (.. ) )
3025import Distribution.Verbosity
3126 ( normal )
3227
@@ -101,142 +96,32 @@ configureAction flags extraArgs globalFlags = do
10196configureAction' :: NixStyleFlags () -> [String ] -> GlobalFlags -> IO (ProjectBaseContext , ProjectConfig )
10297configureAction' flags@ NixStyleFlags {.. } _extraArgs globalFlags = do
10398 -- TODO: deal with _extraArgs, since flags with wrong syntax end up there
99+
100+ baseCtx <- establishProjectBaseContext v cliConfig OtherCommand
104101
105- baseCtx <- establishProjectBaseContext verbosity cliConfig OtherCommand
106-
107- -- Write out the @cabal.project.local@ so it gets picked up by the
108- -- planning phase. If old config exists, then print the contents
109- -- before overwriting
110-
111- let localFile = distProjectFile (distDirLayout baseCtx) " local"
112- -- | Chooses cabal.project.local~, or if it already exists
113- -- cabal.project.local~0, cabal.project.local~1 etc.
114- firstFreeBackup = firstFreeBackup' (0 :: Int )
115- firstFreeBackup' i = do
116- let backup = localFile <> " ~" <> (if i <= 0 then " " else show (i - 1 ))
117- exists <- doesFileExist backup
118- if exists
119- then firstFreeBackup' (i + 1 )
120- else return backup
121-
122- -- If cabal.project.local already exists, back up to cabal.project.local~[n]
102+ let localFile = distProjectFile (distDirLayout baseCtx) " local"
103+ -- If cabal.project.local already exists, and the flags allow, back up to cabal.project.local~
123104 exists <- doesFileExist localFile
124- when exists $ do
125- backup <- firstFreeBackup
126- notice verbosity $
127- quote (takeFileName localFile) <> " already exists, backing it up to "
128- <> quote (takeFileName backup) <> " ."
129- copyFile localFile backup
130-
131- buildCtx <-
132- runProjectPreBuildPhase verbosity baseCtx $ \ elaboratedPlan ->
133-
134- -- TODO: Select the same subset of targets as 'CmdBuild' would
135- -- pick (ignoring, for example, executables in libraries
136- -- we depend on). But we don't want it to fail, so actually we
137- -- have to do it slightly differently from build.
138- return (elaboratedPlan, Map. empty)
139-
140- let baseCtx' = baseCtx {
141- buildSettings = (buildSettings baseCtx) {
142- buildSettingDryRun = True
143- }
144- }
145-
146- -- TODO: Hmm, but we don't have any targets. Currently this prints
147- -- what we would build if we were to build everything. Could pick
148- -- implicit target like "."
149- --
150- -- TODO: should we say what's in the project (+deps) as a whole?
151- printPlan verbosity baseCtx' buildCtx
152-
153- return (baseCtx, cliConfig)
154- where
155- verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
156- cliConfig = commandLineFlagsToProjectConfig globalFlags flags
157- mempty -- ClientInstallFlags, not needed here
158- quote s = " '" <> s <> " '"
105+ let overwrites = fromFlagOrDefault False $ configOverwrite configExFlags
106+ appends = fromFlagOrDefault False $ configAppend configExFlags
107+ backup = localFile ++ " ~"
159108
160- reconfigureCommand :: CommandUI (NixStyleFlags () )
161- reconfigureCommand = CommandUI {
162- commandName = " v2-reconfigure" ,
163- commandSynopsis = " Add extra project configuration" ,
164- commandUsage = usageAlternatives " v2-reconfigure" [ " [FLAGS]" ],
165- commandDescription = Just $ \ _ -> wrapText $
166- " Adjust how the project is built by setting additional package flags "
167- ++ " and other flags.\n\n "
168-
169- ++ " The reconfiguration options are written to the 'cabal.project.local' "
170- ++ " file (or '$project_file.local', if '--project-file' is specified) "
171- ++ " which extends the configuration from the 'cabal.project' file "
172- ++ " (if any).\n\n "
173-
174- ++ " The v2-reconfigure command also checks that the project configuration "
175- ++ " will work. In particular it checks that there is a consistent set of "
176- ++ " dependencies for the project as a whole.\n\n "
177-
178- ++ " It is never necessary to use the 'v2-reconfigure' command. It is "
179- ++ " merely a convenience in cases where you do not want to specify flags "
180- ++ " to 'v2-build' (and other commands) every time and yet do not want "
181- ++ " to alter the 'cabal.project' persistently." ,
182- commandNotes = Just $ \ pname ->
183- " Examples:\n "
184- ++ " " ++ pname ++ " v2-reconfigure --with-compiler ghc-7.10.3\n "
185- ++ " Adjust the project configuration to use the given compiler\n "
186- ++ " program and check the resulting configuration works.\n "
187- ++ " " ++ pname ++ " v2-reconfigure\n "
188- ++ " Reset the local configuration to empty and check the overall\n "
189- ++ " project configuration works.\n "
190-
191- , commandDefaultFlags = defaultNixStyleFlags ()
192- , commandOptions = filter (\ o -> optionName o /= " ignore-project" )
193- . nixStyleOptions (const [] )
194- }
195-
196- reconfigureAction :: NixStyleFlags () -> [String ] -> GlobalFlags -> IO ()
197- reconfigureAction flags extraArgs globalFlags = do
198- reconf <- reconfigureAction' flags extraArgs globalFlags
199- let go (baseCtx, projConfig) =
200- writeProjectLocalExtraConfig (distDirLayout baseCtx) projConfig
201-
202- either go go reconf
203-
204- reconfigureAction'
205- :: NixStyleFlags ()
206- -> [String ]
207- -> GlobalFlags
208- -> IO (Either (ProjectBaseContext , ProjectConfig ) (ProjectBaseContext , ProjectConfig ))
209- reconfigureAction' flags@ NixStyleFlags {.. } extraArgs globalFlags = do
210- baseCtx <- establishProjectBaseContext v cliConfig OtherCommand
211-
212- let localFile = distProjectFile (distDirLayout baseCtx) " local"
213-
214- exists <- doesFileExist localFile
215- if not exists
216- then do
109+ when (exists && not overwrites) $ do
217110 notice v $
218- quote (takeFileName localFile)
219- <> " doesn't exist, calling configure instead."
220-
221- -- @configureAction'@ is called, as opposed to @configureAction@, to avoid
222- -- calling on the writing function for configure, thusly maintaining the
223- -- separation on the behaviour that modify files
224- Left <$> configureAction' flags extraArgs globalFlags
225-
226- else do
227- conf <- runRebuild (distProjectRootDirectory . distDirLayout $ baseCtx) $
228- readProjectLocalExtraConfig v (distDirLayout baseCtx)
229-
230- buildCtx <- runProjectPreBuildPhase v baseCtx $ \ plan ->
231- return (plan, Map. empty)
232-
233- printPlan v
234- (baseCtx {buildSettings = (buildSettings baseCtx) {buildSettingDryRun = True }})
235- buildCtx
236-
237- return $ Right (baseCtx, conf <> cliConfig)
238-
111+ quote (takeFileName localFile) <> " already exists, backing it up to "
112+ <> quote (takeFileName backup) <> " ."
113+ copyFile localFile backup
114+
115+ -- If the flag @configAppend@ is set to true, append and do not overwrite
116+ if exists && appends
117+ then do
118+ conf <- runRebuild (distProjectRootDirectory . distDirLayout $ baseCtx) $
119+ readProjectLocalExtraConfig v (distDirLayout baseCtx)
120+ return (baseCtx, conf <> cliConfig)
121+ else
122+ return (baseCtx, cliConfig)
239123 where
240124 v = fromFlagOrDefault normal (configVerbosity configFlags)
241- cliConfig = commandLineFlagsToProjectConfig globalFlags flags mempty
125+ cliConfig = commandLineFlagsToProjectConfig globalFlags flags
126+ mempty -- ClientInstallFlags, not needed here
242127 quote s = " '" <> s <> " '"
0 commit comments