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

dhall-json: Add --output options #1304

Merged
merged 2 commits into from
Sep 11, 2019
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
17 changes: 15 additions & 2 deletions dhall-json/dhall-to-json/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Options.Applicative (Parser, ParserInfo)
import qualified Control.Exception
import qualified Data.Aeson
import qualified Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Char8
import qualified Data.ByteString.Lazy
import qualified Data.Text.IO as Text.IO
import qualified Dhall
Expand All @@ -33,6 +32,7 @@ data Options
, conversion :: Conversion
, approximateSpecialDoubles :: Bool
, file :: Maybe FilePath
, output :: Maybe FilePath
}
| Version

Expand All @@ -45,6 +45,7 @@ parseOptions =
<*> Dhall.JSON.parseConversion
<*> parseApproximateSpecialDoubles
<*> optional parseFile
<*> optional parseOutput
)
<|> parseVersion
where
Expand Down Expand Up @@ -94,6 +95,13 @@ parseOptions =
<> Options.metavar "FILE"
)

parseOutput =
Options.strOption
( Options.long "output"
<> Options.help "Write JSON to a file instead of standard output"
<> Options.metavar "FILE"
)

parserInfo :: ParserInfo Options
parserInfo =
Options.info
Expand Down Expand Up @@ -137,7 +145,12 @@ main = do

json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode file text)

Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
let write =
case output of
Nothing -> Data.ByteString.Lazy.putStr
Just file_ -> Data.ByteString.Lazy.writeFile file_

write (encode json <> "\n")

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down
15 changes: 14 additions & 1 deletion dhall-json/dhall-to-yaml/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ parseOptions =
<*> parseQuoted
<*> Dhall.JSON.parseConversion
<*> optional parseFile
<*> optional parseOutput
)
<|> parseVersion
where
Expand All @@ -52,6 +53,13 @@ parseOptions =
<> Options.help "Display version"
)

parseOutput =
Options.strOption
( Options.long "output"
<> Options.help "Write YAML to a file instead of standard output"
<> Options.metavar "FILE"
)

parserInfo :: ParserInfo (Maybe Options)
parserInfo =
Options.info
Expand All @@ -76,7 +84,12 @@ main = do
Nothing -> Text.IO.getContents
Just path -> Text.IO.readFile path

Data.ByteString.putStr =<< dhallToYaml options file contents
let write =
case output of
Nothing -> Data.ByteString.putStr
Just file_ -> Data.ByteString.writeFile file_

write =<< dhallToYaml options file contents

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down
32 changes: 25 additions & 7 deletions dhall-json/json-to-dhall/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as ByteString
import qualified Data.Text.IO as Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
import qualified GHC.IO.Encoding
import qualified Options.Applicative as Options
import qualified System.Console.ANSI as ANSI
Expand All @@ -49,6 +50,7 @@ data Options
{ schema :: Text
, conversion :: Conversion
, file :: Maybe FilePath
, output :: Maybe FilePath
, ascii :: Bool
, plain :: Bool
}
Expand All @@ -62,6 +64,7 @@ parseOptions =
<$> parseSchema
<*> parseConversion
<*> optional parseFile
<*> optional parseOutput
<*> parseASCII
<*> parsePlain
)
Expand All @@ -88,6 +91,13 @@ parseOptions =
<> Options.metavar "FILE"
)

parseOutput =
Options.strOption
( Options.long "output"
<> Options.help "Write Dhall expression to a file instead of standard output"
<> Options.metavar "FILE"
)

parseASCII =
Options.switch
( Options.long "ascii"
Expand Down Expand Up @@ -138,16 +148,24 @@ main = do

let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document

supportsANSI <- ANSI.hSupportsANSI IO.stdout
case output of
Nothing -> do
supportsANSI <- ANSI.hSupportsANSI IO.stdout

let ansiStream =
if supportsANSI && not plain
then fmap Dhall.Pretty.annToAnsiStyle stream
else Pretty.unAnnotateS stream

Pretty.Terminal.renderIO IO.stdout ansiStream

let ansiStream =
if supportsANSI && not plain
then fmap Dhall.Pretty.annToAnsiStyle stream
else Pretty.unAnnotateS stream
Text.IO.putStrLn ""

Pretty.Terminal.renderIO IO.stdout ansiStream
Just file_ ->
IO.withFile file_ IO.WriteMode $ \h -> do
Pretty.Text.renderIO h stream

Text.IO.putStrLn ""
Text.IO.hPutStrLn h ""

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down
2 changes: 2 additions & 0 deletions dhall-json/src/Dhall/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ data Options = Options
, quoted :: Bool
, conversion :: Conversion
, file :: Maybe FilePath
, output :: Maybe FilePath
}

defaultOptions :: Options
Expand All @@ -48,6 +49,7 @@ defaultOptions =
, quoted = False
, conversion = NoConversion
, file = Nothing
, output = Nothing
}

parseDocuments :: Parser Bool
Expand Down
31 changes: 24 additions & 7 deletions dhall-json/yaml-to-dhall/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import qualified Data.ByteString.Char8 as BSL8
import qualified Data.Text.IO as Text.IO
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
import qualified Dhall.Pretty
import qualified GHC.IO.Encoding
import qualified Options.Applicative as Options
Expand All @@ -40,6 +41,7 @@ data CommandOptions
{ schema :: Text
, conversion :: Conversion
, file :: Maybe FilePath
, output :: Maybe FilePath
, ascii :: Bool
, plain :: Bool
}
Expand All @@ -61,6 +63,7 @@ parseOptions =
<$> parseSchema
<*> parseConversion
<*> optional parseFile
<*> optional parseOutput
<*> parseASCII
<*> parsePlain
)
Expand All @@ -87,6 +90,13 @@ parseOptions =
<> Options.metavar "FILE"
)

parseOutput =
Options.strOption
( Options.long "output"
<> Options.help "Write Dhall expression to a file instead of standard output"
<> Options.metavar "FILE"
)

parseASCII =
Options.switch
( Options.long "ascii"
Expand Down Expand Up @@ -129,17 +139,24 @@ main = do

let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document

supportsANSI <- ANSI.hSupportsANSI IO.stdout
case output of
Nothing -> do
supportsANSI <- ANSI.hSupportsANSI IO.stdout

let ansiStream =
if supportsANSI && not plain
then fmap Dhall.Pretty.annToAnsiStyle stream
else Pretty.unAnnotateS stream

let ansiStream =
if supportsANSI && not plain
then fmap Dhall.Pretty.annToAnsiStyle stream
else Pretty.unAnnotateS stream
Pretty.Terminal.renderIO IO.stdout ansiStream

Pretty.Terminal.renderIO IO.stdout ansiStream
Text.IO.putStrLn ""

Text.IO.putStrLn ""
Just file_ ->
IO.withFile file_ IO.WriteMode $ \h -> do
Pretty.Text.renderIO h stream

Text.IO.hPutStrLn h ""

handle :: IO a -> IO a
handle = Control.Exception.handle handler
Expand Down