Skip to content

Commit

Permalink
Add --print-type to exe:dhall-to-cabal
Browse files Browse the repository at this point in the history
See #22
  • Loading branch information
ocharles committed Feb 11, 2018
1 parent ce2a5b2 commit 539b0d2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
30 changes: 15 additions & 15 deletions dhall-to-cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ build-type: Simple
license: MIT
homepage: https://github.com/ocharles/dhall-to-cabal
bug-reports: https://github.com/ocharles/dhall-to-cabal/issues
tested-with: GHC ==8.2.2 GHC ==8.4.1

source-repository head
type: git
Expand All @@ -15,16 +14,16 @@ library
exposed-modules:
Distribution.Package.Dhall
build-depends:
base ^>=4.10,
Cabal ^>=2.0,
dhall ^>=1.9,
text ^>=1.2,
base ^>=4.10,
bytestring ^>=0.10,
containers ^>=0.5,
vector ^>=0.12,
trifecta ^>=1.7,
dhall ^>=1.9,
text ^>=1.2,
text-format ^>=0.3,
transformers ^>=0.5.2
transformers ^>=0.5.2,
trifecta ^>=1.7,
vector ^>=0.12
other-extensions: ApplicativeDo GADTs GeneralizedNewtypeDeriving
LambdaCase OverloadedStrings RecordWildCards TypeApplications
hs-source-dirs: lib
Expand All @@ -36,27 +35,28 @@ executable dhall-to-cabal
main-is: Main.hs
scope: public
build-depends:
Cabal ^>=2.0,
base ^>=4.10,
dhall ^>=1.9,
dhall-to-cabal -any,
optparse-applicative ^>=0.13.2 || ^>=0.14,
text ^>=1.2,
dhall ^>=1.9,
Cabal ^>=2.0
prettyprinter ^>=1.1.1,
text ^>=1.2
other-extensions: NamedFieldPuns
hs-source-dirs: exe

test-suite golden-tests
type: exitcode-stdio-1.0
main-is: GoldenTests.hs
build-depends:
bytestring ^>=0.10,
base ^>=4.10,
Cabal ^>=2.0,
text ^>=1.2,
tasty ^>=0.11,
filepath ^>=1.4,
Diff ^>=0.3.4,
bytestring ^>=0.10,
dhall-to-cabal -any,
filepath ^>=1.4,
tasty ^>=0.11,
tasty-golden ^>=2.3,
Diff ^>=0.3.4
text ^>=1.2
hs-source-dirs: golden-tests

3 changes: 3 additions & 0 deletions dhall-to-cabal.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ in let deps =
(majorBoundVersion (v "0.13.2"))
(majorBoundVersion (v "0.14"))
)
, prettyprinter =
package "prettyprinter" (majorBoundVersion (v "1.1.1"))
, tasty =
package "tasty" (majorBoundVersion (v "0.11"))
, tasty-golden =
Expand Down Expand Up @@ -113,6 +115,7 @@ in gitHub-project { owner = "ocharles", repo = "dhall-to-cabal" }
, deps.dhall
, deps.dhall-to-cabal
, deps.optparse-applicative
, deps.prettyprinter
, deps.text
]
, hs-source-dirs =
Expand Down
71 changes: 63 additions & 8 deletions exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,95 @@

module Main (main) where

import Data.Foldable ( asum )
import Data.Function ( (&) )
import System.Environment ( getArgs )

import Distribution.Package.Dhall

import qualified Data.Text.Lazy.IO as LazyText
import qualified Data.Text.Prettyprint.Doc as Pretty
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty
import qualified Dhall
import qualified Distribution.PackageDescription.PrettyPrint as Cabal
import qualified Options.Applicative as OptParse
import qualified System.IO



data Options = Options { dhallFilePath :: String }
data Command
= RunDhallToCabal DhallToCabalOptions
| PrintType



run :: Options -> IO ()
run Options { dhallFilePath } =
data DhallToCabalOptions = DhallToCabalOptions
{ dhallFilePath :: String }



dhallToCabalOptionsParser :: OptParse.Parser DhallToCabalOptions
dhallToCabalOptionsParser =
DhallToCabalOptions
<$> OptParse.argument
OptParse.str ( OptParse.metavar "<dhall input file>" )



printTypeParser :: OptParse.Parser ()
printTypeParser =
OptParse.flag' () ( OptParse.long "print-type" )



runDhallToCabal :: DhallToCabalOptions -> IO ()
runDhallToCabal DhallToCabalOptions { dhallFilePath } =
dhallFileToCabal dhallFilePath
& fmap Cabal.showGenericPackageDescription
>>= putStrLn



main :: IO ()
main =
OptParse.execParser opts >>= run
main = do
command <-
OptParse.execParser opts

case command of
RunDhallToCabal options ->
runDhallToCabal options

PrintType ->
printType

where

parser =
Options
<$> OptParse.argument
OptParse.str ( OptParse.metavar "<dhall input file>" )
asum
[ RunDhallToCabal <$> dhallToCabalOptionsParser
, PrintType <$ printTypeParser
]

opts =
OptParse.info parser mempty



-- Shamelessly taken from dhall-format

opts :: Pretty.LayoutOptions
opts =
Pretty.defaultLayoutOptions
{ Pretty.layoutPageWidth = Pretty.AvailablePerLine 80 1.0 }



printType :: IO ()
printType = do
Pretty.renderIO
System.IO.stdout
( Pretty.layoutSmart opts
( Pretty.pretty ( Dhall.expected genericPackageDescription ) )
)

putStrLn ""

0 comments on commit 539b0d2

Please sign in to comment.