Skip to content

Commit

Permalink
Use atomic-write for inplace file modifications (#1580)
Browse files Browse the repository at this point in the history
Fixes #498.
  • Loading branch information
sjakobi authored and Gabriella439 committed Dec 2, 2019
1 parent 85645a2 commit a0bb273
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
6 changes: 4 additions & 2 deletions dhall/src/Dhall/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import qualified Control.Exception
import qualified Data.Text.IO
import qualified Dhall.Pretty
import qualified Dhall.Util
import qualified System.AtomicWrite.Writer.LazyText as AtomicWrite.LazyText
import qualified System.Console.ANSI
import qualified System.IO

Expand Down Expand Up @@ -68,8 +69,9 @@ format (Format {..}) = do

case inplace of
InputFile file -> do
System.IO.withFile file System.IO.WriteMode (\handle -> do
Pretty.Terminal.renderIO handle (Pretty.unAnnotateS docStream))
AtomicWrite.LazyText.atomicWriteFile
file
(Pretty.Text.renderLazy docStream)

StandardInput -> do
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
Expand Down
9 changes: 6 additions & 3 deletions dhall/src/Dhall/Freeze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import qualified Control.Exception
import qualified Control.Monad.Trans.State.Strict as State
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
import qualified Dhall.Core
import qualified Dhall.Import
import qualified Dhall.Optics
import qualified Dhall.Pretty
import qualified Dhall.TypeCheck
import qualified Dhall.Util
import qualified System.AtomicWrite.Writer.LazyText as AtomicWrite.LazyText
import qualified System.FilePath
import qualified System.IO

Expand Down Expand Up @@ -94,9 +96,10 @@ writeExpr inplace (header, expr) characterSet = do
let unAnnotated = Pretty.unAnnotateS stream

case inplace of
InputFile f ->
System.IO.withFile f System.IO.WriteMode (\handle -> do
Pretty.renderIO handle unAnnotated)
InputFile file ->
AtomicWrite.LazyText.atomicWriteFile
file
(Pretty.Text.renderLazy unAnnotated)

StandardInput -> do
supportsANSI <- System.Console.ANSI.hSupportsANSI System.IO.stdout
Expand Down
33 changes: 18 additions & 15 deletions dhall/src/Dhall/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import qualified Data.Text
import qualified Data.Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
import qualified Dhall
import qualified Dhall.Binary
import qualified Dhall.Core
Expand All @@ -72,6 +73,7 @@ import qualified Dhall.TypeCheck
import qualified Dhall.Util
import qualified GHC.IO.Encoding
import qualified Options.Applicative
import qualified System.AtomicWrite.Writer.LazyText as AtomicWrite.LazyText
import qualified System.Console.ANSI
import qualified System.Exit as Exit
import qualified System.IO
Expand Down Expand Up @@ -500,6 +502,12 @@ command (Options {..}) = do

renderDoc h doc

let writeDocToFile :: FilePath -> Doc ann -> IO ()
writeDocToFile file doc = do
let stream = Dhall.Pretty.layout (doc <> "\n")

AtomicWrite.LazyText.atomicWriteFile file (Pretty.Text.renderLazy stream)

when (not $ ignoreSemanticCache mode) Dhall.Import.warnAboutMissingCaches

handle $ case mode of
Expand Down Expand Up @@ -534,8 +542,11 @@ command (Options {..}) = do

case output of
StandardOutput -> render System.IO.stdout annotatedExpression

OutputFile file_ ->
System.IO.withFile file_ System.IO.WriteMode $ \h -> render h annotatedExpression
writeDocToFile
file_
(Dhall.Pretty.prettyCharacterSet characterSet annotatedExpression)

Resolve { resolveMode = Just Dot, ..} -> do
expression <- getExpression file
Expand Down Expand Up @@ -666,23 +677,15 @@ command (Options {..}) = do
Lint {..} -> do
(Header header, expression) <- getExpressionAndHeader inplace

case inplace of
InputFile file -> do
let lintedExpression = Dhall.Lint.lint expression
let lintedExpression = Dhall.Lint.lint expression

let doc = Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet lintedExpression
let doc = Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet lintedExpression

System.IO.withFile file System.IO.WriteMode (\h -> do
renderDoc h doc )

StandardInput -> do
let lintedExpression = Dhall.Lint.lint expression

let doc = Pretty.pretty header
<> Dhall.Pretty.prettyCharacterSet characterSet lintedExpression
case inplace of
InputFile file -> writeDocToFile file doc

renderDoc System.IO.stdout doc
StandardInput -> renderDoc System.IO.stdout doc

Encode {..} -> do
expression <- getExpression file
Expand Down

0 comments on commit a0bb273

Please sign in to comment.