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 (def bb-edn
(atom nil)) (atom nil))
(defn print-help [command-line-args] (defn print-help [ctx command-line-args]
(if (empty? command-line-args) (if (empty? command-line-args)
(do (do
(println (str "Babashka v" version)) (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) (let [k (first command-line-args)
k (keyword (subs k 1)) k (keyword (subs k 1))
task (get-in @bb-edn [:tasks k]) task (get-in @bb-edn [:tasks k])
main (:main task)
help-text (:task/help task)] help-text (:task/help task)]
(if help-text (if help-text
[(println help-text) 0] [(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 if
,) ;; end defn ,) ;; end defn
@ -550,6 +558,10 @@ Use -- to separate script command line args from bb command line args.
(-> (p/process args {:inherit true}) (-> (p/process args {:inherit true})
p/check p/check
:exit)])}) :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))) (error (str "No such task: " (:task/type task)) 1)))
(def should-load-inits? (def should-load-inits?
@ -701,7 +713,7 @@ Use -- to separate script command line args from bb command line args.
(second (second
(cond version-opt (cond version-opt
[(print-version) 0] [(print-version) 0]
help (print-help command-line-args) help (print-help sci-ctx command-line-args)
tasks (print-tasks tasks) tasks (print-tasks tasks)
describe? describe?
[(print-describe) 0] [(print-describe) 0]
@ -767,6 +779,12 @@ Use -- to separate script command line args from bb command line args.
exit-code)))) exit-code))))
(defn main [& args] (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)] (let [opts (parse-opts args)]
(if-let [do-opts (:do opts)] (if-let [do-opts (:do opts)]
(reduce (fn [prev-exit opts] (reduce (fn [prev-exit opts]
@ -788,12 +806,6 @@ Use -- to separate script command line args from bb command line args.
[& args] [& args]
(handle-pipe!) (handle-pipe!)
(handle-sigint!) (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")] (if-let [dev-opts (System/getenv "BABASHKA_DEV")]
(let [{:keys [:n]} (if (= "true" dev-opts) {:n 1} (let [{:keys [:n]} (if (= "true" dev-opts) {:n 1}
(edn/read-string dev-opts)) (edn/read-string dev-opts))

View file

@ -4,8 +4,8 @@
[babashka.fs :as fs] [babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [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] (defn bb [& args]
(edn/read-string (edn/read-string
@ -90,7 +90,7 @@
:args [:do :div-by-zero :or-do :sum]}}} :args [:do :div-by-zero :or-do :sum]}}}
(is (= 6 (bb :all)))))) (is (= 6 (bb :all))))))
(deftest priotize-user-task-test (deftest prioritize-user-task-test
(is (map? (bb :describe))) (is (map? (bb :describe)))
(with-config {:tasks {:describe {:task/type :babashka (with-config {:tasks {:describe {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]}}} :args ["-e" "(+ 1 2 3)"]}}}
@ -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 "The following tasks are available:"))
(is (str/includes? res ":cool-task-1")) (is (str/includes? res ":cool-task-1"))
(is (str/includes? res ":cool-task-2"))))) (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)) ;; (prn :err (str es))
(if (zero? res) (if (zero? res)
(str os) (str os)
(do
(println (str os))
(throw (ex-info (str es) (throw (ex-info (str es)
{:stdout (str os) {:stdout (str os)
:stderr (str es)}))))) :stderr (str es)}))))))
(finally (finally
(when (string? input-or-opts) (vars/bindRoot sci/in *in*)) (when (string? input-or-opts) (vars/bindRoot sci/in *in*))
(vars/bindRoot sci/out *out*) (vars/bindRoot sci/out *out*)