This commit is contained in:
Michiel Borkent 2021-03-26 19:15:53 +01:00
parent 283d6db591
commit d4287f353e
2 changed files with 25 additions and 17 deletions

View file

@ -95,6 +95,10 @@
{:task task-key {:task task-key
:args args}))) :args args})))
(defn print-error [& msgs]
(binding [*out* *err*]
(apply println msgs)))
(defn print-help [ctx command-line-args] (defn print-help [ctx command-line-args]
(if (empty? command-line-args) (if (empty? command-line-args)
(do (do
@ -150,22 +154,22 @@ Use -- to separate script command line args from bb command line args.
") ")
[nil 0]) ;; end do [nil 0]) ;; end do
(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]) (if-let [task (get-in @bb-edn [:tasks k])]
{:keys [:args] (let [{:keys [:args]
task-key :task} (decode-task task) task-key :task} (decode-task task)]
help-text (:help (meta task))] (if-let [help-text (:help (meta task))]
(if help-text [(println help-text) 0]
[(println help-text) 0] (if-let [main (when (= :main task-key)
(if-let [main (when (= :main task-key) (first args))]
(first args))] (let [main (if (simple-symbol? main)
(let [main (if (simple-symbol? main) (symbol (str main) "-main")
(symbol (str main) "-main") main)]
main)] (if-let [doc (sci/eval-string* ctx (format "(some-> (requiring-resolve '%s) meta :doc)" main))]
(if-let [doc (sci/eval-string* ctx (format "(some-> (requiring-resolve '%s) meta :doc)" main))] [(println doc) 0]
[(println doc) 0] [(print-error "No help found for task:" k) 1]))
[(println "No help found for task:" k) 1])) [(print-error "No help found for task:" k) 1])))
[(println "No help found for task:" k) 1])) [(print-error "Task does not exist:" k) 1])
,)) ;; end if ,)) ;; end if
,) ;; end defn ,) ;; end defn

View file

@ -25,7 +25,11 @@
(with-config {} (with-config {}
(is (thrown-with-msg? (is (thrown-with-msg?
Exception #"Task does not exist: :sum" Exception #"Task does not exist: :sum"
(bb :sum))))) (bb :sum))))
(with-config {}
(is (thrown-with-msg?
Exception #"Task does not exist: :sum"
(bb :help :sum)))))
(deftest babashka-task-test (deftest babashka-task-test
(with-config {:tasks {:sum [:babashka "-e" "(+ 1 2 3)"]}} (with-config {:tasks {:sum [:babashka "-e" "(+ 1 2 3)"]}}