From afed99ad1b18d682eb68d3006d124026b44a7cb4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 17 Mar 2021 13:05:04 +0100 Subject: [PATCH] wip [skip ci] --- src/babashka/main.clj | 28 ++++++++++++++++++++-------- test-resources/bb-edn/user.clj | 8 ++++++-- test-resources/bb.edn | 13 +++++++------ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 6aba1fb5..e65318f5 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -515,8 +515,15 @@ Use -- to separate script command line args from bb command line args. :shell (let [args (get task :args)] {:exec (fn [] - (p/process args {:inherit true}) p/check - 0)})) + (p/process args {:inherit true}) p/check)}) + :fn + (let [var (get task :var) + var-sym (symbol var)] + {:exec-src (pr-str `(do (if-let [f (requiring-resolve '~var-sym)] + (apply f *command-line-args*) + (throw (Exception. (str "Var not found: " '~var-sym + " " (babashka.classpath/get-classpath))))))) + :command-line-args command-line-args})) (error (str "No such task: " task) 1)) (error (str "File does not exist: " task) 1))) @@ -524,6 +531,8 @@ Use -- to separate script command line args from bb command line args. "if true, then we should still load preloads and user.clj" (volatile! true)) +(def env (atom {})) + (defn exec [opts] (binding [*unrestricted* true] (sci/binding [reflection-var false @@ -536,7 +545,8 @@ Use -- to separate script command line args from bb command line args. :repl :socket-repl :nrepl :verbose? :classpath :main :uberscript :describe? - :jar :uberjar :clojure] + :jar :uberjar :clojure + :exec-src] exec-fn :exec} opts _ (when verbose? (vreset! common/verbose? true)) @@ -557,7 +567,6 @@ Use -- to separate script command line args from bb command line args. :else (edn/read {:readers edn-readers} *in*)))))) uberscript-sources (atom ()) - env (atom {}) classpath (or classpath (System/getenv "BABASHKA_CLASSPATH")) _ (when classpath @@ -699,10 +708,13 @@ Use -- to separate script command line args from bb command line args. :verbose? verbose? :preloads preloads :loader (:loader @cp/cp-state)})))) - exec-fn [nil (exec-fn)] - clojure (if-let [proc (deps/clojure (:opts opts))] - (-> @proc :exit) - 0) + exec-fn [(exec-fn) 0] + exec-src [(sci/binding [sci/file (or @sci/file "")] + (sci/eval-string* sci-ctx exec-src)) + 0] + clojure [nil (if-let [proc (deps/clojure (:opts opts))] + (-> @proc :exit) + 0)] uberscript [nil 0] :else [(repl/start-repl! sci-ctx) 0])) 1)] diff --git a/test-resources/bb-edn/user.clj b/test-resources/bb-edn/user.clj index a591355a..0dbcb30a 100644 --- a/test-resources/bb-edn/user.clj +++ b/test-resources/bb-edn/user.clj @@ -1,4 +1,8 @@ -(ns user) +(ns user + (:require [babashka.process :as p] + [clojure.string :as str])) (defn bash [& args] - (prn :args args)) + (-> (p/process ["bash" "-c" (str/join " " args)] + {:inherit true}) + p/check)) diff --git a/test-resources/bb.edn b/test-resources/bb.edn index b8f76710..180fc15a 100644 --- a/test-resources/bb.edn +++ b/test-resources/bb.edn @@ -1,15 +1,16 @@ {:paths ["test-resources/bb-edn"] :tasks {:count-files {:task/type :shell :args ["bash" "-c" "ls | wc -l"]} - :bash {:task/type :babashka - :args [:invoke user/bash]} - :eval-plus {:type :babashka + :bash {:task/type :fn + :var user/bash} + :eval-plus {:task/type :babashka :args [-e (apply + (map (fn [i] (Integer/parseInt i)) *command-line-args*))]} :tree {:task/type :babashka :args [:clojure -Stree]} :all {:task/type :babashka - :args [:do :eval-plus 1 2 3 - :__ :tree - :__ :bash "ls | wc -l"]}}} + :args [:do :count-files + :__ :bash "ls | wc -l" + :__ :eval-plus 1 2 3 + :__ :tree]}}}