[#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))))
opts-map)))
(defn parse-classpath [options]
(defn parse-global-opts [options]
(when-let [f (first options)]
(case f
("--classpath" "-cp") [(nnext options) (second options)]
@ -424,16 +424,14 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(defn parse-opts
([options] (parse-opts options nil))
([options opts]
(let [[options classpath] (parse-classpath options)
(let [[options classpath] (parse-global-opts options)
opts (if classpath (assoc opts :classpath classpath)
opts)
opt (first options)
tasks (into #{} (map str) (keys (:tasks @common/bb-edn)))]
(when opt
(cond (contains? tasks opt)
{:run opt
:classpath classpath
:command-line-args (rest options)}
;; FILE > TASK > SUBCOMMAND
(cond
(fs/regular-file? opt)
(if (str/ends-with? opt ".jar")
{:classpath classpath
@ -442,6 +440,10 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
{:classpath classpath
:file opt
:command-line-args (next options)})
(contains? tasks opt)
{:run opt
:classpath classpath
:command-line-args (rest options)}
(command? opt)
(recur (cons (str "--" opt) (next options)) opts)
:else

View file

@ -1,6 +1,8 @@
(ns babashka.bb-edn-test
(:require
[babashka.fs :as fs]
[babashka.impl.common :as common]
[babashka.main :as main]
[babashka.test-utils :as test-utils]
[clojure.edn :as edn]
[clojure.string :as str]
@ -107,6 +109,19 @@
(let [res (test-utils/bb nil "tasks")]
(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:
;; Do we want to support the same parsing as the clj CLI?
;; Or do we want `--aliases :foo:bar`