module Data.ProtoLens.Setup
( defaultMainGeneratingProtos
, defaultMainGeneratingSpecificProtos
, generatingProtos
, generatingSpecificProtos
, generateProtosWithImports
, generateProtos
) where
#if __GLASGOW_HASKELL__ < 709
import Data.Functor ((<$>))
#endif
import Control.Monad (filterM, forM_, when)
import qualified Distribution.InstalledPackageInfo as InstalledPackageInfo
import Distribution.PackageDescription
( PackageDescription(..)
, benchmarkBuildInfo
, buildInfo
, extraSrcFiles
, hsSourceDirs
, libBuildInfo
, testBuildInfo
)
import qualified Distribution.Simple.BuildPaths as BuildPaths
import Distribution.Simple.InstallDirs (datadir)
import Distribution.Simple.LocalBuildInfo
( LocalBuildInfo(..)
, absoluteInstallDirs
, componentPackageDeps
#if MIN_VERSION_Cabal(2,0,0)
, allComponentsInBuildOrder
#endif
)
import qualified Distribution.Simple.PackageIndex as PackageIndex
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
, doesDirectoryExist
, findExecutable
, removeDirectoryRecursive
)
import System.IO (hPutStrLn, stderr)
import System.Process (callProcess)
defaultMainGeneratingProtos
:: FilePath
-> IO ()
defaultMainGeneratingProtos root
= defaultMainWithHooks $ generatingProtos root simpleUserHooks
defaultMainGeneratingSpecificProtos
:: FilePath
-> (PackageDescription -> IO [FilePath])
-> IO ()
defaultMainGeneratingSpecificProtos root getProtos
= defaultMainWithHooks
$ generatingSpecificProtos root getProtos simpleUserHooks
generatingProtos
:: FilePath
-> UserHooks -> UserHooks
generatingProtos root = generatingSpecificProtos root getProtos
where
getProtos p = do
files <- concat <$> mapM matchFileGlob (extraSrcFiles p)
pure $ map (makeRelative root)
$ filter (isSubdirectoryOf root)
$ filter (\f -> takeExtension f == ".proto")
files
generatingSpecificProtos
:: FilePath
-> (PackageDescription -> IO [FilePath])
-> UserHooks -> UserHooks
generatingSpecificProtos root getProtos hooks = hooks
{ 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
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
generate p l = getProtos p >>= generateSources root l
generateSources :: FilePath
-> LocalBuildInfo
-> [FilePath]
-> IO ()
generateSources root l files = do
importDirs <- filterM doesDirectoryExist
[ InstalledPackageInfo.dataDir info </> protoLensImportsPrefix
| info <- collectDeps l
]
generateProtosWithImports (root : importDirs) (autogenModulesDir l)
(map (root </>) files)
copyProtosToDataDir :: Verbosity
-> FilePath
-> FilePath
-> [FilePath]
-> IO ()
copyProtosToDataDir verb root destDir files = do
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
protoLensImportsPrefix :: FilePath
protoLensImportsPrefix = "proto-lens-imports"
fudgePackageDesc :: LocalBuildInfo -> PackageDescription -> PackageDescription
fudgePackageDesc lbi p = p
{ library =
(\lib -> lib { libBuildInfo = fudgeBuildInfo (libBuildInfo lib) })
<$> library p
, executables =
(\exe -> exe { buildInfo = fudgeBuildInfo (buildInfo exe) })
<$> executables p
, testSuites =
(\test -> test { testBuildInfo = fudgeBuildInfo (testBuildInfo test) })
<$> testSuites p
, benchmarks =
(\bench -> bench { benchmarkBuildInfo =
fudgeBuildInfo (benchmarkBuildInfo bench) })
<$> benchmarks p
}
where
fudgeBuildInfo bi =
bi { hsSourceDirs = autogenModulesDir lbi : hsSourceDirs bi }
isSubdirectoryOf :: FilePath -> FilePath -> Bool
isSubdirectoryOf root f
= isRelative f
&& equalFilePath f (root </> makeRelative root f)
generateProtos
:: FilePath
-> FilePath
-> [FilePath]
-> IO ()
generateProtos root = generateProtosWithImports [root]
generateProtosWithImports
:: [FilePath]
-> FilePath
-> [FilePath]
-> IO ()
generateProtosWithImports imports output files = do
protoLensProtoc
<- findExecutableOrDie "proto-lens-protoc"
$ "Please file a bug at "
++ "https://github.com/google/proto-lens/issues ."
protoc <- findExecutableOrDie "protoc"
$ "Follow the installation instructions at "
++ "https://google.github.io/proto-lens/installing-protoc.html ."
createDirectoryIfMissing True output
callProcess protoc $
[ "--plugin=protoc-gen-haskell=" ++ protoLensProtoc
, "--haskell_out=" ++ output
]
++ ["--proto_path=" ++ p | p <- imports]
++ files
findExecutableOrDie :: String -> String -> IO FilePath
findExecutableOrDie name debugMsg = do
maybePath <- findExecutable name
case maybePath of
Just path -> return path
Nothing -> do
let sep = "=========="
hPutStrLn stderr sep
hPutStrLn stderr $ "Error: couldn't find the executable " ++ show name
++ " in your $PATH."
++ "\n " ++ debugMsg
hPutStrLn stderr sep
error $ "Missing executable " ++ show name
collectDeps :: LocalBuildInfo -> [InstalledPackageInfo.InstalledPackageInfo]
#if MIN_VERSION_Cabal(2,0,0)
collectDeps l = do
c <- allComponentsInBuildOrder l
(i,_) <- componentPackageDeps c
Just p <- [PackageIndex.lookupUnitId (installedPkgs l) i]
return p
#else
collectDeps l = do
(_, c ,_) <- componentsConfigs l
(_, i) <- componentPackageDeps c
PackageIndex.lookupSourcePackageId (installedPkgs l) i
#endif
autogenModulesDir :: LocalBuildInfo -> FilePath
#if MIN_VERSION_Cabal(2,0,0)
autogenModulesDir = BuildPaths.autogenPackageModulesDir
#else
autogenModulesDir = BuildPaths.autogenModulesDir
#endif