[#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,16 +424,14 @@ 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
:command-line-args (rest options)}
(fs/regular-file? opt) (fs/regular-file? opt)
(if (str/ends-with? opt ".jar") (if (str/ends-with? opt ".jar")
{:classpath classpath {:classpath classpath
@ -442,6 +440,10 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
{:classpath classpath {:classpath classpath
:file opt :file opt
:command-line-args (next options)}) :command-line-args (next options)})
(contains? tasks opt)
{:run opt
:classpath classpath
:command-line-args (rest options)}
(command? opt) (command? opt)
(recur (cons (str "--" opt) (next options)) opts) (recur (cons (str "--" opt) (next options)) opts)
:else :else

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`