Tasks: sort by occurence

This commit is contained in:
Michiel Borkent 2021-04-25 21:51:00 +02:00
parent e6e82906f4
commit 6da8fc99b8
5 changed files with 47 additions and 25 deletions

View file

@ -59,7 +59,7 @@
([deps-map {:keys [:aliases]}]
(when-let [paths (:paths deps-map)]
(cp/add-classpath (str/join cp/path-sep paths)))
(when-let [deps-map (not-empty (dissoc deps-map :paths :tasks))]
(when-let [deps-map (not-empty (dissoc deps-map :paths :tasks :raw))]
(let [deps-map (assoc-in deps-map [:aliases :org.babashka/defaults]
'{:replace-paths [] ;; babashka sets paths manually
:classpath-overrides {org.clojure/clojure ""

View file

@ -4,6 +4,9 @@
[babashka.process :as p]
[clojure.java.io :as io]
[clojure.string :as str]
[rewrite-clj.node :as node]
[rewrite-clj.parser :as parser]
[rewrite-clj.zip :as zip]
[sci.core :as sci]))
(def sci-ns (sci/create-ns 'babashka.tasks nil))
@ -279,16 +282,30 @@
fn-sym)]
(sci/eval-string* sci-ctx prog)))))
(defn key-order [edn]
(let [forms (parser/parse-string-all edn)
the-map (some #(when (= :map (node/tag %))
%)
(:children forms))
loc (zip/edn the-map)
loc (zip/down loc)
loc (zip/find-next-value loc :tasks)
loc (zip/right loc)
loc (zip/down loc)]
(filter symbol?
(map zip/sexpr
(take-while #(not (zip/end? %))
(take-nth 2 (iterate zip/right loc)))))))
(defn list-tasks
[sci-ctx]
(let [tasks (:tasks @bb-edn)]
(if (seq tasks)
(let [names (keys tasks)
names (filter symbol? names)
(let [raw-edn (:raw @bb-edn)
names (key-order raw-edn)
names (map str names)
names (remove #(str/starts-with? % "-") names)
names (remove #(:private (get tasks (symbol %))) names)
names (sort names)
longest (apply max (map count names))
fmt (str "%1$-" longest "s")]
(println "The following tasks are available:")

View file

@ -810,7 +810,9 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
"bb.edn")
bb-edn (or (when (fs/exists? bb-edn-file)
(let [edn (edn/read-string (slurp bb-edn-file))]
(let [raw-string (slurp bb-edn-file)
edn (edn/read-string raw-string)
edn (assoc edn :raw raw-string)]
(vreset! common/bb-edn edn)))
;; tests may have modified bb-edn
@common/bb-edn)

View file

@ -131,26 +131,26 @@
(test-utils/with-config {}
(let [res (test-utils/bb nil "tasks")]
(is (str/includes? res "No tasks found."))))
(test-utils/with-config {:paths ["test-resources/task_scripts"]
:tasks {:requires '([tasks :as t])
'task1
{:doc "task1 doc"
:task '(+ 1 2 3)}
'task2
{:doc "task2 doc"
:task '(+ 4 5 6)}
'-task3
{:task '(+ 1 2 3)}
'task4
{:task '(+ 1 2 3)
:private true}
'foo 'tasks/foo
'bar 't/foo
'baz 'non-existing/bar
'quux {:requires '([tasks :as t2])
:task 't2/foo}}}
(test-utils/with-config "{:paths [\"test-resources/task_scripts\"]
:tasks {:requires ([tasks :as t])
task1
{:doc \"task1 doc\"
:task (+ 1 2 3)}
task2
{:doc \"task2 doc\"
:task (+ 4 5 6)}
-task3
{:task (+ 1 2 3)}
task4
{:task (+ 1 2 3)
:private true}
foo tasks/foo
bar t/foo
baz non-existing/bar
quux {:requires ([tasks :as t2])
:task t2/foo}}}"
(let [res (test-utils/bb nil "tasks")]
(is (= "The following tasks are available:\n\nbar Foo docstring\nbaz \nfoo Foo docstring\nquux Foo docstring\ntask1 task1 doc\ntask2 task2 doc\n"
(is (= "The following tasks are available:\n\ntask1 task1 doc\ntask2 task2 doc\nfoo Foo docstring\nbar Foo docstring\nbaz \nquux Foo docstring\n"
res)))))
(deftest task-priority-test

View file

@ -29,7 +29,10 @@
(reset! cp/cp-state nil)
(reset! main/env {})
(if-let [path *bb-edn-path*]
(vreset! common/bb-edn (edn/read-string (slurp path)))
(let [raw (slurp path)]
(vreset! common/bb-edn
(assoc (edn/read-string raw)
:raw raw)))
(vreset! common/bb-edn nil))
(let [os (java.io.StringWriter.)
es (if-let [err (:err input-or-opts)]