diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2e1278ee..0e1ca428 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -373,144 +373,144 @@ Use -- to separate script command line args from bb command line args. tasks (when (and k bb-edn) (:tasks bb-edn)) user-task (when tasks (get tasks k))] - (if user-task (resolve-task user-task {:command-line-args (next options)}) - (let [opts (loop [options options - opts-map {}] - (if options - (let [opt (first options)] - (case opt - ("--") (assoc opts-map :command-line-args (next options)) - ("--clojure" ":clojure") (assoc opts-map :clojure true - :command-line-args (rest options)) - ("--version" ":version") {:version true} - ("--help" "-h" "-?") {:help? true} - ("--verbose")(recur (next options) - (assoc opts-map - :verbose? true)) - ("--describe" ":describe") (recur (next options) - (assoc opts-map - :describe? true)) - ("--stream") (recur (next options) - (assoc opts-map - :stream? true)) - ("-i") (recur (next options) - (assoc opts-map - :shell-in true)) - ("-I") (recur (next options) - (assoc opts-map - :edn-in true)) - ("-o") (recur (next options) - (assoc opts-map - :shell-out true)) - ("-O") (recur (next options) - (assoc opts-map - :edn-out true)) - ("-io") (recur (next options) - (assoc opts-map - :shell-in true - :shell-out true)) - ("-iO") (recur (next options) - (assoc opts-map - :shell-in true - :edn-out true)) - ("-Io") (recur (next options) - (assoc opts-map - :edn-in true - :shell-out true)) - ("-IO") (recur (next options) - (assoc opts-map - :edn-in true - :edn-out true)) - ("--classpath", "-cp") - (let [options (next options)] - (recur (next options) - (assoc opts-map :classpath (first options)))) - ("--uberscript" ":uberscript") - (let [options (next options)] - (recur (next options) - (assoc opts-map - :uberscript (first options)))) - ("--uberjar" ":uberjar") - (let [options (next options)] - (recur (next options) - (assoc opts-map - :uberjar (first options)))) - ("-f" "--file") - (let [options (next options)] - (recur (next options) - (assoc opts-map - :file (first options)))) - ("--jar" "-jar") - (let [options (next options)] - (recur (next options) - (assoc opts-map - :jar (first options)))) - ("--repl" ":repl") - (let [options (next options)] - (recur (next options) - (assoc opts-map - :repl true))) - ("--socket-repl" ":socket-repl") - (let [options (next options) - opt (first options) - opt (when (and opt (not (str/starts-with? opt "-"))) - opt) - options (if opt (next options) + (if user-task + (resolve-task user-task {:command-line-args (next options)}) + (let [opts (loop [options options + opts-map {}] + (if options + (let [opt (first options)] + (case opt + ("--") (assoc opts-map :command-line-args (next options)) + ("--clojure" ":clojure") (assoc opts-map :clojure true + :command-line-args (rest options)) + ("--version" ":version") {:version true} + ("--help" "-h" "-?") {:help? true} + ("--verbose")(recur (next options) + (assoc opts-map + :verbose? true)) + ("--describe" ":describe") (recur (next options) + (assoc opts-map + :describe? true)) + ("--stream") (recur (next options) + (assoc opts-map + :stream? true)) + ("-i") (recur (next options) + (assoc opts-map + :shell-in true)) + ("-I") (recur (next options) + (assoc opts-map + :edn-in true)) + ("-o") (recur (next options) + (assoc opts-map + :shell-out true)) + ("-O") (recur (next options) + (assoc opts-map + :edn-out true)) + ("-io") (recur (next options) + (assoc opts-map + :shell-in true + :shell-out true)) + ("-iO") (recur (next options) + (assoc opts-map + :shell-in true + :edn-out true)) + ("-Io") (recur (next options) + (assoc opts-map + :edn-in true + :shell-out true)) + ("-IO") (recur (next options) + (assoc opts-map + :edn-in true + :edn-out true)) + ("--classpath", "-cp") + (let [options (next options)] + (recur (next options) + (assoc opts-map :classpath (first options)))) + ("--uberscript" ":uberscript") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :uberscript (first options)))) + ("--uberjar" ":uberjar") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :uberjar (first options)))) + ("-f" "--file") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :file (first options)))) + ("--jar" "-jar") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :jar (first options)))) + ("--repl" ":repl") + (let [options (next options)] + (recur (next options) + (assoc opts-map + :repl true))) + ("--socket-repl" ":socket-repl") + (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 + :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)] - (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 #(= % ":--")) - (take-nth 2)) - options) - parsed (map parse-opts options)] - {: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)))) + {: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 + :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]}] (case (:task/type task) @@ -737,11 +737,14 @@ Use -- to separate script command line args from bb command line args. (defn main [& args] (let [opts (parse-opts args)] (if-let [do-opts (:do opts)] - (reduce (fn [_ opts] - (let [ret (exec opts)] - (if (pos? ret) - (reduced ret) - ret))) + (reduce (fn [prev-exit opts] + (if (and (pos? prev-exit) + (not= [":or-do"] opts)) + (reduced prev-exit) + (if (or (= [":and-do"] opts) + (= [":or-do"] opts)) + 0 + (exec (parse-opts opts))))) 0 do-opts) (exec opts)))) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index aa04f4db..5f3a951c 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -33,3 +33,17 @@ (is (fs/exists? temp-file)) (bb :clean) (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))))))