Skip to content

Commit 36d656a

Browse files
committed
Take another swing at bb tasks
Babaska is finalizing its tasks implementation. A good time for me to take them for another spin. Direct benefits: - A user can type bb tasks to get a list of available tasks with a short description of each. Indirect benefits: - Running tasks from a consistent launcher made me think about consistency. Actions I took: - All tasks now accept a --help arg - Tasks are somewhat (ish) more consistent in arg usage and validation - Tasks that take no args fail when args are provided Observations: - The fact that bb tasks shows tasks in the order they are defined in bb.edn is nice. - My scripts usage help listed the script file in its help. For example "Usage: libs_tests.clj [check|gen-code]". I've adapted to focus on the args only, and now instead show: "Valid args: [check|gen-code]". - bb's :enter and :leave and leave hooks are nice, they allow me to easily provide a title for each task and show when a task completed successfully. - For :leave to be called, I had to, of course, stop calling System/exit on success from one of my script Other - I've specified the new-ish :min-bb-version in bb.edn, folks using an older version of bb will not be able to take advantage of tasks. - In testing I noticed that my clj-kondo rewrite-clj export config is being picked up when linting rewrite-clj. This is something we do not want/need, so I've adapted to exclude it for this operation.
1 parent 0e56314 commit 36d656a

22 files changed

+400
-251
lines changed

bb.edn

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
{:paths ["script"]
1+
{:min-bb-version "0.3.7"
2+
:paths ["script"]
23
:deps {org.clojure/data.zip {:mvn/version "1.0.0"}
34
io.aviso/pretty {:mvn/version "0.1.37"}
45
docopt/docopt {:git/url "https://github.com/nubank/docopt.clj"
56
:sha "98814f559d2e50fdf10f43cbe3b7da0ca3cca423"}
67
doric/doric {:mvn/version "0.9.0"}
78
version-clj/version-clj {:mvn/version "2.0.1"}
89
lread/status-line {:git/url "https://github.com/lread/status-line.git"
9-
:sha "35ed39645038e81b42cb15ed6753b8462e60a06d"}}}
10+
:sha "35ed39645038e81b42cb15ed6753b8462e60a06d"}}
11+
:tasks {;; setup
12+
:requires ([lread.status-line :as status])
13+
:enter (let [{:keys [name]} (current-task)] (status/line :head "TASK %s" name))
14+
:leave (let [{:keys [name]} (current-task)] (status/line :detail "\nTASK %s done." name))
15+
;; commands
16+
apply-import-vars {:task apply-import-vars/-main :doc "(check|gen-code) - export APIs statically from templates"}
17+
lint {:task lint/-main :doc "[--rebuild-cache]"}
18+
test-clj {:task clj-tests/-main :doc "[--clojure-version (1.9|1.10)]"}
19+
test-cljs {:task cljs-tests/-main :doc "use --help for args"}
20+
test-shadow-cljs {:task shadow-cljs-test/-main}
21+
test-native {:task pure-native-test/-main :doc "run rewrite-clj and tests after both compiled with GraalVM native-image"}
22+
test-native-sci {:task sci-native-test/-main :doc "interpret rewrite-clj tests via sci from rewrite-clj native image"}
23+
test-jvm-sci {:task sci-jvm-test/-main :doc "JVM only sanity test for test-native-sci"}
24+
test-clj-watch {:task clj-watch/-main :doc "[kaocha args]"}
25+
test-cljs-watch {:task cljs-watch/-main :doc "watch cljs test with fighweel main"}
26+
test-coverage {:task coverage/-main :doc "generate code coverage reports for Clojure tests"}
27+
test-doc {:task doc-tests/-main :doc "test doc code blocks"}
28+
test-libs {:task libs-tests/-main :doc "(run|outdated) - verify taht libs using rewrite-clj* work with current rewrite-clj"}
29+
outdated {:task outdated/-main :doc "report on outdated Clojure and npm dependencies"}
30+
doc-api-diffs {:task gen-api-diffs/-main :doc "generate diff docs for rewrite-clj* APIs"}
31+
doc-update-readme {:task update-readme/-main :doc "honour our contributors in README"}
32+
cljdoc-preview {:task cljdoc-preview/-main :doc "preview what docs will look like on cljdoc, use --help for args"}
33+
ci-unit-tests {:task ci-tests/-main}
34+
ci-release {:task release/-main :doc "release tasks, use --help for args"}}}

deps.edn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
;; linting
2222
:clj-kondo {:extra-deps {clj-kondo/clj-kondo {:mvn/version "2021.04.23"}}
23+
:replace-paths ["src"] ;; avoid including our exported clj-kondo config under resources
2324
:main-opts ["-m" "clj-kondo.main"]}
2425

