44module Distribution.Client.CmdConfigure (
55 configureCommand ,
66 configureAction ,
7+ configureAction' ,
78 ) where
89
910import Distribution.Client.Compat.Prelude
1011import Prelude ()
1112
1213import System.Directory
1314import System.FilePath
14- import qualified Data.Map as Map
1515
16+ import Distribution.Simple.Flag
1617import Distribution.Client.ProjectOrchestration
1718import Distribution.Client.ProjectConfig
18- ( writeProjectLocalExtraConfig )
19+ ( writeProjectLocalExtraConfig , readProjectLocalExtraConfig )
1920
2021import Distribution.Client.NixStyleOptions
2122 ( NixStyleFlags (.. ), nixStyleOptions , defaultNixStyleFlags )
2223import Distribution.Client.Setup
23- ( GlobalFlags , ConfigFlags (.. ) )
24- import Distribution.Simple.Flag
25- ( fromFlagOrDefault )
24+ ( GlobalFlags , ConfigFlags (.. ), ConfigExFlags (.. ) )
2625import Distribution.Verbosity
2726 ( normal )
2827
@@ -33,6 +32,8 @@ import Distribution.Simple.Utils
3332
3433import Distribution.Client.DistDirLayout
3534 ( DistDirLayout (.. ) )
35+ import Distribution.Client.RebuildMonad (runRebuild )
36+ import Distribution.Client.ProjectConfig.Types
3637
3738configureCommand :: CommandUI (NixStyleFlags () )
3839configureCommand = CommandUI {
@@ -88,61 +89,39 @@ configureCommand = CommandUI {
8889-- "Distribution.Client.ProjectOrchestration"
8990--
9091configureAction :: NixStyleFlags () -> [String ] -> GlobalFlags -> IO ()
91- configureAction flags@ NixStyleFlags {.. } _extraArgs globalFlags = do
92- -- TODO: deal with _extraArgs, since flags with wrong syntax end up there
93-
94- baseCtx <- establishProjectBaseContext verbosity cliConfig OtherCommand
92+ configureAction flags extraArgs globalFlags = do
93+ (baseCtx, projConfig) <- configureAction' flags extraArgs globalFlags
94+ writeProjectLocalExtraConfig (distDirLayout baseCtx) projConfig
9595
96- -- Write out the @cabal.project.local@ so it gets picked up by the
97- -- planning phase. If old config exists, then print the contents
98- -- before overwriting
99-
100- let localFile = distProjectFile (distDirLayout baseCtx) " local"
101- -- | Chooses cabal.project.local~, or if it already exists
102- -- cabal.project.local~0, cabal.project.local~1 etc.
103- firstFreeBackup = firstFreeBackup' (0 :: Int )
104- firstFreeBackup' i = do
105- let backup = localFile <> " ~" <> (if i <= 0 then " " else show (i - 1 ))
106- exists <- doesFileExist backup
107- if exists
108- then firstFreeBackup' (i + 1 )
109- else return backup
96+ configureAction' :: NixStyleFlags () -> [String ] -> GlobalFlags -> IO (ProjectBaseContext , ProjectConfig )
97+ configureAction' flags@ NixStyleFlags {.. } _extraArgs globalFlags = do
98+ -- TODO: deal with _extraArgs, since flags with wrong syntax end up there
99+
100+ baseCtx <- establishProjectBaseContext v cliConfig OtherCommand
110101
111- -- 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~
112104 exists <- doesFileExist localFile
113- when exists $ do
114- backup <- firstFreeBackup
115- notice verbosity $
116- quote (takeFileName localFile) <> " already exists, backing it up to "
117- <> quote (takeFileName backup) <> " ."
118- copyFile localFile backup
119- writeProjectLocalExtraConfig (distDirLayout baseCtx)
120- cliConfig
121-
122- buildCtx <-
123- runProjectPreBuildPhase verbosity baseCtx $ \ elaboratedPlan ->
124-
125- -- TODO: Select the same subset of targets as 'CmdBuild' would
126- -- pick (ignoring, for example, executables in libraries
127- -- we depend on). But we don't want it to fail, so actually we
128- -- have to do it slightly differently from build.
129- return (elaboratedPlan, Map. empty)
130-
131- let baseCtx' = baseCtx {
132- buildSettings = (buildSettings baseCtx) {
133- buildSettingDryRun = True
134- }
135- }
136-
137- -- TODO: Hmm, but we don't have any targets. Currently this prints
138- -- what we would build if we were to build everything. Could pick
139- -- implicit target like "."
140- --
141- -- TODO: should we say what's in the project (+deps) as a whole?
142- printPlan verbosity baseCtx' buildCtx
105+ let backups = fromFlagOrDefault True $ configBackup configExFlags
106+ appends = fromFlagOrDefault False $ configAppend configExFlags
107+ backupFile = localFile <> " ~"
108+
109+ when (exists && not backups) $ do
110+ notice v $
111+ quote (takeFileName localFile) <> " already exists, backing it up to "
112+ <> quote (takeFileName backupFile) <> " ."
113+ copyFile localFile backupFile
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)
143123 where
144- verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
124+ v = fromFlagOrDefault normal (configVerbosity configFlags)
145125 cliConfig = commandLineFlagsToProjectConfig globalFlags flags
146126 mempty -- ClientInstallFlags, not needed here
147127 quote s = " '" <> s <> " '"
148-
0 commit comments