1
1
#!/usr/bin/env runhaskell
2
2
3
- import Data.Char (isDigit )
4
- import Data.List (intercalate )
3
+ import Data.Char (isDigit , toLower )
4
+ import Data.Function (on )
5
+ import Data.List (intercalate , sortBy )
5
6
import Data.Monoid ((<>) )
6
7
import Data.Version (showVersion )
7
8
9
+ import Distribution.InstalledPackageInfo
8
10
import Distribution.PackageDescription
9
- import Distribution.Verbosity
10
11
import Distribution.Simple
11
12
import Distribution.Simple.Setup (BuildFlags (.. ), ReplFlags (.. ), TestFlags (.. ), fromFlag )
12
13
import Distribution.Simple.LocalBuildInfo
14
+ import Distribution.Simple.PackageIndex
13
15
import Distribution.Simple.BuildPaths (autogenModulesDir )
14
16
import Distribution.Simple.Utils (createDirectoryIfMissingVerbose , rewriteFile , rawSystemStdout )
17
+ import Distribution.Verbosity
15
18
16
19
main :: IO ()
17
20
main =
@@ -25,12 +28,15 @@ main =
25
28
(sDistHook hooks) pd mlbi uh flags
26
29
, buildHook = \ pd lbi uh flags -> do
27
30
genBuildInfo (fromFlag $ buildVerbosity flags) pd
31
+ genDependencyInfo (fromFlag $ buildVerbosity flags) pd lbi
28
32
(buildHook hooks) pd lbi uh flags
29
33
, replHook = \ pd lbi uh flags args -> do
30
34
genBuildInfo (fromFlag $ replVerbosity flags) pd
35
+ genDependencyInfo (fromFlag $ replVerbosity flags) pd lbi
31
36
(replHook hooks) pd lbi uh flags args
32
37
, testHook = \ args pd lbi uh flags -> do
33
38
genBuildInfo (fromFlag $ testVerbosity flags) pd
39
+ genDependencyInfo (fromFlag $ testVerbosity flags) pd lbi
34
40
(testHook hooks) args pd lbi uh flags
35
41
}
36
42
@@ -57,6 +63,31 @@ genBuildInfo verbosity pkg = do
57
63
]
58
64
rewriteFile targetText buildVersion
59
65
66
+ genDependencyInfo :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO ()
67
+ genDependencyInfo verbosity pkg info = do
68
+ let
69
+ (PackageName pname) = pkgName . package $ pkg
70
+ name = " DependencyInfo_" ++ (map (\ c -> if c == ' -' then ' _' else c) pname)
71
+ targetHs = autogenModulesDir info ++ " /" ++ name ++ " .hs"
72
+ render p =
73
+ let
74
+ n = unPackageName $ pkgName p
75
+ v = intercalate " ." . fmap show . versionBranch $ pkgVersion p
76
+ in
77
+ n ++ " -" ++ v
78
+ deps = fmap (render . sourcePackageId) . allPackages $ installedPkgs info
79
+ sdeps = sortBy (compare `on` fmap toLower) deps
80
+ strs = flip fmap sdeps $ \ d -> " \" " ++ d ++ " \" "
81
+
82
+ createDirectoryIfMissingVerbose verbosity True (autogenModulesDir info)
83
+
84
+ rewriteFile targetHs $ unlines [
85
+ " module " ++ name ++ " where"
86
+ , " import Prelude"
87
+ , " dependencyInfo :: [String]"
88
+ , " dependencyInfo = [\n " ++ intercalate " \n , " strs ++ " \n ]"
89
+ ]
90
+
60
91
gitVersion :: Verbosity -> IO String
61
92
gitVersion verbosity = do
62
93
ver <- rawSystemStdout verbosity " git" [" log" , " --pretty=format:%h" , " -n" , " 1" ]
0 commit comments