-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSetup.hs
62 lines (55 loc) · 3.23 KB
/
Setup.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- Copyright 2017 Bose Corporation.
-- This software is released under the 3-Clause BSD License.
-- The license can be viewed at https://github.com/smudgelang/smudge/blob/master/LICENSE
import Distribution.PackageDescription (PackageDescription(..))
import Distribution.Package (packageVersion, packageName, PackageIdentifier(..), unPackageName)
import Distribution.Simple (defaultMainWithHooks, simpleUserHooks, UserHooks(..), Args)
import Distribution.Simple.BuildPaths (autogenComponentModulesDir)
import Distribution.Simple.LocalBuildInfo (LocalBuildInfo, withExeLBI)
import Distribution.Simple.Setup (BuildFlags(..), fromFlag)
import Distribution.Simple.Utils (createDirectoryIfMissingVerbose, rewriteFileEx)
import System.FilePath ((</>), (<.>))
import System.Exit (ExitCode(ExitSuccess))
import System.Process (readProcessWithExitCode)
import Distribution.Version (showVersion)
import Data.List (dropWhileEnd)
import Data.Char (isSpace)
main = defaultMainWithHooks packageInfoUserHooks
packageInfoUserHooks :: UserHooks
packageInfoUserHooks =
simpleUserHooks {
buildHook = genPackageInfoHook
}
app_name :: PackageIdentifier -> String
app_name packageInfo = unPackageName $ packageName packageInfo
try_commands :: String -> [(String, [String])] -> IO String
try_commands def [] = return def
try_commands def ((cmd, args):cmds) = do
(code, out, _) <- readProcessWithExitCode cmd args ""
if code == ExitSuccess then return out else try_commands def cmds
build_commit :: IO String
build_commit = do
out <- try_commands "UNKNOWN" [("git", ["rev-parse", "--short=12", "HEAD"]), ("hg", ["id", "-i"])]
return $ dropWhileEnd isSpace out
genPackageInfoHook :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
genPackageInfoHook pkg lbi uhs bfs = do
withExeLBI pkg lbi $ \_ clbi -> do
createDirectoryIfMissingVerbose (fromFlag $ buildVerbosity bfs) True (autogenComponentModulesDir lbi clbi)
let packageInfoModulePath = autogenComponentModulesDir lbi clbi </> cfg_name <.> "hs"
rewriteFileEx (fromFlag $ buildVerbosity bfs) packageInfoModulePath . generate =<< build_commit
buildHook simpleUserHooks pkg lbi uhs bfs
where cfg_name = "PackageInfo"
generate cmt = "module " ++ cfg_name ++ " where\n" ++
"\n" ++
"version = " ++ (show $ showVersion $ packageVersion $ package pkg) ++ "\n" ++
"buildCommit = " ++ (show $ cmt) ++ "\n" ++
"appName = " ++ (show $ app_name $ package pkg) ++ "\n" ++
"copyright = " ++ (show $ copyright pkg) ++ "\n" ++
"maintainer = " ++ (show $ maintainer pkg) ++ "\n" ++
"author = " ++ (show $ author pkg) ++ "\n" ++
"stability = " ++ (show $ stability pkg) ++ "\n" ++
"homepage = " ++ (show $ homepage pkg) ++ "\n" ++
"pkgUrl = " ++ (show $ pkgUrl pkg) ++ "\n" ++
"bugReports = " ++ (show $ bugReports pkg) ++ "\n" ++
"synopsis = " ++ (show $ synopsis pkg) ++ "\n" ++
"description = " ++ (show $ description pkg) ++ "\n"