foo
This commit is contained in:
parent
235adfcfe5
commit
f02bf19e80
4 changed files with 256 additions and 235 deletions
|
|
@ -102,9 +102,7 @@
|
|||
(binding [*out* *err*]
|
||||
(apply println msgs)))
|
||||
|
||||
(defn print-help [ctx command-line-args]
|
||||
(if (empty? command-line-args)
|
||||
(do
|
||||
(defn print-help [_ctx _command-line-args]
|
||||
(println (str "Babashka v" version))
|
||||
;; (println (str "sci v" (str/trim (slurp (io/resource "SCI_VERSION")))))
|
||||
(println)
|
||||
|
|
@ -112,9 +110,11 @@
|
|||
(println "
|
||||
Help:
|
||||
|
||||
--help, -h or -? Print this help text.
|
||||
--version Print the current version of babashka.
|
||||
--describe Print an EDN map with information about this version of babashka.
|
||||
help, -h or -? Print this help text.
|
||||
version, Print the current 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:
|
||||
|
||||
|
|
@ -126,9 +126,9 @@ Evaluation:
|
|||
|
||||
REPL:
|
||||
|
||||
--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).
|
||||
--nrepl-server Start nREPL server. Specify port (e.g. 1667) or host and port separated by colon (e.g. 127.0.0.1:1667).
|
||||
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).
|
||||
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:
|
||||
|
||||
|
|
@ -140,28 +140,31 @@ In- and output flags:
|
|||
|
||||
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 <jar> Similar to --uberscript but creates jar file.
|
||||
uberjar <jar> Similar to --uberscript but creates jar file.
|
||||
|
||||
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.
|
||||
Everything after that is bound to *command-line-args*.
|
||||
|
||||
Use -- to separate script command line args from bb command line args.
|
||||
")
|
||||
[nil 0]) ;; end do
|
||||
(let [k (first command-line-args)
|
||||
k (keyword (subs k 1))]
|
||||
[nil 0])
|
||||
|
||||
(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])]
|
||||
(let [{:keys [:args]
|
||||
task-key :task} (decode-task task)]
|
||||
(if-let [help-text (:help task)]
|
||||
(if-let [help-text (:doc task)]
|
||||
[(println help-text) 0]
|
||||
(if-let [main (when (= :main task-key)
|
||||
(first args))]
|
||||
|
|
@ -170,13 +173,17 @@ Use -- to separate script command line args from bb command line args.
|
|||
main)]
|
||||
(if-let [doc (sci/eval-string* ctx (format "(some-> (requiring-resolve '%s) meta :doc)" main))]
|
||||
[(println doc) 0]
|
||||
[(print-error "No help found for task:" k) 1]))
|
||||
[(print-error "No help found for task:" k) 1])))
|
||||
[(print-error "Task does not exist:" k) 1])
|
||||
,)) ;; end if
|
||||
,) ;; end defn
|
||||
[(print-error "No docstring found for task:" k) 1]))
|
||||
[(print-error "No docstring found for task:" k) 1])))
|
||||
[(print-error "Task does not exist:" k) 1]))
|
||||
(let [arg (if (str/includes? arg "/") arg (str "clojure.core/" arg))]
|
||||
(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]
|
||||
(if (seq tasks)
|
||||
(let [tasks (into (sorted-map) tasks)
|
||||
ks (keys tasks)
|
||||
longest (apply max
|
||||
|
|
@ -191,7 +198,9 @@ Use -- to separate script command line args from bb command line args.
|
|||
(str " " d)))))
|
||||
(println)
|
||||
(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 []
|
||||
(println
|
||||
|
|
@ -421,6 +430,21 @@ Use -- to separate script command line args from bb command line args.
|
|||
(println msg)
|
||||
{: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]
|
||||
(let [fst (when options (first options))
|
||||
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)))
|
||||
k (when (and key? (not expanded))
|
||||
(keyword (first keys)))
|
||||
task? (or expanded k)
|
||||
bb-edn (when task? @bb-edn)
|
||||
tasks (when (and task? bb-edn)
|
||||
bb-edn @bb-edn
|
||||
tasks (when 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
|
||||
(resolve-task tasks user-task {:command-line-args (next options)})
|
||||
expanded (resolve-task tasks expanded nil)
|
||||
(and (command? opt)
|
||||
(not (fs/regular-file? opt)))
|
||||
(recur (cons (str "--" opt) (next options)))
|
||||
:else
|
||||
(let [opts (loop [options options
|
||||
opts-map {}]
|
||||
|
|
@ -444,17 +472,21 @@ Use -- to separate script command line args from bb command line args.
|
|||
(let [opt (first options)]
|
||||
(case opt
|
||||
("--") (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))
|
||||
("--version" ":version") {:version true}
|
||||
("--help" "-h" "-?" ":help") {:help true
|
||||
("--version") {:version true}
|
||||
("--help" "-h" "-?" "help")
|
||||
{:help true
|
||||
: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)}
|
||||
("--verbose") (recur (next options)
|
||||
(assoc opts-map
|
||||
:verbose? true))
|
||||
("--describe" ":describe") (recur (next options)
|
||||
("--describe") (recur (next options)
|
||||
(assoc opts-map
|
||||
:describe? true))
|
||||
("--stream") (recur (next options)
|
||||
|
|
@ -492,12 +524,12 @@ Use -- to separate script command line args from bb command line args.
|
|||
(let [options (next options)]
|
||||
(recur (next options)
|
||||
(assoc opts-map :classpath (first options))))
|
||||
("--uberscript" ":uberscript")
|
||||
("--uberscript")
|
||||
(let [options (next options)]
|
||||
(recur (next options)
|
||||
(assoc opts-map
|
||||
:uberscript (first options))))
|
||||
("--uberjar" ":uberjar")
|
||||
("--uberjar")
|
||||
(let [options (next options)]
|
||||
(recur (next options)
|
||||
(assoc opts-map
|
||||
|
|
@ -512,12 +544,12 @@ Use -- to separate script command line args from bb command line args.
|
|||
(recur (next options)
|
||||
(assoc opts-map
|
||||
:jar (first options))))
|
||||
("--repl" ":repl")
|
||||
("--repl")
|
||||
(let [options (next options)]
|
||||
(recur (next options)
|
||||
(assoc opts-map
|
||||
:repl true)))
|
||||
("--socket-repl" ":socket-repl")
|
||||
("--socket-repl")
|
||||
(let [options (next options)
|
||||
opt (first options)
|
||||
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
|
||||
(assoc opts-map
|
||||
:socket-repl (or opt "1666"))))
|
||||
("--nrepl-server" ":nrepl-server")
|
||||
("--nrepl-server")
|
||||
(let [options (next options)
|
||||
opt (first options)
|
||||
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)]
|
||||
(recur (next options)
|
||||
(update opts-map :expressions (fnil conj []) (first options))))
|
||||
("--main", "-m", ":main")
|
||||
("--main", "-m",)
|
||||
(let [options (next options)]
|
||||
(recur (next 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
|
||||
(if (some opts-map [:file :jar :socket-repl :expressions :main])
|
||||
(assoc opts-map
|
||||
|
|
@ -630,7 +647,7 @@ Use -- to separate script command line args from bb command line args.
|
|||
:verbose? :classpath
|
||||
:main :uberscript :describe?
|
||||
:jar :uberjar :clojure
|
||||
:exec-src :tasks]
|
||||
:exec-src :tasks :doc]
|
||||
exec-fn :exec}
|
||||
opts
|
||||
_ (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]
|
||||
help (print-help sci-ctx command-line-args)
|
||||
tasks (print-tasks tasks)
|
||||
doc (print-doc sci-ctx command-line-args)
|
||||
describe?
|
||||
[(print-describe) 0]
|
||||
repl [(repl/start-repl! sci-ctx) 0]
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
(with-config {}
|
||||
(is (thrown-with-msg?
|
||||
Exception #"Task does not exist: :sum"
|
||||
(bb :help :sum)))))
|
||||
(bb "doc" :sum)))))
|
||||
|
||||
(deftest babashka-task-test
|
||||
(with-config {:tasks {:sum [:babashka "-e" "(+ 1 2 3)"]}}
|
||||
|
|
@ -100,38 +100,40 @@
|
|||
(is (= 6 (bb :all))))))
|
||||
|
||||
(deftest prioritize-user-task-test
|
||||
(is (map? (bb :describe)))
|
||||
(is (map? (bb "describe")))
|
||||
(with-config {:tasks {:describe [:babashka "-e" "(+ 1 2 3)"]}}
|
||||
(is (= 6 (bb :describe)))))
|
||||
|
||||
(deftest help-task-test
|
||||
(deftest doc-task-test
|
||||
(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
|
||||
0. ..."
|
||||
:task [:babashka "-e" "(+ 1 2 3)"]}}}
|
||||
(is (str/includes? (apply test-utils/bb nil
|
||||
(map str [:help :cool-task]))
|
||||
(map str ["doc" :cool-task]))
|
||||
"Usage: bb :cool-task"))))
|
||||
|
||||
(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
|
||||
{: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
|
||||
0. ..."}
|
||||
:task [:babashka "-e" "(+ 1 2 3)"]
|
||||
:cool-task-2
|
||||
{: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
|
||||
0. ..."
|
||||
:task [:babashka "-e" "(+ 4 5 6)"]}}}
|
||||
(let [res (apply test-utils/bb nil
|
||||
(map str [:tasks]))]
|
||||
(let [res (test-utils/bb nil "tasks")]
|
||||
(is (str/includes? res "The following tasks are available:"))
|
||||
(is (str/includes? res ":task-1 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]}}
|
||||
(is (= '("1" "2" "3") (bb :main-task)))
|
||||
(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")))))
|
||||
|
|
|
|||
|
|
@ -7,21 +7,10 @@
|
|||
[clojure.java.io :as io]
|
||||
[clojure.java.shell :refer [sh]]
|
||||
[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]]
|
||||
[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]
|
||||
(edn/read-string
|
||||
{:readers *data-readers*
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
[babashka.main :as main]
|
||||
[babashka.process :as p]
|
||||
[clojure.edn :as edn]
|
||||
[clojure.test :as test :refer [*report-counters*]]
|
||||
[sci.core :as sci]
|
||||
[sci.impl.vars :as vars]))
|
||||
|
||||
|
|
@ -11,6 +12,17 @@
|
|||
|
||||
(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]
|
||||
(reset! cp/cp-state nil)
|
||||
(reset! main/env {})
|
||||
|
|
|
|||
Loading…
Reference in a new issue