This commit is contained in:
Michiel Borkent 2021-03-27 14:28:19 +01:00
parent 235adfcfe5
commit f02bf19e80
4 changed files with 256 additions and 235 deletions

View file

@ -102,9 +102,7 @@
(binding [*out* *err*] (binding [*out* *err*]
(apply println msgs))) (apply println msgs)))
(defn print-help [ctx command-line-args] (defn print-help [_ctx _command-line-args]
(if (empty? command-line-args)
(do
(println (str "Babashka v" version)) (println (str "Babashka v" version))
;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION"))))) ;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION")))))
(println) (println)
@ -112,9 +110,11 @@
(println " (println "
Help: Help:
--help, -h or -? Print this help text. help, -h or -? Print this help text.
--version Print the current version of babashka. version, Print the current version of babashka.
--describe Print an EDN map with information about this version of babashka. describe Print an EDN map with information about this version of babashka.
tasks List tasks.
doc <var> Print docstring of var or task.
Evaluation: Evaluation:
@ -126,9 +126,9 @@ Evaluation:
REPL: REPL:
--repl Start REPL. Use rlwrap for history. repl Start REPL. Use rlwrap for history.
--socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666).
--nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667). nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667).
In- and output flags: In- and output flags:
@ -140,28 +140,31 @@ In- and output flags:
Uberscript: Uberscript:
--uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single file. uberscript <file> Collect preloads, -e, -f and -m and all required namespaces from the classpath into a single file.
Uberjar: Uberjar:
--uberjar <jar> Similar to --uberscript but creates jar file. uberjar <jar> Similar to --uberscript but creates jar file.
Clojure: Clojure:
--clojure [args...] Invokes clojure. Takes same args as the official clojure CLI. clojure [args...] Invokes clojure. Takes same args as the official clojure CLI.
If the first argument is not any of the above options, then it treated as a file if it exists, or as an expression otherwise. If the first argument is not any of the above options, then it treated as a file if it exists, or as an expression otherwise.
Everything after that is bound to *command-line-args*. Everything after that is bound to *command-line-args*.
Use -- to separate script command line args from bb command line args. Use -- to separate script command line args from bb command line args.
") ")
[nil 0]) ;; end do [nil 0])
(let [k (first command-line-args)
k (keyword (subs k 1))] (defn print-doc [ctx command-line-args]
(let [arg (first command-line-args)]
(if (str/starts-with? arg ":")
(let [k (keyword (subs arg 1))]
(if-let [task (get-in @bb-edn [:tasks k])] (if-let [task (get-in @bb-edn [:tasks k])]
(let [{:keys [:args] (let [{:keys [:args]
task-key :task} (decode-task task)] task-key :task} (decode-task task)]
(if-let [help-text (:help task)] (if-let [help-text (:doc task)]
[(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))]
@ -170,13 +173,17 @@ Use -- to separate script command line args from bb command line args.
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])) [(print-error "No docstring found for task:" k) 1]))
[(print-error "No help found for task:" k) 1]))) [(print-error "No docstring found for task:" k) 1])))
[(print-error "Task does not exist:" k) 1]) [(print-error "Task does not exist:" k) 1]))
,)) ;; end if (let [arg (if (str/includes? arg "/") arg (str "clojure.core/" arg))]
,) ;; end defn (if-let [doc (sci/eval-string* ctx (format "(some-> (requiring-resolve '%s) meta :doc)" arg))]
[(println doc) 0]
[(print-error "No docstring found for var:" arg) 1]))))
,)
(defn print-tasks [tasks] (defn print-tasks [tasks]
(if (seq tasks)
(let [tasks (into (sorted-map) tasks) (let [tasks (into (sorted-map) tasks)
ks (keys tasks) ks (keys tasks)
longest (apply max longest (apply max
@ -191,7 +198,9 @@ Use -- to separate script command line args from bb command line args.
(str " " d))))) (str " " d)))))
(println) (println)
(println "Run bb :help <task> to view help of a specific task.") (println "Run bb :help <task> to view help of a specific task.")
[nil 0])) [nil 0])
(do (println "No tasks found.")
[nil 0])))
(defn print-describe [] (defn print-describe []
(println (println
@ -421,6 +430,21 @@ Use -- to separate script command line args from bb command line args.
(println msg) (println msg)
{:exec (fn [] [nil exit])})) {:exec (fn [] [nil exit])}))
(defn command? [x]
(case x
("clojure"
"version"
"help"
"doc"
"tasks"
"uberjar"
"uberscript"
"repl"
"socket-repl"
"nrepl-server"
"describe") true
false))
(defn parse-opts [options] (defn parse-opts [options]
(let [fst (when options (first options)) (let [fst (when options (first options))
key? (when fst (str/starts-with? fst ":")) key? (when fst (str/starts-with? fst ":"))
@ -429,14 +453,18 @@ Use -- to separate script command line args from bb command line args.
(into [:do] (map (comp vector keyword) keys))) (into [:do] (map (comp vector keyword) keys)))
k (when (and key? (not expanded)) k (when (and key? (not expanded))
(keyword (first keys))) (keyword (first keys)))
task? (or expanded k) bb-edn @bb-edn
bb-edn (when task? @bb-edn) tasks (when bb-edn
tasks (when (and task? bb-edn)
(:tasks bb-edn)) (:tasks bb-edn))
user-task (when tasks (get tasks k))] user-task (when (and tasks k)
(get tasks k))
opt (first options)]
(cond user-task (cond user-task
(resolve-task tasks user-task {:command-line-args (next options)}) (resolve-task tasks user-task {:command-line-args (next options)})
expanded (resolve-task tasks expanded nil) expanded (resolve-task tasks expanded nil)
(and (command? opt)
(not (fs/regular-file? opt)))
(recur (cons (str "--" opt) (next options)))
:else :else
(let [opts (loop [options options (let [opts (loop [options options
opts-map {}] opts-map {}]
@ -444,17 +472,21 @@ Use -- to separate script command line args from bb command line args.
(let [opt (first options)] (let [opt (first options)]
(case opt (case opt
("--") (assoc opts-map :command-line-args (next options)) ("--") (assoc opts-map :command-line-args (next options))
("--clojure" ":clojure") (assoc opts-map :clojure true ("--clojure") (assoc opts-map :clojure true
:command-line-args (rest options)) :command-line-args (rest options))
("--version" ":version") {:version true} ("--version") {:version true}
("--help" "-h" "-?" ":help") {:help true ("--help" "-h" "-?" "help")
{:help true
:command-line-args (rest options)} :command-line-args (rest options)}
(":tasks") {:tasks tasks ("--doc")
{:doc true
:command-line-args (rest options)}
("--tasks") {:tasks (or tasks {})
:command-line-args (rest options)} :command-line-args (rest options)}
("--verbose") (recur (next options) ("--verbose") (recur (next options)
(assoc opts-map (assoc opts-map
:verbose? true)) :verbose? true))
("--describe" ":describe") (recur (next options) ("--describe") (recur (next options)
(assoc opts-map (assoc opts-map
:describe? true)) :describe? true))
("--stream") (recur (next options) ("--stream") (recur (next options)
@ -492,12 +524,12 @@ Use -- to separate script command line args from bb command line args.
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map :classpath (first options)))) (assoc opts-map :classpath (first options))))
("--uberscript" ":uberscript") ("--uberscript")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map (assoc opts-map
:uberscript (first options)))) :uberscript (first options))))
("--uberjar" ":uberjar") ("--uberjar")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map (assoc opts-map
@ -512,12 +544,12 @@ Use -- to separate script command line args from bb command line args.
(recur (next options) (recur (next options)
(assoc opts-map (assoc opts-map
:jar (first options)))) :jar (first options))))
("--repl" ":repl") ("--repl")
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map (assoc opts-map
:repl true))) :repl true)))
("--socket-repl" ":socket-repl") ("--socket-repl")
(let [options (next options) (let [options (next options)
opt (first options) opt (first options)
opt (when (and opt (not (str/starts-with? opt "-"))) opt (when (and opt (not (str/starts-with? opt "-")))
@ -527,7 +559,7 @@ Use -- to separate script command line args from bb command line args.
(recur options (recur options
(assoc opts-map (assoc opts-map
:socket-repl (or opt "1666")))) :socket-repl (or opt "1666"))))
("--nrepl-server" ":nrepl-server") ("--nrepl-server")
(let [options (next options) (let [options (next options)
opt (first options) opt (first options)
opt (when (and opt (not (str/starts-with? opt "-"))) opt (when (and opt (not (str/starts-with? opt "-")))
@ -541,25 +573,10 @@ Use -- to separate script command line args from bb command line args.
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(update opts-map :expressions (fnil conj []) (first options)))) (update opts-map :expressions (fnil conj []) (first options))))
("--main", "-m", ":main") ("--main", "-m",)
(let [options (next options)] (let [options (next options)]
(recur (next options) (recur (next options)
(assoc opts-map :main (first options)))) (assoc opts-map :main (first options))))
#_#_(":do")
(let [options (next options)
options (into [] (comp (partition-by #(or
(= % ":do")
(= % ":and-do")
(= % ":or-do"))))
options)]
{:do options})
#_#_(":invoke")
{:exec-src
(pr-str '(if-let [f (requiring-resolve (symbol (first *command-line-args*)))]
(apply f (rest *command-line-args*))
(throw (Exception. (str "Var not found: " (first *command-line-args*)
" " (babashka.classpath/get-classpath))))))
:command-line-args (next options)}
;; fallback ;; fallback
(if (some opts-map [:file :jar :socket-repl :expressions :main]) (if (some opts-map [:file :jar :socket-repl :expressions :main])
(assoc opts-map (assoc opts-map
@ -630,7 +647,7 @@ Use -- to separate script command line args from bb command line args.
:verbose? :classpath :verbose? :classpath
:main :uberscript :describe? :main :uberscript :describe?
:jar :uberjar :clojure :jar :uberjar :clojure
:exec-src :tasks] :exec-src :tasks :doc]
exec-fn :exec} exec-fn :exec}
opts opts
_ (when verbose? (vreset! common/verbose? true)) _ (when verbose? (vreset! common/verbose? true))
@ -762,6 +779,7 @@ Use -- to separate script command line args from bb command line args.
[(print-version) 0] [(print-version) 0]
help (print-help sci-ctx command-line-args) help (print-help sci-ctx command-line-args)
tasks (print-tasks tasks) tasks (print-tasks tasks)
doc (print-doc sci-ctx command-line-args)
describe? describe?
[(print-describe) 0] [(print-describe) 0]
repl [(repl/start-repl! sci-ctx) 0] repl [(repl/start-repl! sci-ctx) 0]

View file

@ -29,7 +29,7 @@
(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 :help :sum))))) (bb "doc" :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)"]}}
@ -100,38 +100,40 @@
(is (= 6 (bb :all)))))) (is (= 6 (bb :all))))))
(deftest prioritize-user-task-test (deftest prioritize-user-task-test
(is (map? (bb :describe))) (is (map? (bb "describe")))
(with-config {:tasks {:describe [:babashka "-e" "(+ 1 2 3)"]}} (with-config {:tasks {:describe [:babashka "-e" "(+ 1 2 3)"]}}
(is (= 6 (bb :describe))))) (is (= 6 (bb :describe)))))
(deftest help-task-test (deftest doc-task-test
(with-config {:tasks {:cool-task (with-config {:tasks {:cool-task
{:help "Usage: bb :cool-task {:doc "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..." 0. ..."
:task [:babashka "-e" "(+ 1 2 3)"]}}} :task [:babashka "-e" "(+ 1 2 3)"]}}}
(is (str/includes? (apply test-utils/bb nil (is (str/includes? (apply test-utils/bb nil
(map str [:help :cool-task])) (map str ["doc" :cool-task]))
"Usage: bb :cool-task")))) "Usage: bb :cool-task"))))
(deftest list-tasks-test (deftest list-tasks-test
(with-config {}
(let [res (test-utils/bb nil "tasks")]
(is (str/includes? res "No tasks found."))))
(with-config {:tasks {:task-1 (with-config {:tasks {:task-1
{:description "Return the sum of 1, 2 and 3." {:description "Return the sum of 1, 2 and 3."
:help "Usage: bb :cool-task :doc "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..."} 0. ..."}
:task [:babashka "-e" "(+ 1 2 3)"] :task [:babashka "-e" "(+ 1 2 3)"]
:cool-task-2 :cool-task-2
{:description "Return the sum of 4, 5 and 6." {:description "Return the sum of 4, 5 and 6."
:help "Usage: bb :cool-task :doc "Usage: bb :cool-task
Addition is a pretty advanced topic. Let us start with the identity element Addition is a pretty advanced topic. Let us start with the identity element
0. ..." 0. ..."
:task [:babashka "-e" "(+ 4 5 6)"]}}} :task [:babashka "-e" "(+ 4 5 6)"]}}}
(let [res (apply test-utils/bb nil (let [res (test-utils/bb nil "tasks")]
(map str [:tasks]))]
(is (str/includes? res "The following tasks are available:")) (is (str/includes? res "The following tasks are available:"))
(is (str/includes? res ":task-1 Return the")) (is (str/includes? res ":task-1 Return the"))
(is (str/includes? res ":cool-task-2 Return the"))))) (is (str/includes? res ":cool-task-2 Return the")))))
@ -141,5 +143,5 @@ Addition is a pretty advanced topic. Let us start with the identity element
:tasks {:main-task [:main 'tasks 1 2 3]}} :tasks {:main-task [:main 'tasks 1 2 3]}}
(is (= '("1" "2" "3") (bb :main-task))) (is (= '("1" "2" "3") (bb :main-task)))
(let [res (apply test-utils/bb nil (let [res (apply test-utils/bb nil
(map str [:help :main-task]))] (map str ["doc" :main-task]))]
(is (str/includes? res "Usage: just pass some args"))))) (is (str/includes? res "Usage: just pass some args")))))

View file

@ -7,21 +7,10 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.java.shell :refer [sh]] [clojure.java.shell :refer [sh]]
[clojure.string :as str] [clojure.string :as str]
[clojure.test :as test :refer [deftest is testing *report-counters*]] [clojure.test :as test :refer [deftest is testing]]
[flatland.ordered.map :refer [ordered-map]] [flatland.ordered.map :refer [ordered-map]]
[sci.core :as sci])) [sci.core :as sci]))
(defmethod clojure.test/report :begin-test-var [m]
(println "===" (-> m :var meta :name))
(println))
(defmethod clojure.test/report :end-test-var [_m]
(let [{:keys [:fail :error]} @*report-counters*]
(when (and (= "true" (System/getenv "BABASHKA_FAIL_FAST"))
(or (pos? fail) (pos? error)))
(println "=== Failing fast")
(System/exit 1))))
(defn bb [input & args] (defn bb [input & args]
(edn/read-string (edn/read-string
{:readers *data-readers* {:readers *data-readers*

View file

@ -4,6 +4,7 @@
[babashka.main :as main] [babashka.main :as main]
[babashka.process :as p] [babashka.process :as p]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.test :as test :refer [*report-counters*]]
[sci.core :as sci] [sci.core :as sci]
[sci.impl.vars :as vars])) [sci.impl.vars :as vars]))
@ -11,6 +12,17 @@
(def ^:dynamic *bb-edn-path* nil) (def ^:dynamic *bb-edn-path* nil)
(defmethod clojure.test/report :begin-test-var [m]
(println "===" (-> m :var meta :name))
(println))
(defmethod clojure.test/report :end-test-var [_m]
(let [{:keys [:fail :error]} @*report-counters*]
(when (and (= "true" (System/getenv "BABASHKA_FAIL_FAST"))
(or (pos? fail) (pos? error)))
(println "=== Failing fast")
(System/exit 1))))
(defn bb-jvm [input-or-opts & args] (defn bb-jvm [input-or-opts & args]
(reset! cp/cp-state nil) (reset! cp/cp-state nil)
(reset! main/env {}) (reset! main/env {})