Skip to content

Commit 539b0d2

Browse files
committed
Add --print-type to exe:dhall-to-cabal
See #22
1 parent ce2a5b2 commit 539b0d2

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

dhall-to-cabal.cabal

+15-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ build-type: Simple
55
license: MIT
66
homepage: https://github.com/ocharles/dhall-to-cabal
77
bug-reports: https://github.com/ocharles/dhall-to-cabal/issues
8-
tested-with: GHC ==8.2.2 GHC ==8.4.1
98

109
source-repository head
1110
type: git
@@ -15,16 +14,16 @@ library
1514
exposed-modules:
1615
Distribution.Package.Dhall
1716
build-depends:
18-
base ^>=4.10,
1917
Cabal ^>=2.0,
20-
dhall ^>=1.9,
21-
text ^>=1.2,
18+
base ^>=4.10,
2219
bytestring ^>=0.10,
2320
containers ^>=0.5,
24-
vector ^>=0.12,
25-
trifecta ^>=1.7,
21+
dhall ^>=1.9,
22+
text ^>=1.2,
2623
text-format ^>=0.3,
27-
transformers ^>=0.5.2
24+
transformers ^>=0.5.2,
25+
trifecta ^>=1.7,
26+
vector ^>=0.12
2827
other-extensions: ApplicativeDo GADTs GeneralizedNewtypeDeriving
2928
LambdaCase OverloadedStrings RecordWildCards TypeApplications
3029
hs-source-dirs: lib
@@ -36,27 +35,28 @@ executable dhall-to-cabal
3635
main-is: Main.hs
3736
scope: public
3837
build-depends:
38+
Cabal ^>=2.0,
3939
base ^>=4.10,
40+
dhall ^>=1.9,
4041
dhall-to-cabal -any,
4142
optparse-applicative ^>=0.13.2 || ^>=0.14,
42-
text ^>=1.2,
43-
dhall ^>=1.9,
44-
Cabal ^>=2.0
43+
prettyprinter ^>=1.1.1,
44+
text ^>=1.2
4545
other-extensions: NamedFieldPuns
4646
hs-source-dirs: exe
4747

4848
test-suite golden-tests
4949
type: exitcode-stdio-1.0
5050
main-is: GoldenTests.hs
5151
build-depends:
52-
bytestring ^>=0.10,
5352
base ^>=4.10,
5453
Cabal ^>=2.0,
55-
text ^>=1.2,
56-
tasty ^>=0.11,
57-
filepath ^>=1.4,
54+
Diff ^>=0.3.4,
55+
bytestring ^>=0.10,
5856
dhall-to-cabal -any,
57+
filepath ^>=1.4,
58+
tasty ^>=0.11,
5959
tasty-golden ^>=2.3,
60-
Diff ^>=0.3.4
60+
text ^>=1.2
6161
hs-source-dirs: golden-tests
6262

dhall-to-cabal.dhall

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ in let deps =
4747
(majorBoundVersion (v "0.13.2"))
4848
(majorBoundVersion (v "0.14"))
4949
)
50+
, prettyprinter =
51+
package "prettyprinter" (majorBoundVersion (v "1.1.1"))
5052
, tasty =
5153
package "tasty" (majorBoundVersion (v "0.11"))
5254
, tasty-golden =
@@ -113,6 +115,7 @@ in gitHub-project { owner = "ocharles", repo = "dhall-to-cabal" }
113115
, deps.dhall
114116
, deps.dhall-to-cabal
115117
, deps.optparse-applicative
118+
, deps.prettyprinter
116119
, deps.text
117120
]
118121
, hs-source-dirs =

exe/Main.hs

+63-8
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,95 @@
22

33
module Main (main) where
44

5+
import Data.Foldable ( asum )
56
import Data.Function ( (&) )
67
import System.Environment ( getArgs )
78

89
import Distribution.Package.Dhall
910

1011
import qualified Data.Text.Lazy.IO as LazyText
12+
import qualified Data.Text.Prettyprint.Doc as Pretty
13+
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty
1114
import qualified Dhall
1215
import qualified Distribution.PackageDescription.PrettyPrint as Cabal
1316
import qualified Options.Applicative as OptParse
17+
import qualified System.IO
1418

1519

1620

17-
data Options = Options { dhallFilePath :: String }
21+
data Command
22+
= RunDhallToCabal DhallToCabalOptions
23+
| PrintType
1824

1925

2026

21-
run :: Options -> IO ()
22-
run Options { dhallFilePath } =
27+
data DhallToCabalOptions = DhallToCabalOptions
28+
{ dhallFilePath :: String }
29+
30+
31+
32+
dhallToCabalOptionsParser :: OptParse.Parser DhallToCabalOptions
33+
dhallToCabalOptionsParser =
34+
DhallToCabalOptions
35+
<$> OptParse.argument
36+
OptParse.str ( OptParse.metavar "<dhall input file>" )
37+
38+
39+
40+
printTypeParser :: OptParse.Parser ()
41+
printTypeParser =
42+
OptParse.flag' () ( OptParse.long "print-type" )
43+
44+
45+
46+
runDhallToCabal :: DhallToCabalOptions -> IO ()
47+
runDhallToCabal DhallToCabalOptions { dhallFilePath } =
2348
dhallFileToCabal dhallFilePath
2449
& fmap Cabal.showGenericPackageDescription
2550
>>= putStrLn
2651

2752

2853

2954
main :: IO ()
30-
main =
31-
OptParse.execParser opts >>= run
55+
main = do
56+
command <-
57+
OptParse.execParser opts
58+
59+
case command of
60+
RunDhallToCabal options ->
61+
runDhallToCabal options
62+
63+
PrintType ->
64+
printType
3265

3366
where
3467

3568
parser =
36-
Options
37-
<$> OptParse.argument
38-
OptParse.str ( OptParse.metavar "<dhall input file>" )
69+
asum
70+
[ RunDhallToCabal <$> dhallToCabalOptionsParser
71+
, PrintType <$ printTypeParser
72+
]
3973

4074
opts =
4175
OptParse.info parser mempty
76+
77+
78+
79+
-- Shamelessly taken from dhall-format
80+
81+
opts :: Pretty.LayoutOptions
82+
opts =
83+
Pretty.defaultLayoutOptions
84+
{ Pretty.layoutPageWidth = Pretty.AvailablePerLine 80 1.0 }
85+
86+
87+
88+
printType :: IO ()
89+
printType = do
90+
Pretty.renderIO
91+
System.IO.stdout
92+
( Pretty.layoutSmart opts
93+
( Pretty.pretty ( Dhall.expected genericPackageDescription ) )
94+
)
95+
96+
putStrLn ""

0 commit comments

Comments
 (0)