wip [skip ci]
This commit is contained in:
parent
90443eed80
commit
44f80f2a28
4 changed files with 250 additions and 213 deletions
|
|
@ -28,7 +28,8 @@
|
||||||
resource-paths)))
|
resource-paths)))
|
||||||
|
|
||||||
(defn path-from-jar
|
(defn path-from-jar
|
||||||
[^java.io.File jar-file resource-paths {:keys [:url?]}]
|
[^java.io.File jar-file resource-paths opts]
|
||||||
|
(let [url? (:url? opts)]
|
||||||
(with-open [jar (JarFile. jar-file)]
|
(with-open [jar (JarFile. jar-file)]
|
||||||
(some (fn [path]
|
(some (fn [path]
|
||||||
(when-let [entry (.getEntry jar path)]
|
(when-let [entry (.getEntry jar path)]
|
||||||
|
|
@ -38,7 +39,7 @@
|
||||||
(str "file:" (.getAbsolutePath jar-file) "!/" path))
|
(str "file:" (.getAbsolutePath jar-file) "!/" path))
|
||||||
{:file path
|
{:file path
|
||||||
:source (slurp (.getInputStream jar entry))})))
|
:source (slurp (.getInputStream jar entry))})))
|
||||||
resource-paths)))
|
resource-paths))))
|
||||||
|
|
||||||
(deftype JarFileResolver [jar-file]
|
(deftype JarFileResolver [jar-file]
|
||||||
IResourceResolver
|
IResourceResolver
|
||||||
|
|
@ -57,8 +58,10 @@
|
||||||
(getResources [_ resource-paths opts]
|
(getResources [_ resource-paths opts]
|
||||||
(keep #(getResource % resource-paths opts) entries)))
|
(keep #(getResource % resource-paths opts) entries)))
|
||||||
|
|
||||||
|
(def path-sep (System/getProperty "path.separator"))
|
||||||
|
|
||||||
(defn loader [^String classpath]
|
(defn loader [^String classpath]
|
||||||
(let [parts (.split classpath (System/getProperty "path.separator"))
|
(let [parts (.split classpath path-sep)
|
||||||
entries (map part->entry parts)]
|
entries (map part->entry parts)]
|
||||||
(Loader. entries)))
|
(Loader. entries)))
|
||||||
|
|
||||||
|
|
@ -88,7 +91,7 @@
|
||||||
(fn [{:keys [:cp]}]
|
(fn [{:keys [:cp]}]
|
||||||
(let [new-cp
|
(let [new-cp
|
||||||
(if-not cp extra-classpath
|
(if-not cp extra-classpath
|
||||||
(str cp (System/getProperty "path.separator") extra-classpath))]
|
(str cp path-sep extra-classpath))]
|
||||||
{:loader (loader new-cp)
|
{:loader (loader new-cp)
|
||||||
:cp new-cp})))
|
:cp new-cp})))
|
||||||
nil)
|
nil)
|
||||||
|
|
@ -96,7 +99,7 @@
|
||||||
(defn split-classpath
|
(defn split-classpath
|
||||||
"Returns the classpath as a seq of strings, split by the platform
|
"Returns the classpath as a seq of strings, split by the platform
|
||||||
specific path separator."
|
specific path separator."
|
||||||
([^String cp] (vec (.split cp (System/getProperty "path.separator")))))
|
([^String cp] (vec (.split cp path-sep))))
|
||||||
|
|
||||||
(defn get-classpath
|
(defn get-classpath
|
||||||
"Returns the current classpath as set by --classpath, BABASHKA_CLASSPATH and add-classpath."
|
"Returns the current classpath as set by --classpath, BABASHKA_CLASSPATH and add-classpath."
|
||||||
|
|
|
||||||
|
|
@ -362,7 +362,7 @@ Use -- to separate script command line args from bb command line args.
|
||||||
(defn error [msg exit]
|
(defn error [msg exit]
|
||||||
(binding [*out* *err*]
|
(binding [*out* *err*]
|
||||||
(println msg)
|
(println msg)
|
||||||
{:exit-code exit}))
|
{:exec (fn [] exit)}))
|
||||||
|
|
||||||
(defn parse-opts [options]
|
(defn parse-opts [options]
|
||||||
(let [opts (loop [options options
|
(let [opts (loop [options options
|
||||||
|
|
@ -498,34 +498,37 @@ 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]}]
|
(def bb-edn
|
||||||
|
(delay
|
||||||
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
|
(let [bb-edn-file (or (System/getenv "BABASHKA_EDN")
|
||||||
"bb.edn")]
|
"bb.edn")]
|
||||||
(if (fs/exists? bb-edn-file)
|
(when (fs/exists? bb-edn-file)
|
||||||
(let [bb-edn (edn/read-string (slurp bb-edn-file))]
|
(edn/read-string (slurp bb-edn-file))))))
|
||||||
(if-let [task (get-in bb-edn [:tasks (keyword (subs task 1))])]
|
|
||||||
(let [cmd-line-args (get task :babashka/args)
|
(defn resolve-task [task {:keys [:command-line-args]}]
|
||||||
proc (get task :babashka/process)]
|
(if @bb-edn
|
||||||
;; this is for invoking babashka itself with command-line-args
|
(if-let [task (get-in @bb-edn [:tasks (keyword (subs task 1))])]
|
||||||
(cond cmd-line-args
|
(case (:task/type task)
|
||||||
(parse-opts (seq (map str (concat cmd-line-args command-line-args))))
|
:babashka
|
||||||
proc {:exec (fn []
|
(let [cmd-line-args (get task :args)]
|
||||||
(-> (:args proc) (p/process {:inherit true}) p/check)
|
(parse-opts (seq (map str (concat cmd-line-args command-line-args)))))
|
||||||
|
:shell
|
||||||
|
(let [args (get task :args)]
|
||||||
|
{:exec (fn []
|
||||||
|
(p/process args {:inherit true}) p/check
|
||||||
0)}))
|
0)}))
|
||||||
(error (str "No such task: " task) 1)))
|
(error (str "No such task: " task) 1))
|
||||||
(error (str "File does not exist: " task) 1))))
|
(error (str "File does not exist: " task) 1)))
|
||||||
|
|
||||||
|
(def should-load-inits?
|
||||||
|
"if true, then we should still load preloads and user.clj"
|
||||||
|
(volatile! true))
|
||||||
|
|
||||||
(defn exec [opts]
|
(defn exec [opts]
|
||||||
(binding [*unrestricted* true]
|
(binding [*unrestricted* true]
|
||||||
(sci/binding [reflection-var false
|
(sci/binding [reflection-var false
|
||||||
core/data-readers @core/data-readers
|
core/data-readers @core/data-readers
|
||||||
sci/ns @sci/ns]
|
sci/ns @sci/ns]
|
||||||
(if-let [f (:exec opts)]
|
|
||||||
(f)
|
|
||||||
(if (:clojure opts)
|
|
||||||
(if-let [proc (deps/clojure (:opts opts))]
|
|
||||||
(-> @proc :exit)
|
|
||||||
0)
|
|
||||||
(let [{version-opt :version
|
(let [{version-opt :version
|
||||||
:keys [:shell-in :edn-in :shell-out :edn-out
|
:keys [:shell-in :edn-in :shell-out :edn-out
|
||||||
:help? :file :command-line-args
|
:help? :file :command-line-args
|
||||||
|
|
@ -533,7 +536,8 @@ Use -- to separate script command line args from bb command line args.
|
||||||
:repl :socket-repl :nrepl
|
:repl :socket-repl :nrepl
|
||||||
:verbose? :classpath
|
:verbose? :classpath
|
||||||
:main :uberscript :describe?
|
:main :uberscript :describe?
|
||||||
:jar :uberjar]}
|
:jar :uberjar :clojure]
|
||||||
|
exec-fn :exec}
|
||||||
opts
|
opts
|
||||||
_ (when verbose? (vreset! common/verbose? true))
|
_ (when verbose? (vreset! common/verbose? true))
|
||||||
_ (do ;; set properties
|
_ (do ;; set properties
|
||||||
|
|
@ -613,7 +617,7 @@ Use -- to separate script command line args from bb command line args.
|
||||||
opts (addons/future opts)
|
opts (addons/future opts)
|
||||||
sci-ctx (sci/init opts)
|
sci-ctx (sci/init opts)
|
||||||
_ (vreset! common/ctx sci-ctx)
|
_ (vreset! common/ctx sci-ctx)
|
||||||
preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim))
|
preloads (when @should-load-inits? (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)))
|
||||||
[expressions exit-code]
|
[expressions exit-code]
|
||||||
(cond expressions [expressions nil]
|
(cond expressions [expressions nil]
|
||||||
main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)"
|
main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)"
|
||||||
|
|
@ -628,7 +632,8 @@ Use -- to separate script command line args from bb command line args.
|
||||||
exit-code
|
exit-code
|
||||||
;; handle preloads
|
;; handle preloads
|
||||||
(if exit-code exit-code
|
(if exit-code exit-code
|
||||||
(do (when preloads
|
(do (when @should-load-inits?
|
||||||
|
(when preloads
|
||||||
(sci/binding [sci/file "<preloads>"]
|
(sci/binding [sci/file "<preloads>"]
|
||||||
(try
|
(try
|
||||||
(sci/eval-string* sci-ctx preloads)
|
(sci/eval-string* sci-ctx preloads)
|
||||||
|
|
@ -637,6 +642,18 @@ Use -- to separate script command line args from bb command line args.
|
||||||
:verbose? verbose?
|
:verbose? verbose?
|
||||||
:preloads preloads
|
:preloads preloads
|
||||||
:loader (:loader @cp/cp-state)})))))
|
:loader (:loader @cp/cp-state)})))))
|
||||||
|
(when @cp/cp-state
|
||||||
|
(when-let [{:keys [:file :source]}
|
||||||
|
(cp/source-for-namespace (:loader @cp/cp-state) "user" nil)]
|
||||||
|
(sci/binding [sci/file file]
|
||||||
|
(try
|
||||||
|
(sci/eval-string* sci-ctx source)
|
||||||
|
(catch Throwable e
|
||||||
|
(error-handler e {:expression expression
|
||||||
|
:verbose? verbose?
|
||||||
|
:preloads preloads
|
||||||
|
:loader (:loader @cp/cp-state)}))))))
|
||||||
|
(vreset! should-load-inits? false))
|
||||||
nil))
|
nil))
|
||||||
;; socket REPL is start asynchronously. when no other args are
|
;; socket REPL is start asynchronously. when no other args are
|
||||||
;; provided, a normal REPL will be started as well, which causes the
|
;; provided, a normal REPL will be started as well, which causes the
|
||||||
|
|
@ -682,6 +699,10 @@ Use -- to separate script command line args from bb command line args.
|
||||||
:verbose? verbose?
|
:verbose? verbose?
|
||||||
:preloads preloads
|
:preloads preloads
|
||||||
:loader (:loader @cp/cp-state)}))))
|
:loader (:loader @cp/cp-state)}))))
|
||||||
|
exec-fn [nil (exec-fn)]
|
||||||
|
clojure (if-let [proc (deps/clojure (:opts opts))]
|
||||||
|
(-> @proc :exit)
|
||||||
|
0)
|
||||||
uberscript [nil 0]
|
uberscript [nil 0]
|
||||||
:else [(repl/start-repl! sci-ctx) 0]))
|
:else [(repl/start-repl! sci-ctx) 0]))
|
||||||
1)]
|
1)]
|
||||||
|
|
@ -699,7 +720,7 @@ Use -- to separate script command line args from bb command line args.
|
||||||
:classpath classpath
|
:classpath classpath
|
||||||
:main-class main
|
:main-class main
|
||||||
:verbose verbose?}))
|
:verbose verbose?}))
|
||||||
exit-code))))))
|
exit-code))))
|
||||||
|
|
||||||
(defn main [& args]
|
(defn main [& args]
|
||||||
(let [opts (parse-opts args)]
|
(let [opts (parse-opts args)]
|
||||||
|
|
@ -718,6 +739,9 @@ 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]
|
||||||
|
(when-let [paths (:paths bb-edn)]
|
||||||
|
(cp/add-classpath (str/join cp/path-sep paths))))
|
||||||
(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))
|
||||||
|
|
|
||||||
4
test-resources/bb-edn/user.clj
Normal file
4
test-resources/bb-edn/user.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
(ns user)
|
||||||
|
|
||||||
|
(defn bash [& args]
|
||||||
|
(prn :args args))
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
{:paths ["script"]
|
{:paths ["test-resources/bb-edn"]
|
||||||
:tasks {:bash {:bb/args [:invoke cool-script/bash]}
|
:tasks {:count-files {:task/type :shell
|
||||||
:eval-plus {:bb/args [-e (apply + (map (fn [i]
|
:args ["bash" "-c" "ls | wc -l"]}
|
||||||
|
:bash {:task/type :babashka
|
||||||
|
:args [:invoke user/bash]}
|
||||||
|
:eval-plus {:type :babashka
|
||||||
|
:args [-e (apply + (map (fn [i]
|
||||||
(Integer/parseInt i))
|
(Integer/parseInt i))
|
||||||
*command-line-args*))]}
|
*command-line-args*))]}
|
||||||
:tree {:bb/args [:clojure -Stree]}
|
:tree {:task/type :babashka
|
||||||
:all {:bb/args [:do :eval-plus 1 2 3
|
:args [:clojure -Stree]}
|
||||||
|
:all {:task/type :babashka
|
||||||
|
:args [:do :eval-plus 1 2 3
|
||||||
:__ :tree
|
:__ :tree
|
||||||
:__ :bash "ls | wc -l"]}}}
|
:__ :bash "ls | wc -l"]}}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue