Skip to content

Commit 8f3a353

Browse files
authored
Merge pull request #685 from lean-ja/lake_script
`runCmd` の実装方法を変更し、文字列を渡せばいいようにする
2 parents 60b5253 + e7386f3 commit 8f3a353

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

lakefile.lean

+30-20
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,51 @@ require mathlib from git
2020
lean_lib Examples where
2121
globs := #[.submodules `Examples]
2222

23-
@[inline]
24-
def runCmd (cmd : String) (args : Array String) : ScriptM Bool := do
23+
section Script
24+
25+
/-- 与えられた文字列をシェルで実行する -/
26+
@[inline] def runCmd (input : String) : IO Unit := do
27+
let cmdList := input.splitOn " "
28+
let cmd := cmdList.head!
29+
let args := cmdList.tail |>.toArray
2530
let out ← IO.Process.output {
26-
cmd := cmd
31+
cmd := cmd
2732
args := args
2833
}
29-
let hasError := out.exitCode != 0
30-
if hasError then
31-
IO.eprint out.stderr
32-
return hasError
34+
if out.exitCode != 0 then
35+
IO.eprintln out.stderr
36+
throw <| IO.userError s!"Failed to execute: {input}"
37+
else if !out.stdout.isEmpty then
38+
IO.println out.stdout
3339

3440
/-- mk_exercise を実行し、演習問題の解答に
3541
解答部分を sorry に置き換えるなどの処理を施して演習問題ファイルを生成する。-/
3642
script mk_exercise do
37-
ifrunCmd "lake" #["exe", "mk_exercise", "Examples/Solution", "Examples/Exercise"] then return 1
43+
runCmd "lake exe mk_exercise Examples/Solution Examples/Exercise"
3844
return 0
3945

40-
@[inline]
41-
macro "with_time" x:doElem : doElem => `(doElem| do
42-
let start_time ← IO.monoMsNow;
43-
$x;
44-
let end_time ← IO.monoMsNow;
45-
IO.println s!"{end_time - start_time}ms")
46+
syntax (name := with_time) "with_time" "running" str doElem : doElem
47+
48+
macro_rules
49+
| `(doElem| with_time running $s $x) => `(doElem| do
50+
let start_time ← IO.monoMsNow;
51+
$x;
52+
let end_time ← IO.monoMsNow;
53+
IO.println s!"Running {$s}: {end_time - start_time}ms")
4654

4755
/-- mk_exercise と mdgen と mdbook を順に実行し、
4856
Lean ファイルから Markdown ファイルと HTML ファイルを生成する。-/
4957
script build do
5058
-- `lake run mk_exercise` を使用すると遅くなってしまうのでコピペしている
51-
IO.print "Running mk_exercise: "
52-
with_time ifrunCmd "lake" #["exe", "mk_exercise", "Examples/Solution", "Examples/Exercise"] then return 1
59+
with_time running "mk_exercise"
60+
runCmd "lake exe mk_exercise Examples/Solution Examples/Exercise"
5361

54-
IO.print "Running mdgen: "
55-
with_time ifrunCmd "lake" #["exe", "mdgen", "Examples", "src"] then return 1
62+
with_time running "mdgen"
63+
runCmd "lake exe mdgen Examples src"
5664

57-
IO.print "Running mdbook: "
58-
with_time ifrunCmd "mdbook" #["build"] then return 1
65+
with_time running "mdbook"
66+
runCmd "mdbook build"
5967

6068
return 0
69+
70+
end Script

0 commit comments

Comments
 (0)