Skip to content

Commit

Permalink
Add rewriteFileEx to revert rewriteFile changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Apr 29, 2017
1 parent f101373 commit 5797013
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Cabal/Distribution/Simple/Build.hs
Expand Up @@ -657,7 +657,7 @@ writeAutogenFiles verbosity pkg lbi clbi = do
pathsModuleDir = takeDirectory pathsModulePath
-- Ensure that the directory exists!
createDirectoryIfMissingVerbose verbosity True pathsModuleDir
rewriteFile verbosity pathsModulePath (Build.PathsModule.generate pkg lbi clbi)
rewriteFileEx verbosity pathsModulePath (Build.PathsModule.generate pkg lbi clbi)

--TODO: document what we're doing here, and move it to its own function
case clbi of
Expand All @@ -672,10 +672,10 @@ writeAutogenFiles verbosity pkg lbi clbi = do
let sigPath = autogenComponentModulesDir lbi clbi
</> ModuleName.toFilePath mod_name <.> "hsig"
createDirectoryIfMissingVerbose verbosity True (takeDirectory sigPath)
rewriteFile verbosity sigPath $
rewriteFileEx verbosity sigPath $
"{-# LANGUAGE NoImplicitPrelude #-}\n" ++
"signature " ++ display mod_name ++ " where"
_ -> return ()

let cppHeaderPath = autogenComponentModulesDir lbi clbi </> cppHeaderName
rewriteFile verbosity cppHeaderPath (Build.Macros.generate pkg lbi clbi)
rewriteFileEx verbosity cppHeaderPath (Build.Macros.generate pkg lbi clbi)
11 changes: 9 additions & 2 deletions Cabal/Distribution/Simple/Utils.hs
Expand Up @@ -126,6 +126,7 @@ module Distribution.Simple.Utils (
withFileContents,
writeFileAtomic,
rewriteFile,
rewriteFileEx,

-- * Unicode
fromUTF8,
Expand Down Expand Up @@ -1389,13 +1390,19 @@ withTempDirectoryEx _verbosity opts targetDir template f = withFrozenCallStack $
-----------------------------------
-- Safely reading and writing files

-- | See 'rewriteFileEx'
--
-- This function is provided for backwards-compatibility
rewriteFile :: FilePath -> String -> IO ()
rewriteFile = rewriteFileEx normal

-- | Write a file but only if it would have new content. If we would be writing
-- the same as the existing content then leave the file as is so that we do not
-- update the file's modification time.
--
-- NB: the file is assumed to be ASCII-encoded.
rewriteFile :: Verbosity -> FilePath -> String -> IO ()
rewriteFile verbosity path newContent =
rewriteFileEx :: Verbosity -> FilePath -> String -> IO ()
rewriteFileEx verbosity path newContent =
flip catchIO mightNotExist $ do
existingContent <- annotateIO verbosity $ readFile path
_ <- evaluate (length existingContent)
Expand Down
6 changes: 3 additions & 3 deletions cabal-install/Distribution/Client/SetupWrapper.hs
Expand Up @@ -88,7 +88,7 @@ import Distribution.Simple.Setup
import Distribution.Simple.Utils
( die', debug, info, infoNoWrap, cabalVersion, tryFindPackageDesc, comparing
, createDirectoryIfMissingVerbose, installExecutableFile
, copyFileVerbose, rewriteFile )
, copyFileVerbose, rewriteFileEx )
import Distribution.Client.Utils
( inDir, tryCanonicalizePath, withExtraPathEnv
, existsAndIsMoreRecentThan, moreRecentFile, withEnv
Expand Down Expand Up @@ -692,7 +692,7 @@ getExternalSetupMethod verbosity options pkg bt = do
customSetupLhs = workingDir options </> "Setup.lhs"

updateSetupScript cabalLibVersion _ =
rewriteFile verbosity setupHs (buildTypeScript cabalLibVersion)
rewriteFileEx verbosity setupHs (buildTypeScript cabalLibVersion)

buildTypeScript :: Version -> String
buildTypeScript cabalLibVersion = case bt of
Expand Down Expand Up @@ -888,7 +888,7 @@ getExternalSetupMethod verbosity options pkg bt = do
}
let ghcCmdLine = renderGhcOptions compiler platform ghcOptions
when (useVersionMacros options') $
rewriteFile verbosity cppMacrosFile
rewriteFileEx verbosity cppMacrosFile
(generatePackageVersionMacros (map snd selectedDeps))
case useLoggingHandle options of
Nothing -> runDbProgram verbosity program progdb ghcCmdLine
Expand Down

0 comments on commit 5797013

Please sign in to comment.