proto-lens-protoc 0.2.0.1 → 0.2.1.0
raw patch · 3 files changed
+112/−30 lines, 3 filesdep ~proto-lensdep ~proto-lens-descriptorsPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: proto-lens, proto-lens-descriptors
API changes (from Hackage documentation)
+ Data.ProtoLens.Setup: generateProtosWithImports :: [FilePath] -> FilePath -> [FilePath] -> IO ()
Files
- proto-lens-protoc.cabal +9/−6
- src/Data/ProtoLens/Compiler/Generate.hs +8/−10
- src/Data/ProtoLens/Setup.hs +95/−14
proto-lens-protoc.cabal view
@@ -1,5 +1,5 @@ name: proto-lens-protoc-version: 0.2.0.1+version: 0.2.1.0 synopsis: Protocol buffer compiler for the proto-lens library. description: Turn protocol buffer files (.proto) into Haskell files (.hs) which@@ -47,15 +47,18 @@ , lens-family == 1.2.* , lens-labels == 0.1.* , process >= 1.2 && < 1.5- , proto-lens == 0.2.0.1- , proto-lens-descriptors == 0.2.0.1+ , proto-lens == 0.2.1.0+ , proto-lens-descriptors == 0.2.1.0 , text == 1.2.* reexported-modules: -- Modules that are needed by the generated Haskell files. -- For forwards compatibility, reexport them as new module names so that -- other packages don't accidentally write non-generated code that -- relies on these modules being reexported by proto-lens-protoc.- Data.ByteString as Data.ProtoLens.Reexport.Data.ByteString+ Prelude as Data.ProtoLens.Reexport.Prelude+ , Data.Int as Data.ProtoLens.Reexport.Data.Int+ , Data.Word as Data.ProtoLens.Reexport.Data.Word+ , Data.ByteString as Data.ProtoLens.Reexport.Data.ByteString , Data.Default.Class as Data.ProtoLens.Reexport.Data.Default.Class , Data.Map as Data.ProtoLens.Reexport.Data.Map , Data.ProtoLens as Data.ProtoLens.Reexport.Data.ProtoLens@@ -79,8 +82,8 @@ , lens-family == 1.2.* -- Specify an exact version of `proto-lens`, since it's tied closely -- to the generated code.- , proto-lens == 0.2.0.1- , proto-lens-descriptors == 0.2.0.1+ , proto-lens == 0.2.1.0+ , proto-lens-descriptors == 0.2.1.0 , text == 1.2.* hs-source-dirs: src other-modules:
src/Data/ProtoLens/Compiler/Generate.hs view
@@ -79,20 +79,18 @@ ["ScopedTypeVariables", "DataKinds", "TypeFamilies", "UndecidableInstances", "MultiParamTypeClasses", "FlexibleContexts", "FlexibleInstances",- "PatternSynonyms", "MagicHash"]+ "PatternSynonyms", "MagicHash", "NoImplicitPrelude"] -- Allow unused imports in case we don't import anything from -- Data.Text, Data.Int, etc. , optionsGhcPragma "-fno-warn-unused-imports" ]- (map importSimple- -- Note: we import Prelude explicitly to make it qualified.- [ "Prelude", "Data.Int", "Data.Word"]- ++ map (modifyImport . importSimple)- [ "Data.ProtoLens", "Data.ProtoLens.Message.Enum"- , "Lens.Family2", "Lens.Family2.Unchecked", "Data.Default.Class"- , "Data.Text", "Data.Map" , "Data.ByteString"- , "Lens.Labels"- ]+ (map (modifyImport . importSimple)+ [ "Prelude", "Data.Int", "Data.Word"+ , "Data.ProtoLens", "Data.ProtoLens.Message.Enum"+ , "Lens.Family2", "Lens.Family2.Unchecked", "Data.Default.Class"+ , "Data.Text", "Data.Map" , "Data.ByteString"+ , "Lens.Labels"+ ] ++ map importSimple imports) (concatMap generateDecls (Map.elems definitions) ++ concatMap generateFieldDecls allFieldNames)
src/Data/ProtoLens/Setup.hs view
@@ -19,6 +19,7 @@ , defaultMainGeneratingSpecificProtos , generatingProtos , generatingSpecificProtos+ , generateProtosWithImports , generateProtos ) where @@ -26,6 +27,8 @@ import Data.Functor ((<$>)) #endif +import Control.Monad (filterM, forM_, when)+import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo import Distribution.PackageDescription ( PackageDescription(..) , benchmarkBuildInfo@@ -36,21 +39,39 @@ , testBuildInfo ) import Distribution.Simple.BuildPaths (autogenModulesDir)-import Distribution.Simple.LocalBuildInfo (LocalBuildInfo)-import Distribution.Simple.Utils (matchFileGlob)+import Distribution.Simple.InstallDirs (datadir)+import Distribution.Simple.LocalBuildInfo+ ( LocalBuildInfo(..)+ , absoluteInstallDirs+ , componentPackageDeps+ )+import Distribution.Simple.PackageIndex (lookupSourcePackageId)+import Distribution.Simple.Setup (fromFlag, copyDest, copyVerbosity)+import Distribution.Simple.Utils+ ( createDirectoryIfMissingVerbose+ , installOrdinaryFile+ , matchFileGlob+ ) import Distribution.Simple ( defaultMainWithHooks , simpleUserHooks , UserHooks(..) )+import Distribution.Verbosity (Verbosity) import System.FilePath ( (</>) , equalFilePath , isRelative , makeRelative , takeExtension+ , takeDirectory )-import System.Directory (createDirectoryIfMissing, findExecutable)+import System.Directory+ ( createDirectoryIfMissing+ , doesDirectoryExist+ , findExecutable+ , removeDirectoryRecursive+ ) import System.Process (callProcess) -- | This behaves the same as 'Distribution.Simple.defaultMain', but@@ -123,22 +144,68 @@ -- provided root directory. -> UserHooks -> UserHooks generatingSpecificProtos root getProtos hooks = hooks- { buildHook = \p l h f -> generateSources p l >> buildHook hooks p l h f- , haddockHook = \p l h f -> generateSources p l >> haddockHook hooks p l h f- , replHook = \p l h f args -> generateSources p l- >> replHook hooks p l h f args+ { buildHook = \p l h f -> generate p l >> buildHook hooks p l h f+ , haddockHook = \p l h f -> generate p l >> haddockHook hooks p l h f+ , replHook = \p l h f args -> generate p l >> replHook hooks p l h f args , sDistHook = \p maybe_l h f -> case maybe_l of Nothing -> error "Can't run protoc; run 'cabal configure' first." Just l -> do- generateSources p l+ generate p l sDistHook hooks (fudgePackageDesc l p) maybe_l h f+ , postCopy = \a flags pkg lbi -> do+ let verb = fromFlag $ copyVerbosity flags+ let destDir = datadir (absoluteInstallDirs pkg lbi+ $ fromFlag $ copyDest flags)+ </> protoLensImportsPrefix+ getProtos pkg >>= copyProtosToDataDir verb root destDir+ postCopy hooks a flags pkg lbi } where- generateSources p l = do- -- Applying 'root </>' does nothing if the path is already absolute.- files <- map (root </>) <$> getProtos p- generateProtos root (autogenModulesDir l) files+ generate p l = getProtos p >>= generateSources root l +-- | Generate Haskell source files for the given input .proto files.+generateSources :: FilePath -- ^ The root directory+ -> LocalBuildInfo+ -> [FilePath] -- ^ Proto files relative to the root directory.+ -> IO ()+generateSources root l files = do+ -- Collect import paths from build-depends of this package.+ importDirs <- filterM doesDirectoryExist+ [ InstalledPackageInfo.dataDir info </> protoLensImportsPrefix+ | (_, c, _) <- componentsConfigs l+ , (_, i) <- componentPackageDeps c+ , info <- lookupSourcePackageId (installedPkgs l) i+ ]+ generateProtosWithImports (root : importDirs) (autogenModulesDir l)+ -- Applying 'root </>' does nothing if the path is already+ -- absolute.+ (map (root </>) files)++-- | Copy each .proto file into the installed "data-dir" path,+-- so that it can be included by other packages that depend on this one.+copyProtosToDataDir :: Verbosity+ -> FilePath -- ^ The root for source .proto files in this+ -- package.+ -> FilePath -- ^ The final location where .proto files should+ -- be installed.+ -> [FilePath] -- ^ .proto files relative to the root+ -> IO ()+copyProtosToDataDir verb root destDir files = do+ -- Make the build more hermetic by clearing the output+ -- directory.+ exists <- doesDirectoryExist destDir+ when exists $ removeDirectoryRecursive destDir+ forM_ files $ \f -> do+ let srcFile = root </> f+ let destFile = destDir </> f+ createDirectoryIfMissingVerbose verb True+ (takeDirectory destFile)+ installOrdinaryFile verb srcFile destFile++-- | Imports are stored as $datadir/proto-lens-imports/**/*.proto.+protoLensImportsPrefix :: FilePath+protoLensImportsPrefix = "proto-lens-imports"+ -- | Add the autogen directory to the hs-source-dirs of all the targets in the -- .cabal file. Used to fool 'sdist' by pointing it to the generated source -- files.@@ -181,7 +248,21 @@ -> FilePath -- ^ The output directory for the generated Haskell files. -> [FilePath] -- ^ The .proto files to process. -> IO ()-generateProtos root output files = do+generateProtos root = generateProtosWithImports [root]+--+-- | Run the proto compiler to generate Haskell files from the given .proto files.+--+-- Writes the generated files to the autogen directory (@dist\/build\/autogen@+-- for Cabal, and @.stack-work\/dist\/...\/build\/autogen@ for stack).+--+-- Throws an exception if the @proto-lens-protoc@ executable is not on the PATH.+generateProtosWithImports+ :: [FilePath] -- ^ Directories under which .proto files and/or files that+ -- they import can be found.+ -> FilePath -- ^ The output directory for the generated Haskell files.+ -> [FilePath] -- ^ The .proto files to process.+ -> IO ()+generateProtosWithImports imports output files = do maybeProtoLensProtoc <- findExecutable "proto-lens-protoc" case maybeProtoLensProtoc of Nothing -> error "Couldn't find executable proto-lens-protoc."@@ -190,6 +271,6 @@ callProcess "protoc" $ [ "--plugin=protoc-gen-haskell=" ++ protoLensProtoc , "--haskell_out=" ++ output- , "--proto_path=" ++ root ]+ ++ ["--proto_path=" ++ p | p <- imports] ++ files