diff --git a/README.md b/README.md index 255f8c27..cdec266e 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,18 @@ $ bb script.clj -h {: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 The environment variable `BABASHKA_PRELOADS` allows to define code that will be diff --git a/project.clj b/project.clj index 881f2e13..610be98b 100644 --- a/project.clj +++ b/project.clj @@ -12,7 +12,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [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/tools.cli "0.4.2"]] :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]} diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index fe4031b0..818944f5 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.22-SNAPSHOT +0.0.22 diff --git a/sci b/sci index dcfea77c..f7487133 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit dcfea77c3172c78a6cb053ff51a6faae154b34dc +Subproject commit f7487133fb6ba44be8f9cb38416550a2923b6217 diff --git a/src/babashka/impl/socket_repl.clj b/src/babashka/impl/socket_repl.clj index 59b1bf5c..b4fe4d82 100644 --- a/src/babashka/impl/socket_repl.clj +++ b/src/babashka/impl/socket_repl.clj @@ -24,7 +24,7 @@ (println)) :read (fn [_request-prompt request-exit] (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) (identical? :repl/exit v) (identical? :edamame.impl.parser/eof v)) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2c01c660..5581f83e 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -208,7 +208,8 @@ Everything after that is bound to *command-line-args*.")) 'me.raynes.conch.low-level conch-namespace 'clojure.core.async async-namespace} :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 %)) _preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim) (sci/eval-string ctx)) exit-code diff --git a/test/babashka/impl/socket_repl_test.clj b/test/babashka/impl/socket_repl_test.clj index 9e440094..108ac299 100644 --- a/test/babashka/impl/socket_repl_test.clj +++ b/test/babashka/impl/socket_repl_test.clj @@ -53,6 +53,12 @@ (socket-command '(defn bar [x y z] (bindings))) (is (str/includes? (socket-command '(bar 1 2 3)) "[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 (if tu/jvm? (stop-repl!) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index ebfcce11..98d5215f 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -222,3 +222,6 @@ (deftest try-catch-test (is (zero? (bb nil "(try (/ 1 0) (catch ArithmeticException _ 0))")))) + +(deftest reader-conditionals-test + (is (= :hello (bb nil "#?(:clj (in-ns 'foo)) (println :hello)"))))