Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary writes on --force (see #555) #557

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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