-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShakefile.hs
executable file
·368 lines (305 loc) · 11.6 KB
/
Shakefile.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env stack
{- stack script
--resolver lts-15.9
--package directory
--package executable-path
--package shake
--package time
--
-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wall #-}
-- |
-- Module: Main
-- Description: Install Command Wrapper, its documentation, and required tools.
-- Copyright: (c) 2018-2020 Peter Trško
-- License: BSD3
--
-- Maintainer: peter.trsko@gmail.com
-- Stability: experimental
-- Portability: GHC specific language extensions.
--
-- Install Command Wrapper, its documentation, and required tools.
module Main (main)
where
import Control.Monad (unless)
import Data.Functor ((<&>))
import System.Exit (die)
import Data.Time.Clock.POSIX (getCurrentTime)
import System.Directory
( XdgDirectory(XdgConfig, XdgData)
, getHomeDirectory
, getXdgDirectory
, setCurrentDirectory
, createDirectoryIfMissing
)
import System.Environment.Executable (ScriptPath(..), getScriptPath)
import Development.Shake
--import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Classes (Binary, Hashable, NFData)
newtype ThisGitRepo = ThisGitRepo ()
deriving newtype (Binary, Eq, Hashable, NFData, Show)
type instance RuleResult ThisGitRepo = String
thisGitRepo :: FilePath -> ThisGitRepo -> Action String
thisGitRepo directory ThisGitRepo{} = do
Stdout status <- cmd "git status -s --"
[ "stack.yaml"
, "Shakefile.hs"
, "command-wrapper-core/package.yaml"
, "command-wrapper-core/src/"
, "command-wrapper-core/test/"
, "command-wrapper-subcommand-shake/package.yaml"
, "command-wrapper-subcommand-shake/src/"
, "command-wrapper-subcommand-shake/test/"
, "command-wrapper-subcommand/dhall/"
, "command-wrapper-subcommand/package.yaml"
, "command-wrapper-subcommand/src/"
, "command-wrapper-subcommand/test/"
, "command-wrapper/app-cd/"
, "command-wrapper/app-exec/"
, "command-wrapper/app-skel/"
, "command-wrapper/app/"
, "command-wrapper/bash/"
, "command-wrapper/dhall/"
, "command-wrapper/man/"
, "command-wrapper/package.yaml"
, "command-wrapper/src/"
, "command-wrapper/test/"
]
if null @[] @Char status
then do
Stdout hash <- cmd "git -C" [directory] "show-ref -s origin/HEAD"
pure hash
else
("Workspace dirty " <>) . show <$> liftIO getCurrentTime
newtype HaveDockerImage = HaveDockerImage ()
deriving newtype (Binary, Eq, Hashable, NFData, Show)
type instance RuleResult HaveDockerImage = Bool
haveDockerImage :: String -> HaveDockerImage -> Action Bool
haveDockerImage repoAndTag HaveDockerImage{} = do
Stdout ids <- cmd "docker images" [repoAndTag, "--format={{.ID}}"]
pure (not (null @[] @Char ids))
data Directories = Directories
{ home :: FilePath
, projectRoot :: FilePath
, configDir :: FilePath
, dataDir :: FilePath
, localDir :: FilePath
}
main :: IO ()
main = do
projectRoot <- getScriptPath >>= \case
Executable executable ->
pure (takeDirectory executable)
RunGHC script ->
pure (takeDirectory script)
Interactive ->
die "Interactive mode not supported; call shakeMain directly."
home <- getHomeDirectory
configDir <- getXdgDirectory XdgConfig ""
dataDir <- getXdgDirectory XdgData ""
let localDir = home </> ".local"
setCurrentDirectory projectRoot
shakeMain Directories{..} shakeOptions
data ManDirs = ManDirs
{ man1Dir :: FilePath
, man5Dir :: FilePath
, man7Dir :: FilePath
}
mkManDirs :: FilePath -> ManDirs
mkManDirs dir = ManDirs
{ man1Dir = dir </> "man1"
, man5Dir = dir </> "man5"
, man7Dir = dir </> "man7"
}
shakeMain :: Directories -> ShakeOptions -> IO ()
shakeMain Directories{..} opts = shakeArgs opts do
let localLibDir = localDir </> "lib"
libexecDir = localLibDir </> "command-wrapper"
-- Standard `man` command should be able to pick this up. Try
-- `manpath` after installation to be sure.
manDir = dataDir </> "man"
staticOut = projectRoot </> "out"
staticOutDist = projectRoot </> "out" </> "command-wrapper"
staticOutDistShare = staticOutDist </> "share"
staticLibexecDir = staticOutDist </> "libexec" </> "command-wrapper"
staticManDir = staticOutDistShare </> "man"
staticDocDir = staticOutDistShare </> "doc" </> "command-wrapper"
version = "0.1.0.0-rc10"
staticTarball =
staticOut </> "command-wrapper-" <> version <.> "tar.xz"
staticBuildImage = "ghc-musl:v4-libgmp-ghc865-extended"
loadStaticBuildImageNix = "load-docker-image-ghc-musl.nix"
manPages :: ManDirs -> [FilePath]
manPages ManDirs{..} =
[ man1Dir </> "command-wrapper.1.gz"
, man1Dir </> "command-wrapper-cd.1.gz"
, man1Dir </> "command-wrapper-completion.1.gz"
, man1Dir </> "command-wrapper-config.1.gz"
, man1Dir </> "command-wrapper-exec.1.gz"
, man1Dir </> "command-wrapper-help.1.gz"
, man1Dir </> "command-wrapper-skel.1.gz"
, man1Dir </> "command-wrapper-version.1.gz"
, man5Dir </> "command-wrapper-default.dhall.5.gz"
, man7Dir </> "command-wrapper-bash-library.7.gz"
, man7Dir </> "command-wrapper-direnv-library.7.gz"
, man7Dir </> "command-wrapper-subcommand-protocol.7.gz"
]
manPagePatterns :: ManDirs -> [String]
manPagePatterns ManDirs{..} =
[ man1Dir </> "*.1.gz"
, man5Dir </> "*.5.gz"
, man7Dir </> "*.7.gz"
]
manHtml :: ManDirs -> [FilePath]
manHtml dirs = manPages dirs <&> (-<.> "html")
manHtmlPatterns :: ManDirs -> [FilePath]
manHtmlPatterns ManDirs{..} =
[ man1Dir </> "*.1.html"
, man5Dir </> "*.5.html"
, man7Dir </> "*.7.html"
]
binaries :: FilePath -> [FilePath]
binaries dir =
[ dir </> "command-wrapper"
, dir </> "command-wrapper-cd"
, dir </> "command-wrapper-exec"
, dir </> "command-wrapper-skel"
]
staticHtmlManDirs :: ManDirs
staticHtmlManDirs = ManDirs
{ man1Dir = staticDocDir
, man5Dir = staticDocDir
, man7Dir = staticDocDir
}
want $ mconcat
[ binaries libexecDir
, ["man"]
]
"man" ~>
need (manPages (mkManDirs manDir))
"man-html" ~>
need (manHtml staticHtmlManDirs)
"static" ~>
need
[ staticTarball
, staticTarball <.> "sha256sum"
, staticTarball <.> "sha256nix"
]
staticTarball %> \out -> do
need $ mconcat
[ manPages (mkManDirs staticManDir)
, manHtml staticHtmlManDirs
, binaries staticLibexecDir
]
cmd_ "tar"
[ "-C", takeDirectory out
, "--owner=root:0"
, "--group=root:0"
, "--mtime=1970-01-01T00:00:00Z"
, "--mode=a-w"
, "-cJf", out
, takeFileName staticOutDist
]
staticTarball <.> "sha256sum" %> \out -> do
let src = dropExtension out
dir = takeDirectory out
need [src]
cmd_ [Cwd dir, FileStdout out] "sha256sum" [takeFileName src]
staticTarball <.> "sha256nix" %> \out -> do
let src = out -<.> "sha256sum"
need [src]
Stdout (hash16 :: String) <- cmd (FileStdin src) "cut -f1" ["-d "]
cmd_ (FileStdout out) "nix-hash --type sha256 --to-base32" hash16
"build" ~>
cmd_ "stack" ["build", "--flag=command-wrapper:nix"]
"test" ~>
cmd_ "stack" ["test", "--flag=command-wrapper:nix"]
hasThisRepoChanged <- addOracle (thisGitRepo projectRoot)
binaries libexecDir &%> \outs -> do
_ <- hasThisRepoChanged (ThisGitRepo ())
let dst = takeDirectory (head outs)
liftIO (createDirectoryIfMissing True dst)
cmd_ "stack"
[ "--local-bin-path=" <> dst
, "install"
, "--flag=command-wrapper:nix"
]
staticOut </> loadStaticBuildImageNix %> \out ->
writeFile' out "\
\{ pkgs ? import <nixpkgs> { } }:\n\
\\n\
\let\n\
\ ghc-musl-repo = pkgs.fetchFromGitHub {\n\
\ owner = \"trskop\";\n\
\ repo = \"ghc-musl\";\n\
\ rev = \"f398b10761f4a30dda0ba4b221df1944861d9322\";\n\
\ sha256 = \"1bbl1nfh89wky0wmwx6i6r8483pnn96999znal51y5flrqg5dlar\";\n\
\ };\
\\
\ dockerImage = (import \"${ghc-musl-repo}/extend.nix\" { }).image;\n\
\\
\ docker = \"${pkgs.docker}/bin/docker\";\n\
\\n\
\in pkgs.writeScript \"load-docker-image-ghc-musl.bash\" ''\n\
\ #!/usr/bin/env bash\n\
\\n\
\ docker load --input \"${dockerImage}\"\n\
\ ''\n"
haveDockerImageForStaticBuild <- addOracle (haveDockerImage staticBuildImage)
binaries staticLibexecDir &%> \outs -> do
_ <- hasThisRepoChanged (ThisGitRepo ())
haveImage <- haveDockerImageForStaticBuild (HaveDockerImage ())
unless haveImage do
need [staticOut </> loadStaticBuildImageNix]
cmd_ [Cwd staticOut] "nix-build" ["./" <> loadStaticBuildImageNix]
cmd (staticOut </> "result")
let dst = takeDirectory (head outs)
liftIO (createDirectoryIfMissing True dst)
-- Without closing stdin stack would hang.
cmd_ [Stdin ""] "stack"
[ "--local-bin-path=" <> dst
, "--resolver=lts-14.27" -- GHC 8.6.5 to be consistent with image
, "--docker"
, "--docker-image=ghc-musl:v4-libgmp-ghc865-extended"
, "install"
, "--flag=command-wrapper-core:static"
, "--flag=command-wrapper-subcommand:static"
, "--flag=command-wrapper:static"
, "command-wrapper"
]
manPagePatterns (mkManDirs manDir) |%> \out -> do
let tempOut = dropExtension out
src = "command-wrapper" </> "man" </> takeFileName out -<.> "md"
dst = takeDirectory out
need [src]
liftIO (createDirectoryIfMissing True dst)
cmd_ "pandoc --standalone --to=man" ["--output=" <> tempOut, src]
cmd_ "gzip --force -9" [tempOut]
manPagePatterns (mkManDirs staticManDir) |%> \out -> do
let tempOut = dropExtension out
src = "command-wrapper" </> "man" </> takeFileName out -<.> "md"
dst = takeDirectory out
need [src]
liftIO (createDirectoryIfMissing True dst)
cmd_ "pandoc --standalone --to=man" ["--output=" <> tempOut, src]
cmd_ "gzip --force -9" [tempOut]
manHtmlPatterns staticHtmlManDirs |%> \out -> do
let src = "command-wrapper" </> "man" </> takeFileName out -<.> "md"
dst = takeDirectory out
need [src]
liftIO (createDirectoryIfMissing True dst)
cmd_ "pandoc --standalone --to=html" ["--output=" <> out, src]
-- vim:ft=haskell