2526

script/apply_import_vars.clj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55
[helper.shell :as shell]
66
[lread.status-line :as status]))
77

8+
(def arg-usage (str "Valid args: (gen-code|check|--help)\n"
9+
"\n"
10+
" gen-code Generate API sources from templates\n"
11+
" check Fail if API sources are stale as compared to templates\n"
12+
" --help Show this help"))
13+
814
(defn -main[& args]
915
(env/assert-min-versions)
1016
(let [cmd (first args)]
11-
(when (not (#{"gen-code" "check"} cmd))
12-
(status/die 1 "Usage: apply-import-vars [gen-code|check]"))
13-
(status/line :head (str "Running apply import vars " cmd))
14-
(shell/command ["clojure" "-X:apply-import-vars:script" cmd])
17+
(cond
18+
(= "--help" cmd)
19+
(status/line :detail arg-usage)
20+
21+
(not (#{"gen-code" "check"} cmd))
22+
(status/die 1 arg-usage)
23+
24+
:else
25+
(do (status/line :head (str "Running apply import vars " cmd))
26+
(shell/command ["clojure" "-X:apply-import-vars:script" cmd])))
1527
nil))
1628

1729
(env/when-invoked-as-script

script/ci_tests.clj

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
(ns ci-tests
44
(:require [helper.env :as env]
55
[helper.fs :as fs]
6+
[helper.main :as main]
67
[helper.shell :as shell]
78
[lread.status-line :as status]))
89

@@ -36,17 +37,20 @@
3637
(shell/command ["bb" "./script/cljs_tests.clj" "--env" "planck" "--optimizations" "none"])
3738
(status/line :warn "skipping planck tests, they can only be run on linux and macOS")) )
3839

39-
(defn -main[]
40-
(env/assert-min-versions)
41-
(clean)
42-
(check-import-vars)
43-
(lint)
44-
(doc-tests)
45-
(clojure-tests)
46-
(cljs-tests)
47-
(shadow-cljs-tests)
48-
(cljs-bootstrap-tests)
40+
(defn -main [& args]
41+
(main/run-argless-cmd
42+
args
43+
(fn []
44+
(env/assert-min-versions)
45+
(clean)
46+
(check-import-vars)
47+
(lint)
48+
(doc-tests)
49+
(clojure-tests)
50+
(cljs-tests)
51+
(shadow-cljs-tests)
52+
(cljs-bootstrap-tests)))
4953
nil)
5054

5155
(env/when-invoked-as-script
52-
(-main))
56+
(-main *command-line-args*))

script/clj_tests.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,28 @@
1818
["-h" "--help"]])
1919

2020
(defn usage [options-summary]
21-
(->> ["Usage: cljs_test.clj <options>"
21+
(->> ["Valid args: <options>"
2222
options-summary]
2323
(string/join "\n")))
2424

25-
(defn error-msg [errors]
26-
(string/join "\n" errors))
25+
(defn error-msg [summary errors]
26+
(str (string/join "\n" errors)
27+
"\n\n"
28+
(usage summary)))
2729

2830
(defn validate-args [args]
29-
(let [{:keys [options errors summary]} (cli/parse-opts args cli-options)]
31+
(let [{:keys [options arguments errors summary]} (cli/parse-opts args cli-options)]
3032
(cond
3133
(:help options)
3234
{:exit-message (usage summary) :exit-code 0}
3335

36+
(seq arguments)
37+
{:exit-message (error-msg summary (map #(str "unexpected argument: " %) arguments))
38+
:exit-code 1}
39+
3440
errors
35-
{:exit-message (error-msg errors) :exit-code 1}
41+
{:exit-message (error-msg summary errors)
42+
:exit-code 1}
3643

3744
:else
3845
{:options options})))

script/clj_watch.clj

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

8-
(defn -main []
8+
(defn -main [& args]
99
(env/assert-min-versions)
1010
(status/line :head "launching kaocha watch on clojure sources")
11-
(shell/command (concat ["clojure" "-M:test-common:kaocha" "--watch"] *command-line-args*)))
11+
(shell/command (concat ["clojure" "-M:test-common:kaocha" "--watch"] args)))
1212

1313
(env/when-invoked-as-script
14-
(-main))
14+
(-main *command-line-args*))

script/cljdoc_preview.clj

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
(status/line :head "Waiting for %s to become available" (:name container))
137137
(when (= "down" (status-server container))
138138
(status/die 1
139-
"%s does not seem to be running.\nDid you run this script with the start command yet?"
139+
"%s does not seem to be running.\nDid you run the start command yet?"
140140
(:name container)))
141141
(status/line :detail "%s container is running" (:name container))
142142
(let [url (str "http://localhost:" (:port container))]
@@ -195,7 +195,7 @@
195195
(defn view-in-browser [url]
196196
(status/line :head "opening %s in browser" url)
197197
(when (not= 200 (:status (curl/get url {:throw false})))
198-
(status/die 1 "Could not reach:\n%s\nDid you run this script with ingest command yet?" url))
198+
(status/die 1 "Could not reach:\n%s\nDid you run the ingest command yet?" url))
199199
(browse/browse-url url))
200200

201201

@@ -215,12 +215,28 @@
215215
(defn cleanup-resources []
216216
(fs/delete-file-recursively cljdoc-db-dir true))
217217

218+
(def usage (string/join "\n"
219+
["Valid args: [start|ingest|view|stop|status]"
220+
""
221+
" start - start docker containers supporting cljdoc preview"
222+
" ingest - locally publishes your project for cljdoc preview"
223+
" view - opens cljdoc preview in your default browser"
224+
" stop - stops docker containers supporting cljdoc preview"
225+
" status - status of docker containers supporting cljdoc preview"
226+
""
227+
" --help - show this help"
228+
""
229+
"Must be run from project root directory."]))
230+
218231
(defn -main [& args]
219232

220233
(check-prerequisites)
221234

222235
(let [command (first args)]
223236
(case command
237+
"--help"
238+
(status/line :detail usage)
239+
224240
"start"
225241
(do
226242
(start-cljdoc-server cljdoc-container)
@@ -249,16 +265,7 @@
249265
nil)
250266

251267
;; else
252-
(do (println "Usage: bb script/cljdoc_preview.clj [start|ingest|view|stop|status]")
253-
(println "")
254-
(println " start - start docker containers supporting cljdoc preview")
255-
(println " ingest - locally publishes your project for cljdoc preview")
256-
(println " view - opens cljdoc preview in your default browser")
257-
(println " stop - stops docker containers supporting cljdoc preview")
258-
(println " status - status of docker containers supporting cljdoc preview")
259-
(println "")
260-
(println "Must be run from project root directory.")
261-
(System/exit 1)))))
268+
(status/die 1 usage))))
262269

263270
(env/when-invoked-as-script
264271
(apply -main *command-line-args*))

script/cljs_tests.clj

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,28 @@
2626
["-h" "--help"]])
2727

2828
(defn usage [options-summary]
29-
(->> ["Usage: cljs_test.clj <options>"
29+
(->> ["Valid args: <options>"
3030
options-summary]
3131
(string/join "\n")))
3232

33-
(defn error-msg [errors]
34-
(string/join "\n" errors))
33+
(defn error-msg [summary errors]
34+
(str (string/join "\n" errors)
35+
"\n\n"
36+
(usage summary)))
3537

3638
(defn validate-args [args]
37-
(let [{:keys [options errors summary]} (cli/parse-opts args cli-options)]
39+
(let [{:keys [options arguments errors summary]} (cli/parse-opts args cli-options)]
3840
(cond
3941
(:help options)
4042
{:exit-message (usage summary) :exit-code 0}
4143

44+
(seq arguments)
45+
{:exit-message (error-msg summary (map #(str "unexpected argument: " %) arguments))
46+
:exit-code 1}
47+
4248
errors
43-
{:exit-message (error-msg errors) :exit-code 1}
49+
{:exit-message (error-msg summary errors)
50+
:exit-code 1}
4451

4552
:else
4653
{:options options})))

script/cljs_watch.clj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
(ns cljs-watch
44
(:require [helper.env :as env]
5+
[helper.main :as main]
56
[helper.shell :as shell]
67
[lread.status-line :as status]))
78

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"]))
9+
10+
(defn -main [& args]
11+
(main/run-argless-cmd
12+
args
13+
(fn []
14+
(status/line :detail "compiling code, then opening repl, afterwich your web browser will automatically open to figwheel test run summary")
15+
(shell/command ["clojure" "-M:test-common:cljs:fig-test"]))))
1216

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

script/coverage.clj

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
(ns coverage
44
(:require [helper.env :as env]
5+
[helper.main :as main]
56
[helper.shell :as shell]
67
[lread.status-line :as status]))
78

@@ -17,11 +18,13 @@
1718
"--no-randomize"
1819
"--reporter" "documentation"]))
1920

20-
(defn -main []
21-
(env/assert-min-versions)
22-
(generate-doc-tests)
23-
(run-clj-doc-tests)
21+
(defn -main [& args]
22+
(main/run-argless-cmd
23+
args
24+
(fn []
25+
(generate-doc-tests)
26+
(run-clj-doc-tests)))
2427
nil)
2528

2629
(env/when-invoked-as-script
27-
(-main))
30+
(-main *command-line-args*))

0 commit comments

Comments
 (0)