diff --git a/README.md b/README.md index e07b2d70..43ae53a8 100644 --- a/README.md +++ b/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 `/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)' " (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) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 6ca53232..5402d5fd 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -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))"))))