Tasks: support :continue in shell

This commit is contained in:
Michiel Borkent 2021-04-25 00:24:33 +02:00
parent a2f7bb835e
commit f7e881479b
3 changed files with 32 additions and 8 deletions

View file

@ -28,11 +28,15 @@
(binding [*out* *err*] (binding [*out* *err*]
(println (format "[bb %s]" @task-name) (str/join " " strs)))))) (println (format "[bb %s]" @task-name) (str/join " " strs))))))
(defn- exit-non-zero [proc] (defn- handle-non-zero [proc opts]
(when-let [exit-code (some-> proc deref :exit)] (when-let [proc (when proc (deref proc))]
(when (not (zero? exit-code)) (let [exit-code (:exit proc)]
(log-error "Terminating bb with non-zero exit code: " exit-code) (if (and (not (zero? exit-code))
(System/exit exit-code)))) (not (:continue opts)))
(do (log-error "Terminating with non-zero exit code: " exit-code)
(System/exit exit-code))
(with-meta proc
{:babashka/no-print true})))))
(def default-opts (def default-opts
{:in :inherit {:in :inherit
@ -50,6 +54,11 @@
(update opts :out io/file) (update opts :out io/file)
opts) opts)
opts) opts)
opts (if-let [o (:err opts)]
(if (string? o)
(update opts :err io/file)
opts)
opts)
cmd (if (.exists (io/file cmd)) cmd (if (.exists (io/file cmd))
[cmd] [cmd]
(p/tokenize cmd)) (p/tokenize cmd))
@ -57,7 +66,7 @@
local-log-level (:log-level opts)] local-log-level (:log-level opts)]
(sci/binding [log-level (or local-log-level @log-level)] (sci/binding [log-level (or local-log-level @log-level)]
(apply log-info cmd) (apply log-info cmd)
(exit-non-zero (p/process cmd (merge default-opts opts)))))) (handle-non-zero (p/process cmd (merge default-opts opts)) opts))))
(defn clojure [cmd & args] (defn clojure [cmd & args]
(let [[opts cmd args] (let [[opts cmd args]
@ -69,6 +78,11 @@
(update opts :out io/file) (update opts :out io/file)
opts) opts)
opts) opts)
opts (if-let [o (:err opts)]
(if (string? o)
(update opts :err io/file)
opts)
opts)
cmd (if (.exists (io/file cmd)) cmd (if (.exists (io/file cmd))
[cmd] [cmd]
(p/tokenize cmd)) (p/tokenize cmd))
@ -76,7 +90,7 @@
local-log-level (:log-level opts)] local-log-level (:log-level opts)]
(sci/binding [log-level (or local-log-level @log-level)] (sci/binding [log-level (or local-log-level @log-level)]
(apply log-info cmd) (apply log-info cmd)
(exit-non-zero (deps/clojure cmd (merge default-opts opts)))))) (handle-non-zero (deps/clojure cmd (merge default-opts opts)) opts))))
(defn -wait [res] (defn -wait [res]
(when res (when res

View file

@ -748,7 +748,9 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
input-var in input-var in
core/command-line-args command-line-args] core/command-line-args command-line-args]
(sci/eval-string* sci-ctx expression))] (sci/eval-string* sci-ctx expression))]
(when (some? res) ;; return value printing
(when (and (some? res)
(not (:babashka/no-print (meta res))))
(if-let [pr-f (cond shell-out println (if-let [pr-f (cond shell-out println
edn-out prn)] edn-out prn)]
(if (coll? res) (if (coll? res)

View file

@ -45,6 +45,14 @@
"echo hello")}} "echo hello")}}
(bb "foo") (bb "foo")
(is (= "hello\n" (slurp out))))) (is (= "hello\n" (slurp out)))))
(testing "shell test with :continue"
(test-utils/with-config {:tasks {'foo (list 'shell {:out out
:err out
:continue true}
"ls foobar")}}
(bb "foo")
(is (str/includes? (slurp out)
"foobar"))))
(fs/delete out) (fs/delete out)
(testing "clojure test" (testing "clojure test"
(test-utils/with-config {:tasks {'foo (list 'clojure {:out out} (test-utils/with-config {:tasks {'foo (list 'clojure {:out out}