gRPC-haskell/tools/compile-proto-file/Main.hs
Joel Stanley db3d66c4eb Add compile-proto-file helper cli tool (#87)
* Add compile-proto-file helper cli tool (.proto -> ast -> .hs)

* Update default.nix
2016-12-21 11:47:05 -06:00

26 lines
1 KiB
Haskell

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
import Data.Monoid ((<>))
import Data.Protobuf.Wire.DotProto.Generate
import Filesystem.Path.CurrentOS (encodeString)
import Options.Generic
import Prelude hiding (FilePath)
import Turtle (FilePath)
data Args = Args
{ proto :: FilePath <?> "Path to input .proto file"
} deriving (Generic, Show)
instance ParseRecord Args
main :: IO ()
main = do
protoPath <- encodeString . unHelpful . proto <$> getRecord "Dumps a compiled .proto file to stdout"
readDotProtoWithContext protoPath >>= \case
Left err -> fail (show err)
Right (dp, ctx) -> case renderHsModuleForDotProto dp ctx of
Left err -> fail ("Error compiling " <> protoPath <> ": " <> show err)
Right src -> putStrLn src