[#800] Fix task priority

This commit is contained in:
Michiel Borkent 2021-04-24 11:51:03 +02:00
parent c6beb03635
commit 6806398722
2 changed files with 166 additions and 149 deletions

View file

@ -415,7 +415,7 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(assoc opts-map :run fst :command-line-args (next args)))) (assoc opts-map :run fst :command-line-args (next args))))
opts-map))) opts-map)))
(defn parse-classpath [options] (defn parse-global-opts [options]
(when-let [f (first options)] (when-let [f (first options)]
(case f (case f
("--classpath" "-cp") [(nnext options) (second options)] ("--classpath" "-cp") [(nnext options) (second options)]
@ -424,160 +424,162 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(defn parse-opts (defn parse-opts
([options] (parse-opts options nil)) ([options] (parse-opts options nil))
([options opts] ([options opts]
(let [[options classpath] (parse-classpath options) (let [[options classpath] (parse-global-opts options)
opts (if classpath (assoc opts :classpath classpath) opts (if classpath (assoc opts :classpath classpath)
opts) opts)
opt (first options) opt (first options)
tasks (into #{} (map str) (keys (:tasks @common/bb-edn)))] tasks (into #{} (map str) (keys (:tasks @common/bb-edn)))]
(when opt (when opt
(cond (contains? tasks opt) ;; FILE > TASK > SUBCOMMAND
{:run opt (cond
:classpath classpath (fs/regular-file? opt)
:command-line-args (rest options)} (if (str/ends-with? opt ".jar")
(fs/regular-file? opt) {:classpath classpath
(if (str/ends-with? opt ".jar") :jar opt
{:classpath classpath :command-line-args (next options)}
:jar opt {:classpath classpath
:command-line-args (next options)} :file opt
{:classpath classpath :command-line-args (next options)})
:file opt (contains? tasks opt)
:command-line-args (next options)}) {:run opt
(command? opt) :classpath classpath
(recur (cons (str "--" opt) (next options)) opts) :command-line-args (rest options)}
:else (command? opt)
(let [opts (loop [options options (recur (cons (str "--" opt) (next options)) opts)
opts-map opts] :else
(if options (let [opts (loop [options options
(let [opt (first options)] opts-map opts]
(case opt (if options
("--") (assoc opts-map :command-line-args (next options)) (let [opt (first options)]
("--clojure") (assoc opts-map :clojure true (case opt
:command-line-args (rest options)) ("--") (assoc opts-map :command-line-args (next options))
("--version") {:version true} ("--clojure") (assoc opts-map :clojure true
("--help" "-h" "-?" "help") :command-line-args (rest options))
{:help true ("--version") {:version true}
:command-line-args (rest options)} ("--help" "-h" "-?" "help")
("--doc") {:help true
{:doc true :command-line-args (rest options)}
:command-line-args (rest options)} ("--doc")
("--verbose") (recur (next options) {:doc true
(assoc opts-map :command-line-args (rest options)}
:verbose? true)) ("--verbose") (recur (next options)
("--describe") (recur (next options) (assoc opts-map
(assoc opts-map :verbose? true))
:describe? true)) ("--describe") (recur (next options)
("--stream") (recur (next options) (assoc opts-map
(assoc opts-map :describe? true))
:stream? true)) ("--stream") (recur (next options)
("-i") (recur (next options) (assoc opts-map
(assoc opts-map :stream? true))
:shell-in true)) ("-i") (recur (next options)
("-I") (recur (next options) (assoc opts-map
(assoc opts-map :shell-in true))
:edn-in true)) ("-I") (recur (next options)
("-o") (recur (next options) (assoc opts-map
(assoc opts-map :edn-in true))
:shell-out true)) ("-o") (recur (next options)
("-O") (recur (next options) (assoc opts-map
(assoc opts-map :shell-out true))
:edn-out true)) ("-O") (recur (next options)
("-io") (recur (next options) (assoc opts-map
(assoc opts-map :edn-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 :shell-out true))
:shell-in true ("-iO") (recur (next options)
:edn-out true)) (assoc opts-map
("-Io") (recur (next options) :shell-in true
(assoc opts-map :edn-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 :shell-out true))
:edn-in true ("-IO") (recur (next options)
:edn-out true)) (assoc opts-map
("--classpath", "-cp") :edn-in true
(let [options (next options)] :edn-out true))
(recur (next options) ("--classpath", "-cp")
(assoc opts-map :classpath (first options)))) (let [options (next options)]
("--uberscript") (recur (next options)
(let [options (next options)] (assoc opts-map :classpath (first options))))
(recur (next options) ("--uberscript")
(assoc opts-map (let [options (next options)]
:uberscript (first options)))) (recur (next options)
("--uberjar") (assoc opts-map
(let [options (next options)] :uberscript (first options))))
(recur (next options) ("--uberjar")
(assoc opts-map (let [options (next options)]
:uberjar (first options)))) (recur (next options)
("-f" "--file") (assoc opts-map
(let [options (next options)] :uberjar (first options))))
(recur (next options) ("-f" "--file")
(assoc opts-map (let [options (next options)]
:file (first options)))) (recur (next options)
("--jar" "-jar") (assoc opts-map
(let [options (next options)] :file (first options))))
(recur (next options) ("--jar" "-jar")
(assoc opts-map (let [options (next options)]
:jar (first options)))) (recur (next options)
("--repl") (assoc opts-map
(let [options (next options)] :jar (first options))))
(recur (next options) ("--repl")
(assoc opts-map (let [options (next options)]
:repl true))) (recur (next options)
("--socket-repl") (assoc opts-map
(let [options (next options) :repl true)))
opt (first options) ("--socket-repl")
opt (when (and opt (not (str/starts-with? opt "-"))) (let [options (next options)
opt) opt (first options)
options (if opt (next options) opt (when (and opt (not (str/starts-with? opt "-")))
options)] opt)
(recur options options (if opt (next options)
(assoc opts-map options)]
:socket-repl (or opt "1666")))) (recur options
("--nrepl-server") (assoc opts-map
(let [options (next options) :socket-repl (or opt "1666"))))
opt (first options) ("--nrepl-server")
opt (when (and opt (not (str/starts-with? opt "-"))) (let [options (next options)
opt) opt (first options)
options (if opt (next options) opt (when (and opt (not (str/starts-with? opt "-")))
options)] opt)
(recur options options (if opt (next options)
(assoc opts-map options)]
:nrepl (or opt "1667")))) (recur options
("--eval", "-e") (assoc opts-map
(let [options (next options)] :nrepl (or opt "1667"))))
(recur (next options) ("--eval", "-e")
(update opts-map :expressions (fnil conj []) (first options)))) (let [options (next options)]
("--main", "-m",) (recur (next options)
(let [options (next options)] (update opts-map :expressions (fnil conj []) (first options))))
(recur (next options) ("--main", "-m",)
(assoc opts-map :main (first options)))) (let [options (next options)]
("--run") (recur (next options)
(parse-run-opts opts-map (next options)) (assoc opts-map :main (first options))))
("--tasks") ("--run")
(assoc opts-map :list-tasks true (parse-run-opts opts-map (next options))
:command-line-args (next options)) ("--tasks")
;; fallback (assoc opts-map :list-tasks true
(if (and opts-map :command-line-args (next options))
(some opts-map [:file :jar :socket-repl :expressions :main :run])) ;; fallback
(if (and opts-map
(some opts-map [:file :jar :socket-repl :expressions :main :run]))
(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)))
(assoc opts-map (assoc opts-map
:command-line-args options) (if (str/ends-with? opt ".jar")
(let [trimmed-opt (str/triml opt) :jar
c (.charAt trimmed-opt 0)] :file) opt
(case c :command-line-args (next options)))))))
(\( \{ \[ \* \@ \#) opts-map))]
(-> opts-map opts))))))
(update :expressions (fnil conj []) (first options))
(assoc :command-line-args (next options)))
(assoc opts-map
(if (str/ends-with? opt ".jar")
:jar
:file) opt
:command-line-args (next options)))))))
opts-map))]
opts))))))
(def env (atom {})) (def env (atom {}))

View file

@ -1,6 +1,8 @@
(ns babashka.bb-edn-test (ns babashka.bb-edn-test
(:require (:require
[babashka.fs :as fs] [babashka.fs :as fs]
[babashka.impl.common :as common]
[babashka.main :as main]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.string :as str] [clojure.string :as str]
@ -107,6 +109,19 @@
(let [res (test-utils/bb nil "tasks")] (let [res (test-utils/bb nil "tasks")]
(is (= "The following tasks are available:\n\ntask1 task1 doc\ntask2 task2 doc\n" res))))) (is (= "The following tasks are available:\n\ntask1 task1 doc\ntask2 task2 doc\n" res)))))
(deftest task-priority-test
(testing "FILE > TASK > SUBCOMMAND"
(is (= "foo.jar" (:uberjar (main/parse-opts ["uberjar" "foo.jar"]))))
(test-utils/with-config '{:tasks {uberjar (+ 1 2 3)}}
(vreset! common/bb-edn (edn/read-string (slurp test-utils/*bb-edn-path*)))
(is (= "uberjar" (:run (main/parse-opts ["uberjar"])))))
(try
(test-utils/with-config '{:tasks {uberjar (+ 1 2 3)}}
(spit "uberjar" "#!/usr/bin/env bb\n(+ 1 2 3)")
(vreset! common/bb-edn (edn/read-string (slurp test-utils/*bb-edn-path*)))
(is (= "uberjar" (:file (main/parse-opts ["uberjar"])))))
(finally (fs/delete "uberjar")))))
;; TODO: ;; TODO:
;; Do we want to support the same parsing as the clj CLI? ;; Do we want to support the same parsing as the clj CLI?
;; Or do we want `--aliases :foo:bar` ;; Or do we want `--aliases :foo:bar`