Skip to content

Commit 59668f5

Browse files
committed
dev: Update babashka scripts to avoid side effects
This might be considered a bit overly meticulous at this point but it could save confusion at some later date. Scripts were previously always invoking the main function. Thing is, this is not always technically appropriate. For example, if someone issues a `bb doc` on a rewrite-clj script var, main will be invoked. With a pointer from borkdude to check out https://book.babashka.org/#main_file, we now only automatically invoke main when the clj file was invoked as a script. I also adjusted all scripts to have a proper `-main` so that for example: ./script/apply_import_vars.clj check can alternatively be invoked via: bb -m apply-import-vars check I'll stick with the ./script invocation in our docs, as most shells offer autocompletion for files.
1 parent ab0278d commit 59668f5

20 files changed

+101
-59
lines changed

script/apply_import_vars.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[helper.shell :as shell]
66
[helper.status :as status]))
77

8-
(defn main[args]
8+
(defn -main[& args]
99
(env/assert-min-versions)
1010
(let [cmd (first args)]
1111
(when (not (#{"gen-code" "check"} cmd))
@@ -14,4 +14,5 @@
1414
(shell/command ["clojure" "-X:apply-import-vars:script" cmd])
1515
nil))
1616

17-
(main *command-line-args*)
17+
(env/when-invoked-as-script
18+
(apply -main *command-line-args*))

script/ci_tests.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
(doseq [dir ["target" ".cpcache .shadow-cljs"]]
1111
(fs/delete-file-recursively dir true)))
1212

13-
(defn lint[]
13+
(defn lint []
1414
(shell/command ["bb" "./script/lint.clj"]))
1515

1616
(defn check-import-vars []
@@ -36,7 +36,7 @@
3636
(shell/command ["bb" "./script/cljs_tests.clj" "--env" "planck" "--optimizations" "none"])
3737
(status/line :warn "skipping planck tests, they can only be run on linux and macOS")) )
3838

39-
(defn main[]
39+
(defn -main[]
4040
(env/assert-min-versions)
4141
(clean)
4242
(check-import-vars)
@@ -48,4 +48,5 @@
4848
(cljs-bootstrap-tests)
4949
nil)
5050

51-
(main)
51+
(env/when-invoked-as-script
52+
(-main))

script/clj_tests.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"--profile" "test-isolated"
5656
"--reporter" "documentation"]))
5757

58-
(defn main [args]
58+
(defn -main [& args]
5959
(env/assert-min-versions)
6060
(let [{:keys [options exit-message exit-code]} (validate-args args)]
6161
(if exit-message
@@ -64,4 +64,5 @@
6464
(run-isolated-tests options))))
6565
nil)
6666

67-
(main *command-line-args*)
67+
(env/when-invoked-as-script
68+
(apply -main *command-line-args*))

script/clj_watch.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
[helper.shell :as shell]
66
[helper.status :as status]))
77

8-
(env/assert-min-versions)
9-
(status/line :info "launching kaocha watch on clojure sources")
10-
(shell/command (concat ["clojure" "-M:test-common:kaocha" "--watch" ] *command-line-args*))
8+
(defn -main []
9+
(env/assert-min-versions)
10+
(status/line :info "launching kaocha watch on clojure sources")
11+
(shell/command (concat ["clojure" "-M:test-common:kaocha" "--watch"] *command-line-args*)))
12+
13+
(env/when-invoked-as-script
14+
(-main))

script/cljdoc_preview.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env bb
22

3-
(ns cljdoc-docker-preview
3+
(ns cljdoc-preview
44
(:require [babashka.curl :as curl]
55
[clojure.data.xml :as xml]
66
[clojure.data.zip.xml :as zxml]
77
[clojure.java.browse :as browse]
88
[clojure.string :as string]
99
[clojure.zip :as zip]
10+
[helper.env :as env]
1011
[helper.fs :as fs]
1112
[helper.shell :as shell]
1213
[helper.status :as status]))
@@ -211,7 +212,7 @@
211212
(defn cleanup-resources []
212213
(fs/delete-file-recursively cljdoc-db-dir true))
213214

214-
(defn main [args]
215+
(defn -main [& args]
215216

216217
(check-prerequisites)
217218

@@ -256,4 +257,5 @@
256257
(println "Must be run from project root directory.")
257258
(System/exit 1)))))
258259

259-
(main *command-line-args*)
260+
(env/when-invoked-as-script
261+
(apply -main *command-line-args*))

script/cljs_tests.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@
115115
(shell/command (concat cmd ["--namespace" ns])))
116116
nses)))))))
117117

118-
(defn main [args]
118+
(defn -main [& args]
119119
(env/assert-min-versions)
120120
(let [{:keys [options exit-message exit-code]} (validate-args args)]
121121
(if exit-message
122122
(exit exit-code exit-message)
123123
(run-tests options)))
124124
nil)
125125

126-
(main *command-line-args*)
127-
126+
(env/when-invoked-as-script
127+
(apply -main *command-line-args*))

script/cljs_watch.clj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
[helper.shell :as shell]
66
[helper.status :as status]))
77

8-
(env/assert-min-versions)
9-
(status/line :detail "compiling code, then opening repl, afterwich your web browser will automatically open to figwheel test run summary")
10-
(shell/command ["clojure" "-M:test-common:cljs:fig-test"])
8+
(defn -main []
9+
(env/assert-min-versions)
10+
(status/line :detail "compiling code, then opening repl, afterwich your web browser will automatically open to figwheel test run summary")
11+
(shell/command ["clojure" "-M:test-common:cljs:fig-test"]))
12+
13+
(env/when-invoked-as-script
14+
(-main))

script/coverage.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
"--no-randomize"
1818
"--reporter" "documentation"]))
1919

20-
(defn main []
20+
(defn -main []
2121
(env/assert-min-versions)
2222
(generate-doc-tests)
2323
(run-clj-doc-tests)
2424
nil)
2525

26-
(main)
26+
(env/when-invoked-as-script
27+
(-main))

script/doc_tests.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bb
22

3-
(ns doc_tests
3+
(ns doc-tests
44
(:require [helper.env :as env]
55
[helper.shell :as shell]
66
[helper.status :as status]))
@@ -23,11 +23,12 @@
2323
"--dir" "target/test-doc-blocks/test"
2424
"--out" "target/cljsbuild/doc-tests"]))
2525

26-
(defn main []
26+
(defn -main []
2727
(env/assert-min-versions)
2828
(generate-doc-tests)
2929
(run-clj-doc-tests)
3030
(run-cljs-doc-tests)
3131
nil)
3232

33-
(main)
33+
(env/when-invoked-as-script
34+
(-main))

script/gen_api_diffs.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"--report-filename" (str (io/file report-dir (str report-name ".adoc")))]
4848
extra-args)))
4949

50-
(defn main []
50+
(defn -main []
5151
(env/assert-min-versions)
5252
(let [opts {:notes-dir "doc/diff-notes"
5353
:report-dir "doc/generated/api-diffs"}
@@ -70,4 +70,5 @@
7070
(diff-apis opts rewrite-clj-v1-lang-cljs rewrite-clj-v1-lang-clj "rewrite-clj-v1-lang-cljs-and-rewrite-clj-v1-lang-clj-documented-only" (concat to-self-args documented-only-args)))
7171
nil)
7272

73-
(main)
73+
(env/when-invoked-as-script
74+
(-main))

0 commit comments

Comments
 (0)