Skip to content

Commit 96cb20f

Browse files
committed
Modifying the implementation as per issue #7405
1 parent 55d3f4d commit 96cb20f

File tree

6 files changed

+38
-141
lines changed

6 files changed

+38
-141
lines changed

Cabal/src/Distribution/Simple/Setup.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ data ConfigFlags = ConfigFlags {
200200
-- ProgramDb directly and not via ConfigFlags
201201
configPrograms_ :: Option' (Last' ProgramDb), -- ^All programs that
202202
-- @cabal@ may run
203-
204203
configProgramPaths :: [(String, FilePath)], -- ^user specified programs paths
205204
configProgramArgs :: [(String, [String])], -- ^user specified programs args
206205
configProgramPathExtra :: NubList FilePath, -- ^Extend the $PATH

cabal-install/main/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ mainWorker args = do
249249

250250
] ++ concat
251251
[ newCmd CmdConfigure.configureCommand CmdConfigure.configureAction
252-
, newCmd CmdConfigure.reconfigureCommand CmdConfigure.reconfigureAction
253252
, newCmd CmdUpdate.updateCommand CmdUpdate.updateAction
254253
, newCmd CmdBuild.buildCommand CmdBuild.buildAction
255254
, newCmd CmdRepl.replCommand CmdRepl.replAction

cabal-install/src/Distribution/Client/CmdConfigure.hs

Lines changed: 24 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,23 @@ module Distribution.Client.CmdConfigure (
55
configureCommand,
66
configureAction,
77
configureAction',
8-
reconfigureCommand,
9-
reconfigureAction,
10-
reconfigureAction'
118
) where
129

1310
import Distribution.Client.Compat.Prelude
1411
import Prelude ()
1512

1613
import System.Directory
1714
import System.FilePath
18-
import qualified Data.Map as Map
1915

16+
import Distribution.Simple.Flag
2017
import Distribution.Client.ProjectOrchestration
2118
import Distribution.Client.ProjectConfig
2219
( writeProjectLocalExtraConfig, readProjectLocalExtraConfig )
2320

2421
import Distribution.Client.NixStyleOptions
2522
( NixStyleFlags (..), nixStyleOptions, defaultNixStyleFlags )
2623
import Distribution.Client.Setup
27-
( GlobalFlags, ConfigFlags(..) )
28-
import Distribution.Simple.Flag
29-
( fromFlagOrDefault )
24+
( GlobalFlags, ConfigFlags(..), ConfigExFlags(..) )
3025
import Distribution.Verbosity
3126
( normal )
3227

@@ -101,142 +96,32 @@ configureAction flags extraArgs globalFlags = do
10196
configureAction' :: NixStyleFlags () -> [String] -> GlobalFlags -> IO (ProjectBaseContext, ProjectConfig)
10297
configureAction' 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 <> "'"

cabal-install/src/Distribution/Client/Config.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ instance Semigroup SavedConfig where
419419

420420
combinedSavedConfigureExFlags = ConfigExFlags {
421421
configCabalVersion = combine configCabalVersion,
422+
configAppend = combine configAppend,
423+
configOverwrite = combine configOverwrite,
422424
-- TODO: NubListify
423425
configExConstraints = lastNonEmpty configExConstraints,
424426
-- TODO: NubListify

cabal-install/src/Distribution/Client/ProjectConfig/Legacy.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ convertToLegacySharedConfig
599599

600600
configExFlags = ConfigExFlags {
601601
configCabalVersion = projectConfigCabalVersion,
602+
configAppend = mempty,
603+
configOverwrite = mempty,
602604
configExConstraints = projectConfigConstraints,
603605
configPreferences = projectConfigPreferences,
604606
configSolver = projectConfigSolver,

cabal-install/src/Distribution/Client/Setup.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ configCompilerAux' configFlags =
599599
--
600600
data ConfigExFlags = ConfigExFlags {
601601
configCabalVersion :: Flag Version,
602+
configAppend :: Flag Bool,
603+
configOverwrite :: Flag Bool,
602604
configExConstraints :: [(UserConstraint, ConstraintSource)],
603605
configPreferences :: [PackageVersionConstraint],
604606
configSolver :: Flag PreSolver,
@@ -637,6 +639,14 @@ configureExOptions _showOrParseArgs src =
637639
(reqArg "VERSION" (parsecToReadE ("Cannot parse cabal lib version: "++)
638640
(fmap toFlag parsec))
639641
(map prettyShow. flagToList))
642+
, option "" ["append"]
643+
"appending the new config to the old config file"
644+
configAppend (\v flags -> flags { configAppend = v })
645+
(boolOpt [] [])
646+
, option "" ["overwrite"]
647+
"the backup of the config file before any alterations"
648+
configOverwrite (\v flags -> flags { configOverwrite = v })
649+
(boolOpt [] [])
640650
, option [] ["constraint"]
641651
"Specify constraints on a package (version, installed/source, flags)"
642652
configExConstraints (\v flags -> flags { configExConstraints = v })

0 commit comments

Comments
 (0)