Last active
December 20, 2015 09:59
-
-
Save markhibberd/6112204 to your computer and use it in GitHub Desktop.
Generating runtime source at build step.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env runhaskell | |
import Data.Time (formatTime, getCurrentTime) | |
import Data.List (intercalate) | |
import Distribution.PackageDescription | |
import Distribution.Verbosity | |
import Distribution.Simple | |
import Distribution.Simple.Setup (BuildFlags(..), fromFlag) | |
import Distribution.Simple.LocalBuildInfo | |
import Distribution.Simple.BuildPaths (autogenModulesDir) | |
import Distribution.Simple.Utils (createDirectoryIfMissingVerbose, rewriteFile) | |
import System.Locale (defaultTimeLocale) | |
import System.FilePath ((</>), (<.>)) | |
main :: IO () | |
main = | |
let hooks = simpleUserHooks | |
in defaultMainWithHooks hooks { buildHook = withGenBuildInfo hooks } | |
withGenBuildInfo :: UserHooks -> PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO () | |
withGenBuildInfo old pkg info hooks flags = do | |
let verbosity = fromFlag (buildVerbosity flags) | |
genBuildInfo verbosity pkg info | |
(buildHook old) pkg info hooks flags | |
genBuildInfo :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO () | |
genBuildInfo verbosity pkg info = do | |
createDirectoryIfMissingVerbose verbosity True (autogenModulesDir info) | |
let (PackageName pname) = pkgName . package $ pkg | |
name = "BuildInfo_" ++ pname | |
target = autogenModulesDir info </> name <.> "hs" | |
t <- timestamp | |
rewriteFile target $ intercalate "\n" [ | |
"module " ++ name ++ " where" | |
, "import Prelude" | |
, "data RuntimeBuildInfo = RuntimeBuildInfo { timestamp :: String }" | |
, "buildInfo :: RuntimeBuildInfo" | |
, "buildInfo = RuntimeBuildInfo \"" ++ t ++ "\"" | |
] | |
timestamp :: IO String | |
timestamp = | |
formatTime defaultTimeLocale "%Y%m%d%H%M%S" `fmap` getCurrentTime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Caveat that it doesn't do any cleansing of package name when computing name for module.