From 1b3adaaf64c8499ccdbde3cc43787a21b3c132bb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 29 Jul 2022 12:23:28 +0200 Subject: [PATCH] -x --- deps.edn | 2 +- project.clj | 2 +- src/babashka/impl/cli.clj | 11 +++++++++++ src/babashka/main.clj | 23 ++++++++++++++++++----- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/deps.edn b/deps.edn index 2ac4a8d2..5a17ef32 100644 --- a/deps.edn +++ b/deps.edn @@ -49,7 +49,7 @@ org.clojure/data.priority-map {:mvn/version "1.1.0"} insn/insn {:mvn/version "0.5.2"} org.clojure/core.rrb-vector {:mvn/version "0.1.2"} - org.babashka/cli {:mvn/version "0.3.33"}} + org.babashka/cli {:mvn/version "0.3.34"}} :aliases {:babashka/dev {:main-opts ["-m" "babashka.main"]} :profile diff --git a/project.clj b/project.clj index e8163ea4..4f232155 100644 --- a/project.clj +++ b/project.clj @@ -32,7 +32,7 @@ [com.github.clj-easy/graal-build-time "0.1.0"] [rewrite-clj/rewrite-clj "1.0.699-alpha"] [insn/insn "0.5.2"] - [org.babashka/cli "0.3.33"]] + [org.babashka/cli "0.3.34"]] :plugins [[org.kipz/lein-meta-bom "0.1.1"]] :metabom {:jar-name "metabom.jar"} :profiles {:feature/xml {:source-paths ["feature-xml"] diff --git a/src/babashka/impl/cli.clj b/src/babashka/impl/cli.clj index d0f91284..85fe6667 100644 --- a/src/babashka/impl/cli.clj +++ b/src/babashka/impl/cli.clj @@ -7,3 +7,14 @@ (def cli-namespace (sci/copy-ns babashka.cli cns)) + +(defn exec-fn-snippet [ns var-name] + (format " +(require '%1$s) +(require '[babashka.cli]) +(def ns-meta (meta (find-ns '%1$s))) +(def var-meta (meta (resolve '%1$s/%2$s))) +(def cli-opts (babashka.cli/merge-opts (:org.babashka/cli ns-meta) (:org.babashka/cli var-meta))) +(def opts (babashka.cli/parse-opts *command-line-args* cli-opts)) +(%1$s/%2$s opts)" + ns var-name)) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index c6d23dc8..2ed4ff5f 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -645,6 +645,12 @@ Use bb run --help to show this help output. :command-line-args (if (= "--" (second options)) (nthrest options 2) (rest options)))) + ("--exec", "-x",) + (let [options (next options)] + (assoc opts-map :exec (first options) + :command-line-args (if (= "--" (second options)) + (nthrest options 2) + (rest options)))) ("--run") (parse-run-opts opts-map (next options)) ("--tasks") @@ -751,11 +757,13 @@ Use bb run --help to show this help output. :main :uberscript :describe? :jar :uberjar :clojure :doc :run :list-tasks - :print-deps :prepare]} + :print-deps :prepare] + exec-fn :exec} cli-opts _ (when debug (vreset! common/debug true)) _ (do ;; set properties (when main (System/setProperty "babashka.main" main)) + ;; TODO: what about exec here? (System/setProperty "babashka.version" version)) read-next (fn [*in*] (if (pipe-signal-received?) @@ -865,6 +873,11 @@ Use bb run --help to show this help output. "-main")] [[(format "(ns user (:require [%1$s])) (apply %1$s/%2$s *command-line-args*)" ns var-name)] nil]) + exec-fn + (let [sym (symbol exec-fn) + ns (namespace sym) + var-name (name sym)] + [[(cli/exec-fn-snippet ns var-name)] nil]) run (if (:run-help cli-opts) [(print-run-help) 0] (do @@ -1008,10 +1021,10 @@ Use bb run --help to show this help output. (defn load-edn [string] (try (edn/read-string string) - (catch java.lang.RuntimeException e - (if (re-find #"No dispatch macro for: \"" (.getMessage e)) - (throw (ex-info "Invalid regex literal found in EDN config, use re-pattern instead" {})) - (throw e))))) + (catch java.lang.RuntimeException e + (if (re-find #"No dispatch macro for: \"" (.getMessage e)) + (throw (ex-info "Invalid regex literal found in EDN config, use re-pattern instead" {})) + (throw e))))) (defn main [& args] (let [[args global-opts] (parse-global-opts args)