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
|
||||
```
|
||||
|
||||
## 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
|
||||
|
||||
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:
|
||||
|
||||
``` shellsession
|
||||
$ cat /tmp/https_get.clj
|
||||
#!/usr/bin/env bb -f
|
||||
|
||||
(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)'
|
||||
$ export BABASHKA_PRELOADS="(System/setProperty \"java.library.path\" \"$JAVA_HOME/jre/lib\")"
|
||||
$ bb '(slurp "https://www.clojure.org")' | bb '(subs *in* 0 50)'
|
||||
"<!doctype html><html itemscope=\"\" itemtype=\"http:/"
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eo pipefail
|
||||
export BABASHKA_PRELOADS='(defn __bb__foo [] "foo") (defn __bb__bar [] "bar")'
|
||||
|
||||
if [ "$BABASHKA_TEST_ENV" = "native" ]; then
|
||||
lein test
|
||||
|
|
|
|||
|
|
@ -133,14 +133,14 @@
|
|||
:eof ::EOF} *in*))
|
||||
|
||||
#_(defn set-ssl []
|
||||
(let [home (System/getProperty "user.home")
|
||||
bb-lib-dir (io/file home ".babashka" "lib")
|
||||
lib-path (System/getProperty "java.library.path")
|
||||
ca-certs-dir (io/file bb-lib-dir "security")
|
||||
ca-certs (.getPath (io/file ca-certs-dir "cacerts"))]
|
||||
(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.tru stAnchors" ca-certs)))
|
||||
(let [home (System/getProperty "user.home")
|
||||
bb-lib-dir (io/file home ".babashka" "lib")
|
||||
lib-path (System/getProperty "java.library.path")
|
||||
ca-certs-dir (io/file bb-lib-dir "security")
|
||||
ca-certs (.getPath (io/file ca-certs-dir "cacerts"))]
|
||||
(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.tru stAnchors" ca-certs)))
|
||||
|
||||
(defn load-file* [ctx file]
|
||||
(let [s (slurp file)
|
||||
|
|
@ -156,7 +156,18 @@
|
|||
:help? :file :command-line-args
|
||||
:expression :stream? :time?] :as _opts}
|
||||
(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 {})
|
||||
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
|
||||
(or
|
||||
#_(binding [*out* *err*]
|
||||
|
|
@ -168,31 +179,15 @@
|
|||
[(print-help) 0]
|
||||
:else
|
||||
(try
|
||||
|
||||
(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)))))]
|
||||
(let [expr (if file (read-file file) (wrap-do expression))]
|
||||
(loop [in (read-next)]
|
||||
(let [ctx {:bindings (assoc bindings
|
||||
(with-meta '*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 %))]
|
||||
(let [ctx (update ctx :bindings assoc (with-meta '*in*
|
||||
(when-not stream? {:sci/deref! true})) in)]
|
||||
(if (identical? ::EOF in)
|
||||
[nil 0] ;; done streaming
|
||||
(let [res [(do (when-not (or expression file)
|
||||
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
|
||||
(let [res (sci/eval-string
|
||||
expr
|
||||
ctx)]
|
||||
(let [res (sci/eval-string expr ctx)]
|
||||
(if raw-out
|
||||
(if (coll? res)
|
||||
(doseq [l res]
|
||||
|
|
@ -208,7 +203,7 @@
|
|||
exit-code (:bb/exit-code d)]
|
||||
(if exit-code [nil exit-code]
|
||||
(do (when-let [msg (or (:stderr d )
|
||||
(.getMessage e))]
|
||||
(.getMessage e))]
|
||||
(println (str/trim msg)))
|
||||
[nil 1]))))))))
|
||||
1)
|
||||
|
|
|
|||
|
|
@ -105,3 +105,8 @@
|
|||
(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)"
|
||||
(.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