This commit is contained in:
Michiel Borkent 2021-03-20 20:47:03 +01:00
parent 4a58ad8c9f
commit 423789a8a9
3 changed files with 42 additions and 18 deletions

View file

@ -84,7 +84,7 @@
(def bb-edn
(atom nil))
(defn print-help [command-line-args]
(defn print-help [ctx command-line-args]
(if (empty? command-line-args)
(do
(println (str "Babashka v" version))
@ -141,10 +141,18 @@ Use -- to separate script command line args from bb command line args.
(let [k (first command-line-args)
k (keyword (subs k 1))
task (get-in @bb-edn [:tasks k])
main (:main task)
help-text (:task/help task)]
(if help-text
[(println help-text) 0]
[(println "No help found for task:" k) 1])
(if main
(let [main (if (simple-symbol? main)
(symbol (str main) "-main")
main)]
(if-let [doc (sci/eval-string* ctx (format "(some-> (requiring-resolve '%s) meta :doc)" main))]
[(println doc) 0]
[(println "No help found for task:" k) 1]))
[(println "No help found for task:" k) 1]))
,)) ;; end if
,) ;; end defn
@ -550,6 +558,10 @@ Use -- to separate script command line args from bb command line args.
(-> (p/process args {:inherit true})
p/check
:exit)])})
:main
(let [main-arg (:main task)
cmd-line-args (:args task)]
(parse-opts (seq (map str (concat ["--main" main-arg] cmd-line-args command-line-args)))))
(error (str "No such task: " (:task/type task)) 1)))
(def should-load-inits?
@ -701,7 +713,7 @@ Use -- to separate script command line args from bb command line args.
(second
(cond version-opt
[(print-version) 0]
help (print-help command-line-args)
help (print-help sci-ctx command-line-args)
tasks (print-tasks tasks)
describe?
[(print-describe) 0]
@ -767,6 +779,12 @@ Use -- to separate script command line args from bb command line args.
exit-code))))
(defn main [& args]
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
"bb.edn")]
(when (fs/exists? bb-edn-file)
(let [edn (edn/read-string (slurp bb-edn-file))]
(reset! bb-edn edn)
(deps/add-deps edn))))
(let [opts (parse-opts args)]
(if-let [do-opts (:do opts)]
(reduce (fn [prev-exit opts]
@ -788,12 +806,6 @@ Use -- to separate script command line args from bb command line args.
[& args]
(handle-pipe!)
(handle-sigint!)
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
"bb.edn")]
(when (fs/exists? bb-edn-file)
(let [edn (edn/read-string (slurp bb-edn-file))]
(reset! bb-edn edn)
(deps/add-deps edn))))
(if-let [dev-opts (System/getenv "BABASHKA_DEV")]
(let [{:keys [:n]} (if (= "true" dev-opts) {:n 1}
(edn/read-string dev-opts))

View file

@ -4,8 +4,8 @@
[babashka.fs :as fs]
[babashka.test-utils :as test-utils]
[clojure.edn :as edn]
[clojure.test :as test :refer [deftest is testing]]
[clojure.string :as str]))
[clojure.string :as str]
[clojure.test :as test :refer [deftest is testing]]))
(defn bb [& args]
(edn/read-string
@ -75,7 +75,7 @@
(bb :all)))))
(testing ":or-do short-cutting"
(with-config {:tasks {:sum-1 {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]}
:args ["-e" "(+ 1 2 3)"]}
:sum-2 {:task/type :babashka
:args ["-e" "(+ 4 5 6)"]}
:all {:task/type :babashka
@ -90,7 +90,7 @@
:args [:do :div-by-zero :or-do :sum]}}}
(is (= 6 (bb :all))))))
(deftest priotize-user-task-test
(deftest prioritize-user-task-test
(is (map? (bb :describe)))
(with-config {:tasks {:describe {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]}}}
@ -109,8 +109,8 @@ Addition is a pretty advanced topic. Let us start with the identity element
(deftest list-tasks-test
(with-config {:tasks {:cool-task-1 {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]
:task/help "Usage: bb :cool-task
:args ["-e" "(+ 1 2 3)"]
:task/help "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element
0. ..."}
@ -125,3 +125,13 @@ Addition is a pretty advanced topic. Let us start with the identity element
(is (str/includes? res "The following tasks are available:"))
(is (str/includes? res ":cool-task-1"))
(is (str/includes? res ":cool-task-2")))))
(deftest main-task-test
(with-config {:paths ["test-resources/task_scripts"]
:tasks {:main-task {:task/type :main
:main 'tasks ;; this calls tasks/-main
:args [1 2 3]}}}
(is (= '("1" "2" "3") (bb :main-task)))
(let [res (apply test-utils/bb nil
(map str [:help :main-task]))]
(is (str/includes? res "Usage: just pass some args")))))

View file

@ -39,9 +39,11 @@
;; (prn :err (str es))
(if (zero? res)
(str os)
(throw (ex-info (str es)
{:stdout (str os)
:stderr (str es)})))))
(do
(println (str os))
(throw (ex-info (str es)
{:stdout (str os)
:stderr (str es)}))))))
(finally
(when (string? input-or-opts) (vars/bindRoot sci/in *in*))
(vars/bindRoot sci/out *out*)