Skip to content

Commit e70b5eb

Browse files
ptkatoemilypi
andcommitted
cabal v2-configure, see issue #7405
This commit straightens the v2-configure command: * Removes the pre-build phase * Adds two flags, --append and --overwrite Co-authored-by: Emily Pillmore <emilypi@cohomolo.gy>
1 parent 873216f commit e70b5eb

File tree

6 files changed

+50
-57
lines changed

6 files changed

+50
-57
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/src/Distribution/Client/CmdConfigure.hs

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44
module Distribution.Client.CmdConfigure (
55
configureCommand,
66
configureAction,
7+
configureAction',
78
) where
89

910
import Distribution.Client.Compat.Prelude
1011
import Prelude ()
1112

1213
import System.Directory
1314
import System.FilePath
14-
import qualified Data.Map as Map
1515

16+
import Distribution.Simple.Flag
1617
import Distribution.Client.ProjectOrchestration
1718
import Distribution.Client.ProjectConfig
18-
( writeProjectLocalExtraConfig )
19+
( writeProjectLocalExtraConfig, readProjectLocalExtraConfig )
1920

2021
import Distribution.Client.NixStyleOptions
2122
( NixStyleFlags (..), nixStyleOptions, defaultNixStyleFlags )
2223
import Distribution.Client.Setup
23-
( GlobalFlags, ConfigFlags(..) )
24-
import Distribution.Simple.Flag
25-
( fromFlagOrDefault )
24+
( GlobalFlags, ConfigFlags(..), ConfigExFlags(..) )
2625
import Distribution.Verbosity
2726
( normal )
2827

@@ -33,6 +32,8 @@ import Distribution.Simple.Utils
3332

3433
import Distribution.Client.DistDirLayout
3534
( DistDirLayout(..) )
35+
import Distribution.Client.RebuildMonad (runRebuild)
36+
import Distribution.Client.ProjectConfig.Types
3637

3738
configureCommand :: CommandUI (NixStyleFlags ())
3839
configureCommand = CommandUI {
@@ -88,61 +89,39 @@ configureCommand = CommandUI {
8889
-- "Distribution.Client.ProjectOrchestration"
8990
--
9091
configureAction :: 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-

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+
configBackup = combine configBackup,
422424
-- TODO: NubListify
423425
configExConstraints = lastNonEmpty configExConstraints,
424426
-- TODO: NubListify

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Distribution.Client.ProjectConfig (
2727
-- * Project config files
2828
readProjectConfig,
2929
readGlobalConfig,
30+
readProjectLocalExtraConfig,
3031
readProjectLocalFreezeConfig,
3132
withProjectOrGlobalConfig,
3233
writeProjectLocalExtraConfig,

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+
configBackup = 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+
configBackup :: 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 "" ["backup"]
647+
"the backup of the config file before any alterations"
648+
configBackup (\v flags -> flags { configBackup = 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)