|
1 |
| -{-# LANGUAGE OverloadedStrings #-} |
2 |
| -{-# LANGUAGE RecordWildCards #-} |
3 | 1 | module Main where
|
4 | 2 |
|
5 |
| -import Control.Applicative (optional, (<|>)) |
6 |
| -import Control.Exception (SomeException) |
7 |
| -import Data.Monoid ((<>)) |
8 |
| -import Dhall.JSON (parsePreservationAndOmission, parseConversion) |
9 |
| -import Dhall.Yaml (Options(..), dhallToYaml, parseDocuments, parseQuoted) |
10 |
| -import Options.Applicative (Parser, ParserInfo) |
11 |
| - |
12 |
| -import qualified Control.Exception |
13 |
| -import qualified Data.ByteString |
14 |
| -import qualified Data.Text.IO as Text.IO |
15 |
| -import qualified Data.Version |
16 |
| -import qualified GHC.IO.Encoding |
17 |
| -import qualified Options.Applicative as Options |
18 |
| -import qualified Paths_dhall_json as Meta |
19 |
| -import qualified System.Exit |
20 |
| -import qualified System.IO |
21 |
| - |
22 |
| -parseOptions :: Parser (Maybe Options) |
23 |
| -parseOptions = |
24 |
| - Just |
25 |
| - <$> ( Options |
26 |
| - <$> parseExplain |
27 |
| - <*> Dhall.JSON.parsePreservationAndOmission |
28 |
| - <*> parseDocuments |
29 |
| - <*> parseQuoted |
30 |
| - <*> Dhall.JSON.parseConversion |
31 |
| - <*> optional parseFile |
32 |
| - <*> optional parseOutput |
33 |
| - ) |
34 |
| - <|> parseVersion |
35 |
| - where |
36 |
| - parseExplain = |
37 |
| - Options.switch |
38 |
| - ( Options.long "explain" |
39 |
| - <> Options.help "Explain error messages in detail" |
40 |
| - ) |
41 |
| - |
42 |
| - parseFile = |
43 |
| - Options.strOption |
44 |
| - ( Options.long "file" |
45 |
| - <> Options.help "Read expression from a file instead of standard input" |
46 |
| - <> Options.metavar "FILE" |
47 |
| - ) |
48 |
| - |
49 |
| - parseVersion = |
50 |
| - Options.flag' |
51 |
| - Nothing |
52 |
| - ( Options.long "version" |
53 |
| - <> Options.help "Display version" |
54 |
| - ) |
55 |
| - |
56 |
| - parseOutput = |
57 |
| - Options.strOption |
58 |
| - ( Options.long "output" |
59 |
| - <> Options.help "Write YAML to a file instead of standard output" |
60 |
| - <> Options.metavar "FILE" |
61 |
| - ) |
62 |
| - |
63 |
| -parserInfo :: ParserInfo (Maybe Options) |
64 |
| -parserInfo = |
65 |
| - Options.info |
66 |
| - (Options.helper <*> parseOptions) |
67 |
| - ( Options.fullDesc |
68 |
| - <> Options.progDesc "Compile Dhall to YAML" |
69 |
| - ) |
| 3 | +import qualified Dhall.DhallToYaml.Main |
| 4 | +import qualified Dhall.JSON.Yaml |
| 5 | +import qualified Paths_dhall_json as Meta |
70 | 6 |
|
71 | 7 | main :: IO ()
|
72 |
| -main = do |
73 |
| - GHC.IO.Encoding.setLocaleEncoding GHC.IO.Encoding.utf8 |
74 |
| - |
75 |
| - maybeOptions <- Options.execParser parserInfo |
76 |
| - |
77 |
| - case maybeOptions of |
78 |
| - Nothing -> do |
79 |
| - putStrLn (Data.Version.showVersion Meta.version) |
80 |
| - |
81 |
| - Just options@(Options {..}) -> do |
82 |
| - handle $ do |
83 |
| - contents <- case file of |
84 |
| - Nothing -> Text.IO.getContents |
85 |
| - Just path -> Text.IO.readFile path |
86 |
| - |
87 |
| - let write = |
88 |
| - case output of |
89 |
| - Nothing -> Data.ByteString.putStr |
90 |
| - Just file_ -> Data.ByteString.writeFile file_ |
91 |
| - |
92 |
| - write =<< dhallToYaml options file contents |
93 |
| - |
94 |
| -handle :: IO a -> IO a |
95 |
| -handle = Control.Exception.handle handler |
96 |
| - where |
97 |
| - handler :: SomeException -> IO a |
98 |
| - handler e = do |
99 |
| - System.IO.hPutStrLn System.IO.stderr "" |
100 |
| - System.IO.hPrint System.IO.stderr e |
101 |
| - System.Exit.exitFailure |
| 8 | +main = Dhall.DhallToYaml.Main.main Meta.version Dhall.JSON.Yaml.dhallToYaml |
0 commit comments