forked from avh4/elm-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shakefile.hs
294 lines (249 loc) · 10.9 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
import Development.Shake
import Development.Shake.Command
import Development.Shake.FilePath
import Development.Shake.Util
import Control.Monad (forM_)
main :: IO ()
main = do
shakefilesHash <- getHashedShakeVersion [ "Shakefile.hs" ]
shakeArgs shakeOptions{
shakeChange = ChangeModtimeAndDigest,
shakeColor = True,
shakeVersion = shakefilesHash
} $ do
StdoutTrim stackLocalInstallRoot <- liftIO $ cmd "stack path --local-install-root"
StdoutTrim stackLocalBin <- liftIO $ cmd "stack path --local-bin"
StdoutTrim gitDescribe <- liftIO $ cmd "git" [ "describe", "--abbrev=8", "--always" ]
StdoutTrim gitSha <- liftIO $ cmd "git" [ "describe", "--always", "--match", "NOT A TAG", "--dirty" ]
let elmFormat = stackLocalInstallRoot </> "bin/elm-format" <.> exe
let shellcheck = stackLocalBin </> "shellcheck" <.> exe
want [ "test" ]
phony "test" $ do
need
[ "stack-test"
, "integration-tests"
, "_build/shellcheck.ok"
]
phony "build" $ need [ elmFormat ]
phony "stack-test" $ need [ "_build/stack-test.ok" ]
phony "profile" $ need [ "_build/tests/test-files/prof.ok" ]
phony "clean" $ do
cmd_ "stack clean"
removeFilesAfter "_build" [ "//*" ]
removeFilesAfter ""
[ "_input.elm"
, "_input2.elm"
, "formatted.elm"
, "formatted.json"
, "_stdout.txt"
]
--
-- build
--
let generatedSourceFiles = [ "generated/Build_elm_format.hs" ]
let sourceFilesPattern =
[ "src//*.hs"
, "parser/src//*.hs"
, "markdown//*.hs"
, "elm-format.cabal"
, "stack.yaml"
]
"generated/Build_elm_format.hs" %> \out -> do
alwaysRerun
writeFileChanged out $ unlines
[ "module Build_elm_format where"
, ""
, "gitDescribe :: String"
, "gitDescribe = " ++ show (gitDescribe :: String)
]
elmFormat %> \out -> do
sourceFiles <- getDirectoryFiles "" sourceFilesPattern
need sourceFiles
need generatedSourceFiles
cmd_ "stack build --test --no-run-tests"
"_build/bin/elm-format-prof" %> \out -> do
StdoutTrim profileInstallRoot <- liftIO $ cmd "stack path --profile --local-install-root"
sourceFiles <- getDirectoryFiles "" sourceFilesPattern
need sourceFiles
need generatedSourceFiles
cmd_ "stack build --profile --executable-profiling --library-profiling"
copyFileChanged (profileInstallRoot </> "bin/elm-format" <.> exe) out
--
-- Haskell tests
--
"_build/stack-test.ok" %> \out -> do
testFiles <- getDirectoryFiles ""
[ "tests//*.hs"
, "tests//*.stdout"
, "tests//*.stderr"
]
need testFiles
sourceFiles <- getDirectoryFiles "" sourceFilesPattern
need sourceFiles
need generatedSourceFiles
cmd_ "stack test"
writeFile' out ""
--
-- integration tests
--
phony "integration-tests" $ do
need
[ "_build/run-tests.ok"
, "_build/tests/test-files/good-json.ok"
, "_build/tests/test-files/good/Elm-0.19.ok"
, "_build/tests/test-files/good/Elm-0.18.ok"
, "_build/tests/test-files/good/Elm-0.17.ok"
, "_build/tests/test-files/transform/Elm-0.19.ok"
, "_build/tests/test-files/transform/Elm-0.18.ok"
, "_build/tests/test-files/transform/Elm-0.17.ok"
, "_build/tests/test-files/upgrade/Elm-0.19.ok"
, "_build/tests/test-files/upgrade/Elm-0.18.ok"
, "_build/tests/test-files/upgrade/Elm-0.17.ok"
, "_build/tests/test-files/bad/Elm-0.19.ok"
, "_build/tests/test-files/bad/Elm-0.18.ok"
, "_build/tests/test-files/bad/Elm-0.17.ok"
]
"_build/run-tests.ok" %> \out -> do
let script = "tests/run-tests.sh"
need [ script, elmFormat ]
testFiles <- getDirectoryFiles ""
[ "tests/test-files/good/json//*.elm"
, "tests/test-files/bad//*.elm"
, "tests/test-files/directory//*.elm"
, "tests/test-files/recursive-directory//*.elm"
, "tests/test-files/*.json"
]
need testFiles
cmd_ ("bash" <.> exe) script elmFormat
writeFile' out ""
"_build/tests/test-files/prof.ok" %> \out -> do
let oks =
[ "_build/tests/test-files/good/Elm-0.17/prof.ok"
, "_build/tests/test-files/good/Elm-0.18/prof.ok"
, "_build/tests/test-files/good/Elm-0.19/prof.ok"
]
need oks
writeFile' out (unlines oks)
-- Elm files
let elmVersions = [ "0.17", "0.18", "0.19" ]
forM_ elmVersions $ \elmVersion -> do
("_build/tests/test-files/good/Elm-" ++ elmVersion ++ ".ok") %> \out -> do
elmFiles <- getDirectoryFiles ""
[ "tests/test-files/good/Elm-" ++ elmVersion ++ "//*.elm"
]
let oks = ["_build" </> f -<.> "elm_matches" | f <- elmFiles]
need oks
writeFile' out (unlines elmFiles)
("_build/tests/test-files/good/Elm-" ++ elmVersion ++ "/prof.ok") %> \out -> do
alwaysRerun
elmFiles <- getDirectoryFiles ""
[ "tests/test-files/good/Elm-" ++ elmVersion ++ "//*.elm"
]
let oks = ["_profile" </> f -<.> (gitSha ++ ".prof") | f <- elmFiles]
need oks
writeFile' out (unlines oks)
("_profile/tests/test-files/good/Elm-" ++ elmVersion ++ "//*." ++ gitSha ++ ".prof") %> \out -> do
let source = dropDirectory1 $ dropExtensions out <.> "elm"
need [ "_build/bin/elm-format-prof", source ]
cmd_ "_build/bin/elm-format-prof" source "--output" "/dev/null" ("--elm-version=" ++ elmVersion) "+RTS" "-p" ("-po" ++ (out -<.> ""))
("_build/tests/test-files/*/Elm-" ++ elmVersion ++ "//*.elm_formatted") %> \out -> do
let source = dropDirectory1 $ out -<.> "elm"
need [ elmFormat, source ]
cmd_ elmFormat source "--output" out ("--elm-version=" ++ elmVersion)
("_build/tests/test-files/*/Elm-" ++ elmVersion ++ "//*.elm_upgraded") %> \out -> do
let source = dropDirectory1 $ out -<.> "elm"
need [ elmFormat, source ]
cmd_ elmFormat source "--output" out "--upgrade" ("--elm-version=" ++ elmVersion)
("_build/tests/test-files/*/Elm-" ++ elmVersion ++ "//*.elm_stderr") %> \out -> do
let source = dropDirectory1 $ out -<.> "elm"
need [ elmFormat, source ]
(Stderr stderr, Exit _) <- cmd (FileStdin source) BinaryPipes elmFormat "--stdin" ("--elm-version=" ++ elmVersion)
cmd_ (FileStdout out) (Stdin stderr) BinaryPipes "tr" [ "-d", "\r" ]
("_build/tests/test-files/transform/Elm-" ++ elmVersion ++ ".ok") %> \out -> do
elmFiles <- getDirectoryFiles ""
[ "tests/test-files/transform/Elm-" ++ elmVersion ++ "//*.elm"
]
let oks = ["_build" </> f -<.> "elm_transform_matches" | f <- elmFiles, ( takeExtension $ dropExtension f) /= ".formatted" ]
need oks
writeFile' out (unlines oks)
("_build/tests/test-files/upgrade/Elm-" ++ elmVersion ++ ".ok") %> \out -> do
elmFiles <- getDirectoryFiles ""
[ "tests/test-files/upgrade/Elm-" ++ elmVersion ++ "//*.elm"
]
let oks = ["_build" </> f -<.> "elm_upgrade_matches" | f <- elmFiles, ( takeExtension $ dropExtension f) /= ".formatted" ]
need oks
writeFile' out (unlines oks)
("_build/tests/test-files/bad/Elm-" ++ elmVersion ++ ".ok") %> \out -> do
elmFiles <- getDirectoryFiles ""
[ "tests/test-files/bad/Elm-" ++ elmVersion ++ "//*.elm"
]
let oks = ["_build" </> f -<.> "elm_bad_matches" | f <- elmFiles ]
need oks
writeFile' out (unlines elmFiles)
"_build/tests//*.elm_matches" %> \out -> do
let actual = out -<.> "elm_formatted"
let expected = dropDirectory1 $ out -<.> "elm"
need [ actual, expected ]
cmd_ "diff" "-u" actual expected
writeFile' out ""
"_build/tests//*.elm_transform_matches" %> \out -> do
let actual = out -<.> "elm_formatted"
let expected = dropDirectory1 $ out -<.> "formatted.elm"
need [ actual, expected ]
cmd_ "diff" "-u" actual expected
writeFile' out ""
"_build/tests//*.elm_upgrade_matches" %> \out -> do
let actual = out -<.> "elm_upgraded"
let expected = dropDirectory1 $ out -<.> "formatted.elm"
need [ actual, expected ]
cmd_ "diff" "-u" actual expected
writeFile' out ""
"_build/tests//*.elm_bad_matches" %> \out -> do
let actual = out -<.> "elm_stderr"
let expected = dropDirectory1 $ out -<.> "output.txt"
need [ actual, expected ]
cmd_ "diff" "--strip-trailing-cr" "-u" actual expected
writeFile' out ""
-- JSON files
"_build/tests/test-files/good-json.ok" %> \out -> do
jsonFiles <- getDirectoryFiles ""
[ "tests/test-files/good//*.json"
]
let oks = ["_build" </> f -<.> "json_matches" | f <- jsonFiles]
need oks
writeFile' out (unlines jsonFiles)
"_build/tests//*.json_formatted" %> \out -> do
let source = dropDirectory1 $ out -<.> "elm"
need [ elmFormat, source ]
(Stdout rawJson) <- cmd (FileStdin source) elmFormat "--elm-version=0.19" "--stdin" "--json"
(Stdout formattedJson) <- cmd (Stdin rawJson) "python3" "-mjson.tool" "--sort-keys"
writeFileChanged out formattedJson
"_build/tests//*.json_matches" %> \out -> do
let actual = out -<.> "json_formatted"
let expected = dropDirectory1 $ out -<.> "json"
need [ actual, expected ]
cmd_ "diff" "--strip-trailing-cr" "-u" actual expected
writeFile' out ""
--
-- shellcheck
--
shellcheck %> \out -> do
cmd_ "stack install ShellCheck"
"_build/shellcheck.ok" %> \out -> do
scriptFiles <- getDirectoryFiles ""
[ "tests/run-tests.sh"
, "package/collect_files.sh"
, "package/linux/build-in-docker.sh"
, "package/linux/build-package.sh"
, "package/mac/build-package.sh"
, "package/nix/build.sh"
, "package/win/build-package.sh"
]
let oks = ["_build" </> f <.> "shellcheck.ok" | f <- scriptFiles]
need oks
writeFile' out (unlines scriptFiles)
"_build//*.shellcheck.ok" %> \out -> do
let script = dropDirectory1 $ dropExtension $ dropExtension out
need [ shellcheck, script ]
cmd_ shellcheck script
writeFile' out ""