forked from NixOS/cabal2nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-cabal-file.hs
137 lines (123 loc) · 4.75 KB
/
generate-cabal-file.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module Main ( main ) where
import Cartel
import Control.Monad.Trans ( liftIO )
import System.Process ( readProcess )
import Data.List ( intercalate )
import System.Environment ( getArgs )
getVersion :: IO Version
getVersion = do
v <- readProcess "git" ["log", "-1", "--format=%ci"] ""
let [y4,y3,y2,y1,'-',m2,m1,'-',d2,d1] = head (words v)
return (read ('[':y4:y3:y2:y1:m2:m1:d2:d1:"]"))
getGitVersion :: IO String
getGitVersion = do
[v] <- fmap lines (readProcess "git" ["describe", "--dirty"] "")
return v
getAuthors :: IO [NonEmptyString]
getAuthors = do
buf <- readProcess "sh" ["-c", "git log --pretty=short | git shortlog --numbered --summary"] ""
return [ unwords (tail (words l)) | l <- lines buf, not (null l) ]
properties :: Betsy IO Properties
properties = do
v <- liftIO getVersion
as <- liftIO getAuthors
return $ blank
{ name = "cabal2nix"
, version = v
, buildType = Just simple
, category = "Distribution"
, maintainer = "Peter Simons <simons@cryp.to>"
, synopsis = "Convert Cabal files into Nix build instructions."
, description = [ "Convert Cabal files into Nix build instructions." ]
, license = Just bsd3
, licenseFile = "LICENSE"
, copyright = "Copyright (c) 2011-2015 by Peter Simons et al"
, author = intercalate ", " as
, stability = "Experimental"
, cabalVersion = Just (1,10)
, homepage = "http://github.com/NixOS/cabal2nix/"
, bugReports = "http://github.com/NixOS/cabal2nix/issues"
, extraSourceFiles = ["README.md"]
, testedWith = [(ghc, eq [7,10,1])]
}
commonBuildOptions :: HasBuildInfo a => [a]
commonBuildOptions = hsSourceDirs ["src"] : haskell2010 : commonOptions
commonOptions :: HasBuildInfo a => [a]
commonOptions = commonGhcOptions : commonBuildDepends
commonGhcOptions :: HasBuildInfo a => a
commonGhcOptions = ghcOptions ["-Wall"]
commonBuildDepends :: HasBuildInfo a => [a]
commonBuildDepends =
[ buildDepends
[ package "base" (lt [5])
, unconstrained "aeson"
, unconstrained "ansi-wl-pprint"
, unconstrained "bytestring"
, package "Cabal" (gtEq [1,22,2])
, unconstrained "containers"
, unconstrained "deepseq-generics"
, unconstrained "directory"
, unconstrained "filepath"
, unconstrained "hackage-db"
, unconstrained "lens"
, unconstrained "monad-par"
, unconstrained "monad-par-extras"
, unconstrained "mtl"
, unconstrained "optparse-applicative"
, package "pretty" (gtEq [1,1,2])
, unconstrained "process"
, unconstrained "regex-posix"
, unconstrained "SHA"
, unconstrained "split"
, unconstrained "transformers"
, unconstrained "utf8-string"
]
]
commonTestOptions :: HasBuildInfo a => [a]
commonTestOptions = buildDepends
[ unconstrained "deepseq"
, unconstrained "doctest"
, unconstrained "hspec"
, unconstrained "QuickCheck"
] : commonBuildOptions
mkExecutable :: NonEmptyString -> [ExecutableField] -> Section
mkExecutable exe opt = executable exe $ mainIs (exe++".hs") : opt ++ commonBuildOptions
mkTest :: NonEmptyString -> [TestSuiteField] -> Section
mkTest test opt = testSuite test $ exitcodeFields (test++".hs") ++ opt ++ hsSourceDirs ["test"] : commonTestOptions
main :: IO ()
main = do
args <- getArgs
let releaseMode = "--release" `elem` args
defaultMainWithHeader (const (return "")) $ do
liftIO $ do ('v':gv) <- getGitVersion
writeFile "src/Cabal2Nix/Version.hs" $
"module Cabal2Nix.Version where\n\
\version :: String\n\
\version = " ++ show gv ++ "\n"
libraryModules <- modules "src"
liftIO $ writeFile "test/doctest.hs" (mkDoctest libraryModules)
props <- properties
return ( props
, exposedModules libraryModules : commonBuildOptions
, [ githubHead "NixOS" "cabal2nix"
, mkExecutable "cabal2nix" []
, mkExecutable "hackage2nix" [ buildable (not releaseMode)
, ghcOptions ["-threaded", "-rtsopts", "-with-rtsopts=-N"]
]
, mkTest "spec" []
, mkTest "doctest" []
]
)
mkDoctest :: [NonEmptyString] -> String
mkDoctest libraryModules =
let paths = [ "src/" ++ map toPath l ++ ".hs" | l <- libraryModules ]
toPath '.' = '/'
toPath c = c
in
"module Main ( main ) where\n\
\import Test.DocTest\n\
\main :: IO ()\n\
\main = do\n\
\ doctest $ [\"-isrc\", \"-optP-include\", \"-optPdist/build/autogen/cabal_macros.h\"] ++ " ++ show paths ++ "\n\
\ doctest [\"-isrc\", \"-optP-include\", \"-optPdist/build/autogen/cabal_macros.h\", \"src/cabal2nix.hs\"]\n\
\ doctest [\"-isrc\", \"-optP-include\", \"-optPdist/build/autogen/cabal_macros.h\", \"src/hackage2nix.hs\"]\n"