Skip to content

Commit

Permalink
Backup local files further, and make it respect --project-file
Browse files Browse the repository at this point in the history
  • Loading branch information
lukel97 committed Jun 6, 2020
1 parent 74ff63d commit 55f965d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ctags

# stack artifacts
/.stack-work/
stack.yaml.lock

# Shake artifacts
.shake*
Expand All @@ -60,3 +61,6 @@ register.sh

# python artifacts from documentation builds
*.pyc

# macOS folder metadata
.DS_Store
27 changes: 24 additions & 3 deletions cabal-install/Distribution/Client/CmdConfigure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Distribution.Client.Compat.Prelude
import Prelude ()

import System.Directory
import System.FilePath
import qualified Data.Map as Map

import Distribution.Client.ProjectOrchestration
Expand All @@ -30,6 +31,9 @@ import Distribution.Simple.Command
import Distribution.Simple.Utils
( wrapText, notice )

import Distribution.Client.DistDirLayout
( DistDirLayout(..) )

configureCommand :: CommandUI (NixStyleFlags ())
configureCommand = CommandUI {
commandName = "v2-configure",
Expand Down Expand Up @@ -93,10 +97,26 @@ configureAction flags@NixStyleFlags {..} _extraArgs globalFlags = do
-- Write out the @cabal.project.local@ so it gets picked up by the
-- planning phase. If old config exists, then print the contents
-- before overwriting
exists <- doesFileExist "cabal.project.local"

let localFile = distProjectFile (distDirLayout baseCtx) "local"
-- | Chooses cabal.project.local~, or if it already exists
-- cabal.project.local~0, cabal.project.local~1 etc.
firstFreeBackup = firstFreeBackup' (0 :: Int)
firstFreeBackup' i = do
let backup = localFile <> "~" <> (if i <= 0 then "" else show (i - 1))
exists <- doesFileExist backup
if exists
then firstFreeBackup' (i + 1)
else return backup

-- If cabal.project.local already exists, back up to cabal.project.local~[n]
exists <- doesFileExist localFile
when exists $ do
notice verbosity "'cabal.project.local' already exists, backing it up to 'cabal.project.local~'."
copyFile "cabal.project.local" "cabal.project.local~"
backup <- firstFreeBackup
notice verbosity $
quote (takeFileName localFile) <> " already exists, backing it up to "
<> quote (takeFileName backup) <> "."
copyFile localFile backup
writeProjectLocalExtraConfig (distDirLayout baseCtx)
cliConfig

Expand Down Expand Up @@ -125,4 +145,5 @@ configureAction flags@NixStyleFlags {..} _extraArgs globalFlags = do
verbosity = fromFlagOrDefault normal (configVerbosity configFlags)
cliConfig = commandLineFlagsToProjectConfig globalFlags flags
mempty -- ClientInstallFlags, not needed here
quote s = "'" <> s <> "'"

2 changes: 1 addition & 1 deletion cabal-install/Distribution/Client/TargetProblem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ data TargetProblem a
| TargetProblemNoSuchPackage PackageId
| TargetProblemNoSuchComponent PackageId ComponentName

| CustomTargetProblem a
-- | A custom target problem
| CustomTargetProblem a
deriving (Eq, Show, Functor)

-- | Type alias for a 'TargetProblem' with no user-defined problems/errors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@ Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- NewConfigure-0.1.0.0 (lib) (first run)
# cabal v2-configure
'cabal.project.local' already exists, backing it up to 'cabal.project.local~0'.
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- NewConfigure-0.1.0.0 (lib) (first run)
# cabal v2-configure
'cabal.project.local' already exists, backing it up to 'cabal.project.local~1'.
Build profile: -w ghc-<GHCVER> -O1
In order, the following would be built:
- NewConfigure-0.1.0.0 (lib) (first run)
# cabal v2-configure
Warning: There are no packages or optional-packages in the project
'foo.project.local' already exists, backing it up to 'foo.project.local~'.
Resolving dependencies...
Up to date
# cabal v2-configure
Warning: There are no packages or optional-packages in the project
'foo.project.local' already exists, backing it up to 'foo.project.local~0'.
Up to date
# cabal v2-configure
Warning: There are no packages or optional-packages in the project
'foo.project.local' already exists, backing it up to 'foo.project.local~1'.
Up to date
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,20 @@ import Test.Cabal.Prelude

main = cabalTest $
withSourceCopy $ do
r <- cabal' "v2-configure" []
assertOutputContains "backing it up to" r
cabal' "v2-configure" [] >>=
assertOutputContains "backing it up to 'cabal.project.local~'"
cabal' "v2-configure" [] >>=
assertOutputContains "backing it up to 'cabal.project.local~0'"
cabal' "v2-configure" [] >>=
assertOutputContains "backing it up to 'cabal.project.local~1'"

-- With --project-file
cabal' "v2-configure" ["--project-file", "foo.project"] >>=
assertOutputContains
"'foo.project.local' already exists, backing it up to 'foo.project.local~'"
cabal' "v2-configure" ["--project-file", "foo.project"] >>=
assertOutputContains
"'foo.project.local' already exists, backing it up to 'foo.project.local~0'"
cabal' "v2-configure" ["--project-file", "foo.project"] >>=
assertOutputContains
"'foo.project.local' already exists, backing it up to 'foo.project.local~1'"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
verbose: verbose +nowrap +markoutput
builddir: /Users/luke/Source/cabal/cabal-testsuite/PackageTests/NewConfigure/LocalConfigOverwrite/cabal.dist/work/./dist
jobs: 1
project-file: foo.project
ignore-project: False

0 comments on commit 55f965d

Please sign in to comment.