-
Notifications
You must be signed in to change notification settings - Fork 272
/
Setup.lhs
42 lines (37 loc) · 1.76 KB
/
Setup.lhs
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
#!/usr/bin/runhaskell
\begin{code}
module Main (main) where
import Data.List ( nub )
import Data.Version ( showVersion )
import Distribution.Package ( PackageName(PackageName), PackageId, InstalledPackageId, packageVersion, packageName )
import Distribution.Simple ( defaultMainWithHooks, UserHooks(..), simpleUserHooks )
import Distribution.Simple.Utils ( rewriteFile, createDirectoryIfMissingVerbose )
import Distribution.Verbosity ( Verbosity )
import Distribution.Simple.BuildPaths ( autogenModulesDir )
import Distribution.Simple.Setup ( BuildFlags(buildVerbosity), fromFlag )
import Distribution.Simple.LocalBuildInfo ( LocalBuildInfo(libraryConfig, testSuiteConfigs), ComponentLocalBuildInfo(componentPackageDeps) )
import System.FilePath ( (</>) )
main :: IO ()
main = defaultMainWithHooks simpleUserHooks
{ buildHook = \pkg lbi hooks flags -> do
generateBuildModule "doctests" (fromFlag (buildVerbosity flags)) lbi
buildHook simpleUserHooks pkg lbi hooks flags
}
generateBuildModule :: String -> Verbosity -> LocalBuildInfo -> IO ()
generateBuildModule testSuite verbosity lbi = do
let dir = autogenModulesDir lbi
createDirectoryIfMissingVerbose verbosity True dir
rewriteFile (dir </> "Build_" ++ testSuite ++ ".hs") $ unlines
[ "module Build_" ++ testSuite ++ " where"
, "deps :: [String]"
, "deps = " ++ (show $ formatdeps (testDeps testSuite lbi))
]
where
formatdeps = map (formatone . snd)
formatone p = case packageName p of
PackageName n -> n ++ "-" ++ showVersion (packageVersion p)
testDeps :: String -> LocalBuildInfo -> [(InstalledPackageId, PackageId)]
testDeps testSuite lbi = nub $
maybe [] componentPackageDeps (lookup testSuite (testSuiteConfigs lbi))
++ maybe [] componentPackageDeps (libraryConfig lbi)
\end{code}