add support for --init as a file to be loaded before evaluation actions (#1033)
This commit is contained in:
parent
b9396ac318
commit
bf3b6d2dca
5 changed files with 57 additions and 1 deletions
|
|
@ -132,6 +132,7 @@ Global opts:
|
||||||
-cp, --classpath Classpath to use. Overrides bb.edn classpath.
|
-cp, --classpath Classpath to use. Overrides bb.edn classpath.
|
||||||
--debug Print debug information and internal stacktrace in case of exception.
|
--debug Print debug information and internal stacktrace in case of exception.
|
||||||
--force Passes -Sforce to deps.clj, forcing recalculation of the classpath.
|
--force Passes -Sforce to deps.clj, forcing recalculation of the classpath.
|
||||||
|
--init <file> Load file after any preloads and prior to evaluation/subcommands.
|
||||||
|
|
||||||
Help:
|
Help:
|
||||||
|
|
||||||
|
|
@ -586,6 +587,10 @@ Use bb run --help to show this help output.
|
||||||
(let [options (next options)]
|
(let [options (next options)]
|
||||||
(recur (next options)
|
(recur (next options)
|
||||||
(assoc opts-map :main (first options))))
|
(assoc opts-map :main (first options))))
|
||||||
|
("--init")
|
||||||
|
(let [options (next options)]
|
||||||
|
(recur (next options)
|
||||||
|
(assoc opts-map :init (first options))))
|
||||||
("--run")
|
("--run")
|
||||||
(parse-run-opts opts-map (next options))
|
(parse-run-opts opts-map (next options))
|
||||||
("--tasks")
|
("--tasks")
|
||||||
|
|
@ -662,7 +667,7 @@ Use bb run --help to show this help output.
|
||||||
(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
|
||||||
:expressions :stream?
|
:expressions :stream? :init
|
||||||
:repl :socket-repl :nrepl
|
:repl :socket-repl :nrepl
|
||||||
:debug :classpath :force?
|
:debug :classpath :force?
|
||||||
:main :uberscript :describe?
|
:main :uberscript :describe?
|
||||||
|
|
@ -762,6 +767,7 @@ Use bb run --help to show this help output.
|
||||||
(error-handler e {:expression expressions
|
(error-handler e {:expression expressions
|
||||||
:debug debug
|
:debug debug
|
||||||
:preloads preloads
|
:preloads preloads
|
||||||
|
:init init
|
||||||
:loader (:loader @cp/cp-state)}))))
|
:loader (:loader @cp/cp-state)}))))
|
||||||
expression (str/join " " expressions) ;; this might mess with the locations...
|
expression (str/join " " expressions) ;; this might mess with the locations...
|
||||||
exit-code
|
exit-code
|
||||||
|
|
@ -775,8 +781,22 @@ Use bb run --help to show this help output.
|
||||||
(error-handler e {:expression expression
|
(error-handler e {:expression expression
|
||||||
:debug debug
|
:debug debug
|
||||||
:preloads preloads
|
:preloads preloads
|
||||||
|
:init init
|
||||||
:loader (:loader @cp/cp-state)})))))
|
:loader (:loader @cp/cp-state)})))))
|
||||||
nil))
|
nil))
|
||||||
|
exit-code
|
||||||
|
;; handle --init
|
||||||
|
(if exit-code exit-code
|
||||||
|
(do (when init
|
||||||
|
(try
|
||||||
|
(load-file* init)
|
||||||
|
(catch Throwable e
|
||||||
|
(error-handler e {:expression expression
|
||||||
|
:debug debug
|
||||||
|
:preloads preloads
|
||||||
|
:init init
|
||||||
|
:loader (:loader @cp/cp-state)}))))
|
||||||
|
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
|
||||||
;; process to wait until SIGINT
|
;; process to wait until SIGINT
|
||||||
|
|
|
||||||
4
test-resources/babashka/init_caller.clj
Normal file
4
test-resources/babashka/init_caller.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
(ns init-caller
|
||||||
|
(:require [init-test :as i]))
|
||||||
|
|
||||||
|
(i/do-a-thing)
|
||||||
6
test-resources/babashka/init_test.clj
Normal file
6
test-resources/babashka/init_test.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
(ns init-test)
|
||||||
|
|
||||||
|
(defn do-a-thing [] "foo")
|
||||||
|
|
||||||
|
(defn -main [& _]
|
||||||
|
"Hello from init!")
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
(ns call-init-main
|
||||||
|
(:require [init-test :as i]))
|
||||||
|
|
||||||
|
(defn foobar [] (str (i/do-a-thing) "bar"))
|
||||||
|
|
||||||
|
(defn -main [& _] (i/do-a-thing))
|
||||||
|
|
@ -189,6 +189,26 @@
|
||||||
(defn bar [x y] (* x y))
|
(defn bar [x y] (* x y))
|
||||||
(bar (foo 10 30) 3)))"))))
|
(bar (foo 10 30) 3)))"))))
|
||||||
|
|
||||||
|
(deftest init-test
|
||||||
|
(testing "init with a file"
|
||||||
|
(is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj"
|
||||||
|
"-f" "test-resources/babashka/init_caller.clj"))))
|
||||||
|
(testing "init with eval(s)"
|
||||||
|
(is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj"
|
||||||
|
"-e" "(init-test/do-a-thing)"))))
|
||||||
|
(testing "init with main from init'ed ns"
|
||||||
|
(is (= "Hello from init!" (bb nil "--init" "test-resources/babashka/init_test.clj"
|
||||||
|
"-m" "init-test"))))
|
||||||
|
(testing "init with main from another namespace"
|
||||||
|
(test-utils/with-config '{:paths ["test-resources/babashka/src_for_classpath_test"]}
|
||||||
|
(is (= "foo" (bb nil "--init" "test-resources/babashka/init_test.clj"
|
||||||
|
"-m" "call-init-main")))))
|
||||||
|
(testing "init with a qualified function passed to --main"
|
||||||
|
(test-utils/with-config '{:paths ["test-resources/babashka/src_for_classpath_test"]}
|
||||||
|
(is (= "foobar" (bb nil "--init" "test-resources/babashka/init_test.clj"
|
||||||
|
"-m" "call-init-main/foobar"))))))
|
||||||
|
|
||||||
|
|
||||||
(deftest preloads-test
|
(deftest preloads-test
|
||||||
;; THIS TEST REQUIRES:
|
;; THIS TEST REQUIRES:
|
||||||
;; export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'
|
;; export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue