Implement BABASHKA_PRELOADS (#24)
This commit is contained in:
parent
31b106e285
commit
257ef9efb1
4 changed files with 58 additions and 41 deletions
40
README.md
40
README.md
|
|
@ -202,6 +202,32 @@ Fetching url: https://www.clojure.org
|
||||||
Writing file: /tmp/clojure.org.html
|
Writing file: /tmp/clojure.org.html
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Preloads
|
||||||
|
|
||||||
|
The environment variable `BABASHKA_PRELOADS` allows to define code that will be
|
||||||
|
available in all subsequent usages of babashka.
|
||||||
|
|
||||||
|
``` shellsession
|
||||||
|
BABASHKA_PRELOADS='(defn foo [x] (+ x 2))'
|
||||||
|
BABASHKA_PRELOADS=$BABASHKA_PRELOADS' (defn bar [x] (* x 2))'
|
||||||
|
export BABASHKA_PRELOADS
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that you can concatenate multiple expressions. Now you can use these functions in babashka:
|
||||||
|
|
||||||
|
``` shellsession
|
||||||
|
$ bb '(-> (foo *in*) bar)' <<< 1
|
||||||
|
6
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also preload an entire file using `load-file`:
|
||||||
|
|
||||||
|
``` shellsession
|
||||||
|
export BABASHKA_PRELOADS='(load-file "my_awesome_prelude.clj")'
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that `*in*` is not available in preloads.
|
||||||
|
|
||||||
## Enabling SSL
|
## Enabling SSL
|
||||||
|
|
||||||
If you want to be able to use SSL to e.g. run `(slurp
|
If you want to be able to use SSL to e.g. run `(slurp
|
||||||
|
|
@ -214,18 +240,8 @@ have it on your machine. It is usually located in `<JAVA_HOME>/jre/lib` or
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
``` shellsession
|
``` shellsession
|
||||||
$ cat /tmp/https_get.clj
|
$ export BABASHKA_PRELOADS="(System/setProperty \"java.library.path\" \"$JAVA_HOME/jre/lib\")"
|
||||||
#!/usr/bin/env bb -f
|
$ bb '(slurp "https://www.clojure.org")' | bb '(subs *in* 0 50)'
|
||||||
|
|
||||||
(System/setProperty
|
|
||||||
"java.library.path"
|
|
||||||
"/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib")
|
|
||||||
|
|
||||||
(slurp (first *command-line-args*))
|
|
||||||
```
|
|
||||||
|
|
||||||
``` shellsession
|
|
||||||
$ /tmp/https_get.clj https://www.google.com | bb '(subs *in* 0 50)'
|
|
||||||
"<!doctype html><html itemscope=\"\" itemtype=\"http:/"
|
"<!doctype html><html itemscope=\"\" itemtype=\"http:/"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -eo pipefail
|
set -eo pipefail
|
||||||
|
export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'
|
||||||
|
|
||||||
if [ "$BABASHKA_TEST_ENV" = "native" ]; then
|
if [ "$BABASHKA_TEST_ENV" = "native" ]; then
|
||||||
lein test
|
lein test
|
||||||
|
|
|
||||||
|
|
@ -133,14 +133,14 @@
|
||||||
:eof ::EOF} *in*))
|
:eof ::EOF} *in*))
|
||||||
|
|
||||||
#_(defn set-ssl []
|
#_(defn set-ssl []
|
||||||
(let [home (System/getProperty "user.home")
|
(let [home (System/getProperty "user.home")
|
||||||
bb-lib-dir (io/file home ".babashka" "lib")
|
bb-lib-dir (io/file home ".babashka" "lib")
|
||||||
lib-path (System/getProperty "java.library.path")
|
lib-path (System/getProperty "java.library.path")
|
||||||
ca-certs-dir (io/file bb-lib-dir "security")
|
ca-certs-dir (io/file bb-lib-dir "security")
|
||||||
ca-certs (.getPath (io/file ca-certs-dir "cacerts"))]
|
ca-certs (.getPath (io/file ca-certs-dir "cacerts"))]
|
||||||
(System/setProperty "java.library.path" (str (.getPath bb-lib-dir) ":" lib-path))
|
(System/setProperty "java.library.path" (str (.getPath bb-lib-dir) ":" lib-path))
|
||||||
(System/setProperty "javax.net.ssl.trustStore" ca-certs)
|
(System/setProperty "javax.net.ssl.trustStore" ca-certs)
|
||||||
(System/setProperty "javax.net.ssl.tru stAnchors" ca-certs)))
|
(System/setProperty "javax.net.ssl.tru stAnchors" ca-certs)))
|
||||||
|
|
||||||
(defn load-file* [ctx file]
|
(defn load-file* [ctx file]
|
||||||
(let [s (slurp file)
|
(let [s (slurp file)
|
||||||
|
|
@ -156,7 +156,18 @@
|
||||||
:help? :file :command-line-args
|
:help? :file :command-line-args
|
||||||
:expression :stream? :time?] :as _opts}
|
:expression :stream? :time?] :as _opts}
|
||||||
(parse-opts args)
|
(parse-opts args)
|
||||||
|
read-next #(if stream?
|
||||||
|
(if raw-in (or (read-line) ::EOF)
|
||||||
|
(read-edn))
|
||||||
|
(delay (let [in (slurp *in*)]
|
||||||
|
(if raw-in
|
||||||
|
(parse-shell-string in)
|
||||||
|
(edn/read-string in)))))
|
||||||
env (atom {})
|
env (atom {})
|
||||||
|
ctx {:bindings (assoc bindings '*command-line-args* command-line-args)
|
||||||
|
:env env}
|
||||||
|
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))
|
||||||
|
_preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (wrap-do) (sci/eval-string ctx))
|
||||||
exit-code
|
exit-code
|
||||||
(or
|
(or
|
||||||
#_(binding [*out* *err*]
|
#_(binding [*out* *err*]
|
||||||
|
|
@ -168,31 +179,15 @@
|
||||||
[(print-help) 0]
|
[(print-help) 0]
|
||||||
:else
|
:else
|
||||||
(try
|
(try
|
||||||
|
(let [expr (if file (read-file file) (wrap-do expression))]
|
||||||
(let [expr (if file (read-file file) (wrap-do expression))
|
|
||||||
read-next #(if stream?
|
|
||||||
(if raw-in (or (read-line) ::EOF)
|
|
||||||
(read-edn))
|
|
||||||
(delay (let [in (slurp *in*)]
|
|
||||||
(if raw-in
|
|
||||||
(parse-shell-string in)
|
|
||||||
(edn/read-string in)))))]
|
|
||||||
(loop [in (read-next)]
|
(loop [in (read-next)]
|
||||||
(let [ctx {:bindings (assoc bindings
|
(let [ctx (update ctx :bindings assoc (with-meta '*in*
|
||||||
(with-meta '*in*
|
(when-not stream? {:sci/deref! true})) in)]
|
||||||
(when-not stream? {:sci/deref! true})) in
|
|
||||||
#_(with-meta 'bb/*in*
|
|
||||||
{:sci/deref! true}) #_do-in
|
|
||||||
'*command-line-args* command-line-args)
|
|
||||||
:env env}
|
|
||||||
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))]
|
|
||||||
(if (identical? ::EOF in)
|
(if (identical? ::EOF in)
|
||||||
[nil 0] ;; done streaming
|
[nil 0] ;; done streaming
|
||||||
(let [res [(do (when-not (or expression file)
|
(let [res [(do (when-not (or expression file)
|
||||||
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
|
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
|
||||||
(let [res (sci/eval-string
|
(let [res (sci/eval-string expr ctx)]
|
||||||
expr
|
|
||||||
ctx)]
|
|
||||||
(if raw-out
|
(if raw-out
|
||||||
(if (coll? res)
|
(if (coll? res)
|
||||||
(doseq [l res]
|
(doseq [l res]
|
||||||
|
|
@ -208,7 +203,7 @@
|
||||||
exit-code (:bb/exit-code d)]
|
exit-code (:bb/exit-code d)]
|
||||||
(if exit-code [nil exit-code]
|
(if exit-code [nil exit-code]
|
||||||
(do (when-let [msg (or (:stderr d )
|
(do (when-let [msg (or (:stderr d )
|
||||||
(.getMessage e))]
|
(.getMessage e))]
|
||||||
(println (str/trim msg)))
|
(println (str/trim msg)))
|
||||||
[nil 1]))))))))
|
[nil 1]))))))))
|
||||||
1)
|
1)
|
||||||
|
|
|
||||||
|
|
@ -105,3 +105,8 @@
|
||||||
(spit tmp "(defn foo [x y] (+ x y)) (defn bar [x y] (* x y))")
|
(spit tmp "(defn foo [x y] (+ x y)) (defn bar [x y] (* x y))")
|
||||||
(is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)"
|
(is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)"
|
||||||
(.getPath tmp)))))))
|
(.getPath tmp)))))))
|
||||||
|
|
||||||
|
(deftest preloads-test
|
||||||
|
;; THIS TEST REQUIRES:
|
||||||
|
;; export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'
|
||||||
|
(is (= "foobar" (bb nil "(str (__bb__foo) (__bb__bar))"))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue