This commit is contained in:
Michiel Borkent 2021-03-20 13:55:39 +01:00
parent 343c363319
commit 612edd6fc9
2 changed files with 159 additions and 142 deletions

View file

@ -373,144 +373,144 @@ Use -- to separate script command line args from bb command line args.
tasks (when (and k bb-edn) tasks (when (and k bb-edn)
(:tasks bb-edn)) (:tasks bb-edn))
user-task (when tasks (get tasks k))] user-task (when tasks (get tasks k))]
(if user-task (resolve-task user-task {:command-line-args (next options)}) (if user-task
(let [opts (loop [options options (resolve-task user-task {:command-line-args (next options)})
opts-map {}] (let [opts (loop [options options
(if options opts-map {}]
(let [opt (first options)] (if options
(case opt (let [opt (first options)]
("--") (assoc opts-map :command-line-args (next options)) (case opt
("--clojure" ":clojure") (assoc opts-map :clojure true ("--") (assoc opts-map :command-line-args (next options))
:command-line-args (rest options)) ("--clojure" ":clojure") (assoc opts-map :clojure true
("--version" ":version") {:version true} :command-line-args (rest options))
("--help" "-h" "-?") {:help? true} ("--version" ":version") {:version true}
("--verbose")(recur (next options) ("--help" "-h" "-?") {:help? true}
(assoc opts-map ("--verbose")(recur (next options)
:verbose? true)) (assoc opts-map
("--describe" ":describe") (recur (next options) :verbose? true))
(assoc opts-map ("--describe" ":describe") (recur (next options)
:describe? true)) (assoc opts-map
("--stream") (recur (next options) :describe? true))
(assoc opts-map ("--stream") (recur (next options)
:stream? true)) (assoc opts-map
("-i") (recur (next options) :stream? true))
(assoc opts-map ("-i") (recur (next options)
:shell-in true)) (assoc opts-map
("-I") (recur (next options) :shell-in true))
(assoc opts-map ("-I") (recur (next options)
:edn-in true)) (assoc opts-map
("-o") (recur (next options) :edn-in true))
(assoc opts-map ("-o") (recur (next options)
:shell-out true)) (assoc opts-map
("-O") (recur (next options) :shell-out true))
(assoc opts-map ("-O") (recur (next options)
:edn-out true)) (assoc opts-map
("-io") (recur (next options) :edn-out true))
(assoc opts-map ("-io") (recur (next options)
:shell-in true (assoc opts-map
:shell-out true)) :shell-in true
("-iO") (recur (next options) :shell-out true))
(assoc opts-map ("-iO") (recur (next options)
:shell-in true (assoc opts-map
:edn-out true)) :shell-in true
("-Io") (recur (next options) :edn-out true))
(assoc opts-map ("-Io") (recur (next options)
:edn-in true (assoc opts-map
:shell-out true)) :edn-in true
("-IO") (recur (next options) :shell-out true))
(assoc opts-map ("-IO") (recur (next options)
:edn-in true (assoc opts-map
:edn-out true)) :edn-in true
("--classpath", "-cp") :edn-out true))
(let [options (next options)] ("--classpath", "-cp")
(recur (next options) (let [options (next options)]
(assoc opts-map :classpath (first options)))) (recur (next options)
("--uberscript" ":uberscript") (assoc opts-map :classpath (first options))))
(let [options (next options)] ("--uberscript" ":uberscript")
(recur (next options) (let [options (next options)]
(assoc opts-map (recur (next options)
:uberscript (first options)))) (assoc opts-map
("--uberjar" ":uberjar") :uberscript (first options))))
(let [options (next options)] ("--uberjar" ":uberjar")
(recur (next options) (let [options (next options)]
(assoc opts-map (recur (next options)
:uberjar (first options)))) (assoc opts-map
("-f" "--file") :uberjar (first options))))
(let [options (next options)] ("-f" "--file")
(recur (next options) (let [options (next options)]
(assoc opts-map (recur (next options)
:file (first options)))) (assoc opts-map
("--jar" "-jar") :file (first options))))
(let [options (next options)] ("--jar" "-jar")
(recur (next options) (let [options (next options)]
(assoc opts-map (recur (next options)
:jar (first options)))) (assoc opts-map
("--repl" ":repl") :jar (first options))))
(let [options (next options)] ("--repl" ":repl")
(recur (next options) (let [options (next options)]
(assoc opts-map (recur (next options)
:repl true))) (assoc opts-map
("--socket-repl" ":socket-repl") :repl true)))
(let [options (next options) ("--socket-repl" ":socket-repl")
opt (first options) (let [options (next options)
opt (when (and opt (not (str/starts-with? opt "-"))) opt (first options)
opt) opt (when (and opt (not (str/starts-with? opt "-")))
options (if opt (next options) opt)
options (if opt (next options)
options)]
(recur options
(assoc opts-map
:socket-repl (or opt "1666"))))
("--nrepl-server" ":nrepl-server")
(let [options (next options)
opt (first options)
opt (when (and opt (not (str/starts-with? opt "-")))
opt)
options (if opt (next options)
options)]
(recur options
(assoc opts-map
:nrepl (or opt "1667"))))
("--eval", "-e")
(let [options (next options)]
(recur (next options)
(update opts-map :expressions (fnil conj []) (first options))))
("--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 (= % ":and-do")
(= % ":or-do"))))
options)] options)]
(recur options {:do options})
(assoc opts-map (":invoke")
:socket-repl (or opt "1666")))) {:exec-src
("--nrepl-server" ":nrepl-server") (pr-str '(if-let [f (requiring-resolve (symbol (first *command-line-args*)))]
(let [options (next options) (apply f (rest *command-line-args*))
opt (first options) (throw (Exception. (str "Var not found: " (first *command-line-args*)
opt (when (and opt (not (str/starts-with? opt "-"))) " " (babashka.classpath/get-classpath))))))
opt) :command-line-args (next options)}
options (if opt (next options) ;; fallback
options)] (if (some opts-map [:file :jar :socket-repl :expressions :main])
(recur options (assoc opts-map
(assoc opts-map :command-line-args options)
:nrepl (or opt "1667")))) (let [trimmed-opt (str/triml opt)
("--eval", "-e") c (.charAt trimmed-opt 0)]
(let [options (next options)] (case c
(recur (next options) (\( \{ \[ \* \@ \#)
(update opts-map :expressions (fnil conj []) (first options)))) (-> opts-map
("--main", "-m") (update :expressions (fnil conj []) (first options))
(let [options (next options)] (assoc :command-line-args (next options)))
(recur (next options) (if (fs/exists? opt)
(assoc opts-map :main (first options)))) (assoc opts-map
(":do") (if (str/ends-with? opt ".jar")
(let [options (next options) :jar :file) opt
options (into [] (comp (partition-by #(= % ":--")) :command-line-args (next options))
(take-nth 2)) (error (str "File does not exist: " opt) 1)))))))
options) opts-map))]
parsed (map parse-opts options)] opts))))
{:do parsed})
(":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
:command-line-args options)
(let [trimmed-opt (str/triml opt)
c (.charAt trimmed-opt 0)]
(case c
(\( \{ \[ \* \@ \#)
(-> opts-map
(update :expressions (fnil conj []) (first options))
(assoc :command-line-args (next options)))
(if (fs/exists? opt)
(assoc opts-map
(if (str/ends-with? opt ".jar")
:jar :file) opt
:command-line-args (next options))
(error (str "File does not exist: " opt) 1)))))))
opts-map))]
opts))))
(defn resolve-task [task {:keys [:command-line-args]}] (defn resolve-task [task {:keys [:command-line-args]}]
(case (:task/type task) (case (:task/type task)
@ -737,11 +737,14 @@ Use -- to separate script command line args from bb command line args.
(defn main [& args] (defn main [& args]
(let [opts (parse-opts args)] (let [opts (parse-opts args)]
(if-let [do-opts (:do opts)] (if-let [do-opts (:do opts)]
(reduce (fn [_ opts] (reduce (fn [prev-exit opts]
(let [ret (exec opts)] (if (and (pos? prev-exit)
(if (pos? ret) (not= [":or-do"] opts))
(reduced ret) (reduced prev-exit)
ret))) (if (or (= [":and-do"] opts)
(= [":or-do"] opts))
0
(exec (parse-opts opts)))))
0 0
do-opts) do-opts)
(exec opts)))) (exec opts))))

View file

@ -33,3 +33,17 @@
(is (fs/exists? temp-file)) (is (fs/exists? temp-file))
(bb :clean) (bb :clean)
(is (not (fs/exists? temp-file)))))) (is (not (fs/exists? temp-file))))))
(deftest do-task-test
(let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))]
(with-config {:tasks {:clean {:task/type :shell
:args ["rm" (str temp-file)]}
:sum {:task/type :babashka
:args ["-e" "(+ 1 2 3)"]}
:all {:task/type :babashka
:args [:do :clean :and-do :sum]}}}
(is (fs/exists? temp-file))
(let [res (bb :all)]
(is (= 6 res)))
(is (not (fs/exists? temp-file))))))