Skip to content

Commit

Permalink
Avoid unnecessary writes on --force (see #555)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Aug 6, 2023
1 parent a906d35 commit 4afeca6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changes in 0.36.0
- Add `--canonical`
- Avoid unnecessary writes on `--force` (see #555)
- When an existing `.cabal` does not align fields then do not align fields in
the generated `.cabal` file.
- Fix a bug related to git conflict markers in existing `.cabal` files: When a
Expand Down
2 changes: 1 addition & 1 deletion src/Hpack.hs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ writeCabalFile :: DecodeOptions -> Bool -> FilePath -> NewCabalFile -> IO ()
writeCabalFile options toStdout name cabalFile = do
write . unlines $ renderCabalFile (decodeOptionsTarget options) cabalFile
where
write = if toStdout then Utf8.putStr else Utf8.writeFile name
write = if toStdout then Utf8.putStr else Utf8.ensureFile name

makeCabalFile :: OutputStrategy -> GenerateHashStrategy -> Maybe ExistingCabalFile -> [String] -> Version -> Package -> NewCabalFile
makeCabalFile outputStrategy generateHashStrategy mExistingCabalFile cabalVersion v pkg = cabalFile
Expand Down
15 changes: 12 additions & 3 deletions src/Hpack/Utf8.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Hpack.Utf8 (
encodeUtf8
, readFile
, writeFile
, ensureFile
, putStr
, hPutStr
, hPutStrLn
) where

import Prelude hiding (readFile, writeFile, putStr)

import Control.Monad
import Control.Exception (try, IOException)
import qualified Data.Text as T
import qualified Data.Text.Encoding as Encoding
import Data.Text.Encoding.Error (lenientDecode)
Expand Down Expand Up @@ -48,8 +52,13 @@ decodeNewlines = go
readFile :: FilePath -> IO String
readFile = fmap decodeText . B.readFile

writeFile :: FilePath -> String -> IO ()
writeFile name xs = withFile name WriteMode (`hPutStr` xs)
ensureFile :: FilePath -> String -> IO ()
ensureFile name new = do
try (readFile name) >>= \ case
Left (_ :: IOException) -> do
withFile name WriteMode (`hPutStr` new)
Right old -> unless (old == new) $ do
withFile name WriteMode (`hPutStr` new)

putStr :: String -> IO ()
putStr = hPutStr stdout
Expand Down
4 changes: 2 additions & 2 deletions test/Hpack/Utf8Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec = do
B.writeFile name "foo\r\nbar"
Utf8.readFile name `shouldReturn` "foo\nbar"

describe "writeFile" $ do
describe "ensureFile" $ do
it "uses system specific newline encoding" $ do
inTempDirectory $ do
let
Expand All @@ -28,5 +28,5 @@ spec = do
writeFile name c
systemSpecific <- B.readFile name

Utf8.writeFile name c
Utf8.ensureFile name c
B.readFile name `shouldReturn` systemSpecific

0 comments on commit 4afeca6

Please sign in to comment.