From 6da8fc99b8e707c8aeff0a0e9629929db72f6a04 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 25 Apr 2021 21:51:00 +0200 Subject: [PATCH] Tasks: sort by occurence --- src/babashka/impl/deps.clj | 2 +- src/babashka/impl/tasks.clj | 23 ++++++++++++++++++--- src/babashka/main.clj | 4 +++- test/babashka/bb_edn_test.clj | 38 +++++++++++++++++------------------ test/babashka/test_utils.clj | 5 ++++- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/babashka/impl/deps.clj b/src/babashka/impl/deps.clj index 08d6ff20..ae84bea3 100644 --- a/src/babashka/impl/deps.clj +++ b/src/babashka/impl/deps.clj @@ -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 "" diff --git a/src/babashka/impl/tasks.clj b/src/babashka/impl/tasks.clj index 752b5732..fccc4258 100644 --- a/src/babashka/impl/tasks.clj +++ b/src/babashka/impl/tasks.clj @@ -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:") diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 123a07d1..b48f5f00 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -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) diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index fd22a9e6..412a9615 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -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 diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index 7badd6cc..c3f18752 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -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)]