[#83] support reader conditionals using :bb feature
This commit is contained in:
parent
57581d2406
commit
7ecf2bb6ec
8 changed files with 27 additions and 5 deletions
12
README.md
12
README.md
|
|
@ -299,6 +299,18 @@ $ bb script.clj -h
|
||||||
{:port 80, :help true}
|
{:port 80, :help true}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Reader conditionals
|
||||||
|
|
||||||
|
Babashka supports reader conditionals using the `:bb` feature:
|
||||||
|
|
||||||
|
``` clojure
|
||||||
|
cat example.clj
|
||||||
|
#?(:clj (in-ns 'foo) :bb (println "babashka doesn't support in-ns yet!"))
|
||||||
|
|
||||||
|
$ ./bb example.clj
|
||||||
|
babashka doesn't support in-ns yet!
|
||||||
|
```
|
||||||
|
|
||||||
## Preloads
|
## Preloads
|
||||||
|
|
||||||
The environment variable `BABASHKA_PRELOADS` allows to define code that will be
|
The environment variable `BABASHKA_PRELOADS` allows to define code that will be
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
:resource-paths ["resources" "sci/resources"]
|
:resource-paths ["resources" "sci/resources"]
|
||||||
:dependencies [[org.clojure/clojure "1.10.1"]
|
:dependencies [[org.clojure/clojure "1.10.1"]
|
||||||
[org.clojure/tools.reader "1.3.2"]
|
[org.clojure/tools.reader "1.3.2"]
|
||||||
[borkdude/edamame "0.0.7"]
|
[borkdude/edamame "0.0.8-alpha.2"]
|
||||||
[org.clojure/core.async "0.4.500"]
|
[org.clojure/core.async "0.4.500"]
|
||||||
[org.clojure/tools.cli "0.4.2"]]
|
[org.clojure/tools.cli "0.4.2"]]
|
||||||
:profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]}
|
:profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
0.0.22-SNAPSHOT
|
0.0.22
|
||||||
|
|
|
||||||
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit dcfea77c3172c78a6cb053ff51a6faae154b34dc
|
Subproject commit f7487133fb6ba44be8f9cb38416550a2923b6217
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
(println))
|
(println))
|
||||||
:read (fn [_request-prompt request-exit]
|
:read (fn [_request-prompt request-exit]
|
||||||
(if (r/peek-char in) ;; if this is nil, we reached EOF
|
(if (r/peek-char in) ;; if this is nil, we reached EOF
|
||||||
(let [v (parser/parse-next in)]
|
(let [v (parser/parse-next in #{:bb})]
|
||||||
(if (or (identical? :repl/quit v)
|
(if (or (identical? :repl/quit v)
|
||||||
(identical? :repl/exit v)
|
(identical? :repl/exit v)
|
||||||
(identical? :edamame.impl.parser/eof v))
|
(identical? :edamame.impl.parser/eof v))
|
||||||
|
|
|
||||||
|
|
@ -208,7 +208,8 @@ Everything after that is bound to *command-line-args*."))
|
||||||
'me.raynes.conch.low-level conch-namespace
|
'me.raynes.conch.low-level conch-namespace
|
||||||
'clojure.core.async async-namespace}
|
'clojure.core.async async-namespace}
|
||||||
:bindings (assoc bindings '*command-line-args* command-line-args)
|
:bindings (assoc bindings '*command-line-args* command-line-args)
|
||||||
:env env}
|
:env env
|
||||||
|
:features #{:bb}}
|
||||||
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))
|
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))
|
||||||
_preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx))
|
_preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx))
|
||||||
exit-code
|
exit-code
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,12 @@
|
||||||
(socket-command '(defn bar [x y z] (bindings)))
|
(socket-command '(defn bar [x y z] (bindings)))
|
||||||
(is (str/includes? (socket-command '(bar 1 2 3))
|
(is (str/includes? (socket-command '(bar 1 2 3))
|
||||||
"[x y z]")))
|
"[x y z]")))
|
||||||
|
(testing "reader conditionals"
|
||||||
|
(is (str/includes? (let [ret (sh "bash" "-c"
|
||||||
|
(format "echo \"%s\n:repl/exit\" | nc 127.0.0.1 1666"
|
||||||
|
"#?(:bb 1337 :clj 8888)"))]
|
||||||
|
(:out ret))
|
||||||
|
"1337")))
|
||||||
(finally
|
(finally
|
||||||
(if tu/jvm?
|
(if tu/jvm?
|
||||||
(stop-repl!)
|
(stop-repl!)
|
||||||
|
|
|
||||||
|
|
@ -222,3 +222,6 @@
|
||||||
|
|
||||||
(deftest try-catch-test
|
(deftest try-catch-test
|
||||||
(is (zero? (bb nil "(try (/ 1 0) (catch ArithmeticException _ 0))"))))
|
(is (zero? (bb nil "(try (/ 1 0) (catch ArithmeticException _ 0))"))))
|
||||||
|
|
||||||
|
(deftest reader-conditionals-test
|
||||||
|
(is (= :hello (bb nil "#?(:clj (in-ns 'foo)) (println :hello)"))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue