Skip to content

Instantly share code, notes, and snippets.

@mxswd
Created August 7, 2014 00:10
Show Gist options
  • Save mxswd/d1c3b6270fc2c2baea40 to your computer and use it in GitHub Desktop.
Save mxswd/d1c3b6270fc2c2baea40 to your computer and use it in GitHub Desktop.
-- executable awsenv
-- main-is: awsenv.c
-- hs-source-dirs: awsenv
-- default-language: Haskell2010
-- x-exe-type: bash
import Data.Maybe
import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Utils
import Distribution.PackageDescription
import Distribution.Verbosity
main = do defaultMainWithHooks simpleUserHooks {
buildHook = \packageDescription localBuildInfo userHooks buildFlags -> do
let exeTypes = map (\x -> (x, lookup "x-exe-type" . customFieldsBI . buildInfo $ x))
$ executables $ localPkgDescr localBuildInfo
let targets = case buildArgs buildFlags of
[] -> exeTypes
xs -> filter (\(n, _) -> elem (exeName n) xs || elem ("exe:" ++ exeName n) xs) exeTypes
notYetBuilt <- fmap catMaybes $ flip mapM targets $ \(buildExe, method) -> case method of
Just t -> buildExeType buildExe (buildDir localBuildInfo) t >> return Nothing
Nothing -> return (Just buildExe)
let pkgDesc = (localPkgDescr localBuildInfo) { executables = notYetBuilt }
buildHook simpleUserHooks (packageDescription { executables = notYetBuilt }) (localBuildInfo { localPkgDescr = pkgDesc }) userHooks buildFlags
}
buildExeType :: Executable -> FilePath -> String -> IO ()
buildExeType (Executable { exeName = name, modulePath = source, buildInfo = info}) dest "bash" = do
let sourceDir = case hsSourceDirs info of
[] -> ""
(x:_) -> x ++ "/"
createDirectoryIfMissingVerbose normal True (dest ++ "/" ++ name)
copyFileVerbose normal (sourceDir ++ source) (dest ++ "/" ++ name ++ "/" ++ name)
setFileExecutable (dest ++ "/" ++ name ++ "/" ++ name)
buildExeType _ _ x = error $ "unknown exe type \"" ++ x ++ "\". can not build"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment