From 5ff9d8328a44f639bf5eded1740b014a3a425af7 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 24 Apr 2021 13:05:27 +0200 Subject: [PATCH] [#796] Support map literal in task --- src/babashka/impl/tasks.clj | 28 ++++++++++++++-------------- test/babashka/bb_edn_test.clj | 7 ++++--- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/babashka/impl/tasks.clj b/src/babashka/impl/tasks.clj index de7d2085..cb99bea5 100644 --- a/src/babashka/impl/tasks.clj +++ b/src/babashka/impl/tasks.clj @@ -100,22 +100,22 @@ (assemble-task-1 task-name task parallel? nil nil)) ([task-name task parallel? last?] (assemble-task-1 task-name task parallel? last? nil)) ([task-name task parallel? last? depends] - (cond (qualified-symbol? task) - (let [prog (format "(apply %s *command-line-args*)" task) - prog (wrap-depends prog depends parallel?) - prog (wrap-def task-name prog parallel? last?) - prog (format " + (let [[task depends] (if (map? task) + [(:task task) (:depends task)] + [task depends])] + (if (qualified-symbol? task) + (let [prog (format "(apply %s *command-line-args*)" task) + prog (wrap-depends prog depends parallel?) + prog (wrap-def task-name prog parallel? last?) + prog (format " (do (require (quote %s)) %s)" - (namespace task) - prog)] - prog) - (map? task) - (let [t (:task task)] - (assemble-task-1 task-name t parallel? last? (:depends task))) - :else (let [task (pr-str task) - prog (wrap-depends task depends parallel?)] - (wrap-def task-name prog parallel? last?))))) + (namespace task) + prog)] + prog) + (let [task (pr-str task) + prog (wrap-depends task depends parallel?)] + (wrap-def task-name prog parallel? last?)))))) (defn format-task [init requires prog] (format " diff --git a/test/babashka/bb_edn_test.clj b/test/babashka/bb_edn_test.clj index 9fcd3a70..4e44c1d1 100644 --- a/test/babashka/bb_edn_test.clj +++ b/test/babashka/bb_edn_test.clj @@ -77,8 +77,6 @@ foo {:depends [bar] :task (fs/exists? ".")}}} (is (= true (bb "foo"))))) - ;; Note: this behavior with :when was complex, since the place where :when - ;; is inserted isn't very intuitive here ;; This is why we don't support :when for now #_(testing "depends with :when" (test-utils/with-config {:tasks {'quux (list 'spit out "quux\n") @@ -89,7 +87,10 @@ 'foo {:depends ['quux 'bar] :task (list 'spit out "foo\n" :append true)}}} (bb "foo") - (is (= "quux\nbaz\nbar\nfoo\n" (slurp out))))))) + (is (= "quux\nbaz\nbar\nfoo\n" (slurp out))))) + (testing "map returned from task" + (test-utils/with-config '{:tasks {foo {:task {:a 1 :b 2}}}} + (is (= {:a 1 :b 2} (bb "foo"))))))) (deftest list-tasks-test (test-utils/with-config {}