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*]
(println (format "[bb %s]" @task-name) (str/join " " strs))))))
(defn- exit-non-zero [proc]
(when-let [exit-code (some-> proc deref :exit)]
(when (not (zero? exit-code))
(log-error "Terminating bb with non-zero exit code: " exit-code)
(System/exit exit-code))))
(defn- handle-non-zero [proc opts]
(when-let [proc (when proc (deref proc))]
(let [exit-code (:exit proc)]
(if (and (not (zero? 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
{:in :inherit
@ -50,6 +54,11 @@
(update opts :out io/file)
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]
(p/tokenize cmd))
@ -57,7 +66,7 @@
local-log-level (:log-level opts)]
(sci/binding [log-level (or local-log-level @log-level)]
(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]
(let [[opts cmd args]
@ -69,6 +78,11 @@
(update opts :out io/file)
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]
(p/tokenize cmd))
@ -76,7 +90,7 @@
local-log-level (:log-level opts)]
(sci/binding [log-level (or local-log-level @log-level)]
(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]
(when res

View file

@ -748,7 +748,9 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
input-var in
core/command-line-args command-line-args]
(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
edn-out prn)]
(if (coll? res)

View file

@ -45,6 +45,14 @@
"echo hello")}}
(bb "foo")
(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)
(testing "clojure test"
(test-utils/with-config {:tasks {'foo (list 'clojure {:out out}