Skip to content

Instantly share code, notes, and snippets.

@boris-stepanov
Created September 10, 2019 20:21
Show Gist options
  • Save boris-stepanov/f8cf411f270659cf9232415fa146dd67 to your computer and use it in GitHub Desktop.
Save boris-stepanov/f8cf411f270659cf9232415fa146dd67 to your computer and use it in GitHub Desktop.
Setup.hs with Thrift support
{-# LANGUAGE NoImplicitPrelude #-}
--{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -Wall -Wcompat -Wincomplete-uni-patterns -Wincomplete-record-updates -Wmissing-exported-signatures -Wmissing-home-modules -Widentities -Wredundant-constraints #-}
module Main where
import Distribution.Simple
import Distribution.Simple.Glob
import Distribution.Simple.Setup
--import Distribution.Simple.PreProcess
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Program
import Distribution.Verbosity
import Distribution.Types.PackageDescription
import System.IO
import Data.Function
--import Data.Bool
import System.FilePath
import Data.Semigroup
import Control.Monad
main :: IO ()
main = defaultMainWithHooks $ addThriftPP simpleUserHooks
addThriftPP :: UserHooks -> UserHooks
addThriftPP uh = uh { buildHook = buildWithThrift uh
-- hookedPreProcessors = thriftPP:hookedPreProcessors uh
, testHook = testWithThrift uh
, hookedPrograms = thriftProg:hookedPrograms uh }
buildWithThrift :: UserHooks -> PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
buildWithThrift uh pd lbi uh' bf = do
processThrift pd lbi (buildVerbosity bf)
buildHook uh pd lbi uh' bf
testWithThrift :: UserHooks -> Args -> PackageDescription -> LocalBuildInfo -> UserHooks -> TestFlags -> IO ()
testWithThrift uh args pd lbi uh' tf = do
processThrift pd lbi (testVerbosity tf)
testHook uh args pd lbi uh' tf
processThrift :: PackageDescription -> LocalBuildInfo -> Flag Verbosity -> IO ()
processThrift pd lbi flag = forM_ (extraSrcFiles pd) $ \p -> let dir = takeDirectory p in
matchDirFileGlob verb (specVersion pd) dir (takeFileName p) >>= mapM (\f ->
runThrift lbi (dir, takeFileName f) (buildDir lbi, "") verb)
where
verb = fromFlagOrDefault normal flag
{-
thriftPP :: PPSuffixHandler
thriftPP = ("thrift",) $ \_ lbi _ -> PreProcessor {
platformIndependent = False
, runPreProcessor = runThrift lbi
}
-}
runThrift :: LocalBuildInfo -> (FilePath, FilePath) -> (FilePath, FilePath) -> Verbosity -> IO ()
runThrift lbi (srcDir, srcName) (outDir, _) verb = do
(thrift, _) <- requireProgram verb (simpleProgram "thrift") (withPrograms lbi)
runProgram verb thrift $ [ "-r", "--gen", "hs"]
<> [ "-I", srcDir, "-I", outDir ]
<> [ "-out", outDir ]
<> [ srcDir </> srcName ]
thriftProg :: Program
thriftProg = simpleProgram "thrift"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment