From db3d66c4eba48daa210a09a1c6d6ef9f09ccb11e Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Wed, 21 Dec 2016 11:47:05 -0600 Subject: [PATCH] Add `compile-proto-file` helper cli tool (#87) * Add compile-proto-file helper cli tool (.proto -> ast -> .hs) * Update default.nix --- default.nix | 13 +++++++------ grpc-haskell.cabal | 21 +++++++++++++++++++-- tools/compile-proto-file/Main.hs | 26 ++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 tools/compile-proto-file/Main.hs diff --git a/default.nix b/default.nix index 9f1f795..2685f99 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,8 @@ { mkDerivation, async, base, bytestring, c2hs, clock, containers -, grpc, managed, pipes, proto3-wire, protobuf-wire, QuickCheck -, random, safe, sorted-list, stdenv, stm, tasty, tasty-hunit -, tasty-quickcheck, text, time, transformers, turtle, unix, vector +, grpc, managed, optparse-generic, pipes, proto3-wire +, protobuf-wire, QuickCheck, random, safe, sorted-list, stdenv, stm +, system-filepath, tasty, tasty-hunit, tasty-quickcheck, text, time +, transformers, turtle, unix, vector }: mkDerivation { pname = "grpc-haskell"; @@ -17,15 +18,15 @@ mkDerivation { librarySystemDepends = [ grpc ]; libraryToolDepends = [ c2hs ]; executableHaskellDepends = [ - async base bytestring containers proto3-wire protobuf-wire random - text transformers + async base bytestring containers optparse-generic proto3-wire + protobuf-wire random system-filepath text transformers turtle ]; testHaskellDepends = [ async base bytestring clock containers managed pipes protobuf-wire QuickCheck safe tasty tasty-hunit tasty-quickcheck text time transformers turtle unix ]; - homepage = "http://github.com/aloiscochard/grpc-haskell"; + homepage = "https://github.mv.awakenetworks.net/awakenetworks/gRPC-haskell"; description = "Haskell implementation of gRPC layered on shared C library"; license = stdenv.lib.licenses.asl20; } diff --git a/grpc-haskell.cabal b/grpc-haskell.cabal index c62f82a..92e34f5 100644 --- a/grpc-haskell.cabal +++ b/grpc-haskell.cabal @@ -4,7 +4,7 @@ synopsis: Haskell implementation of gRPC layered on shared C library. homepage: https://github.mv.awakenetworks.net/awakenetworks/gRPC-haskell license: Apache-2.0 license-file: LICENSE -author: Connor Clark +author: Connor Clark maintainer: connor@awakenetworks.com copyright: Copyright 2016 Awake Networks category: Network @@ -93,6 +93,24 @@ library CPP-Options: -DDEBUG CC-Options: -DGRPC_HASKELL_DEBUG -std=c99 +executable compile-proto-file + build-depends: base >=4.8 && <5.0 + , async + , bytestring == 0.10.* + , containers ==0.5.* + , grpc-haskell + , optparse-generic + , proto3-wire + , protobuf-wire + , system-filepath + , text + , transformers + , turtle + default-language: Haskell2010 + ghc-options: -Wall -g -threaded -rtsopts -with-rtsopts=-N -O2 + hs-source-dirs: tools/compile-proto-file + main-is: Main.hs + executable hellos-server if flag(with-examples) build-depends: @@ -221,4 +239,3 @@ benchmark bench CPP-Options: -DDEBUG CC-Options: -DGRPC_HASKELL_DEBUG default-language: Haskell2010 - diff --git a/tools/compile-proto-file/Main.hs b/tools/compile-proto-file/Main.hs new file mode 100644 index 0000000..937c4d5 --- /dev/null +++ b/tools/compile-proto-file/Main.hs @@ -0,0 +1,26 @@ +{-# 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