This commit is contained in:
Michiel Borkent 2021-03-20 13:13:28 +01:00
parent 91106b5401
commit a44f07a665
3 changed files with 50 additions and 22 deletions

View file

@ -362,18 +362,14 @@ Use -- to separate script command line args from bb command line args.
(println msg) (println msg)
{:exec (fn [] [nil exit])})) {:exec (fn [] [nil exit])}))
(def ^:dynamic *bb-edn* (def bb-edn
(delay (atom nil))
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
"bb.edn")]
(when (fs/exists? bb-edn-file)
(edn/read-string (slurp bb-edn-file))))))
(defn parse-opts [options] (defn parse-opts [options]
(let [fst (when options (first options)) (let [fst (when options (first options))
key? (when fst (str/starts-with? fst ":")) key? (when fst (str/starts-with? fst ":"))
k (when key? (keyword (subs fst 1))) k (when key? (keyword (subs fst 1)))
bb-edn (when k @*bb-edn*) bb-edn (when k @bb-edn)
tasks (when (and k bb-edn) tasks (when (and k bb-edn)
(:tasks bb-edn)) (:tasks bb-edn))
user-task (when tasks (get tasks k))] user-task (when tasks (get tasks k))]
@ -516,6 +512,21 @@ Use -- to separate script command line args from bb command line args.
opts-map))] opts-map))]
opts)))) opts))))
(defn resolve-task [task {:keys [:command-line-args]}]
(case (:task/type task)
:babashka
(let [cmd-line-args (get task :args)]
(parse-opts (seq (map str (concat cmd-line-args command-line-args)))))
:shell
(let [args (get task :args)
args (into (vec args) command-line-args)]
{:exec (fn []
[nil
(-> (p/process args {:inherit true})
p/check
:exit)])})
(error (str "No such task: " (:task/type task)) 1)))
(def should-load-inits? (def should-load-inits?
"if true, then we should still load preloads and user.clj" "if true, then we should still load preloads and user.clj"
(volatile! true)) (volatile! true))
@ -739,8 +750,12 @@ Use -- to separate script command line args from bb command line args.
[& args] [& args]
(handle-pipe!) (handle-pipe!)
(handle-sigint!) (handle-sigint!)
(when-let [bb-edn @*bb-edn*] (let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
(deps/add-deps bb-edn)) "bb.edn")]
(when (fs/exists? bb-edn-file)
(let [edn (edn/read-string (slurp bb-edn-file))]
(reset! bb-edn edn)
(deps/add-deps edn))))
(if-let [dev-opts (System/getenv "BABASHKA_DEV")] (if-let [dev-opts (System/getenv "BABASHKA_DEV")]
(let [{:keys [:n]} (if (= "true" dev-opts) {:n 1} (let [{:keys [:n]} (if (= "true" dev-opts) {:n 1}
(edn/read-string dev-opts)) (edn/read-string dev-opts))

View file

@ -1,22 +1,26 @@
(ns babashka.bb-edn-test (ns babashka.bb-edn-test
{:clj-kondo/config '{:linters {:unresolved-symbol {:exclude [working?]}}}} {:clj-kondo/config '{:linters {:unresolved-symbol {:exclude [working?]}}}}
(:require (:require
[babashka.fs :as fs]
[babashka.test-utils :as test-utils] [babashka.test-utils :as test-utils]
[clojure.edn :as edn] [clojure.edn :as edn]
[clojure.java.io :as io] [clojure.test :as test :refer [deftest is]]))
[clojure.java.shell :refer [sh]]
[clojure.string :as str]
[clojure.test :as test :refer [deftest is testing *report-counters*]]
[flatland.ordered.map :refer [ordered-map]]
[sci.core :as sci])
)
(defn bb [input & args] (defn bb [& args]
(edn/read-string (edn/read-string
{:readers *data-readers* {:readers *data-readers*
:eof nil} :eof nil}
(apply test-utils/bb (when (some? input) (str input)) (map str args)))) (apply test-utils/bb nil (map str args))))
(deftest foobar-test (deftest foobar-test
(prn :foobar)) (let [temp-dir (fs/create-temp-dir)
temp-file (fs/create-file (fs/path temp-dir "temp-file.txt"))
bb-edn-file (fs/file temp-dir "bb.edn")
bb-edn `{:tasks {:clean {:task/type :shell
:args ["rm" ~(str temp-file)]}}}]
(spit bb-edn-file bb-edn)
(is (fs/exists? temp-file))
(binding [test-utils/*bb-edn-path* (str bb-edn-file)]
(bb :clean))
(is (not (fs/exists? temp-file)))))

View file

@ -3,14 +3,19 @@
[babashka.impl.classpath :as cp] [babashka.impl.classpath :as cp]
[babashka.main :as main] [babashka.main :as main]
[babashka.process :as p] [babashka.process :as p]
[clojure.edn :as edn]
[sci.core :as sci] [sci.core :as sci]
[sci.impl.vars :as vars])) [sci.impl.vars :as vars]))
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
(def ^:dynamic *bb-edn-path* nil)
(defn bb-jvm [input-or-opts & args] (defn bb-jvm [input-or-opts & args]
(reset! cp/cp-state nil) (reset! cp/cp-state nil)
(reset! main/env {}) (reset! main/env {})
(when-let [path *bb-edn-path*]
(reset! main/bb-edn (edn/read-string (slurp path))))
(let [os (java.io.StringWriter.) (let [os (java.io.StringWriter.)
es (if-let [err (:err input-or-opts)] es (if-let [err (:err input-or-opts)]
err (java.io.StringWriter.)) err (java.io.StringWriter.))
@ -44,9 +49,13 @@
(defn bb-native [input & args] (defn bb-native [input & args]
(let [res (p/process (into ["./bb"] args) (let [res (p/process (into ["./bb"] args)
{:in input (cond-> {:in input
:out :string :out :string
:err :string}) :err :string}
*bb-edn-path*
(assoc
:env (assoc (into {} (System/getenv))
"BABASHKA_EDN" *bb-edn-path*))))
res (deref res) res (deref res)
exit (:exit res) exit (:exit res)
error? (pos? exit)] error? (pos? exit)]