From a7940ad85c4427c2f3fa27f989c8c92c11a4dfef Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 11:11:12 +0100 Subject: [PATCH 001/152] Deprecate NATIVE_IMAGE variable --- script/compile | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/script/compile b/script/compile index ac925a5a..9c5fa7f6 100755 --- a/script/compile +++ b/script/compile @@ -2,19 +2,13 @@ set -eo pipefail -NATIVE_IMAGE=`which native-image` || true - -if [ -z "$NATIVE_IMAGE" ]; then - if [ -z "$GRAALVM_HOME" ]; then - echo "Please set GRAALVM_HOME" - exit 1 - fi - - "$GRAALVM_HOME/bin/gu" install native-image || true - - NATIVE_IMAGE="$GRAALVM_HOME/bin/native-image" +if [ -z "$GRAALVM_HOME" ]; then + echo "Please set GRAALVM_HOME" + exit 1 fi +"$GRAALVM_HOME/bin/gu" install native-image || true + BABASHKA_VERSION=$(cat resources/BABASHKA_VERSION) # # We also need to AOT sci, else something didn't work in the Mac build on CircleCI @@ -23,10 +17,12 @@ BABASHKA_VERSION=$(cat resources/BABASHKA_VERSION) # mkdir -p src/sci # cp -R /tmp/sci/src/* src +export JAVA_HOME=$GRAALVM_HOME + lein with-profiles +reflection do run lein do clean, uberjar -$NATIVE_IMAGE \ +$GRAALVM_HOME/bin/native-image \ -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ -H:Name=bb \ -H:+ReportExceptionStackTraces \ From 7a63c40a46a69b5b071697cc311d25c9de89f74b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 11:19:48 +0100 Subject: [PATCH 002/152] Add xmx flag --- script/compile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/script/compile b/script/compile index 9c5fa7f6..99969384 100755 --- a/script/compile +++ b/script/compile @@ -7,6 +7,10 @@ if [ -z "$GRAALVM_HOME" ]; then exit 1 fi +if [ -z "$BABASHKA_XMX"]; then + export BABASHKA_XMX="-J-Xmx3g" +fi + "$GRAALVM_HOME/bin/gu" install native-image || true BABASHKA_VERSION=$(cat resources/BABASHKA_VERSION) @@ -40,6 +44,6 @@ $GRAALVM_HOME/bin/native-image \ --verbose \ --no-fallback \ --no-server \ - "-J-Xmx3g" + "$BABASHKA_XMX" lein clean From c700f90ec1e29f68c0bcaf618c56ff83d381810d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 11:28:11 +0100 Subject: [PATCH 003/152] Bash typo --- script/compile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/compile b/script/compile index 99969384..fbb59dc7 100755 --- a/script/compile +++ b/script/compile @@ -7,7 +7,7 @@ if [ -z "$GRAALVM_HOME" ]; then exit 1 fi -if [ -z "$BABASHKA_XMX"]; then +if [ -z "$BABASHKA_XMX" ]; then export BABASHKA_XMX="-J-Xmx3g" fi From 7c5c91899006eafbd7e9cf9205420eaa9db2aad0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 14:19:09 +0100 Subject: [PATCH 004/152] v0.0.61 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index e2e3d510..96d26eda 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.61-SNAPSHOT \ No newline at end of file +0.0.61 \ No newline at end of file From c87e5f56c651e83b484893241e8ec33e2a117835 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 14:32:25 +0100 Subject: [PATCH 005/152] Version bump --- .circleci/script/publish_artifact.clj | 8 ++++++-- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.circleci/script/publish_artifact.clj b/.circleci/script/publish_artifact.clj index cb55f321..1b5f5b74 100755 --- a/.circleci/script/publish_artifact.clj +++ b/.circleci/script/publish_artifact.clj @@ -1,14 +1,18 @@ (require '[clojure.java.shell :refer [sh]] - '[cheshire.core :refer [generate-string]]) + '[clojure.java.io :as io] + '[cheshire.core :refer [generate-string]] + '[clojure.string :as str]) (def channel "#babashka_circleci_builds") #_(def channel "#_test") +(def babashka-version (str/trim (slurp (io/file "resources" "BABASHKA_VERSION")))) -(def text (format "[%s - %s@%s]: https://%s-201467090-gh.circle-artifacts.com/0/release/babashka-0.0.61-SNAPSHOT-%s-amd64.zip" +(def text (format "[%s - %s@%s]: https://%s-201467090-gh.circle-artifacts.com/0/release/babashka-%s-%s-amd64.zip" (System/getenv "BABASHKA_PLATFORM") (System/getenv "CIRCLE_BRANCH") (System/getenv "CIRCLE_SHA1") (System/getenv "CIRCLE_BUILD_NUM") + babashka-version (System/getenv "BABASHKA_PLATFORM"))) (def slack-hook-url (System/getenv "SLACK_HOOK_URL")) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 67ccd586..96d26eda 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.60 \ No newline at end of file +0.0.61 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 96d26eda..27aa0571 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.61 \ No newline at end of file +0.0.62-SNAPSHOT \ No newline at end of file From f1eb902f4113b84db22d4b498ecc9975ee65e640 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 9 Jan 2020 14:58:03 +0100 Subject: [PATCH 006/152] README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 3adb0722..137e75c6 100644 --- a/README.md +++ b/README.md @@ -594,6 +594,10 @@ Clojure / babashka. A minimal test framework compatible with babashka. +#### [spartan.spec](https://github.com/borkdude/spartan.spec/) + +An babashka-compatible implementation of `clojure.spec.alpha`. + #### [medley](https://github.com/borkdude/medley/) A fork of [medley](https://github.com/weavejester/medley) made compatible with From 68627a076f2f1946ae38e69446ff93dbffaa3caf Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 11:44:41 +0100 Subject: [PATCH 007/152] sci: upgrade edamame --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 57b584ba..09553860 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 57b584ba0a6a1f74a887d350463a700976dd37d8 +Subproject commit 09553860ae169e09820e583075c5c1ae6db26488 From b130d28c5a784fd272d16c39a7f1be7ddce525cf Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 11:45:27 +0100 Subject: [PATCH 008/152] Upgrade edamame --- deps.edn | 2 +- project.clj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deps.edn b/deps.edn index e3051185..ed5def07 100644 --- a/deps.edn +++ b/deps.edn @@ -1,7 +1,7 @@ {:paths ["src" "sci/src" "resources" "sci/resources"], :deps {org.clojure/clojure {:mvn/version "1.10.1"}, org.clojure/tools.reader {:mvn/version "1.3.2"}, - borkdude/edamame {:mvn/version "0.0.10-alpha.4"}, + borkdude/edamame {:mvn/version "0.0.10-alpha.5"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} org.clojure/core.async {:mvn/version "0.4.500"}, diff --git a/project.clj b/project.clj index 8da51448..4ffeed72 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/tools.reader "1.3.2"] - [borkdude/edamame "0.0.10-alpha.4"] + [borkdude/edamame "0.0.10-alpha.5"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] [org.clojure/core.async "0.4.500"] From be0c573a0d372d473086bc81eca13bbe4cc0f283 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 16:44:21 +0100 Subject: [PATCH 009/152] sci: pre- and postconditions --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 09553860..6f54500f 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 09553860ae169e09820e583075c5c1ae6db26488 +Subproject commit 6f54500fb27600b00ccb1fd47617eb9211c5afb7 From 9b3954aa882a93b560fc7fb83a09941bb9fdff86 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 18:33:13 +0100 Subject: [PATCH 010/152] sci: add alias --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 6f54500f..1041fc7e 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 6f54500fb27600b00ccb1fd47617eb9211c5afb7 +Subproject commit 1041fc7ebe57520323586f596f545baf691b1164 From 0446fd52ef3ba676d0763c43669e981919bdf6e5 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 19:08:17 +0100 Subject: [PATCH 011/152] sci: add some->> --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 1041fc7e..27431c1a 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 1041fc7ebe57520323586f596f545baf691b1164 +Subproject commit 27431c1a120d7ef7b7885b314d2fbc814970de0b From f3e51f9109a9354d3a36df229c1d7c3df0ca6411 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 11 Jan 2020 23:23:29 +0100 Subject: [PATCH 012/152] sci: add find-ns, the-ns, ns-aliases --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 27431c1a..36f4f7d9 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 27431c1a120d7ef7b7885b314d2fbc814970de0b +Subproject commit 36f4f7d93bb00c9a632eaad08f636bd1cb44bb2d From d815c35f716452c919468361eb9483a925d83288 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 00:05:55 +0100 Subject: [PATCH 013/152] Sci: fix *ns* --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 36f4f7d9..172749f8 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 36f4f7d93bb00c9a632eaad08f636bd1cb44bb2d +Subproject commit 172749f8117ad4c2c76332f9685a20707fc9daaf From 7b858630203bb623f8f2dd499fb8bf04325ae149 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 11:51:40 +0100 Subject: [PATCH 014/152] Bind current-ns to user --- sci | 2 +- src/babashka/main.clj | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sci b/sci index 172749f8..50a09c27 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 172749f8117ad4c2c76332f9685a20707fc9daaf +Subproject commit 50a09c277da48b58282489bcab41109a27091b43 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index ff8071f4..0d8c9e41 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -347,7 +347,8 @@ Everything after that is bound to *command-line-args*.")) nil)) exit-code (or exit-code - (sci/with-bindings {reflection-var false} + (sci/with-bindings {reflection-var false + vars/current-ns (vars/create-sci-ns ctx 'user)} (or #_(binding [*out* *err*] (prn ">>" _opts)) From 06cccf1a1470e7d982c73d4e2ce464a6a3d2a6da Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 13:33:56 +0100 Subject: [PATCH 015/152] Add ns-publics + clojure.repl/dir --- sci | 2 +- src/babashka/main.clj | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/sci b/sci index 50a09c27..358c428e 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 50a09c277da48b58282489bcab41109a27091b43 +Subproject commit 358c428ecf6049b061b43725e8a7c62c7a7050e5 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 0d8c9e41..2641735d 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -350,8 +350,6 @@ Everything after that is bound to *command-line-args*.")) (sci/with-bindings {reflection-var false vars/current-ns (vars/create-sci-ns ctx 'user)} (or - #_(binding [*out* *err*] - (prn ">>" _opts)) (second (cond version [(print-version) 0] From a3b3984f056ee1a72b1d70eb69c6785c47de29db Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 13:35:03 +0100 Subject: [PATCH 016/152] Upgrade sci --- src/babashka/main.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2641735d..b2c5faef 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -348,7 +348,7 @@ Everything after that is bound to *command-line-args*.")) exit-code (or exit-code (sci/with-bindings {reflection-var false - vars/current-ns (vars/create-sci-ns ctx 'user)} + vars/current-ns (vars/->SciNamespace 'user)} (or (second (cond version From 1268792b47577f56d508a5db1fb6cdadd83f4993 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 14:01:06 +0100 Subject: [PATCH 017/152] Refer 'clojure.repl/dir in user ns --- sci | 2 +- src/babashka/impl/repl.clj | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sci b/sci index 358c428e..381a158f 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 358c428ecf6049b061b43725e8a7c62c7a7050e5 +Subproject commit 381a158f72c1efbb8e48229bc67c7a109dffe7c0 diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index 3460aaeb..cf21ed85 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -29,7 +29,8 @@ "REPL.") (sio/println "Use :repl/quit or :repl/exit to quit the REPL.") (sio/println "Clojure rocks, Bash reaches.") - (sio/println))) + (sio/println) + (eval-form sci-ctx '(require '[clojure.repl :refer [dir]])))) :read (or read (fn [_request-prompt request-exit] ;; (prn "PEEK" @sci/in (r/peek-char @sci/in)) From 8e24744d351be5d869e6bffb204682c2f82b17cc Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 17:27:17 +0100 Subject: [PATCH 018/152] [#219] better error message for unrecognized cli option --- src/babashka/main.clj | 18 ++++++++++-------- test/babashka/main_test.clj | 26 +++++++------------------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index b2c5faef..8606a932 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -107,14 +107,16 @@ (if (some opts-map [:file :socket-repl :expression :main]) (assoc opts-map :command-line-args options) - (if (and (not= \( (first (str/trim opt))) - (.exists (io/file opt))) - (assoc opts-map - :file opt - :command-line-args (next options)) - (assoc opts-map - :expression opt - :command-line-args (next options)))))) + (let [opt (str/triml opt) + c (.charAt opt 0)] + (case c + (\( \{ \[ \* \@ \#) + (assoc opts-map + :expression opt + :command-line-args (next options)) + (assoc opts-map + :file opt + :command-line-args (next options))))))) opts-map))] opts)) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 0e90881b..f622fa72 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -13,24 +13,13 @@ (edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args)))) (deftest parse-opts-test - (is (= {:expression "(println 123)"} - (main/parse-opts ["-e" "(println 123)"]))) - - (is (= {:expression "(println 123)"} - (main/parse-opts ["--eval" "(println 123)"]))) - + (is (= 123 (bb nil "(println 123)"))) + (is (= 123 (bb nil "-e" "(println 123)"))) + (is (= 123 (bb nil "--eval" "(println 123)"))) (testing "distinguish automatically between expression or file name" - (is (= {:expression "(println 123)" - :command-line-args nil} - (main/parse-opts ["(println 123)"]))) - - (is (= {:file "src/babashka/main.clj" - :command-line-args nil} - (main/parse-opts ["src/babashka/main.clj"]))) - - (is (= {:expression "does-not-exist" - :command-line-args nil} - (main/parse-opts ["does-not-exist"]))))) + (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb"))) + (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) + (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))) (deftest main-test (testing "-io behaves as identity" @@ -250,8 +239,7 @@ (deftest Pattern-test (is (= ["1" "2" "3"] (bb nil "(vec (.split (java.util.regex.Pattern/compile \"f\") \"1f2f3\"))"))) - (is (= java.util.regex.Pattern/CANON_EQ - (bb nil "java.util.regex.Pattern/CANON_EQ")))) + (is (true? (bb nil "(some? java.util.regex.Pattern/CANON_EQ)")))) (deftest writer-test (let [tmp-file (java.io.File/createTempFile "bbb" "bbb") From 19767b54a31c8f895de4de11697fc7baba9eb170 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 17:40:41 +0100 Subject: [PATCH 019/152] [#221] print name of exception --- src/babashka/main.clj | 4 +++- test/babashka/main_test.clj | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 8606a932..f2f38d7f 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -247,7 +247,9 @@ Everything after that is bound to *command-line-args*.")) (if exit-code [nil exit-code] (do (if verbose? (print-stack-trace e) - (println (.getMessage e))) + (println (str (.. e getClass getName) + (when-let [m (.getMessage e)] + (str ": " m)) ))) (flush) [nil 1]))))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index f622fa72..90e65898 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -21,6 +21,10 @@ (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))) +(deftest print-error-test + (is (thrown-with-msg? Exception #"java.lang.NullPointerException" + (bb nil "(subs nil 0 0)")))) + (deftest main-test (testing "-io behaves as identity" (= "foo\nbar\n" (test-utils/bb "foo\nbar\n" "-io" "*input*"))) From b9336106268c324c9f1d10508ba137e456d3b3a3 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 18:15:34 +0100 Subject: [PATCH 020/152] Add experimental add-classpath! --- src/babashka/main.clj | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index f2f38d7f..91d1f561 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -253,6 +253,19 @@ Everything after that is bound to *command-line-args*.")) (flush) [nil 1]))))) +(def cp-state (atom {:loader nil + :cp nil})) + +(defn add-classpath! [add-to-cp] + (swap! cp-state + (fn [{:keys [:cp]}] + (let [new-cp + (if-not cp add-to-cp + (str cp (System/getProperty "path.separator") add-to-cp))] + {:loader (cp/loader new-cp) + :cp new-cp}))) + nil) + (defn main [& args] (handle-pipe!) @@ -282,10 +295,10 @@ Everything after that is bound to *command-line-args*.")) env (atom {}) classpath (or classpath (System/getenv "BABASHKA_CLASSPATH")) - loader (when classpath - (cp/loader classpath)) - load-fn (when classpath - (fn [{:keys [:namespace]}] + _ (when classpath + (add-classpath! classpath)) + load-fn (fn [{:keys [:namespace]}] + (when-let [{:keys [:loader]} @cp-state] (let [res (cp/source-for-namespace loader namespace nil)] (when uberscript (swap! uberscript-sources conj (:source res))) res))) @@ -297,9 +310,10 @@ Everything after that is bound to *command-line-args*.")) '*command-line-args* (sci/new-dynamic-var '*command-line-args* command-line-args) '*file* vars/file-var - '*warn-on-reflection* reflection-var)) + '*warn-on-reflection* reflection-var + 'add-classpath! add-classpath!)) (assoc-in ['clojure.java.io 'resource] - #(when classpath (cp/getResource loader % {:url? true})))) + #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) :bindings bindings :env env :features #{:bb} From 5a63d3748600948c572fe576e274bc4c6b45e74e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:02:48 +0100 Subject: [PATCH 021/152] babashka.classpath --- src/babashka/main.clj | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 91d1f561..0237df88 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -220,6 +220,19 @@ Everything after that is bound to *command-line-args*.")) csv clojure.data.csv json cheshire.core}) +(def cp-state (atom {:loader nil + :cp nil})) + +(defn add-classpath* [add-to-cp] + (swap! cp-state + (fn [{:keys [:cp]}] + (let [new-cp + (if-not cp add-to-cp + (str cp (System/getProperty "path.separator") add-to-cp))] + {:loader (cp/loader new-cp) + :cp new-cp}))) + nil) + (def namespaces {'clojure.tools.cli tools-cli-namespace 'clojure.edn {'read edn/read @@ -234,7 +247,8 @@ Everything after that is bound to *command-line-args*.")) 'cheshire.core cheshire-core-namespace 'clojure.stacktrace stacktrace-namespace 'clojure.main {'demunge demunge} - 'clojure.repl {'demunge demunge}}) + 'clojure.repl {'demunge demunge} + 'babashka.classpath {'add-classpath add-classpath*}}) (def bindings {'java.lang.System/exit exit ;; override exit, so we have more control @@ -253,19 +267,6 @@ Everything after that is bound to *command-line-args*.")) (flush) [nil 1]))))) -(def cp-state (atom {:loader nil - :cp nil})) - -(defn add-classpath! [add-to-cp] - (swap! cp-state - (fn [{:keys [:cp]}] - (let [new-cp - (if-not cp add-to-cp - (str cp (System/getProperty "path.separator") add-to-cp))] - {:loader (cp/loader new-cp) - :cp new-cp}))) - nil) - (defn main [& args] (handle-pipe!) @@ -296,7 +297,7 @@ Everything after that is bound to *command-line-args*.")) classpath (or classpath (System/getenv "BABASHKA_CLASSPATH")) _ (when classpath - (add-classpath! classpath)) + (add-classpath* classpath)) load-fn (fn [{:keys [:namespace]}] (when-let [{:keys [:loader]} @cp-state] (let [res (cp/source-for-namespace loader namespace nil)] @@ -310,8 +311,7 @@ Everything after that is bound to *command-line-args*.")) '*command-line-args* (sci/new-dynamic-var '*command-line-args* command-line-args) '*file* vars/file-var - '*warn-on-reflection* reflection-var - 'add-classpath! add-classpath!)) + '*warn-on-reflection* reflection-var)) (assoc-in ['clojure.java.io 'resource] #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) :bindings bindings From 8c41237f7af69b55f50d325cda2fe618f2aab9c6 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:18:37 +0100 Subject: [PATCH 022/152] sig -> signal --- README.md | 4 ++-- src/babashka/main.clj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 137e75c6..f65af552 100644 --- a/README.md +++ b/README.md @@ -263,10 +263,10 @@ Waits for TCP connection to be available on host and port. Options map supports Waits for file path to be available. Options map supports `:default`, `:timeout` and `:pause`. If `:timeout` is provided and reached, `:default`'s value (if any) is returned. The `:pause` option determines the time waited between retries. -- `sig/pipe-signal-received?`. Usage: +- `signal/pipe-signal-received?`. Usage: ``` clojure -(sig/pipe-signal-received?) +(signal/pipe-signal-received?) ``` Returns true if `PIPE` signal was received. Example: diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 0237df88..1761a550 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -213,7 +213,7 @@ Everything after that is bound to *command-line-args*.")) '{tools.cli 'clojure.tools.cli edn clojure.edn wait babashka.wait - sig babashka.signal + signal babashka.signal shell clojure.java.shell io clojure.java.io async clojure.core.async From f066e32494aaf85e9aedb501146c062553a06a46 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:41:53 +0100 Subject: [PATCH 023/152] Doc --- README.md | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f65af552..c3f13d10 100644 --- a/README.md +++ b/README.md @@ -241,11 +241,27 @@ $ bb example.clj Command-line arguments can be retrieved using `*command-line-args*`. -### Additional functions +### Additional namespaces -Additionally, babashka adds the following functions: +#### babashka.classpath -- `wait/wait-for-port`. Usage: +Contains the function `add-classpath` which can be used to add to the classpath +dynamically: + +``` clojure +(def medley-dep '{:deps {medley {:git/url "https://github.com/borkdude/medley" + :sha "91adfb5da33f8d23f75f0894da1defe567a625c0"}}}) +(def cp (:out (sh "bb" "clojure" "-Spath" "-Sdeps" (str medley-dep)))) +(add-classpath cp) +(require '[medley.core :as m]) +(m/index-by :id [{:id 1} {:id 2}]) ;;=> {1 {:id 1}, 2 {:id 2}} +``` + +#### babashka.wait + +Contains the functions: `wait-for-port` and `wait-for-path`. + +Usage of `wait-for-port`: ``` clojure (wait/wait-for-port "localhost" 8080) @@ -254,16 +270,22 @@ Additionally, babashka adds the following functions: Waits for TCP connection to be available on host and port. Options map supports `:timeout` and `:pause`. If `:timeout` is provided and reached, `:default`'s value (if any) is returned. The `:pause` option determines the time waited between retries. -- `wait/wait-for-path`. Usage: +Usage of `wait-for-path`: ``` clojure (wait/wait-for-path "/tmp/wait-path-test") (wait/wait-for-path "/tmp/wait-path-test" {:timeout 1000 :pause 1000}) ``` -Waits for file path to be available. Options map supports `:default`, `:timeout` and `:pause`. If `:timeout` is provided and reached, `:default`'s value (if any) is returned. The `:pause` option determines the time waited between retries. +Waits for file path to be available. Options map supports `:default`, `:timeout` +and `:pause`. If `:timeout` is provided and reached, `:default`'s value (if any) +is returned. The `:pause` option determines the time waited between retries. -- `signal/pipe-signal-received?`. Usage: +Aliased as `wait` in the `user` namespace. + +#### babashka.signal + +Contains the function `signal/pipe-signal-received?`. Usage: ``` clojure (signal/pipe-signal-received?) @@ -277,6 +299,8 @@ $ bb '((fn [x] (println x) (when (not (sig/pipe-signal-received?)) (recur (inc x 2 ``` +Aliased as `signal` in the `user` namespace. + ## Running a file Scripts may be executed from a file using `-f` or `--file`: From 880ac267934b451770ecf485551aae8564b817af Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:46:00 +0100 Subject: [PATCH 024/152] Doc --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c3f13d10..1416846c 100644 --- a/README.md +++ b/README.md @@ -249,9 +249,11 @@ Contains the function `add-classpath` which can be used to add to the classpath dynamically: ``` clojure +(require '[babashka.classpath :refer [add-classpath]] + '[clojure.java.shell :refer [sh]]) (def medley-dep '{:deps {medley {:git/url "https://github.com/borkdude/medley" :sha "91adfb5da33f8d23f75f0894da1defe567a625c0"}}}) -(def cp (:out (sh "bb" "clojure" "-Spath" "-Sdeps" (str medley-dep)))) +(def cp (:out (sh "clojure" "-Spath" "-Sdeps" (str medley-dep)))) (add-classpath cp) (require '[medley.core :as m]) (m/index-by :id [{:id 1} {:id 2}]) ;;=> {1 {:id 1}, 2 {:id 2}} From 8e5b19c80bc56cc7014658b72137279fb4cb28c5 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:50:09 +0100 Subject: [PATCH 025/152] doc --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1416846c..db53a13e 100644 --- a/README.md +++ b/README.md @@ -283,7 +283,7 @@ Waits for file path to be available. Options map supports `:default`, `:timeout` and `:pause`. If `:timeout` is provided and reached, `:default`'s value (if any) is returned. The `:pause` option determines the time waited between retries. -Aliased as `wait` in the `user` namespace. +The namespace `babashka.wait` is aliased as `wait` in the `user` namespace. #### babashka.signal @@ -301,7 +301,7 @@ $ bb '((fn [x] (println x) (when (not (sig/pipe-signal-received?)) (recur (inc x 2 ``` -Aliased as `signal` in the `user` namespace. +The namespace `babashka.signal` is aliased as `signal` in the `user` namespace. ## Running a file From ad72e068f9d4a928ad69ae11fa627f618ac6e6d1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 20:50:18 +0100 Subject: [PATCH 026/152] v0.0.62 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 27aa0571..8effcb05 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.62-SNAPSHOT \ No newline at end of file +0.0.62 \ No newline at end of file From d23c9baa72422743fab27370588634c32ff5211b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 12 Jan 2020 21:05:58 +0100 Subject: [PATCH 027/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 96d26eda..8effcb05 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.61 \ No newline at end of file +0.0.62 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 8effcb05..4c804068 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.62 \ No newline at end of file +0.0.63-SNAPSHOT \ No newline at end of file From c71cfa637a2e6bd65153f7c9b3414d0504265c3f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 14 Jan 2020 10:00:40 +0100 Subject: [PATCH 028/152] Add appveyor.yml --- appveyor.yml | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 00000000..97aafe35 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,54 @@ +--- + +version: "v-{build}" + +image: Visual Studio 2015 + +clone_folder: C:\projects\babashka + +environment: + GRAALVM_HOME: C:\projects\babashka\graalvm\graalvm-ce-java8-19.3.0 + +cache: + - C:\ProgramData\chocolatey\lib -> project.clj, appveyor.yml + - '%USERPROFILE%\.m2 -> project.clj' + - 'graalvm -> appveyor.yml' + +clone_script: + - cmd: >- + git clone -q --branch=%APPVEYOR_REPO_BRANCH% https://github.com/%APPVEYOR_REPO_NAME%.git %APPVEYOR_BUILD_FOLDER% + && cd %APPVEYOR_BUILD_FOLDER% + && git checkout -qf %APPVEYOR_REPO_COMMIT% + && git submodule update --init --recursive + +build_script: +- cmd: >- + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein.bat', 'lein.bat')" + + call lein self-install + +# set CLJ_KONDO_TEST_ENV=jvm + +# call script/test.bat + +- cmd: >- + choco install windows-sdk-7.1 + + call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" + + powershell -Command "if (Test-Path('graalvm')) { return } else { (New-Object Net.WebClient).DownloadFile('https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.0/graalvm-ce-java8-windows-amd64-19.3.0.zip', 'graalvm.zip') }" + + powershell -Command "if (Test-Path('graalvm')) { return } else { Expand-Archive graalvm.zip graalvm }" + +# call script/compile.bat + +# - cmd: >- +# lein clean + +# set CLJ_KONDO_TEST_ENV=native + +# call script/test.bat + +# artifacts: +# - path: babashka-*-windows-amd64.zip +# name: babashka From bfa8b45e34ced3a21f79ca6d1646cfdc2e979ed6 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 14 Jan 2020 17:12:57 +0100 Subject: [PATCH 029/152] sci: performance improvement --- deps.edn | 8 +++++++- sci | 2 +- src/babashka/main.clj | 4 ++-- test/babashka/profile.clj | 12 ++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/babashka/profile.clj diff --git a/deps.edn b/deps.edn index ed5def07..94654b7b 100644 --- a/deps.edn +++ b/deps.edn @@ -9,4 +9,10 @@ org.clojure/data.csv {:mvn/version "0.1.4"}, cheshire {:mvn/version "5.9.0"}} :aliases {:main - {:main-opts ["-m" "babashka.main"]}}} + {:main-opts ["-m" "babashka.main"]} + :profile + {:extra-deps + {com.clojure-goes-fast/clj-async-profiler {:mvn/version "0.4.0"}} + :extra-paths ["test"] + :jvm-opts ["-Djdk.attach.allowAttachSelf"] + :main-opts ["-m" "babashka.profile"]}}} diff --git a/sci b/sci index 381a158f..6a63e421 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 381a158f72c1efbb8e48229bc67c7a109dffe7c0 +Subproject commit 6a63e4212567ad0304a88094eeb2970317d0bfd8 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 1761a550..f176f078 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -107,8 +107,8 @@ (if (some opts-map [:file :socket-repl :expression :main]) (assoc opts-map :command-line-args options) - (let [opt (str/triml opt) - c (.charAt opt 0)] + (let [trimmed-opt (str/triml opt) + c (.charAt trimmed-opt 0)] (case c (\( \{ \[ \* \@ \#) (assoc opts-map diff --git a/test/babashka/profile.clj b/test/babashka/profile.clj new file mode 100644 index 00000000..6f338e5f --- /dev/null +++ b/test/babashka/profile.clj @@ -0,0 +1,12 @@ +(ns babashka.profile + (:require [babashka.main :as main])) + +(comment) + +;; clojure -A:profile -e "(prn (loop [val 0 cnt 1000000] (if (pos? cnt) (recur (inc val) (dec cnt)) val)))" + +#_(require '[clj-async-profiler.core :as prof]) + +(defn -main [& options] + #_(prof/profile (apply main/main options)) + (shutdown-agents)) From 3fb6a484ac62cc46bbbf0488d869046e4b47fb11 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 14 Jan 2020 17:37:38 +0100 Subject: [PATCH 030/152] sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 6a63e421..63cb269e 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 6a63e4212567ad0304a88094eeb2970317d0bfd8 +Subproject commit 63cb269e3542b8c1c942fb68669f67a89865bed8 From aacc5c4811bca063d172871b1270e90eae7eca28 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 15 Jan 2020 11:57:50 +0100 Subject: [PATCH 031/152] sci: more performance optimizations --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 63cb269e..873645b1 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 63cb269e3542b8c1c942fb68669f67a89865bed8 +Subproject commit 873645b1af5fb6db0aa9092ce6df49ad039d6e13 From 73ec6da56ae4bf493a1cc9791a511a6023623952 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 15 Jan 2020 20:11:44 +0100 Subject: [PATCH 032/152] More optimizations --- project.clj | 3 ++- sci | 2 +- test/babashka/profile.clj | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 4ffeed72..00686d16 100644 --- a/project.clj +++ b/project.clj @@ -18,7 +18,8 @@ [org.clojure/tools.cli "0.4.2"] [org.clojure/data.csv "0.1.4"] [cheshire "5.9.0"]] - :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"]]} + :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"] + [com.clojure-goes-fast/clj-async-profiler "0.4.0"]]} :uberjar {:global-vars {*assert* false} :jvm-opts ["-Dclojure.compiler.direct-linking=true" "-Dclojure.spec.skip-macros=true"] diff --git a/sci b/sci index 873645b1..33bc0b0a 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 873645b1af5fb6db0aa9092ce6df49ad039d6e13 +Subproject commit 33bc0b0a14d3f9d95ad9ecf9f1b29e95527153a3 diff --git a/test/babashka/profile.clj b/test/babashka/profile.clj index 6f338e5f..2bb90052 100644 --- a/test/babashka/profile.clj +++ b/test/babashka/profile.clj @@ -5,8 +5,8 @@ ;; clojure -A:profile -e "(prn (loop [val 0 cnt 1000000] (if (pos? cnt) (recur (inc val) (dec cnt)) val)))" -#_(require '[clj-async-profiler.core :as prof]) +(require '[clj-async-profiler.core :as prof]) (defn -main [& options] - #_(prof/profile (apply main/main options)) + (prof/profile (apply main/main options)) (shutdown-agents)) From 6edb3b91f84769f7d291e7d05bab1e6a7c7ced44 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 15 Jan 2020 22:53:45 +0100 Subject: [PATCH 033/152] Performance improvement --- appveyor.yml | 17 ++++++++++++----- sci | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 97aafe35..29ad77d2 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -15,11 +15,18 @@ cache: - 'graalvm -> appveyor.yml' clone_script: - - cmd: >- - git clone -q --branch=%APPVEYOR_REPO_BRANCH% https://github.com/%APPVEYOR_REPO_NAME%.git %APPVEYOR_BUILD_FOLDER% - && cd %APPVEYOR_BUILD_FOLDER% - && git checkout -qf %APPVEYOR_REPO_COMMIT% - && git submodule update --init --recursive +- ps: >- + if(-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { + git clone -q --branch=$env:APPVEYOR_REPO_BRANCH https://github.com/$env:APPVEYOR_REPO_NAME.git $env:APPVEYOR_BUILD_FOLDER + cd $env:APPVEYOR_BUILD_FOLDER + git checkout -qf $env:APPVEYOR_REPO_COMMIT + } else { + git clone -q https://github.com/$env:APPVEYOR_REPO_NAME.git $env:APPVEYOR_BUILD_FOLDER + cd $env:APPVEYOR_BUILD_FOLDER + git fetch -q origin +refs/pull/$env:APPVEYOR_PULL_REQUEST_NUMBER/merge: + git checkout -qf FETCH_HEAD + } +- cmd: git submodule update --init --recursive build_script: - cmd: >- diff --git a/sci b/sci index 33bc0b0a..01625017 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 33bc0b0a14d3f9d95ad9ecf9f1b29e95527153a3 +Subproject commit 01625017d5734eab8716db98f07f2253883a197e From 0713038457488869b3ece333c888cea06426d635 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 10:28:17 +0100 Subject: [PATCH 034/152] [#239] Fix issue with clj-http-lite --- script/lib_tests/clj_http_lite_test | 12 +++++++++++- src/babashka/impl/classes.clj | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/script/lib_tests/clj_http_lite_test b/script/lib_tests/clj_http_lite_test index d9c0cf43..43821d03 100755 --- a/script/lib_tests/clj_http_lite_test +++ b/script/lib_tests/clj_http_lite_test @@ -13,5 +13,15 @@ export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {clj-http-lite {:git/url "htt (prn (:status (client/post \"https://postman-echo.com/post\"))) -(prn (:status (client/put \"https://postman-echo.com/put\"))) +(prn (:status (client/post \"https://postman-echo.com/post\" + {:body (json/generate-string {:a 1}) + :headers {\"X-Hasura-Role\" \"admin\"} + :content-type :json + :accept :json}))) + +(prn (:status (client/put \"https://postman-echo.com/put\" + {:body (json/generate-string {:a 1}) + :headers {\"X-Hasura-Role\" \"admin\"} + :content-type :json + :accept :json}))) " diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 415c2e41..0a06e01d 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -179,7 +179,10 @@ (cond (instance? java.nio.file.Path v) java.nio.file.Path (instance? java.lang.Process v) - java.lang.Process))))) + java.lang.Process + ;; added for issue #239 regarding clj-http-lite + (instance? java.io.ByteArrayOutputStream v) + java.io.ByteArrayOutputStream))))) (def class-map (gen-class-map)) From 79becbc727c818cf454b59c273f84dbf69bdacdc Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 10:29:14 +0100 Subject: [PATCH 035/152] v0.0.63 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 4c804068..1d8b740d 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.63-SNAPSHOT \ No newline at end of file +0.0.63 \ No newline at end of file From 72fe4d1e0186a6fb69a1e8c53685f15036dcb3ef Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 10:58:57 +0100 Subject: [PATCH 036/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 8effcb05..1d8b740d 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.62 \ No newline at end of file +0.0.63 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 1d8b740d..54682e9d 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.63 \ No newline at end of file +0.0.64-SNAPSHOT \ No newline at end of file From 7deb1b91f9cce6f468d6c43a259e3b65021e0a62 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 16:35:50 +0100 Subject: [PATCH 037/152] sci: various perf improvements --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 01625017..08ed4772 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 01625017d5734eab8716db98f07f2253883a197e +Subproject commit 08ed47722ccd65e60ca9f24991e94fe549ea0585 From a5e88b06203ad8b4d2c61df66f35f34538c90289 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 16:44:35 +0100 Subject: [PATCH 038/152] sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 08ed4772..cf360149 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 08ed47722ccd65e60ca9f24991e94fe549ea0585 +Subproject commit cf360149b18269cbc4fd963ad83454be2846fe55 From 4cd85917ab277f93ef276d2673ea382a2004f273 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 20:47:20 +0100 Subject: [PATCH 039/152] sci: perf --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index cf360149..383128e4 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit cf360149b18269cbc4fd963ad83454be2846fe55 +Subproject commit 383128e4f422465874b463e834b49bd1469983c4 From b96919646fe4363480272d3522c7fb2699bd5cde Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 21:21:05 +0100 Subject: [PATCH 040/152] sci: more unrolls --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 383128e4..4f228a59 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 383128e4f422465874b463e834b49bd1469983c4 +Subproject commit 4f228a5914f91512866b31a2b0bcf55b0b35a0fe From e745da4be423f4fffeee72550442a459a9ba6f48 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 16 Jan 2020 22:41:39 +0100 Subject: [PATCH 041/152] sci: minor --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 4f228a59..9f687d75 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 4f228a5914f91512866b31a2b0bcf55b0b35a0fe +Subproject commit 9f687d75c6b5be953d3e4f183df5cd3f2bffe258 From 751f76d06797b8ab85fa70a6c92020060e1c0104 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 17 Jan 2020 10:12:40 +0100 Subject: [PATCH 042/152] v0.0.64 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 54682e9d..36e52555 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.64-SNAPSHOT \ No newline at end of file +0.0.64 \ No newline at end of file From 8d0c03c8f61cd6bcc9c45eec6c127dbe38bf3a5f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 17 Jan 2020 11:00:48 +0100 Subject: [PATCH 043/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 1d8b740d..36e52555 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.63 \ No newline at end of file +0.0.64 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 36e52555..583087de 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.64 \ No newline at end of file +0.0.65-SNAPSHOT \ No newline at end of file From d9a75cf8cf2e34a5e756df2faeda20c5e60c36fc Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 17 Jan 2020 17:26:03 +0100 Subject: [PATCH 044/152] dev --- src/babashka/main.clj | 13 +++++++++++-- test/babashka/profile.clj | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index f176f078..d9c59fc2 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -419,8 +419,17 @@ Everything after that is bound to *command-line-args*.")) (defn -main [& args] - (let [exit-code (apply main args)] - (System/exit exit-code))) + (if-let [dev-opts (System/getenv "BABASHKA_DEV")] + (let [{:keys [:n]} (edn/read-string dev-opts) + last-iteration (dec n)] + (dotimes [i n] + (if (< i last-iteration) + (with-out-str (apply main args)) + (do (apply main args) + (binding [*out* *err*] + (println "ran" n "times")))))) + (let [exit-code (apply main args)] + (System/exit exit-code)))) ;;;; Scratch diff --git a/test/babashka/profile.clj b/test/babashka/profile.clj index 2bb90052..8f9f5e55 100644 --- a/test/babashka/profile.clj +++ b/test/babashka/profile.clj @@ -8,5 +8,6 @@ (require '[clj-async-profiler.core :as prof]) (defn -main [& options] - (prof/profile (apply main/main options)) + (prof/profile (dotimes [_ 100] + (apply main/main options))) (shutdown-agents)) From d7b93441586e66f85e344efbc686aa7ff5ded150 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 17 Jan 2020 19:42:21 +0100 Subject: [PATCH 045/152] Revert dotimes --- test/babashka/profile.clj | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/babashka/profile.clj b/test/babashka/profile.clj index 8f9f5e55..2bb90052 100644 --- a/test/babashka/profile.clj +++ b/test/babashka/profile.clj @@ -8,6 +8,5 @@ (require '[clj-async-profiler.core :as prof]) (defn -main [& options] - (prof/profile (dotimes [_ 100] - (apply main/main options))) + (prof/profile (apply main/main options)) (shutdown-agents)) From c3961c6dc50b12ea2780e0bf13dcc78cf29e6f36 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 17 Jan 2020 23:46:09 +0100 Subject: [PATCH 046/152] sci: reset bindings when requiring file --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 9f687d75..ccf2ab32 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 9f687d75c6b5be953d3e4f183df5cd3f2bffe258 +Subproject commit ccf2ab32afa251b38821591b5174c0a26e9ee3e8 From 37fa6dbf0c24ce5778206817969b90b25890e6cf Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 11:44:20 +0100 Subject: [PATCH 047/152] v0.0.65 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 583087de..b0fea7d7 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.65-SNAPSHOT \ No newline at end of file +0.0.65 \ No newline at end of file From e43a7028cf56c7937e2eebf13ebd037c4dc1385a Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 11:53:01 +0100 Subject: [PATCH 048/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 36e52555..b0fea7d7 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.64 \ No newline at end of file +0.0.65 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index b0fea7d7..10ccdfd9 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.65 \ No newline at end of file +0.0.66-SNAPSHOT \ No newline at end of file From ef7a75be5b53ff4d59452e69d9332ed8a0d3d8eb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 16:54:58 +0100 Subject: [PATCH 049/152] sci: add alter-meta! and reset-meta! --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index ccf2ab32..35ed8cce 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit ccf2ab32afa251b38821591b5174c0a26e9ee3e8 +Subproject commit 35ed8cced594f2733ca02d9b7c0b2e7a984adfb3 From d1aaa97df629f3fe3edd1bc499f31caf238a1473 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 18:25:38 +0100 Subject: [PATCH 050/152] sci: record file metadata on var --- sci | 2 +- src/babashka/main.clj | 5 ++--- test/babashka/file_var_test.clj | 5 +++-- test/babashka/scripts/file_var.bb | 2 ++ 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sci b/sci index 35ed8cce..c84dbaa1 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 35ed8cced594f2733ca02d9b7c0b2e7a984adfb3 +Subproject commit c84dbaa110cf01b56be38f58c5a7f14cc030f90d diff --git a/src/babashka/main.clj b/src/babashka/main.clj index d9c59fc2..d4d3e973 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -191,7 +191,7 @@ Everything after that is bound to *command-line-args*.")) (defn load-file* [sci-ctx f] (let [f (io/file f) s (slurp f)] - (sci/with-bindings {vars/file-var (.getCanonicalPath f)} + (sci/with-bindings {vars/current-file (.getCanonicalPath f)} (eval-string* sci-ctx s)))) (defn eval* [sci-ctx form] @@ -303,14 +303,13 @@ Everything after that is bound to *command-line-args*.")) (let [res (cp/source-for-namespace loader namespace nil)] (when uberscript (swap! uberscript-sources conj (:source res))) res))) - _ (when file (vars/bindRoot vars/file-var (.getCanonicalPath (io/file file)))) + _ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file)))) ctx {:aliases aliases :namespaces (-> namespaces (assoc 'clojure.core (assoc core-extras '*command-line-args* (sci/new-dynamic-var '*command-line-args* command-line-args) - '*file* vars/file-var '*warn-on-reflection* reflection-var)) (assoc-in ['clojure.java.io 'resource] #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) diff --git a/test/babashka/file_var_test.clj b/test/babashka/file_var_test.clj index 909373bb..0ab76cb9 100644 --- a/test/babashka/file_var_test.clj +++ b/test/babashka/file_var_test.clj @@ -8,10 +8,11 @@ (apply tu/bb (when (some? input) (str input)) (map str args))) (deftest file-var-test - (let [[f1 f2 f3] + (let [[f1 f2 f3 f4] (str/split (bb nil "--classpath" "test/babashka/scripts" "test/babashka/scripts/file_var.bb") #"\n")] (is (str/ends-with? f1 "file_var_classpath.bb")) (is (str/ends-with? f2 "loaded_by_file_var.bb")) - (is (str/ends-with? f3 "file_var.bb")))) + (is (str/ends-with? f3 "file_var.bb")) + (is (str/ends-with? f4 "file_var.bb")))) diff --git a/test/babashka/scripts/file_var.bb b/test/babashka/scripts/file_var.bb index a35bf126..d92a8976 100644 --- a/test/babashka/scripts/file_var.bb +++ b/test/babashka/scripts/file_var.bb @@ -4,3 +4,5 @@ (require '[file-var-classpath]) (load-file (io/file "test" "babashka" "scripts" "loaded_by_file_var.bb")) (println *file*) +(defn foo []) +(println (:file (meta #'foo))) From 4008546ad732ff95fec8b610b3991fcd6c98da92 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 19:49:20 +0100 Subject: [PATCH 051/152] sci: alter-meta and reset-meta are synchronous --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index c84dbaa1..c8148094 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit c84dbaa110cf01b56be38f58c5a7f14cc030f90d +Subproject commit c814809420b0349a8e6f9ac601a83b4d49f5b7dc From 45da56a964f36b6730edd819c4450b7a993eb8c0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 18 Jan 2020 20:41:46 +0100 Subject: [PATCH 052/152] sci: support reload --- sci | 2 +- src/babashka/main.clj | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sci b/sci index c8148094..79c998a8 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit c814809420b0349a8e6f9ac601a83b4d49f5b7dc +Subproject commit 79c998a87b1f02b97b490224fcf8c491567a88b3 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index d4d3e973..2fe238fe 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -220,8 +220,7 @@ Everything after that is bound to *command-line-args*.")) csv clojure.data.csv json cheshire.core}) -(def cp-state (atom {:loader nil - :cp nil})) +(def cp-state (atom nil)) (defn add-classpath* [add-to-cp] (swap! cp-state From e515018451be86df68b81eea85e7d1f63a8c77a1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 13:01:43 +0100 Subject: [PATCH 053/152] sci: rename :row and :col to :line and :column --- deps.edn | 2 +- project.clj | 2 +- sci | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deps.edn b/deps.edn index 94654b7b..3d02c4ac 100644 --- a/deps.edn +++ b/deps.edn @@ -1,7 +1,7 @@ {:paths ["src" "sci/src" "resources" "sci/resources"], :deps {org.clojure/clojure {:mvn/version "1.10.1"}, org.clojure/tools.reader {:mvn/version "1.3.2"}, - borkdude/edamame {:mvn/version "0.0.10-alpha.5"}, + borkdude/edamame {:mvn/version "0.0.10-alpha.6"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} org.clojure/core.async {:mvn/version "0.4.500"}, diff --git a/project.clj b/project.clj index 00686d16..86c8ebf2 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/tools.reader "1.3.2"] - [borkdude/edamame "0.0.10-alpha.5"] + [borkdude/edamame "0.0.10-alpha.6"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] [org.clojure/core.async "0.4.500"] diff --git a/sci b/sci index 79c998a8..1bb1e2f7 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 79c998a87b1f02b97b490224fcf8c491567a88b3 +Subproject commit 1bb1e2f783996d4db3219619f70d3d3f7c14c50b From 37913fd012b3154a76846a853a4bd89b8353a6ef Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 14:04:56 +0100 Subject: [PATCH 054/152] sci bump --- deps.edn | 2 +- project.clj | 2 +- sci | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deps.edn b/deps.edn index 3d02c4ac..dcc6a46d 100644 --- a/deps.edn +++ b/deps.edn @@ -1,7 +1,7 @@ {:paths ["src" "sci/src" "resources" "sci/resources"], :deps {org.clojure/clojure {:mvn/version "1.10.1"}, org.clojure/tools.reader {:mvn/version "1.3.2"}, - borkdude/edamame {:mvn/version "0.0.10-alpha.6"}, + borkdude/edamame {:mvn/version "0.0.10-alpha.7"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} org.clojure/core.async {:mvn/version "0.4.500"}, diff --git a/project.clj b/project.clj index 86c8ebf2..e3c0f95a 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/tools.reader "1.3.2"] - [borkdude/edamame "0.0.10-alpha.6"] + [borkdude/edamame "0.0.10-alpha.7"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] [org.clojure/core.async "0.4.500"] diff --git a/sci b/sci index 1bb1e2f7..51961ef2 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 1bb1e2f783996d4db3219619f70d3d3f7c14c50b +Subproject commit 51961ef24817bb2e33a75946a74c493eac9bcbcf From 073fbcac207195a3729dd2712e1e458530f7ae20 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 14:18:52 +0100 Subject: [PATCH 055/152] Add Throwable to default imports --- src/babashka/main.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2fe238fe..6114fe4b 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -330,7 +330,8 @@ Everything after that is bound to *command-line-args*.")) ProcessBuilder java.lang.ProcessBuilder String java.lang.String System java.lang.System - Thread java.lang.Thread} + Thread java.lang.Thread + Throwable java.lang.Throwable} :load-fn load-fn :dry-run uberscript} ctx (addons/future ctx) From 6c4a54745ad061e14f6dcf573f02838d8a6b05dd Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 15:28:50 +0100 Subject: [PATCH 056/152] Add time macro --- sci | 2 +- src/babashka/impl/clojure/core.clj | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/sci b/sci index 51961ef2..689a4d01 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 51961ef24817bb2e33a75946a74c493eac9bcbcf +Subproject commit 689a4d018f8f729ff79408b6a43aed631138b19a diff --git a/src/babashka/impl/clojure/core.clj b/src/babashka/impl/clojure/core.clj index d3a69ee4..bb7502af 100644 --- a/src/babashka/impl/clojure/core.clj +++ b/src/babashka/impl/clojure/core.clj @@ -6,6 +6,15 @@ (defn locking* [form bindings v f & args] (apply @#'locking/locking form bindings v f args)) +(defn time* + "Evaluates expr and prints the time it took. Returns the value of + expr." + [_ _ expr] + `(let [start# (. System (nanoTime)) + ret# ~expr] + (prn (str "Elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs")) + ret#)) + (def core-extras {'file-seq file-seq 'agent agent @@ -18,5 +27,6 @@ 'shutdown-agents shutdown-agents 'slurp slurp 'spit spit + 'time (with-meta time* {:sci/macro true}) 'Throwable->map Throwable->map 'compare-and-set! compare-and-set!}) From 3f066210f2fa1f38c8f32e71b81548b9d0d146b2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 17:53:24 +0100 Subject: [PATCH 057/152] sci: throw error when using special form not in call --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 689a4d01..641a1d94 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 689a4d018f8f729ff79408b6a43aed631138b19a +Subproject commit 641a1d943d8df4f7ffdf0f243dcda0ad54f7bad5 From e2cf871dfa936eb0ac078931e7b68e330b927e22 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 18:08:08 +0100 Subject: [PATCH 058/152] [#232] better error when requiring unknown namespace --- test/babashka/classpath_test.clj | 5 ++++- test/babashka/test_utils.clj | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/babashka/classpath_test.clj b/test/babashka/classpath_test.clj index dd20b197..eb592f5a 100644 --- a/test/babashka/classpath_test.clj +++ b/test/babashka/classpath_test.clj @@ -14,7 +14,10 @@ "(require '[my-script :as ms]) (ms/foo)"))) (is (= "hello from foo\n" (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test/foo.jar" - "(require '[foo :as f]) (f/foo)")))) + "(require '[foo :as f]) (f/foo)"))) + (is (thrown-with-msg? Exception #"not require" + (tu/bb nil + "(require '[foo :as f])")))) (deftest classpath-env-test ;; for this test you have to set `BABASHKA_CLASSPATH` to test-resources/babashka/src_for_classpath_test/env diff --git a/test/babashka/test_utils.clj b/test/babashka/test_utils.clj index 071e263c..b5bfd387 100644 --- a/test/babashka/test_utils.clj +++ b/test/babashka/test_utils.clj @@ -8,6 +8,7 @@ (set! *warn-on-reflection* true) (defn bb-jvm [input & args] + (reset! main/cp-state nil) (let [os (java.io.StringWriter.) es (java.io.StringWriter.) is (when input From 9269f1087fae0646bd09f7c799f890eef58c37a3 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 19 Jan 2020 20:01:49 +0100 Subject: [PATCH 059/152] [#243] ns form doesn't work with socket repl --- src/babashka/impl/clojure/core/server.clj | 6 ++++-- test/babashka/impl/socket_repl_test.clj | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/babashka/impl/clojure/core/server.clj b/src/babashka/impl/clojure/core/server.clj index 2b1c821f..dd58f737 100644 --- a/src/babashka/impl/clojure/core/server.clj +++ b/src/babashka/impl/clojure/core/server.clj @@ -14,7 +14,8 @@ :no-doc true} babashka.impl.clojure.core.server (:refer-clojure :exclude [locking]) - (:require [sci.core :as sci]) + (:require [sci.core :as sci] + [sci.impl.vars :as vars]) (:import [clojure.lang LineNumberingPushbackReader] [java.net InetAddress Socket ServerSocket SocketException] @@ -44,7 +45,8 @@ (try (sci/with-bindings {sci/in in sci/out out - sci/err err} + sci/err err + vars/current-ns (vars/->SciNamespace 'user)} (swap! server assoc-in [:sessions client-id] {}) (apply accept args)) (catch SocketException _disconnect) diff --git a/test/babashka/impl/socket_repl_test.clj b/test/babashka/impl/socket_repl_test.clj index 83038f0e..8bad7823 100644 --- a/test/babashka/impl/socket_repl_test.clj +++ b/test/babashka/impl/socket_repl_test.clj @@ -55,6 +55,8 @@ (is (socket-command "#?(:bb 1337 :clj 8888)" "1337"))) (testing "*1, *2, *3, *e" (is (socket-command "1\n*1" "1"))) + (testing "*ns*" + (is (socket-command "(ns foo.bar) (ns-name *ns*)" "foo.bar"))) (finally (if tu/jvm? (stop-repl!) From 8f698894f65885d9cac07a8e3132cdee8bb07304 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 20 Jan 2020 10:41:11 +0100 Subject: [PATCH 060/152] [#234] *ns* should be controllable set per socket REPL connection --- sci | 2 +- src/babashka/impl/repl.clj | 3 ++- test/babashka/impl/repl_test.clj | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sci b/sci index 641a1d94..3f448903 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 641a1d943d8df4f7ffdf0f243dcda0ad54f7bad5 +Subproject commit 3f448903550feaeb7d752c958f6422a5fde667f7 diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index cf21ed85..e83f56de 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -7,6 +7,7 @@ [clojure.tools.reader.reader-types :as r] [sci.impl.interpreter :refer [eval-form]] [sci.impl.parser :as parser] + [sci.impl.vars :as vars] [sci.core :as sci] [sci.impl.io :as sio])) @@ -58,7 +59,7 @@ expr)] ret))) :need-prompt (or need-prompt (fn [] true)) - :prompt (or prompt #(sio/printf "%s=> " (-> sci-ctx :env deref :current-ns))) + :prompt (or prompt #(sio/printf "%s=> " (vars/current-ns-name))) :flush (or flush sio/flush) :print (or print sio/prn) :caught (or caught repl-caught))))) diff --git a/test/babashka/impl/repl_test.clj b/test/babashka/impl/repl_test.clj index d0f1bc5b..3c9cb72f 100644 --- a/test/babashka/impl/repl_test.clj +++ b/test/babashka/impl/repl_test.clj @@ -14,9 +14,10 @@ (vars/bindRoot sci/err *err*) (defn repl! [] - (start-repl! (init {:bindings {'*command-line-args* - ["a" "b" "c"]} - :env (atom {})}))) + (sci/with-bindings {vars/current-ns (vars/->SciNamespace 'user)} + (start-repl! (init {:bindings {'*command-line-args* + ["a" "b" "c"]} + :env (atom {})})))) (defn assert-repl [expr expected] (is (str/includes? (sci/with-out-str From a442bde76a870234980fe97816e0eff5b48434a7 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 20 Jan 2020 11:00:53 +0100 Subject: [PATCH 061/152] [#213] Allow -e argument to be repeated --- src/babashka/main.clj | 23 ++++++++++++----------- test/babashka/main_test.clj | 3 ++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 6114fe4b..46d702ac 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -99,21 +99,21 @@ ("--eval", "-e") (let [options (next options)] (recur (next options) - (assoc opts-map :expression (first options)))) + (update opts-map :expressions (fnil conj []) (first options)))) ("--main", "-m") (let [options (next options)] (recur (next options) (assoc opts-map :main (first options)))) - (if (some opts-map [:file :socket-repl :expression :main]) + (if (some opts-map [:file :socket-repl :expressions :main]) (assoc opts-map :command-line-args options) (let [trimmed-opt (str/triml opt) c (.charAt trimmed-opt 0)] (case c (\( \{ \[ \* \@ \#) - (assoc opts-map - :expression opt - :command-line-args (next options)) + (-> opts-map + (update :expressions (fnil conj []) (first options)) + (assoc :command-line-args (next options))) (assoc opts-map :file opt :command-line-args (next options))))))) @@ -274,7 +274,7 @@ Everything after that is bound to *command-line-args*.")) (let [t0 (System/currentTimeMillis) {:keys [:version :shell-in :edn-in :shell-out :edn-out :help? :file :command-line-args - :expression :stream? :time? + :expressions :stream? :time? :repl :socket-repl :verbose? :classpath :main :uberscript] :as _opts} @@ -348,13 +348,14 @@ Everything after that is bound to *command-line-args*.")) (let [opts (apply hash-map opts)] (repl/start-repl! sci-ctx opts)))))) preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) - [expression exit-code] - (cond expression [expression nil] - main [(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" - main) nil] - file (try [(read-file file) nil] + [expressions exit-code] + (cond expressions [expressions nil] + main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" + main)] nil] + file (try [[(read-file file)] nil] (catch Exception e (error-handler* e verbose?)))) + expression (str/join " " expressions) ;; this might mess with the locations... exit-code ;; handle preloads (if exit-code exit-code diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 90e65898..5323610c 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -19,7 +19,8 @@ (testing "distinguish automatically between expression or file name" (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) - (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help"))))) + (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help")))) + (is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])")))) (deftest print-error-test (is (thrown-with-msg? Exception #"java.lang.NullPointerException" From 00df495697abbf5f0249e71d0a1f3de74616017b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 20 Jan 2020 11:07:58 +0100 Subject: [PATCH 062/152] v0.0.66 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 10ccdfd9..9990aece 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.66-SNAPSHOT \ No newline at end of file +0.0.66 \ No newline at end of file From 55238b52acc086bb08e91161a3bfdba32171e55f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 20 Jan 2020 11:19:19 +0100 Subject: [PATCH 063/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index b0fea7d7..9990aece 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.65 \ No newline at end of file +0.0.66 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 9990aece..69e0d6aa 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.66 \ No newline at end of file +0.0.67-SNAPSHOT \ No newline at end of file From e427060c0b98b031ffb3a214b9abce80dee8e358 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 21 Jan 2020 15:14:33 +0100 Subject: [PATCH 064/152] Add java.io.Reader and StringBuilder --- sci | 2 +- src/babashka/impl/classes.clj | 2 ++ src/babashka/main.clj | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/sci b/sci index 3f448903..74878d25 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 3f448903550feaeb7d752c958f6422a5fde667f7 +Subproject commit 74878d25594ca19f7ac743d10ebf6e206e6af09e diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 0a06e01d..cff89023 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -21,6 +21,7 @@ java.io.InputStream java.io.IOException java.io.OutputStream + java.io.Reader java.io.StringReader java.io.StringWriter java.lang.ArithmeticException @@ -34,6 +35,7 @@ java.util.concurrent.LinkedBlockingQueue java.lang.Object java.lang.String + java.lang.StringBuilder java.lang.System java.lang.Throwable java.lang.Process diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 46d702ac..3b9293c1 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -329,6 +329,7 @@ Everything after that is bound to *command-line-args*.")) Object java.lang.Object ProcessBuilder java.lang.ProcessBuilder String java.lang.String + StringBuilder java.lang.StringBuilder System java.lang.System Thread java.lang.Thread Throwable java.lang.Throwable} From 736555bdb65dd0e8abda649a6de3f7a122e3f802 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 21 Jan 2020 15:27:38 +0100 Subject: [PATCH 065/152] Add Writer, PrintWriter and PushbackReader --- src/babashka/impl/classes.clj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index cff89023..addcc391 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -21,9 +21,12 @@ java.io.InputStream java.io.IOException java.io.OutputStream + java.io.PrintWriter + java.io.PushbackReader java.io.Reader java.io.StringReader java.io.StringWriter + java.io.Writer java.lang.ArithmeticException java.lang.AssertionError java.lang.Boolean From aad41086f7af583b39bcedf2f4036e9a043f5bc5 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 21 Jan 2020 15:39:03 +0100 Subject: [PATCH 066/152] Minor --- src/babashka/impl/classes.clj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index addcc391..04499cf9 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -21,8 +21,6 @@ java.io.InputStream java.io.IOException java.io.OutputStream - java.io.PrintWriter - java.io.PushbackReader java.io.Reader java.io.StringReader java.io.StringWriter @@ -96,7 +94,10 @@ java.util.zip.GZIPOutputStream] :constructors [clojure.lang.Delay clojure.lang.MapEntry - clojure.lang.LineNumberingPushbackReader] + clojure.lang.LineNumberingPushbackReader + java.io.EOFException + java.io.PrintWriter + java.io.PushbackReader] :methods [borkdude.graal.LockFix ;; support for locking ] :fields [clojure.lang.PersistentQueue] From 55ff698908b01f3161bb72ab68d95572307883e2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 21 Jan 2020 15:48:39 +0100 Subject: [PATCH 067/152] Add clojure_csv test --- script/lib_tests/clojure_csv_test | 10 ++++++++++ script/run_lib_tests | 1 + 2 files changed, 11 insertions(+) create mode 100755 script/lib_tests/clojure_csv_test diff --git a/script/lib_tests/clojure_csv_test b/script/lib_tests/clojure_csv_test new file mode 100755 index 00000000..3b8c6e26 --- /dev/null +++ b/script/lib_tests/clojure_csv_test @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -eo pipefail + +export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version "RELEASE"}}}' -Spath)" + +./bb -e " +(require '[clojure-csv.core :as csv]) +(prn (csv/write-csv (csv/parse-csv \"a,b,c\n1,2,3\"))) +" diff --git a/script/run_lib_tests b/script/run_lib_tests index 9e107365..e1a95e0b 100755 --- a/script/run_lib_tests +++ b/script/run_lib_tests @@ -5,3 +5,4 @@ set -eo pipefail script/lib_tests/clj_http_lite_test script/lib_tests/deps_clj_test script/lib_tests/spartan_spec_test +script/lib_tests/clojure_csv_test From 603e318f1b41328c0479071d814e7c86fb221c47 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 21 Jan 2020 15:53:04 +0100 Subject: [PATCH 068/152] Add lib to README --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index db53a13e..9c287ab9 100644 --- a/README.md +++ b/README.md @@ -656,6 +656,21 @@ break> x 1 ``` +#### [clojure-csv](https://github.com/davidsantiago/clojure-csv) + +A library for reading and writing CSV files. Note that babashka already comes +with `clojure.data.csv`, but in case you need this other library, this is how +you can use it: + +``` shell +export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version "RELEASE"}}}' -Spath)" + +./bb -e " +(require '[clojure-csv.core :as csv]) +(csv/write-csv (csv/parse-csv \"a,b,c\n1,2,3\")) +" +``` + ### Blogs - [Clojure Start Time in 2019](https://stuartsierra.com/2019/12/21/clojure-start-time-in-2019) by Stuart Sierra From bc62576152f913706331fc253aed38ae4fc28430 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 22 Jan 2020 21:51:23 +0100 Subject: [PATCH 069/152] Bump edamame and sci --- deps.edn | 2 +- project.clj | 2 +- sci | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deps.edn b/deps.edn index dcc6a46d..62abe3b1 100644 --- a/deps.edn +++ b/deps.edn @@ -1,7 +1,7 @@ {:paths ["src" "sci/src" "resources" "sci/resources"], :deps {org.clojure/clojure {:mvn/version "1.10.1"}, org.clojure/tools.reader {:mvn/version "1.3.2"}, - borkdude/edamame {:mvn/version "0.0.10-alpha.7"}, + borkdude/edamame {:mvn/version "0.0.10-alpha.8"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} org.clojure/core.async {:mvn/version "0.4.500"}, diff --git a/project.clj b/project.clj index e3c0f95a..7d1cd4cf 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/tools.reader "1.3.2"] - [borkdude/edamame "0.0.10-alpha.7"] + [borkdude/edamame "0.0.10-alpha.8"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] [org.clojure/core.async "0.4.500"] diff --git a/sci b/sci index 74878d25..a74089d0 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 74878d25594ca19f7ac743d10ebf6e206e6af09e +Subproject commit a74089d0487fbfd33feefe71f4e60f39f07d7afd From 485fef7df54d6701936704573468a1ec4c66d221 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 23 Jan 2020 20:36:18 +0100 Subject: [PATCH 070/152] Bump edamame and sci --- deps.edn | 2 +- project.clj | 2 +- sci | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/deps.edn b/deps.edn index 62abe3b1..9f63e001 100644 --- a/deps.edn +++ b/deps.edn @@ -1,7 +1,7 @@ {:paths ["src" "sci/src" "resources" "sci/resources"], :deps {org.clojure/clojure {:mvn/version "1.10.1"}, org.clojure/tools.reader {:mvn/version "1.3.2"}, - borkdude/edamame {:mvn/version "0.0.10-alpha.8"}, + borkdude/edamame {:mvn/version "0.0.10"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} org.clojure/core.async {:mvn/version "0.4.500"}, diff --git a/project.clj b/project.clj index 7d1cd4cf..a399146b 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :resource-paths ["resources" "sci/resources"] :dependencies [[org.clojure/clojure "1.10.1"] [org.clojure/tools.reader "1.3.2"] - [borkdude/edamame "0.0.10-alpha.8"] + [borkdude/edamame "0.0.10"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] [org.clojure/core.async "0.4.500"] diff --git a/sci b/sci index a74089d0..d11dcee1 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit a74089d0487fbfd33feefe71f4e60f39f07d7afd +Subproject commit d11dcee1c3a03b7bef2f275d05083c1c60f41407 From 43eef7075f9dac038d8d28a5ee4e49b6affd9864 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 24 Jan 2020 10:26:13 +0100 Subject: [PATCH 071/152] sci: add hierarchies --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index d11dcee1..a269eae2 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit d11dcee1c3a03b7bef2f275d05083c1c60f41407 +Subproject commit a269eae279232a49d365158a515b4e418b900fe8 From 7843ed7716d3c506380f9d6916198cedfb9eb2a8 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 24 Jan 2020 11:11:54 +0100 Subject: [PATCH 072/152] Add dev note --- doc/dev.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/dev.md b/doc/dev.md index 99cf3e1c..6c3d4655 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -58,6 +58,12 @@ Keep notes here about how adding libraries and classes to Babashka affects the b -2020/01/08, 303ca9e825d76a4a45bc4240a59139d342c13964: 36.9mb / 10.8mb zipped. +2020/01/24, 43eef7075f9dac038d8d28a5ee4e49b6affd9864: 38.3mb, 11.1mb zipped +Added hierarchies (derive, isa?, etc). + +2020/01/23, 485fef7df54d6701936704573468a1ec4c66d221: 37.4mb / 10.9mb zipped +Added: StringBuilder, java.io.{Reader,Writer,PrinterWriter,PushbackReader} + +2020/01/08, 303ca9e825d76a4a45bc4240a59139d342c13964: 36.9mb / 10.8mb zipped Removing cheshire from bb: 36.2mb / 10.5mb zipped. From c3d594465f063056d52c416e2a330bf79fe2bddd Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 25 Jan 2020 17:00:40 +0100 Subject: [PATCH 073/152] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c287ab9..5860868c 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ enumerated explicitly. - `clojure.set` aliased as `set` - `clojure.edn` aliased as `edn`: - `read-string` -- `clojure.java.shell` aliases as `shell` +- `clojure.java.shell` aliased as `shell` - `clojure.java.io` aliased as `io`: - `as-relative-path`, `as-url`, `copy`, `delete-file`, `file`, `input-stream`, `make-parents`, `output-stream`, `reader`, `resource`, `writer` From 8cf1dd71f1bb77545fe03eeec7721c670b407879 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 27 Jan 2020 18:13:41 +0100 Subject: [PATCH 074/152] Bump sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index a269eae2..7fd592e7 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit a269eae279232a49d365158a515b4e418b900fe8 +Subproject commit 7fd592e7387f2ef3fd10b8b77fef280e578489bf From 923d229a5c2ad344377a03178452568b759ea965 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 28 Jan 2020 22:14:52 +0100 Subject: [PATCH 075/152] sci: add multimethods --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 7fd592e7..48b61aa9 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 7fd592e7387f2ef3fd10b8b77fef280e578489bf +Subproject commit 48b61aa96cbba1a0d91c0dc2982e6bf37c89e5b5 From 9cd940428f2e4a64bec72d15716f95ca68c572e3 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 11:08:35 +0100 Subject: [PATCH 076/152] sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 48b61aa9..0f7aa220 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 48b61aa96cbba1a0d91c0dc2982e6bf37c89e5b5 +Subproject commit 0f7aa2204dc7176a3ad383b1bd49031a17c8f0d0 From f86cc843039895fbd63b9cac9df8f1b1650fd2a0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 23:22:26 +0100 Subject: [PATCH 077/152] [#247] add clojure.test as built-in lib --- .clj-kondo/config.edn | 3 +- sci | 2 +- src/babashka/impl/clojure/test.clj | 801 ++++++++++++++++++++++++ src/babashka/impl/common.clj | 4 + src/babashka/impl/test.clj | 55 ++ src/babashka/main.clj | 4 + test-resources/babashka/assert_expr.clj | 21 + test/babashka/classpath_test.clj | 4 +- test/babashka/test_test.clj | 71 +++ 9 files changed, 961 insertions(+), 4 deletions(-) create mode 100644 src/babashka/impl/clojure/test.clj create mode 100644 src/babashka/impl/common.clj create mode 100644 src/babashka/impl/test.clj create mode 100644 test-resources/babashka/assert_expr.clj create mode 100644 test/babashka/test_test.clj diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn index e27865ec..e4db19e4 100644 --- a/.clj-kondo/config.edn +++ b/.clj-kondo/config.edn @@ -3,4 +3,5 @@ babashka.impl.Pattern/gen-wrapper-fn clojure.core/def babashka.impl.File/gen-wrapper-fn-2 clojure.core/def babashka.impl.Pattern/gen-wrapper-fn-2 clojure.core/def - babashka.impl.Pattern/gen-constants clojure.core/declare}} + babashka.impl.Pattern/gen-constants clojure.core/declare} + :linters {:unsorted-namespaces {:level :warning}}} diff --git a/sci b/sci index 0f7aa220..2474e34e 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 0f7aa2204dc7176a3ad383b1bd49031a17c8f0d0 +Subproject commit 2474e34eb071c69b9b9140325722008d4e986a6d diff --git a/src/babashka/impl/clojure/test.clj b/src/babashka/impl/clojure/test.clj new file mode 100644 index 00000000..4d1ecc95 --- /dev/null +++ b/src/babashka/impl/clojure/test.clj @@ -0,0 +1,801 @@ + ; Copyright (c) Rich Hickey. All rights reserved. + ; The use and distribution terms for this software are covered by the + ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) + ; which can be found in the file epl-v10.html at the root of this distribution. + ; By using this software in any fashion, you are agreeing to be bound by + ; the terms of this license. + ; You must not remove this notice, or any other, from this software. + +;;; test.clj: test framework for Clojure + +;; by Stuart Sierra +;; March 28, 2009 + +;; Thanks to Chas Emerick, Allen Rohner, and Stuart Halloway for +;; contributions and suggestions. + +(ns + ^{:author "Stuart Sierra, with contributions and suggestions by + Chas Emerick, Allen Rohner, and Stuart Halloway", + :doc "A unit testing framework. + + ASSERTIONS + + The core of the library is the \"is\" macro, which lets you make + assertions of any arbitrary expression: + + (is (= 4 (+ 2 2))) + (is (instance? Integer 256)) + (is (.startsWith \"abcde\" \"ab\")) + + You can type an \"is\" expression directly at the REPL, which will + print a message if it fails. + + user> (is (= 5 (+ 2 2))) + + FAIL in (:1) + expected: (= 5 (+ 2 2)) + actual: (not (= 5 4)) + false + + The \"expected:\" line shows you the original expression, and the + \"actual:\" shows you what actually happened. In this case, it + shows that (+ 2 2) returned 4, which is not = to 5. Finally, the + \"false\" on the last line is the value returned from the + expression. The \"is\" macro always returns the result of the + inner expression. + + There are two special assertions for testing exceptions. The + \"(is (thrown? c ...))\" form tests if an exception of class c is + thrown: + + (is (thrown? ArithmeticException (/ 1 0))) + + \"(is (thrown-with-msg? c re ...))\" does the same thing and also + tests that the message on the exception matches the regular + expression re: + + (is (thrown-with-msg? ArithmeticException #\"Divide by zero\" + (/ 1 0))) + + DOCUMENTING TESTS + + \"is\" takes an optional second argument, a string describing the + assertion. This message will be included in the error report. + + (is (= 5 (+ 2 2)) \"Crazy arithmetic\") + + In addition, you can document groups of assertions with the + \"testing\" macro, which takes a string followed by any number of + assertions. The string will be included in failure reports. + Calls to \"testing\" may be nested, and all of the strings will be + joined together with spaces in the final report, in a style + similar to RSpec + + (testing \"Arithmetic\" + (testing \"with positive integers\" + (is (= 4 (+ 2 2))) + (is (= 7 (+ 3 4)))) + (testing \"with negative integers\" + (is (= -4 (+ -2 -2))) + (is (= -1 (+ 3 -4))))) + + Note that, unlike RSpec, the \"testing\" macro may only be used + INSIDE a \"deftest\" or \"with-test\" form (see below). + + + DEFINING TESTS + + There are two ways to define tests. The \"with-test\" macro takes + a defn or def form as its first argument, followed by any number + of assertions. The tests will be stored as metadata on the + definition. + + (with-test + (defn my-function [x y] + (+ x y)) + (is (= 4 (my-function 2 2))) + (is (= 7 (my-function 3 4)))) + + As of Clojure SVN rev. 1221, this does not work with defmacro. + See http://code.google.com/p/clojure/issues/detail?id=51 + + The other way lets you define tests separately from the rest of + your code, even in a different namespace: + + (deftest addition + (is (= 4 (+ 2 2))) + (is (= 7 (+ 3 4)))) + + (deftest subtraction + (is (= 1 (- 4 3))) + (is (= 3 (- 7 4)))) + + This creates functions named \"addition\" and \"subtraction\", which + can be called like any other function. Therefore, tests can be + grouped and composed, in a style similar to the test framework in + Peter Seibel's \"Practical Common Lisp\" + + + (deftest arithmetic + (addition) + (subtraction)) + + The names of the nested tests will be joined in a list, like + \"(arithmetic addition)\", in failure reports. You can use nested + tests to set up a context shared by several tests. + + + RUNNING TESTS + + Run tests with the function \"(run-tests namespaces...)\": + + (run-tests 'your.namespace 'some.other.namespace) + + If you don't specify any namespaces, the current namespace is + used. To run all tests in all namespaces, use \"(run-all-tests)\". + + By default, these functions will search for all tests defined in + a namespace and run them in an undefined order. However, if you + are composing tests, as in the \"arithmetic\" example above, you + probably do not want the \"addition\" and \"subtraction\" tests run + separately. In that case, you must define a special function + named \"test-ns-hook\" that runs your tests in the correct order: + + (defn test-ns-hook [] + (arithmetic)) + + Note: test-ns-hook prevents execution of fixtures (see below). + + + OMITTING TESTS FROM PRODUCTION CODE + + You can bind the variable \"*load-tests*\" to false when loading or + compiling code in production. This will prevent any tests from + being created by \"with-test\" or \"deftest\". + + + FIXTURES + + Fixtures allow you to run code before and after tests, to set up + the context in which tests should be run. + + A fixture is just a function that calls another function passed as + an argument. It looks like this: + + (defn my-fixture [f] + Perform setup, establish bindings, whatever. + (f) Then call the function we were passed. + Tear-down / clean-up code here. + ) + + Fixtures are attached to namespaces in one of two ways. \"each\" + fixtures are run repeatedly, once for each test function created + with \"deftest\" or \"with-test\". \"each\" fixtures are useful for + establishing a consistent before/after state for each test, like + clearing out database tables. + + \"each\" fixtures can be attached to the current namespace like this: + (use-fixtures :each fixture1 fixture2 ...) + The fixture1, fixture2 are just functions like the example above. + They can also be anonymous functions, like this: + (use-fixtures :each (fn [f] setup... (f) cleanup...)) + + The other kind of fixture, a \"once\" fixture, is only run once, + around ALL the tests in the namespace. \"once\" fixtures are useful + for tasks that only need to be performed once, like establishing + database connections, or for time-consuming tasks. + + Attach \"once\" fixtures to the current namespace like this: + (use-fixtures :once fixture1 fixture2 ...) + + Note: Fixtures and test-ns-hook are mutually incompatible. If you + are using test-ns-hook, fixture functions will *never* be run. + + + SAVING TEST OUTPUT TO A FILE + + All the test reporting functions write to the var *test-out*. By + default, this is the same as *out*, but you can rebind it to any + PrintWriter. For example, it could be a file opened with + clojure.java.io/writer. + + + EXTENDING TEST-IS (ADVANCED) + + You can extend the behavior of the \"is\" macro by defining new + methods for the \"assert-expr\" multimethod. These methods are + called during expansion of the \"is\" macro, so they should return + quoted forms to be evaluated. + + You can plug in your own test-reporting framework by rebinding + the \"report\" function: (report event) + + The 'event' argument is a map. It will always have a :type key, + whose value will be a keyword signaling the type of event being + reported. Standard events with :type value of :pass, :fail, and + :error are called when an assertion passes, fails, and throws an + exception, respectively. In that case, the event will also have + the following keys: + + :expected The form that was expected to be true + :actual A form representing what actually occurred + :message The string message given as an argument to 'is' + + The \"testing\" strings will be a list in \"*testing-contexts*\", and + the vars being tested will be a list in \"*testing-vars*\". + + Your \"report\" function should wrap any printing calls in the + \"with-test-out\" macro, which rebinds *out* to the current value + of *test-out*. + + For additional event types, see the examples in the code. +"} + babashka.impl.clojure.test + (:require [babashka.impl.clojure.stacktrace :as stack] + [babashka.impl.common :refer [ctx]] + [clojure.string :as str] + [clojure.template :as temp] + [sci.core :as sci] + [sci.impl.analyzer :as ana] + [sci.impl.namespaces :as sci-namespaces] + [sci.impl.vars :as vars])) + +;; Nothing is marked "private" here, so you can rebind things to plug +;; in your own testing or reporting frameworks. + + +;;; USER-MODIFIABLE GLOBALS + +(defonce + ^{:doc "True by default. If set to false, no test functions will + be created by deftest, set-test, or with-test. Use this to omit + tests when compiling or loading production code."} + load-tests + (sci/new-dynamic-var '*load-tests* true)) + +(def + ^{:doc "The maximum depth of stack traces to print when an Exception + is thrown during a test. Defaults to nil, which means print the + complete stack trace."} + stack-trace-depth + (sci/new-dynamic-var '*stack-trace-depth* nil)) + + +;;; GLOBALS USED BY THE REPORTING FUNCTIONS + +(def report-counters (sci/new-dynamic-var '*report-counters* nil)) ; bound to a ref of a map in test-ns + +(def initial-report-counters ; used to initialize *report-counters* + (sci/new-dynamic-var '*initial-report-counters* {:test 0, :pass 0, :fail 0, :error 0})) + +(def testing-vars (sci/new-dynamic-var '*testing-vars* (list))) ; bound to hierarchy of vars being tested + +(def testing-contexts (sci/new-dynamic-var '*testing-contexts* (list))) ; bound to hierarchy of "testing" strings + +(def test-out (sci/new-dynamic-var '*test-out* sci/out)) ; PrintWriter for test reporting output + +(defmacro with-test-out-internal + "Runs body with *out* bound to the value of *test-out*." + {:added "1.1"} + [& body] + `(sci/binding [sci/out @test-out] + ~@body)) + +;;; UTILITIES FOR REPORTING FUNCTIONS + +(defn file-position + "Returns a vector [filename line-number] for the nth call up the + stack. + + Deprecated in 1.2: The information needed for test reporting is + now on :file and :line keys in the result map." + {:added "1.1" + :deprecated "1.2"} + [n] + (let [^StackTraceElement s (nth (.getStackTrace (new java.lang.Throwable)) n)] + [(.getFileName s) (.getLineNumber s)])) + +(defn testing-vars-str + "Returns a string representation of the current test. Renders names + in *testing-vars* as a list, then the source file and line of + current assertion." + {:added "1.1"} + [m] + (let [{:keys [file line]} m] + (str + ;; Uncomment to include namespace in failure report: + ;;(ns-name (:ns (meta (first *testing-vars*)))) "/ " + (reverse (map #(:name (meta %)) @testing-vars)) + " (" file ":" line ")"))) + +(defn testing-contexts-str + "Returns a string representation of the current test context. Joins + strings in *testing-contexts* with spaces." + {:added "1.1"} + [] + (apply str (interpose " " (reverse @testing-contexts)))) + +(defn inc-report-counter + "Increments the named counter in *report-counters*, a ref to a map. + Does nothing if *report-counters* is nil." + {:added "1.1"} + [name] + (when @report-counters + (swap! @report-counters update-in [name] (fnil inc 0)))) + +;;; TEST RESULT REPORTING + +(defmulti + ^{:doc "Generic reporting function, may be overridden to plug in + different report formats (e.g., TAP, JUnit). Assertions such as + 'is' call 'report' to indicate results. The argument given to + 'report' will be a map with a :type key. See the documentation at + the top of test_is.clj for more information on the types of + arguments for 'report'." + :dynamic true + :added "1.1"} + report :type) + +#_(defn- file-and-line + {:deprecated "1.8"} + [^Throwable exception depth] + (let [stacktrace (.getStackTrace exception)] + (if (< depth (count stacktrace)) + (let [^StackTraceElement s (nth stacktrace depth)] + {:file (.getFileName s) :line (.getLineNumber s)}) + {:file nil :line nil}))) + +(defn- stacktrace-file-and-line + [stacktrace] + (if (seq stacktrace) + (let [^StackTraceElement s (first stacktrace)] + {:file (.getFileName s) :line (.getLineNumber s)}) + {:file nil :line nil})) + +(defn do-report + "Add file and line information to a test result and call report. + If you are writing a custom assert-expr method, call this function + to pass test results to report." + {:added "1.2"} + [m] + (report + (case + (:type m) + :fail (merge (stacktrace-file-and-line (drop-while + #(let [cl-name (.getClassName ^StackTraceElement %)] + (or (str/starts-with? cl-name "java.lang.") + (str/starts-with? cl-name "clojure.test$") + (str/starts-with? cl-name "clojure.core$ex_info"))) + (.getStackTrace (Thread/currentThread)))) m) + :error (merge (stacktrace-file-and-line (.getStackTrace ^Throwable (:actual m))) m) + m))) + +(defmethod report :default [m] + (with-test-out-internal (prn m))) + +(defmethod report :pass [m] + (with-test-out-internal (inc-report-counter :pass))) + +(defmethod report :fail [m] + (with-test-out-internal + (inc-report-counter :fail) + (println "\nFAIL in" (testing-vars-str m)) + (when (seq @testing-contexts) (println (testing-contexts-str))) + (when-let [message (:message m)] (println message)) + (println "expected:" (pr-str (:expected m))) + (println " actual:" (pr-str (:actual m))))) + +(defmethod report :error [m] + (with-test-out-internal + (inc-report-counter :error) + (println "\nERROR in" (testing-vars-str m)) + (when (seq @testing-contexts) (println (testing-contexts-str))) + (when-let [message (:message m)] (println message)) + (println "expected:" (pr-str (:expected m))) + (print " actual: ") + (let [actual (:actual m)] + (if (instance? Throwable actual) + (stack/print-cause-trace actual @stack-trace-depth) + (prn actual))))) + +(defmethod report :summary [m] + (with-test-out-internal + (println "\nRan" (:test m) "tests containing" + (+ (:pass m) (:fail m) (:error m)) "assertions.") + (println (:fail m) "failures," (:error m) "errors."))) + +(defmethod report :begin-test-ns [m] + (with-test-out-internal + (println "\nTesting" (sci-namespaces/sci-ns-name (:ns m))))) + +;; Ignore these message types: +(defmethod report :end-test-ns [m]) +(defmethod report :begin-test-var [m]) +(defmethod report :end-test-var [m]) + + + +;;; UTILITIES FOR ASSERTIONS + +(defn function? + "Returns true if argument is a function or a symbol that resolves to + a function (not a macro)." + {:added "1.1"} + [x] + (if (symbol? x) ;; TODO + (when-let [v (second (ana/lookup @ctx x false))] + (when-let [value (if (vars/var? v) @v v)] + (and (fn? value) + (not (:sci/macro (meta v)))))) + (fn? x))) + +(defn assert-predicate + "Returns generic assertion code for any functional predicate. The + 'expected' argument to 'report' will contains the original form, the + 'actual' argument will contain the form with all its sub-forms + evaluated. If the predicate returns false, the 'actual' form will + be wrapped in (not...)." + {:added "1.1"} + [msg form] + (let [args (rest form) + pred (first form)] + `(let [values# (list ~@args) + result# (apply ~pred values#)] + (if result# + (clojure.test/do-report {:type :pass, :message ~msg, + :expected '~form, :actual (cons ~pred values#)}) + (clojure.test/do-report {:type :fail, :message ~msg, + :expected '~form, :actual (list '~'not (cons '~pred values#))})) + result#))) + +(defn assert-any + "Returns generic assertion code for any test, including macros, Java + method calls, or isolated symbols." + {:added "1.1"} + [msg form] + `(let [value# ~form] + (if value# + (clojure.test/do-report {:type :pass, :message ~msg, + :expected '~form, :actual value#}) + (clojure.test/do-report {:type :fail, :message ~msg, + :expected '~form, :actual value#})) + value#)) + + + +;;; ASSERTION METHODS + +;; You don't call these, but you can add methods to extend the 'is' +;; macro. These define different kinds of tests, based on the first +;; symbol in the test expression. + +(defmulti assert-expr + (fn [msg form] + (cond + (nil? form) :always-fail + (seq? form) (first form) + :else :default))) + +(defmethod assert-expr :always-fail [msg form] + ;; nil test: always fail + `(clojure.test/do-report {:type :fail, :message ~msg})) + +(defmethod assert-expr :default [msg form] + (if (and (sequential? form) (function? (first form))) + (assert-predicate msg form) + (assert-any msg form))) + +(defmethod assert-expr 'instance? [msg form] + ;; Test if x is an instance of y. + `(let [klass# ~(nth form 1) + object# ~(nth form 2)] + (let [result# (instance? klass# object#)] + (if result# + (clojure.test/do-report {:type :pass, :message ~msg, + :expected '~form, :actual (class object#)}) + (clojure.test/do-report {:type :fail, :message ~msg, + :expected '~form, :actual (class object#)})) + result#))) + +(defmethod assert-expr 'thrown? [msg form] + ;; (is (thrown? c expr)) + ;; Asserts that evaluating expr throws an exception of class c. + ;; Returns the exception thrown. + (let [klass (second form) + body (nthnext form 2)] + `(try ~@body + (clojure.test/do-report {:type :fail, :message ~msg, + :expected '~form, :actual nil}) + (catch ~klass e# + (clojure.test/do-report {:type :pass, :message ~msg, + :expected '~form, :actual e#}) + e#)))) + +(defmethod assert-expr 'thrown-with-msg? [msg form] + ;; (is (thrown-with-msg? c re expr)) + ;; Asserts that evaluating expr throws an exception of class c. + ;; Also asserts that the message string of the exception matches + ;; (with re-find) the regular expression re. + (let [klass (nth form 1) + re (nth form 2) + body (nthnext form 3)] + `(try ~@body + (clojure.test/do-report {:type :fail, :message ~msg, :expected '~form, :actual nil}) + (catch ~klass e# + (let [m# (.getMessage e#)] + (if (re-find ~re m#) + (clojure.test/do-report {:type :pass, :message ~msg, + :expected '~form, :actual e#}) + (clojure.test/do-report {:type :fail, :message ~msg, + :expected '~form, :actual e#}))) + e#)))) + + +(defmacro try-expr + "Used by the 'is' macro to catch unexpected exceptions. + You don't call this." + {:added "1.1"} + [msg form] + `(try ~(assert-expr msg form) + (catch Throwable t# + (clojure.test/do-report {:type :error, :message ~msg, + :expected '~form, :actual t#})))) + + + +;;; ASSERTION MACROS + +;; You use these in your tests. + +(defmacro is + "Generic assertion macro. 'form' is any predicate test. + 'msg' is an optional message to attach to the assertion. + + Example: (is (= 4 (+ 2 2)) \"Two plus two should be 4\") + + Special forms: + + (is (thrown? c body)) checks that an instance of c is thrown from + body, fails if not; then returns the thing thrown. + + (is (thrown-with-msg? c re body)) checks that an instance of c is + thrown AND that the message on the exception matches (with + re-find) the regular expression re." + {:added "1.1"} + ([form] + `(clojure.test/is ~form nil)) + ([form msg] `(clojure.test/try-expr ~msg ~form))) + +(defmacro are + "Checks multiple assertions with a template expression. + See clojure.template/do-template for an explanation of + templates. + + Example: (are [x y] (= x y) + 2 (+ 1 1) + 4 (* 2 2)) + Expands to: + (do (is (= 2 (+ 1 1))) + (is (= 4 (* 2 2)))) + + Note: This breaks some reporting features, such as line numbers." + {:added "1.1"} + [argv expr & args] + (if (or + ;; (are [] true) is meaningless but ok + (and (empty? argv) (empty? args)) + ;; Catch wrong number of args + (and (pos? (count argv)) + (pos? (count args)) + (zero? (mod (count args) (count argv))))) + `(temp/do-template ~argv (clojure.test/is ~expr) ~@args) + (throw (IllegalArgumentException. "The number of args doesn't match are's argv.")))) + +(defmacro testing + "Adds a new string to the list of testing contexts. May be nested, + but must occur inside a test function (deftest)." + {:added "1.1"} + [string & body] + `(binding [clojure.test/*testing-contexts* (conj clojure.test/*testing-contexts* ~string)] + ~@body)) + + + +;;; DEFINING TESTS + +(defmacro with-test + "Takes any definition form (that returns a Var) as the first argument. + Remaining body goes in the :test metadata function for that Var. + + When *load-tests* is false, only evaluates the definition, ignoring + the tests." + {:added "1.1"} + [definition & body] + (if @load-tests + `(doto ~definition (alter-meta! assoc :test (fn [] ~@body))) + definition)) + + +(defmacro deftest + "Defines a test function with no arguments. Test functions may call + other tests, so tests may be composed. If you compose tests, you + should also define a function named test-ns-hook; run-tests will + call test-ns-hook instead of testing all vars. + + Note: Actually, the test body goes in the :test metadata on the var, + and the real function (the value of the var) calls test-var on + itself. + + When *load-tests* is false, deftest is ignored." + {:added "1.1"} + [name & body] + (when @load-tests + `(def ~(vary-meta name assoc :test `(fn [] ~@body)) + (fn [] (clojure.test/test-var (var ~name)))))) + +(defmacro deftest- + "Like deftest but creates a private var." + {:added "1.1"} + [name & body] + (when @load-tests + `(def ~(vary-meta name assoc :test `(fn [] ~@body) :private true) + (fn [] (test-var (var ~name)))))) + + +(defmacro set-test + "Experimental. + Sets :test metadata of the named var to a fn with the given body. + The var must already exist. Does not modify the value of the var. + + When *load-tests* is false, set-test is ignored." + {:added "1.1"} + [name & body] + (when @load-tests + `(alter-meta! (var ~name) assoc :test (fn [] ~@body)))) + + + +;;; DEFINING FIXTURES + +(def ^:private ns->fixtures (atom {})) + +(defn- add-ns-meta + "Adds elements in coll to the current namespace metadata as the + value of key." + {:added "1.1"} + [key coll] + (swap! ns->fixtures assoc-in [(sci-namespaces/sci-ns-name @vars/current-ns) key] coll)) + +(defmulti use-fixtures + "Wrap test runs in a fixture function to perform setup and + teardown. Using a fixture-type of :each wraps every test + individually, while :once wraps the whole run in a single function." + {:added "1.1"} + (fn [fixture-type & args] fixture-type)) + +(defmethod use-fixtures :each [fixture-type & args] + (add-ns-meta ::each-fixtures args)) + +(defmethod use-fixtures :once [fixture-type & args] + (add-ns-meta ::once-fixtures args)) + +(defn- default-fixture + "The default, empty, fixture function. Just calls its argument." + {:added "1.1"} + [f] + (f)) + +(defn compose-fixtures + "Composes two fixture functions, creating a new fixture function + that combines their behavior." + {:added "1.1"} + [f1 f2] + (fn [g] (f1 (fn [] (f2 g))))) + +(defn join-fixtures + "Composes a collection of fixtures, in order. Always returns a valid + fixture function, even if the collection is empty." + {:added "1.1"} + [fixtures] + (reduce compose-fixtures default-fixture fixtures)) + + + + +;;; RUNNING TESTS: LOW-LEVEL FUNCTIONS + +(defn test-var + "If v has a function in its :test metadata, calls that function, + with *testing-vars* bound to (conj *testing-vars* v)." + {:dynamic true, :added "1.1"} + [v] + (when-let [t (:test (meta v))] + (sci/binding [testing-vars (conj @testing-vars v)] + (do-report {:type :begin-test-var, :var v}) + (inc-report-counter :test) + (try (t) + (catch Throwable e + (do-report {:type :error, :message "Uncaught exception, not in assertion." + :expected nil, :actual e}))) + (do-report {:type :end-test-var, :var v})))) + +(defn test-vars + "Groups vars by their namespace and runs test-vars on them with + appropriate fixtures applied." + {:added "1.6"} + [vars] + (doseq [[ns vars] (group-by (comp :ns meta) vars) + :when ns] + (let [ns-name (sci-namespaces/sci-ns-name ns) + fixtures (get @ns->fixtures ns-name) + once-fixture-fn (join-fixtures (::once-fixtures fixtures)) + each-fixture-fn (join-fixtures (::each-fixtures fixtures))] + (once-fixture-fn + (fn [] + (doseq [v vars] + (when (:test (meta v)) + (each-fixture-fn (fn [] (test-var v)))))))))) + +(defn test-all-vars + "Calls test-vars on every var interned in the namespace, with fixtures." + {:added "1.1"} + [ctx ns] + (test-vars (vals (sci-namespaces/sci-ns-interns ctx ns)))) + +(defn test-ns + "If the namespace defines a function named test-ns-hook, calls that. + Otherwise, calls test-all-vars on the namespace. 'ns' is a + namespace object or a symbol. + + Internally binds *report-counters* to a ref initialized to + *initial-report-counters*. Returns the final, dereferenced state of + *report-counters*." + {:added "1.1"} + [ctx ns] + (sci/binding [report-counters (atom @initial-report-counters)] + (let [ns-obj (sci-namespaces/sci-the-ns ctx ns)] + (do-report {:type :begin-test-ns, :ns ns-obj}) + ;; If the namespace has a test-ns-hook function, call that: + (let [ns-sym (sci-namespaces/sci-ns-name ns-obj)] + (if-let [v (get-in @(:env ctx) [:namespaces ns-sym 'test-ns-hook])] + (@v) + ;; Otherwise, just test every var in the namespace. + (test-all-vars ctx ns-obj))) + (do-report {:type :end-test-ns, :ns ns-obj})) + @@report-counters)) + + + +;;; RUNNING TESTS: HIGH-LEVEL FUNCTIONS + +(defn run-tests + "Runs all tests in the given namespaces; prints results. + Defaults to current namespace if none given. Returns a map + summarizing test results." + {:added "1.1"} + ([ctx] (run-tests ctx @vars/current-ns)) + ([ctx & namespaces] + (let [summary (assoc (apply merge-with + (map #(test-ns ctx %) namespaces)) + :type :summary)] + (do-report summary) + summary))) + +(defn run-all-tests + "Runs all tests in all namespaces; prints results. + Optional argument is a regular expression; only namespaces with + names matching the regular expression (with re-matches) will be + tested." + {:added "1.1"} + ([ctx] (apply run-tests ctx (sci-namespaces/sci-all-ns ctx))) + ([ctx re] (apply run-tests ctx + (filter #(re-matches re (name (sci-namespaces/sci-ns-name %))) + (sci-namespaces/sci-all-ns ctx))))) + +(defn successful? + "Returns true if the given test summary indicates all tests + were successful, false otherwise." + {:added "1.1"} + [summary] + (and (zero? (:fail summary 0)) + (zero? (:error summary 0)))) diff --git a/src/babashka/impl/common.clj b/src/babashka/impl/common.clj new file mode 100644 index 00000000..938b89a6 --- /dev/null +++ b/src/babashka/impl/common.clj @@ -0,0 +1,4 @@ +(ns babashka.impl.common) + +;; placeholder for ctx +(def ctx (volatile! nil)) diff --git a/src/babashka/impl/test.clj b/src/babashka/impl/test.clj new file mode 100644 index 00000000..9a7a59b3 --- /dev/null +++ b/src/babashka/impl/test.clj @@ -0,0 +1,55 @@ +(ns babashka.impl.test + (:require [babashka.impl.clojure.test :as t])) + +(defn macrofy [v] + (with-meta v {:sci/macro true})) + +(defn contextualize [v] + (with-meta v {:sci.impl/op :needs-ctx})) + +(def clojure-test-namespace + {'*load-tests* t/load-tests + '*stack-trace-depth* t/stack-trace-depth + '*report-counters* t/report-counters + '*initial-report-counters* t/initial-report-counters + '*testing-vars* t/testing-vars + '*testing-contexts* t/testing-contexts + '*test-out* t/test-out + ;; 'with-test-out (macrofy @#'t/with-test-out) + ;; 'file-position t/file-position + 'testing-vars-str t/testing-vars-str + 'testing-contexts-str t/testing-contexts-str + 'inc-report-counter t/inc-report-counter + 'do-report t/do-report + ;; assertion utilities + 'function? t/function? + 'assert-predicate t/assert-predicate + 'assert-any t/assert-any + ;; assertion methods + 'assert-expr t/assert-expr + 'try-expr (with-meta @#'t/try-expr + {:sci/macro true}) + ;; assertion macros + 'is (with-meta @#'t/is + {;; :sci.impl/op :needs-ctx + :sci/macro true}) + 'are (macrofy @#'t/are) + 'testing (macrofy @#'t/testing) + ;; defining tests + 'with-test (macrofy @#'t/with-test) + 'deftest (macrofy @#'t/deftest) + 'deftest- (macrofy @#'t/deftest-) + 'set-test (macrofy @#'t/set-test) + ;; fixtures + 'use-fixtures t/use-fixtures + 'compose-fixtures t/compose-fixtures + 'join-fixtures t/join-fixtures + ;; running tests: low level + 'test-var t/test-var + 'test-vars t/test-vars + 'test-all-vars (with-meta t/test-all-vars {:sci.impl/op :needs-ctx}) + 'test-ns (with-meta t/test-ns {:sci.impl/op :needs-ctx}) + ;; running tests: high level + 'run-tests (contextualize t/run-tests) + 'run-all-tests (contextualize t/run-all-tests) + 'successful? t/successful?}) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 3b9293c1..4e87122e 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -10,10 +10,12 @@ [babashka.impl.clojure.java.shell :refer [shell-namespace]] [babashka.impl.clojure.main :refer [demunge]] [babashka.impl.clojure.stacktrace :refer [stacktrace-namespace print-stack-trace]] + [babashka.impl.common :as common] [babashka.impl.csv :as csv] [babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]] [babashka.impl.repl :as repl] [babashka.impl.socket-repl :as socket-repl] + [babashka.impl.test :as t] [babashka.impl.tools.cli :refer [tools-cli-namespace]] [babashka.wait :as wait] [clojure.edn :as edn] @@ -247,6 +249,7 @@ Everything after that is bound to *command-line-args*.")) 'clojure.stacktrace stacktrace-namespace 'clojure.main {'demunge demunge} 'clojure.repl {'demunge demunge} + 'clojure.test t/clojure-test-namespace 'babashka.classpath {'add-classpath add-classpath*}}) (def bindings @@ -337,6 +340,7 @@ Everything after that is bound to *command-line-args*.")) :dry-run uberscript} ctx (addons/future ctx) sci-ctx (sci-opts/init ctx) + _ (vreset! common/ctx sci-ctx) _ (swap! (:env sci-ctx) (fn [env] (update-in env [:namespaces 'clojure.core] assoc diff --git a/test-resources/babashka/assert_expr.clj b/test-resources/babashka/assert_expr.clj new file mode 100644 index 00000000..6983e91e --- /dev/null +++ b/test-resources/babashka/assert_expr.clj @@ -0,0 +1,21 @@ +(require '[clojure.test :refer [is deftest] :as t]) + +(defmethod t/assert-expr 'roughly [msg form] + `(let [op1# ~(nth form 1) + op2# ~(nth form 2) + tolerance# (if (= 4 ~(count form)) ~(last form) 2) + decimals# (/ 1. (Math/pow 10 tolerance#)) + result# (< (Math/abs (- op1# op2#)) decimals#)] + (t/do-report + {:type (if result# :pass :fail) + :message ~msg + :expected (format "%s should be roughly %s with %s tolerance" + op1# op2# decimals#) + :actual result#}) + result#)) + +(deftest PI-test + (is (roughly 3.14 Math/PI 2)) + (is (roughly 3.14 Math/PI 3))) + +(t/test-var #'PI-test) diff --git a/test/babashka/classpath_test.clj b/test/babashka/classpath_test.clj index eb592f5a..95fcc56b 100644 --- a/test/babashka/classpath_test.clj +++ b/test/babashka/classpath_test.clj @@ -2,8 +2,8 @@ (:require [babashka.test-utils :as tu] [clojure.edn :as edn] - [clojure.test :as t :refer [deftest is]] - [clojure.java.io :as io])) + [clojure.java.io :as io] + [clojure.test :as t :refer [deftest is]])) (defn bb [input & args] (edn/read-string (apply tu/bb (when (some? input) (str input)) (map str args)))) diff --git a/test/babashka/test_test.clj b/test/babashka/test_test.clj new file mode 100644 index 00000000..eb6bc975 --- /dev/null +++ b/test/babashka/test_test.clj @@ -0,0 +1,71 @@ +(ns babashka.test-test + (:require + [babashka.test-utils :as tu] + [clojure.string :as str] + [clojure.test :as t :refer [deftest is]] + [clojure.java.io :as io])) + +(defn bb [& args] + (apply tu/bb nil (map str args))) + +(deftest deftest-test + (is (str/includes? + (bb "(require '[clojure.test :as t]) (t/deftest foo (t/is (= 4 5))) (foo)") + "expected: (= 4 5)\n actual: (not (= 4 5))\n"))) + +(deftest run-tests-test + (let [output (bb "(require '[clojure.test :as t]) (t/deftest foo (t/is (= 4 5))) (t/run-tests)")] + (is (str/includes? output "Testing user")) + (is (str/includes? output "{:test 1, :pass 0, :fail 1, :error 0, :type :summary}")))) + +(deftest run-all-tests-test + (let [output (bb " +(require '[clojure.test :as t]) +(t/deftest foo (t/is (= 4 5))) +(ns foobar) +(require '[clojure.test :as t]) +(t/run-all-tests)")] + (is (str/includes? output "Testing user")) + (is (str/includes? output "Testing foobar")) + (is (str/includes? output "{:test 1, :pass 0, :fail 1, :error 0, :type :summary}")))) + +(deftest fixtures-test + (let [output (bb " +(require '[clojure.test :as t]) +(defn once [f] (prn :once-before) (f) (prn :once-after)) +(defn each [f] (prn :each-before) (f) (prn :each-after)) +(t/use-fixtures :once once) +(t/use-fixtures :each each) +(t/deftest foo) +(t/deftest bar) +(t/run-tests)")] + (is (str/includes? output (str/trim " +:once-before +:each-before +:each-after +:each-before +:each-after +:once-after"))))) + +(deftest with-test + (let [output (bb " +(require '[clojure.test :as t]) +(t/with-test + (defn my-function [x y] + (+ x y)) + (t/is (= 4 (my-function 2 2))) + (t/is (= 7 (my-function 3 4)))) +(t/run-tests)")] + (is (str/includes? output "Ran 1 tests containing 2 assertions.")))) + +(deftest testing-test + (is (str/includes? (bb "(require '[clojure.test :as t]) (t/testing \"foo\" (t/is (= 4 5)))") + "foo"))) + +(deftest are-test + (is (str/includes? (bb "(require '[clojure.test :as t]) (t/are [x y] (= x y) 2 (+ 1 2))") + "expected: (= 2 (+ 1 2))"))) + +(deftest assert-expr-test + (is (str/includes? (bb (.getPath (io/file "test-resources" "babashka" "assert_expr.clj"))) + "3.14 should be roughly 3.141592653589793"))) From 710eee3e5162fa1a1bb4ef001acf24ceea16af35 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 23:31:29 +0100 Subject: [PATCH 078/152] [#247] add clojure.test/report --- src/babashka/impl/test.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/babashka/impl/test.clj b/src/babashka/impl/test.clj index 9a7a59b3..497963eb 100644 --- a/src/babashka/impl/test.clj +++ b/src/babashka/impl/test.clj @@ -20,6 +20,7 @@ 'testing-vars-str t/testing-vars-str 'testing-contexts-str t/testing-contexts-str 'inc-report-counter t/inc-report-counter + 'report t/report 'do-report t/do-report ;; assertion utilities 'function? t/function? From 584666097bbe9f2ce1097ab640f0fb7690258ffb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 23:44:52 +0100 Subject: [PATCH 079/152] sci: minor --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 2474e34e..a82e2023 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 2474e34eb071c69b9b9140325722008d4e986a6d +Subproject commit a82e20235c65c4a1ecc50885597e1df8e334d113 From 8e36d3cf9ace66ab36a13c97b474eb391e8d8a70 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 23:53:01 +0100 Subject: [PATCH 080/152] docs --- README.md | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5860868c..05016b96 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,7 @@ enumerated explicitly. `async`. The `alt` and `go` macros are not available but `alts!!` does work as it is a function. - `clojure.stacktrace` +- `clojure.test` - [`clojure.tools.cli`](https://github.com/clojure/tools.cli) aliased as `tools.cli` - [`clojure.data.csv`](https://github.com/clojure/data.csv) aliased as `csv` - [`cheshire.core`](https://github.com/dakrone/cheshire) aliased as `json` @@ -597,8 +598,10 @@ Differences with Clojure: - A subset of Java classes are supported. -- Only the `clojure.core`, `clojure.set`, `clojure.string` and `clojure.walk` - namespaces are available from Clojure. +- Only the `clojure.core`, `clojure.edn`, `clojue.java.io`, + `clojure.java.shell`, `clojure.set`, `clojure.stacktrace`, `clojure.string`, + `clojure.template`, `clojure.test` and `clojure.walk` namespaces are available + from Clojure. - Interpretation comes with overhead. Therefore tight loops are likely slower than in Clojure on the JVM. @@ -616,14 +619,27 @@ The following libraries are known to work with Babashka: A port of the [clojure](https://github.com/clojure/brew-install/) bash script to Clojure / babashka. -#### [spartan.test](https://github.com/borkdude/spartan.test/) - -A minimal test framework compatible with babashka. - #### [spartan.spec](https://github.com/borkdude/spartan.spec/) An babashka-compatible implementation of `clojure.spec.alpha`. +#### [missing.test.assertions](https://github.com/borkdude/missing.test.assertions) + +This library checks if no assertions have been made in a test: + +``` shell +$ export BABASHKA_CLASSPATH=$(clojure -Spath -Sdeps '{:deps {borkdude/missing.test.assertions {:git/url "https://github.com/borkdude/missing.test.assertions" :sha "603cb01bee72fb17addacc53c34c85612684ad70"}}}') + +$ lein bb "(require '[missing.test.assertions] '[clojure.test :as t]) (t/deftest foo) (t/run-tests)" + +Testing user +WARNING: no assertions made in test foo + +Ran 1 tests containing 0 assertions. +0 failures, 0 errors. +{:test 1, :pass 0, :fail 0, :error 0, :type :summary} +``` + #### [medley](https://github.com/borkdude/medley/) A fork of [medley](https://github.com/weavejester/medley) made compatible with @@ -671,6 +687,12 @@ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version " " ``` +#### [spartan.test](https://github.com/borkdude/spartan.test/) + +A minimal test framework compatible with babashka. This library is deprecated +since babashka v0.0.68 which has `clojure.test` built-in. + + ### Blogs - [Clojure Start Time in 2019](https://stuartsierra.com/2019/12/21/clojure-start-time-in-2019) by Stuart Sierra From 6e8a2e2e9a6608502a3298f36706fbc608599b37 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 29 Jan 2020 23:55:21 +0100 Subject: [PATCH 081/152] [#245] add -iO and -Io --- src/babashka/main.clj | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 4e87122e..2af63fd6 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -70,6 +70,14 @@ (assoc opts-map :shell-in true :shell-out true)) + ("-iO") (recur (next options) + (assoc opts-map + :shell-in true + :edn-out true)) + ("-Io") (recur (next options) + (assoc opts-map + :edn-in true + :shell-out true)) ("-IO") (recur (next options) (assoc opts-map :edn-in true From 77cd68be1c59ee2dca76cb0989bf5bc7f9be2d5e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 30 Jan 2020 00:07:29 +0100 Subject: [PATCH 082/152] v0.0.67 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 69e0d6aa..c21ada26 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.67-SNAPSHOT \ No newline at end of file +0.0.67 \ No newline at end of file From d8eb74a13b0362e35f8a96ab14603b801a89c4a4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 30 Jan 2020 00:27:20 +0100 Subject: [PATCH 083/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 9990aece..c21ada26 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.66 \ No newline at end of file +0.0.67 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index c21ada26..60b7753c 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.67 \ No newline at end of file +0.0.68-SNAPSHOT \ No newline at end of file From 8570eef0197efc0baec10a09403ea6b3155325d0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 30 Jan 2020 10:30:11 +0100 Subject: [PATCH 084/152] Fix reported file --- src/babashka/impl/clojure/test.clj | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/babashka/impl/clojure/test.clj b/src/babashka/impl/clojure/test.clj index 4d1ecc95..60fcec78 100644 --- a/src/babashka/impl/clojure/test.clj +++ b/src/babashka/impl/clojure/test.clj @@ -302,7 +302,7 @@ current assertion." {:added "1.1"} [m] - (let [{:keys [file line]} m] + (let [{:keys [:file :line]} (meta (first @testing-vars))] (str ;; Uncomment to include namespace in failure report: ;;(ns-name (:ns (meta (first *testing-vars*)))) "/ " @@ -337,15 +337,6 @@ :added "1.1"} report :type) -#_(defn- file-and-line - {:deprecated "1.8"} - [^Throwable exception depth] - (let [stacktrace (.getStackTrace exception)] - (if (< depth (count stacktrace)) - (let [^StackTraceElement s (nth stacktrace depth)] - {:file (.getFileName s) :line (.getLineNumber s)}) - {:file nil :line nil}))) - (defn- stacktrace-file-and-line [stacktrace] (if (seq stacktrace) @@ -362,12 +353,7 @@ (report (case (:type m) - :fail (merge (stacktrace-file-and-line (drop-while - #(let [cl-name (.getClassName ^StackTraceElement %)] - (or (str/starts-with? cl-name "java.lang.") - (str/starts-with? cl-name "clojure.test$") - (str/starts-with? cl-name "clojure.core$ex_info"))) - (.getStackTrace (Thread/currentThread)))) m) + :fail m :error (merge (stacktrace-file-and-line (.getStackTrace ^Throwable (:actual m))) m) m))) From 43f879de2b8f3a75201d02d7a1c96971aaffd7b8 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 30 Jan 2020 11:08:12 +0100 Subject: [PATCH 085/152] README --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 05016b96..9e7cacaa 100644 --- a/README.md +++ b/README.md @@ -522,6 +522,19 @@ $ ./bb example.clj babashka doesn't support in-ns yet! ``` +## Running tests + +Babashka bundles `clojure.test`. To make CI scripts fail you can use a simple +runner like this: + +``` shell +#!/usr/bin/env bash +bb -cp "src:test:resources" \ + -e "(require '[clojure.test :as t] '[borkdude.deps-test]) + (let [{:keys [:fail :error]} (t/run-tests 'borkdude.deps-test)] + (System/exit (+ fail error)))" +``` + ## Socket REPL Start the socket REPL like this: From f454d3e36df6ed9f2adb668c5ea9588ecff5a42a Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 09:14:58 +0100 Subject: [PATCH 086/152] Update FUNDING.yml --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index e45310df..c7fbd994 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,6 +1,6 @@ # These are supported funding model platforms -github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +github: borkdude # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] patreon: borkdude open_collective: # Replace with a single Open Collective username ko_fi: borkdude From f652aa3f177d49dda04cfe72ac2313b6b9bf9fe4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:15:43 +0100 Subject: [PATCH 087/152] Fix uberscript --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index a82e2023..41ea00aa 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit a82e20235c65c4a1ecc50885597e1df8e334d113 +Subproject commit 41ea00aa575e2144a35f6908aa082218885c0508 From c8d9c1ee5f34f750c7e5bf33051b9d910df352e2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:19:30 +0100 Subject: [PATCH 088/152] Fix uberscript --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 41ea00aa..bff25c5f 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 41ea00aa575e2144a35f6908aa082218885c0508 +Subproject commit bff25c5f4a709dfa36003c29b26e5fe46169e202 From 6abff1dbdd575d0e9e6a010d3358c5921c59e034 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:23:22 +0100 Subject: [PATCH 089/152] [#250] implement -- option --- src/babashka/main.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 2af63fd6..dc6751f0 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -43,6 +43,7 @@ (if options (let [opt (first options)] (case opt + ("--") (assoc opts-map :command-line-args (next options)) ("--version") {:version true} ("--help" "-h" "-?") {:help? true} ("--verbose")(recur (next options) From ecf3caa251df1e4a53310a1b8ba4edff56e30db9 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:29:13 +0100 Subject: [PATCH 090/152] Test for uberscript --- test/babashka/classpath_test.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/babashka/classpath_test.clj b/test/babashka/classpath_test.clj index 95fcc56b..733cf2e0 100644 --- a/test/babashka/classpath_test.clj +++ b/test/babashka/classpath_test.clj @@ -33,7 +33,7 @@ (deftest uberscript-test (let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")] (.deleteOnExit tmp-file) - (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "--uberscript" (.getPath tmp-file)) + (is (empty? (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main" "--uberscript" (.getPath tmp-file)))) (is (= "(\"1\" \"2\" \"3\" \"4\")\n" (tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))))) From f27c7063401bd6a16f5159cfb6e7dfad47d00b9d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:33:01 +0100 Subject: [PATCH 091/152] [#250] test --- test/babashka/main_test.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 5323610c..66c4a961 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -20,7 +20,8 @@ (is (= {:result 8080} (bb nil "test/babashka/scripts/tools.cli.bb"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "foo.clj"))) (is (thrown-with-msg? Exception #"does not exist" (bb nil "-help")))) - (is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])")))) + (is (= "1 2 3" (bb nil "-e" "(require '[clojure.string :as str1])" "-e" "(str1/join \" \" [1 2 3])"))) + (is (= '("-e" "1") (bb nil "-e" "*command-line-args*" "--" "-e" "1")))) (deftest print-error-test (is (thrown-with-msg? Exception #"java.lang.NullPointerException" From b931b98f4c4edd4dbd38f543e8701a5bac0739e0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 17:34:39 +0100 Subject: [PATCH 092/152] Doc --- README.md | 1 + src/babashka/main.clj | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 9e7cacaa..08c6cda9 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ Options: --repl Start REPL --socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --time Print execution time before exiting. + -- Stop parsing args and pass everything after -- to *command-line-args* If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. Everything after that is bound to *command-line-args*. diff --git a/src/babashka/main.clj b/src/babashka/main.clj index dc6751f0..8e5430b0 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -181,6 +181,7 @@ --repl Start REPL --socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --time Print execution time before exiting. + -- Stop parsing args and pass everything after -- to *command-line-args* If neither -e, -f, or --socket-repl are specified, then the first argument that is not parsed as a option is treated as a file if it exists, or as an expression otherwise. Everything after that is bound to *command-line-args*.")) From 64ba1a5afbac2412a2dbe4783d8b4d0f4a485de4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 21:01:49 +0100 Subject: [PATCH 093/152] v0.0.68 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 60b7753c..c2f75a98 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.68-SNAPSHOT \ No newline at end of file +0.0.68 \ No newline at end of file From e75475506c733e9ef728161b71c8b014849bba02 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 21:10:21 +0100 Subject: [PATCH 094/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index c21ada26..c2f75a98 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.67 \ No newline at end of file +0.0.68 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index c2f75a98..d3b5b8d8 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.68 \ No newline at end of file +0.0.69-SNAPSHOT \ No newline at end of file From 634084e5921e51426f9eb3b892c433340b7bdecb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 31 Jan 2020 22:31:16 +0100 Subject: [PATCH 095/152] [#251] Test for uberscript with System/exit --- test/babashka/main_test.clj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 66c4a961..a4c2cb35 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -330,6 +330,12 @@ (testing "the clojure.lang.MapEntry constructor works" (is (true? (bb nil "(= (first {1 2}) (clojure.lang.MapEntry. 1 2))"))))) +(deftest uberscript-test + (let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")] + (.deleteOnExit tmp-file) + (is (empty? (bb nil "--uberscript" (.getPath tmp-file) "-e" "(System/exit 1)"))) + (is (= "(System/exit 1)" (slurp tmp-file))))) + ;;;; Scratch (comment From 280a2a00164e6843c019370d5c4720603b22b0a2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 2 Feb 2020 13:14:29 +0100 Subject: [PATCH 096/152] README --- README.md | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 08c6cda9..2ac817d6 100644 --- a/README.md +++ b/README.md @@ -785,14 +785,28 @@ bb '(-> *input* first :name (subs 1))' "0.0.4" ``` -### Get latest OS-specific download url from Github +### Generate deps.edn entry for a gitlib -``` shellsession -$ curl -s https://api.github.com/repos/borkdude/babashka/releases | -jet --from json --keywordize | -bb '(-> *input* first :assets)' | -bb '(some #(re-find #".*linux.*" (:browser_download_url %)) *input*)' -"https://github.com/borkdude/babashka/releases/download/v0.0.4/babashka-0.0.4-linux-amd64.zip" +``` clojure +#!/usr/bin/env bb + +(require '[clojure.java.shell :refer [sh]] + '[clojure.string :as str]) + +(let [[username project branch] *command-line-args* + branch (or branch "master") + url (str "https://github.com/" username "/" project) + sha (-> (sh "git" "ls-remote" url branch) + :out + (str/split #"\s") + first)] + {:git/url url + :sha sha}) +``` + +``` shell +$ gitlib.clj nate fs +{:git/url "https://github.com/nate/fs", :sha "75b9fcd399ac37cb4f9752a4c7a6755f3fbbc000"} ``` ### View download statistics from Clojars From f124a8a2b11570aa69fa96f25cb3bdd440a6c72d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 2 Feb 2020 13:29:50 +0100 Subject: [PATCH 097/152] README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 2ac817d6..86316b34 100644 --- a/README.md +++ b/README.md @@ -807,6 +807,8 @@ bb '(-> *input* first :name (subs 1))' ``` shell $ gitlib.clj nate fs {:git/url "https://github.com/nate/fs", :sha "75b9fcd399ac37cb4f9752a4c7a6755f3fbbc000"} +$ clj -Sdeps "{:deps {fs $(gitlib.clj nate fs)}}" -e "(require '[nate.fs :as fs]) (fs/creation-time \".\")" +#object[java.nio.file.attribute.FileTime 0x5c748168 "2019-07-05T14:06:26Z"] ``` ### View download statistics from Clojars From a87fc718216adc271b8bbfddd5cc33c966435430 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 2 Feb 2020 13:33:26 +0100 Subject: [PATCH 098/152] README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 86316b34..e5cc7cd8 100644 --- a/README.md +++ b/README.md @@ -807,7 +807,8 @@ bb '(-> *input* first :name (subs 1))' ``` shell $ gitlib.clj nate fs {:git/url "https://github.com/nate/fs", :sha "75b9fcd399ac37cb4f9752a4c7a6755f3fbbc000"} -$ clj -Sdeps "{:deps {fs $(gitlib.clj nate fs)}}" -e "(require '[nate.fs :as fs]) (fs/creation-time \".\")" +$ clj -Sdeps "{:deps {fs $(gitlib.clj nate fs)}}" \ + -e "(require '[nate.fs :as fs]) (fs/creation-time \".\")" #object[java.nio.file.attribute.FileTime 0x5c748168 "2019-07-05T14:06:26Z"] ``` From 00670bfafe6510a6abccc50a33e59e9eb54e44b0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 2 Feb 2020 13:44:34 +0100 Subject: [PATCH 099/152] Windows --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index e5cc7cd8..37112faf 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,11 @@ Upgrade: yay -S babashka-bin +### Windows + +On Windows you can install using [scoop](https://scoop.sh/) and the +[scoop-clojure](https://github.com/littleli/scoop-clojure) bucket. + ### Installer script Install via the installer script: From 1224919bd19033386c732bdfd93f4b012e9da9c3 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 2 Feb 2020 18:24:58 +0100 Subject: [PATCH 100/152] sci: now includes clojure.edn --- sci | 2 +- src/babashka/main.clj | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/sci b/sci index bff25c5f..e31ed647 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit bff25c5f4a709dfa36003c29b26e5fe46169e202 +Subproject commit e31ed647874237fbfd5c766ef3d0e9d26a634165 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 8e5430b0..fb2ac116 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -246,8 +246,6 @@ Everything after that is bound to *command-line-args*.")) (def namespaces {'clojure.tools.cli tools-cli-namespace - 'clojure.edn {'read edn/read - 'read-string edn/read-string} 'clojure.java.shell shell-namespace 'babashka.wait {'wait-for-port wait/wait-for-port 'wait-for-path wait/wait-for-path} From 1d3eaae943d0ca6d40e8cd7243fa8e788848ed77 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 11:01:11 +0100 Subject: [PATCH 101/152] sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index e31ed647..81552362 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit e31ed647874237fbfd5c766ef3d0e9d26a634165 +Subproject commit 81552362542e790386d90f990d9dca5f118509ba From ba3eac83b15270f5190087aa3638931015292438 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 11:09:29 +0100 Subject: [PATCH 102/152] Fix preloads --- src/babashka/main.clj | 295 +++++++++++++++++++++--------------------- 1 file changed, 148 insertions(+), 147 deletions(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index fb2ac116..d8a623cc 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -282,153 +282,154 @@ Everything after that is bound to *command-line-args*.")) (handle-pipe!) #_(binding [*out* *err*] (prn "M" (meta (get bindings 'future)))) - (let [t0 (System/currentTimeMillis) - {:keys [:version :shell-in :edn-in :shell-out :edn-out - :help? :file :command-line-args - :expressions :stream? :time? - :repl :socket-repl - :verbose? :classpath - :main :uberscript] :as _opts} - (parse-opts args) - read-next (fn [*in*] - (if (pipe-signal-received?) - ::EOF - (if stream? - (if shell-in (or (read-line) ::EOF) - (read-edn)) - (delay (cond shell-in - (shell-seq *in*) - edn-in - (edn-seq *in*) - :else - (edn/read *in*)))))) - uberscript-sources (atom ()) - env (atom {}) - classpath (or classpath - (System/getenv "BABASHKA_CLASSPATH")) - _ (when classpath - (add-classpath* classpath)) - load-fn (fn [{:keys [:namespace]}] - (when-let [{:keys [:loader]} @cp-state] - (let [res (cp/source-for-namespace loader namespace nil)] - (when uberscript (swap! uberscript-sources conj (:source res))) - res))) - _ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file)))) - ctx {:aliases aliases - :namespaces (-> namespaces - (assoc 'clojure.core - (assoc core-extras - '*command-line-args* - (sci/new-dynamic-var '*command-line-args* command-line-args) - '*warn-on-reflection* reflection-var)) - (assoc-in ['clojure.java.io 'resource] - #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) - :bindings bindings - :env env - :features #{:bb} - :classes classes/class-map - :imports '{ArithmeticException java.lang.ArithmeticException - AssertionError java.lang.AssertionError - Boolean java.lang.Boolean - Class java.lang.Class - Double java.lang.Double - Exception java.lang.Exception - IllegalArgumentException java.lang.IllegalArgumentException - Integer java.lang.Integer - File java.io.File - Math java.lang.Math - Object java.lang.Object - ProcessBuilder java.lang.ProcessBuilder - String java.lang.String - StringBuilder java.lang.StringBuilder - System java.lang.System - Thread java.lang.Thread - Throwable java.lang.Throwable} - :load-fn load-fn - :dry-run uberscript} - ctx (addons/future ctx) - sci-ctx (sci-opts/init ctx) - _ (vreset! common/ctx sci-ctx) - _ (swap! (:env sci-ctx) - (fn [env] - (update-in env [:namespaces 'clojure.core] assoc - 'eval #(eval* sci-ctx %) - 'load-file #(load-file* sci-ctx %)))) - _ (swap! (:env sci-ctx) - (fn [env] - (assoc-in env [:namespaces 'clojure.main 'repl] - (fn [& opts] - (let [opts (apply hash-map opts)] - (repl/start-repl! sci-ctx opts)))))) - preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) - [expressions exit-code] - (cond expressions [expressions nil] - main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" - main)] nil] - file (try [[(read-file file)] nil] - (catch Exception e - (error-handler* e verbose?)))) - expression (str/join " " expressions) ;; this might mess with the locations... - exit-code - ;; handle preloads - (if exit-code exit-code - (do (when preloads (try (eval-string* sci-ctx preloads) - (catch Throwable e - (error-handler* e verbose?)))) - nil)) - exit-code - (or exit-code - (sci/with-bindings {reflection-var false - vars/current-ns (vars/->SciNamespace 'user)} - (or - (second - (cond version - [(print-version) 0] - help? - [(print-help) 0] - repl [(repl/start-repl! sci-ctx) 0] - socket-repl [(start-socket-repl! socket-repl sci-ctx) 0] - (not (str/blank? expression)) - (try - (loop [in (read-next *in*)] - (let [_ (swap! env update-in [:namespaces 'user] - assoc (with-meta '*input* - (when-not stream? - {:sci.impl/deref! true})) - (sci/new-dynamic-var '*input* in))] - (if (identical? ::EOF in) - [nil 0] ;; done streaming - (let [res [(let [res (eval-string* sci-ctx expression)] - (when (some? res) - (if-let [pr-f (cond shell-out println - edn-out prn)] - (if (coll? res) - (doseq [l res - :while (not (pipe-signal-received?))] - (pr-f l)) - (pr-f res)) - (prn res)))) 0]] - (if stream? - (recur (read-next *in*)) - res))))) - (catch Throwable e - (error-handler* e verbose?))) - uberscript [nil 0] - :else [(repl/start-repl! sci-ctx) 0])) - 1))) - t1 (System/currentTimeMillis)] - (flush) - (when uberscript - uberscript - (let [uberscript-out uberscript] - (spit uberscript-out "") ;; reset file - (doseq [s @uberscript-sources] - (spit uberscript-out s :append true)) - (spit uberscript-out preloads :append true) - (spit uberscript-out expression :append true))) - (when time? (binding [*out* *err*] - (println "bb took" (str (- t1 t0) "ms.")))) - exit-code)) + (sci/with-bindings {reflection-var false + vars/current-ns (vars/->SciNamespace 'user)} + (let [t0 (System/currentTimeMillis) + {:keys [:version :shell-in :edn-in :shell-out :edn-out + :help? :file :command-line-args + :expressions :stream? :time? + :repl :socket-repl + :verbose? :classpath + :main :uberscript] :as _opts} + (parse-opts args) + read-next (fn [*in*] + (if (pipe-signal-received?) + ::EOF + (if stream? + (if shell-in (or (read-line) ::EOF) + (read-edn)) + (delay (cond shell-in + (shell-seq *in*) + edn-in + (edn-seq *in*) + :else + (edn/read *in*)))))) + uberscript-sources (atom ()) + env (atom {}) + classpath (or classpath + (System/getenv "BABASHKA_CLASSPATH")) + _ (when classpath + (add-classpath* classpath)) + load-fn (fn [{:keys [:namespace]}] + (when-let [{:keys [:loader]} @cp-state] + (let [res (cp/source-for-namespace loader namespace nil)] + (when uberscript (swap! uberscript-sources conj (:source res))) + res))) + _ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file)))) + ctx {:aliases aliases + :namespaces (-> namespaces + (assoc 'clojure.core + (assoc core-extras + '*command-line-args* + (sci/new-dynamic-var '*command-line-args* command-line-args) + '*warn-on-reflection* reflection-var)) + (assoc-in ['clojure.java.io 'resource] + #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) + :bindings bindings + :env env + :features #{:bb} + :classes classes/class-map + :imports '{ArithmeticException java.lang.ArithmeticException + AssertionError java.lang.AssertionError + Boolean java.lang.Boolean + Class java.lang.Class + Double java.lang.Double + Exception java.lang.Exception + IllegalArgumentException java.lang.IllegalArgumentException + Integer java.lang.Integer + File java.io.File + Math java.lang.Math + Object java.lang.Object + ProcessBuilder java.lang.ProcessBuilder + String java.lang.String + StringBuilder java.lang.StringBuilder + System java.lang.System + Thread java.lang.Thread + Throwable java.lang.Throwable} + :load-fn load-fn + :dry-run uberscript} + ctx (addons/future ctx) + sci-ctx (sci-opts/init ctx) + _ (vreset! common/ctx sci-ctx) + _ (swap! (:env sci-ctx) + (fn [env] + (update-in env [:namespaces 'clojure.core] assoc + 'eval #(eval* sci-ctx %) + 'load-file #(load-file* sci-ctx %)))) + _ (swap! (:env sci-ctx) + (fn [env] + (assoc-in env [:namespaces 'clojure.main 'repl] + (fn [& opts] + (let [opts (apply hash-map opts)] + (repl/start-repl! sci-ctx opts)))))) + preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) + [expressions exit-code] + (cond expressions [expressions nil] + main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" + main)] nil] + file (try [[(read-file file)] nil] + (catch Exception e + (error-handler* e verbose?)))) + expression (str/join " " expressions) ;; this might mess with the locations... + exit-code + ;; handle preloads + (if exit-code exit-code + (do (when preloads + (try + (eval-string* sci-ctx preloads) + (catch Throwable e + (error-handler* e verbose?)))) + nil)) + exit-code + (or exit-code + (second + (cond version + [(print-version) 0] + help? + [(print-help) 0] + repl [(repl/start-repl! sci-ctx) 0] + socket-repl [(start-socket-repl! socket-repl sci-ctx) 0] + (not (str/blank? expression)) + (try + (loop [in (read-next *in*)] + (let [_ (swap! env update-in [:namespaces 'user] + assoc (with-meta '*input* + (when-not stream? + {:sci.impl/deref! true})) + (sci/new-dynamic-var '*input* in))] + (if (identical? ::EOF in) + [nil 0] ;; done streaming + (let [res [(let [res (eval-string* sci-ctx expression)] + (when (some? res) + (if-let [pr-f (cond shell-out println + edn-out prn)] + (if (coll? res) + (doseq [l res + :while (not (pipe-signal-received?))] + (pr-f l)) + (pr-f res)) + (prn res)))) 0]] + (if stream? + (recur (read-next *in*)) + res))))) + (catch Throwable e + (error-handler* e verbose?))) + uberscript [nil 0] + :else [(repl/start-repl! sci-ctx) 0])) + 1) + t1 (System/currentTimeMillis)] + (flush) + (when uberscript + uberscript + (let [uberscript-out uberscript] + (spit uberscript-out "") ;; reset file + (doseq [s @uberscript-sources] + (spit uberscript-out s :append true)) + (spit uberscript-out preloads :append true) + (spit uberscript-out expression :append true))) + (when time? (binding [*out* *err*] + (println "bb took" (str (- t1 t0) "ms.")))) + exit-code))) (defn -main [& args] From 300c72e43e58fef76cd68e480ba80a4d11d62755 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 11:29:43 +0100 Subject: [PATCH 103/152] v0.0.69 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index d3b5b8d8..652ab34e 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.69-SNAPSHOT \ No newline at end of file +0.0.69 \ No newline at end of file From b38e73d33cd2a52392f668620b87d2fc295e3854 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 11:42:18 +0100 Subject: [PATCH 104/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index c2f75a98..652ab34e 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.68 \ No newline at end of file +0.0.69 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 652ab34e..8d2b133c 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.69 \ No newline at end of file +0.0.70-SNAPSHOT \ No newline at end of file From f588319ac16af54289cb7a996391e38b3f98610c Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 12:32:40 +0100 Subject: [PATCH 105/152] Sci: add satisfies\? --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 81552362..5d05c75d 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 81552362542e790386d90f990d9dca5f118509ba +Subproject commit 5d05c75d433b0fbaf399eaee50c8b98dd902fd56 From d3f1b051022e1979c9c25944c4374aa8f38622f0 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 12:42:05 +0100 Subject: [PATCH 106/152] Make core.async.impl.protocols namespace reachable from within bb --- src/babashka/impl/async.clj | 5 ++++- src/babashka/main.clj | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/babashka/impl/async.clj b/src/babashka/impl/async.clj index 61022448..9cfa421a 100644 --- a/src/babashka/impl/async.clj +++ b/src/babashka/impl/async.clj @@ -1,6 +1,7 @@ (ns babashka.impl.async {:no-doc true} - (:require [clojure.core.async :as async])) + (:require [clojure.core.async :as async] + [clojure.core.async.impl.protocols :as protocols])) (defn thread [_ _ & body] @@ -66,3 +67,5 @@ 'untap async/untap 'untap-all async/untap-all}) +(def async-protocols-namespace + {'ReadPort protocols/ReadPort}) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index d8a623cc..eca08e65 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -1,7 +1,7 @@ (ns babashka.main {:no-doc true} (:require - [babashka.impl.async :refer [async-namespace]] + [babashka.impl.async :refer [async-namespace async-protocols-namespace]] [babashka.impl.cheshire :refer [cheshire-core-namespace]] [babashka.impl.classes :as classes] [babashka.impl.classpath :as cp] @@ -252,6 +252,7 @@ Everything after that is bound to *command-line-args*.")) 'babashka.signal {'pipe-signal-received? pipe-signal-received?} 'clojure.java.io io-namespace 'clojure.core.async async-namespace + 'clojure.core.async.impl.protocols async-protocols-namespace 'clojure.data.csv csv/csv-namespace 'cheshire.core cheshire-core-namespace 'clojure.stacktrace stacktrace-namespace From d5ff6f73087413983941d5fb8b8e777e99a4da78 Mon Sep 17 00:00:00 2001 From: Stijn Opheide Date: Tue, 4 Feb 2020 22:07:11 +0100 Subject: [PATCH 107/152] add java.lang.Long to classes (#253) --- src/babashka/impl/classes.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 04499cf9..d63cf2da 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -32,6 +32,7 @@ java.lang.Double java.lang.Exception java.lang.Integer + java.lang.Long java.lang.Math java.util.concurrent.LinkedBlockingQueue java.lang.Object From 1a9dc12c3806b3218afa5c636697b2afb0b1e90b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 22:15:14 +0100 Subject: [PATCH 108/152] Add java.lang.Long to imports --- src/babashka/main.clj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index eca08e65..8b5cf180 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -339,6 +339,7 @@ Everything after that is bound to *command-line-args*.")) IllegalArgumentException java.lang.IllegalArgumentException Integer java.lang.Integer File java.io.File + Long java.io.Long Math java.lang.Math Object java.lang.Object ProcessBuilder java.lang.ProcessBuilder From debd180681a6c78c663225fd7c857e0976608500 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 4 Feb 2020 22:15:59 +0100 Subject: [PATCH 109/152] Fix previous commit --- src/babashka/main.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 8b5cf180..603862db 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -339,7 +339,7 @@ Everything after that is bound to *command-line-args*.")) IllegalArgumentException java.lang.IllegalArgumentException Integer java.lang.Integer File java.io.File - Long java.io.Long + Long java.lang.Long Math java.lang.Math Object java.lang.Object ProcessBuilder java.lang.ProcessBuilder From 6f07f3af743cc17c92c890fe4f038ebdc00bc3a2 Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Thu, 6 Feb 2020 17:03:32 -0500 Subject: [PATCH 110/152] Fix typo (#256) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 37112faf..bf82cabc 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,7 @@ Contains the function `signal/pipe-signal-received?`. Usage: Returns true if `PIPE` signal was received. Example: ``` shellsession -$ bb '((fn [x] (println x) (when (not (sig/pipe-signal-received?)) (recur (inc x)))) 0)' | head -n2 +$ bb '((fn [x] (println x) (when (not (signal/pipe-signal-received?)) (recur (inc x)))) 0)' | head -n2 1 2 ``` From 3240ecddc629f68ddd7ced9e506a072b9ae87f34 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 7 Feb 2020 09:27:29 +0100 Subject: [PATCH 111/152] sci --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 5d05c75d..dab6b495 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 5d05c75d433b0fbaf399eaee50c8b98dd902fd56 +Subproject commit dab6b495087f2fbf49c50051cc90a439c4980fc0 From 01714e4bbb840dbbe5d1b5adb586256604e6157a Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 7 Feb 2020 09:33:02 +0100 Subject: [PATCH 112/152] Add note about rlwrap --- README.md | 18 +++++++++++++++--- src/babashka/main.clj | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bf82cabc..a1ab8e5d 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ Options: -f, --file Evaluate a file. -cp, --classpath Classpath to use. -m, --main Call the -main function from namespace with args. - --repl Start REPL + --repl Start REPL. Use rlwrap for history. --socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --time Print execution time before exiting. -- Stop parsing args and pass everything after -- to *command-line-args* @@ -541,9 +541,21 @@ bb -cp "src:test:resources" \ (System/exit (+ fail error)))" ``` -## Socket REPL +## REPL -Start the socket REPL like this: +Babashka supports both a REPL and socket REPL. To start the REPL, type: + +``` shell +$ bb --repl +``` + +To get history with up and down arrows, use `rlwrap`: + +``` shell +$ rlwrap bb --repl +``` + +To start the socket REPL you can do this: ``` shellsession $ bb --socket-repl 1666 diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 603862db..9f49f98c 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -178,7 +178,7 @@ -f, --file Evaluate a file. -cp, --classpath Classpath to use. -m, --main Call the -main function from namespace with args. - --repl Start REPL + --repl Start REPL. Use rlwrap for history. --socket-repl Start socket REPL. Specify port (e.g. 1666) or host and port separated by colon (e.g. 127.0.0.1:1666). --time Print execution time before exiting. -- Stop parsing args and pass everything after -- to *command-line-args* From cfc1b909a3f9a912b10c8d779d5b72c31b218f73 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 7 Feb 2020 20:48:13 +0100 Subject: [PATCH 113/152] Update dev.md --- doc/dev.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dev.md b/doc/dev.md index 6c3d4655..ec86deb0 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -43,7 +43,7 @@ Test the native version: ## Build -To build this project, set `$GRAALVM_HOME` to the GraalVM distribution directory. +To build this project, set `$GRAALVM_HOME` to the GraalVM distribution directory. Currently we are using GraalVM JDK8. Then run: From 9361fd67972cc37c566d34bc3268e2a3aeeffa23 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 8 Feb 2020 23:30:42 +0100 Subject: [PATCH 114/152] sci: promise and deliver --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index dab6b495..f5dce9b2 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit dab6b495087f2fbf49c50051cc90a439c4980fc0 +Subproject commit f5dce9b20e5f71f98df10cbd6cec971ac4d93f90 From 50f0630b38fc8f422a3945f3c9bda056f7e8b479 Mon Sep 17 00:00:00 2001 From: Jeroen van Dijk Date: Sat, 8 Feb 2020 23:43:15 +0100 Subject: [PATCH 115/152] Add promise and deliver (#260) --- test/babashka/main_test.clj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index a4c2cb35..6b2f2220 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -160,6 +160,12 @@ (deftest future-test (is (= 6 (bb nil "@(future (+ 1 2 3))")))) + +(deftest promise-test + (is (= :timeout (bb nil "(deref (promise) 1 :timeout)"))) + (is (= :ok (bb nil "(let [x (promise)] + (deliver x :ok) + @x)")))) (deftest process-builder-test (is (str/includes? (bb nil " From 08ac36ebe1d9cd1e21b6791b8a8af141845d0403 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 18:35:13 +0100 Subject: [PATCH 116/152] sci: add clojure.core/prefers --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index f5dce9b2..c1752524 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit f5dce9b20e5f71f98df10cbd6cec971ac4d93f90 +Subproject commit c1752524724e6e9e47b0c44fd7b77c831ceff740 From c8fd1c7931d7842ebaec1fa8faf06d4ab58573bd Mon Sep 17 00:00:00 2001 From: Jeroen van Dijk Date: Sun, 9 Feb 2020 20:45:29 +0100 Subject: [PATCH 117/152] Add Security classes in order to be able to generate signatures (#261) --- doc/dev.md | 3 +++ src/babashka/impl/classes.clj | 6 +++++- test/babashka/java_security_test.clj | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/babashka/java_security_test.clj diff --git a/doc/dev.md b/doc/dev.md index ec86deb0..72e4b6bf 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -58,6 +58,9 @@ Keep notes here about how adding libraries and classes to Babashka affects the b +2020/02/09: added java.lang.BigInteger and java.security.MessageDigest. +39281972 - 39072764 = 209kb added. + 2020/01/24, 43eef7075f9dac038d8d28a5ee4e49b6affd9864: 38.3mb, 11.1mb zipped Added hierarchies (derive, isa?, etc). diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index d63cf2da..e0451f08 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -43,6 +43,7 @@ java.lang.Process java.lang.ProcessBuilder java.lang.ProcessBuilder$Redirect + java.math.BigInteger java.net.URI java.net.HttpURLConnection java.net.ServerSocket @@ -62,6 +63,7 @@ java.nio.file.attribute.FileTime java.nio.file.attribute.PosixFilePermission java.nio.file.attribute.PosixFilePermissions + java.security.MessageDigest java.time.format.DateTimeFormatter java.time.Clock java.time.DateTimeException @@ -189,7 +191,9 @@ java.lang.Process ;; added for issue #239 regarding clj-http-lite (instance? java.io.ByteArrayOutputStream v) - java.io.ByteArrayOutputStream))))) + java.io.ByteArrayOutputStream + (instance? java.security.MessageDigest v) + java.security.MessageDigest))))) (def class-map (gen-class-map)) diff --git a/test/babashka/java_security_test.clj b/test/babashka/java_security_test.clj new file mode 100644 index 00000000..d619a4b2 --- /dev/null +++ b/test/babashka/java_security_test.clj @@ -0,0 +1,20 @@ +(ns babashka.java-security-test + (:require + [babashka.test-utils :as test-utils] + [clojure.edn :as edn] + [clojure.test :as test :refer [deftest is]])) + +(defn bb [expr] + (edn/read-string (apply test-utils/bb nil [(str expr)]))) + +(defn signature [algo] + (clojure.walk/postwalk-replace {::algo algo} + '(defn signature [^String s] + (let [algorithm (java.security.MessageDigest/getInstance ::algo) + digest (.digest algorithm (.getBytes s))] + (format "%032x" (java.math.BigInteger. 1 digest)))))) + +(deftest java-security-test + (is (= "49f68a5c8493ec2c0bf489821c21fc3b" (bb (list 'do (signature "MD5") '(signature "hi"))))) + (is (= "c22b5f9178342609428d6f51b2c5af4c0bde6a42" (bb (list 'do (signature "SHA-1") '(signature "hi"))))) + (is (= "8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4" (bb (list 'do (signature "SHA-256") '(signature "hi")))))) From 794438b7c1062bb9d262e4477792e512871f9da1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 20:49:55 +0100 Subject: [PATCH 118/152] Binary size notes --- doc/dev.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/dev.md b/doc/dev.md index 72e4b6bf..0545a29f 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -58,9 +58,12 @@ Keep notes here about how adding libraries and classes to Babashka affects the b -2020/02/09: added java.lang.BigInteger and java.security.MessageDigest. +2020/02/09, c8fd1c7931d7842ebaec1fa8faf06d4ab58573bd +Added java.lang.BigInteger and java.security.MessageDigest. 39281972 - 39072764 = 209kb added. +2020/04/02 v0.0.69 38883676 + 2020/01/24, 43eef7075f9dac038d8d28a5ee4e49b6affd9864: 38.3mb, 11.1mb zipped Added hierarchies (derive, isa?, etc). From 148e3c922966d0e27ce632278d2d5d6819d6638b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 22:55:12 +0100 Subject: [PATCH 119/152] Docker --- .circleci/config.yml | 35 +++++++++++++++++------------------ .circleci/script/docker | 6 ++++-- Dockerfile | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 Dockerfile diff --git a/.circleci/config.yml b/.circleci/config.yml index c25a8ea2..8fb40122 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -227,16 +227,15 @@ jobs: paths: - ~/.m2 key: v1-dependencies-{{ checksum "project.clj" }} - # docker: - # docker: - # - image: circleci/buildpack-deps:stretch - # steps: - # - checkout - # - setup_remote_docker: - # docker_layer_caching: true - # - run: - # name: Build Docker image - # command: .circleci/script/docker + docker: + docker: + - image: circleci/buildpack-deps:stretch + steps: + - checkout + - setup_remote_docker: + - run: + name: Build Docker image + command: .circleci/script/docker workflows: version: 2 @@ -253,11 +252,11 @@ workflows: - jvm - linux - mac - # - docker: - # filters: - # branches: - # only: master - # requires: - # - jvm - # - linux - # - mac + - docker: + filters: + branches: + only: master + requires: + - jvm + - linux + - mac diff --git a/.circleci/script/docker b/.circleci/script/docker index 69384b31..d2bda10e 100755 --- a/.circleci/script/docker +++ b/.circleci/script/docker @@ -1,7 +1,9 @@ #!/usr/bin/env bash -image_name="borkdude/clj-kondo" -image_tag=$(cat resources/CLJ_KONDO_VERSION) +set -eo pipefail + +image_name="borkdude/babashka" +image_tag=$(cat resources/BABASHKA_VERSION) latest_tag="latest" if [[ $image_tag =~ SNAPSHOT$ ]]; then diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..bbb7c244 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM ubuntu AS BASE + +RUN apt-get update +RUN apt-get install -yy curl unzip build-essential zlib1g-dev +WORKDIR "/opt" +RUN curl -sLO https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.1/graalvm-ce-java8-linux-amd64-19.3.1.tar.gz +RUN tar -xzf graalvm-ce-java8-linux-amd64-19.3.1.tar.gz +ENV GRAALVM_HOME="/opt/graalvm-ce-19.3.1" +ENV JAVA_HOME="/opt/graalvm-ce-19.3.1/bin" +ENV PATH="$PATH:$JAVA_HOME" +COPY . . +RUN apt install -y sudo +RUN ./.circleci/script/install-leiningen +RUN ./script/compile +RUN cp bb /usr/local/bin + + +FROM alpine:3.9 + +# See https://github.com/sgerrand/alpine-pkg-glibc +RUN apk --no-cache add ca-certificates curl +RUN curl -o /etc/apk/keys/sgerrand.rsa.pub -sL https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub +RUN curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk +RUN apk add glibc-2.29-r0.apk +COPY --from=BASE /usr/local/bin/clj-kondo /usr/local/bin +ENV LD_LIBRARY_PATH /lib +CMD ["bb"] From 1d6987c8676af1261620c980cdb467392e01bf02 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 22:56:58 +0100 Subject: [PATCH 120/152] Docker: fix java path --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index bbb7c244..7afd2d6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,8 @@ RUN apt-get install -yy curl unzip build-essential zlib1g-dev WORKDIR "/opt" RUN curl -sLO https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-19.3.1/graalvm-ce-java8-linux-amd64-19.3.1.tar.gz RUN tar -xzf graalvm-ce-java8-linux-amd64-19.3.1.tar.gz -ENV GRAALVM_HOME="/opt/graalvm-ce-19.3.1" -ENV JAVA_HOME="/opt/graalvm-ce-19.3.1/bin" +ENV GRAALVM_HOME="/opt/graalvm-ce-java8-19.3.1" +ENV JAVA_HOME="/opt/graalvm-ce-java8-19.3.1/bin" ENV PATH="$PATH:$JAVA_HOME" COPY . . RUN apt install -y sudo From c598c4b4a8bb9c5f2aac2c6d100d10717b088c28 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 22:58:32 +0100 Subject: [PATCH 121/152] Fix circleci config --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 8fb40122..54579647 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -232,7 +232,7 @@ jobs: - image: circleci/buildpack-deps:stretch steps: - checkout - - setup_remote_docker: + - setup_remote_docker - run: name: Build Docker image command: .circleci/script/docker From 804a7d10c4c78fa578af85a547c6fbe8ba37763f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 23:07:14 +0100 Subject: [PATCH 122/152] Fix path in Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7afd2d6e..e2c96de9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,6 @@ RUN apk --no-cache add ca-certificates curl RUN curl -o /etc/apk/keys/sgerrand.rsa.pub -sL https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub RUN curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk RUN apk add glibc-2.29-r0.apk -COPY --from=BASE /usr/local/bin/clj-kondo /usr/local/bin +COPY --from=BASE /usr/local/bin/bb /usr/local/bin ENV LD_LIBRARY_PATH /lib CMD ["bb"] From f2084746b02fe96c4f1c06d20596738091ad09ae Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 23:08:46 +0100 Subject: [PATCH 123/152] Checkout recursively in docker step --- .circleci/config.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 54579647..cc4f3b34 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -232,6 +232,11 @@ jobs: - image: circleci/buildpack-deps:stretch steps: - checkout + - run: + name: "Pull Submodules" + command: | + git submodule init + git submodule update - setup_remote_docker - run: name: Build Docker image From ffb92bc8db83225f346f5cec36c4f843993a7c5e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 23:28:18 +0100 Subject: [PATCH 124/152] Docker --- Dockerfile | 2 +- script/compile | 61 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index e2c96de9..d62d3154 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ ENV PATH="$PATH:$JAVA_HOME" COPY . . RUN apt install -y sudo RUN ./.circleci/script/install-leiningen -RUN ./script/compile +RUN GRAALVM_STATIC=true ./script/compile RUN cp bb /usr/local/bin diff --git a/script/compile b/script/compile index fbb59dc7..eacd71e8 100755 --- a/script/compile +++ b/script/compile @@ -26,24 +26,47 @@ export JAVA_HOME=$GRAALVM_HOME lein with-profiles +reflection do run lein do clean, uberjar -$GRAALVM_HOME/bin/native-image \ - -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ - -H:Name=bb \ - -H:+ReportExceptionStackTraces \ - -J-Dclojure.spec.skip-macros=true \ - -J-Dclojure.compiler.direct-linking=true \ - "-H:IncludeResources=BABASHKA_VERSION" \ - "-H:IncludeResources=SCI_VERSION" \ - -H:ReflectionConfigurationFiles=reflection.json \ - --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ - --initialize-at-build-time \ - -H:Log=registerResource: \ - -H:EnableURLProtocols=http,https \ - --enable-all-security-services \ - -H:+JNI \ - --verbose \ - --no-fallback \ - --no-server \ - "$BABASHKA_XMX" +if [ "$GRAALVM_STATIC" = "true" ]; then + $GRAALVM_HOME/bin/native-image \ + -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ + -H:Name=bb \ + -H:+ReportExceptionStackTraces \ + -J-Dclojure.spec.skip-macros=true \ + -J-Dclojure.compiler.direct-linking=true \ + "-H:IncludeResources=BABASHKA_VERSION" \ + "-H:IncludeResources=SCI_VERSION" \ + -H:ReflectionConfigurationFiles=reflection.json \ + --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ + --initialize-at-build-time \ + -H:Log=registerResource: \ + -H:EnableURLProtocols=http,https \ + --enable-all-security-services \ + -H:+JNI \ + --verbose \ + --no-fallback \ + --no-server \ + "$BABASHKA_XMX" \ + --static +else + $GRAALVM_HOME/bin/native-image \ + -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ + -H:Name=bb \ + -H:+ReportExceptionStackTraces \ + -J-Dclojure.spec.skip-macros=true \ + -J-Dclojure.compiler.direct-linking=true \ + "-H:IncludeResources=BABASHKA_VERSION" \ + "-H:IncludeResources=SCI_VERSION" \ + -H:ReflectionConfigurationFiles=reflection.json \ + --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ + --initialize-at-build-time \ + -H:Log=registerResource: \ + -H:EnableURLProtocols=http,https \ + --enable-all-security-services \ + -H:+JNI \ + --verbose \ + --no-fallback \ + --no-server \ + "$BABASHKA_XMX" +fi lein clean From 55d1d2faf8827b285a10a51dd48483fddd95d1e7 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 9 Feb 2020 23:37:01 +0100 Subject: [PATCH 125/152] Revert --- Dockerfile | 2 +- script/compile | 61 ++++++++++++++++---------------------------------- 2 files changed, 20 insertions(+), 43 deletions(-) diff --git a/Dockerfile b/Dockerfile index d62d3154..e2c96de9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,7 @@ ENV PATH="$PATH:$JAVA_HOME" COPY . . RUN apt install -y sudo RUN ./.circleci/script/install-leiningen -RUN GRAALVM_STATIC=true ./script/compile +RUN ./script/compile RUN cp bb /usr/local/bin diff --git a/script/compile b/script/compile index eacd71e8..fbb59dc7 100755 --- a/script/compile +++ b/script/compile @@ -26,47 +26,24 @@ export JAVA_HOME=$GRAALVM_HOME lein with-profiles +reflection do run lein do clean, uberjar -if [ "$GRAALVM_STATIC" = "true" ]; then - $GRAALVM_HOME/bin/native-image \ - -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ - -H:Name=bb \ - -H:+ReportExceptionStackTraces \ - -J-Dclojure.spec.skip-macros=true \ - -J-Dclojure.compiler.direct-linking=true \ - "-H:IncludeResources=BABASHKA_VERSION" \ - "-H:IncludeResources=SCI_VERSION" \ - -H:ReflectionConfigurationFiles=reflection.json \ - --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ - --initialize-at-build-time \ - -H:Log=registerResource: \ - -H:EnableURLProtocols=http,https \ - --enable-all-security-services \ - -H:+JNI \ - --verbose \ - --no-fallback \ - --no-server \ - "$BABASHKA_XMX" \ - --static -else - $GRAALVM_HOME/bin/native-image \ - -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ - -H:Name=bb \ - -H:+ReportExceptionStackTraces \ - -J-Dclojure.spec.skip-macros=true \ - -J-Dclojure.compiler.direct-linking=true \ - "-H:IncludeResources=BABASHKA_VERSION" \ - "-H:IncludeResources=SCI_VERSION" \ - -H:ReflectionConfigurationFiles=reflection.json \ - --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ - --initialize-at-build-time \ - -H:Log=registerResource: \ - -H:EnableURLProtocols=http,https \ - --enable-all-security-services \ - -H:+JNI \ - --verbose \ - --no-fallback \ - --no-server \ - "$BABASHKA_XMX" -fi +$GRAALVM_HOME/bin/native-image \ + -jar target/babashka-$BABASHKA_VERSION-standalone.jar \ + -H:Name=bb \ + -H:+ReportExceptionStackTraces \ + -J-Dclojure.spec.skip-macros=true \ + -J-Dclojure.compiler.direct-linking=true \ + "-H:IncludeResources=BABASHKA_VERSION" \ + "-H:IncludeResources=SCI_VERSION" \ + -H:ReflectionConfigurationFiles=reflection.json \ + --initialize-at-run-time=java.lang.Math\$RandomNumberGeneratorHolder \ + --initialize-at-build-time \ + -H:Log=registerResource: \ + -H:EnableURLProtocols=http,https \ + --enable-all-security-services \ + -H:+JNI \ + --verbose \ + --no-fallback \ + --no-server \ + "$BABASHKA_XMX" lein clean From 192e2676fce24364a93320aed8e048ca3bd18ad0 Mon Sep 17 00:00:00 2001 From: Haokang Den Date: Mon, 10 Feb 2020 17:57:36 +0900 Subject: [PATCH 126/152] Using ubuntu:bionic (#262) --- Dockerfile | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index e2c96de9..267c3304 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,13 +15,6 @@ RUN ./script/compile RUN cp bb /usr/local/bin -FROM alpine:3.9 - -# See https://github.com/sgerrand/alpine-pkg-glibc -RUN apk --no-cache add ca-certificates curl -RUN curl -o /etc/apk/keys/sgerrand.rsa.pub -sL https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub -RUN curl -sLO https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.29-r0/glibc-2.29-r0.apk -RUN apk add glibc-2.29-r0.apk +FROM ubuntu:bionic COPY --from=BASE /usr/local/bin/bb /usr/local/bin -ENV LD_LIBRARY_PATH /lib CMD ["bb"] From 108567f74e36b0cae922367b8b9e4f1ad0f6243e Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Feb 2020 22:58:39 +0100 Subject: [PATCH 127/152] v0.0.70 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 8d2b133c..e01df265 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.70-SNAPSHOT \ No newline at end of file +0.0.70 \ No newline at end of file From 8abb8142b1a8f7f8aa874a4275fedd95c4a04dfb Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 10 Feb 2020 23:18:07 +0100 Subject: [PATCH 128/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index 652ab34e..e01df265 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.69 \ No newline at end of file +0.0.70 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index e01df265..479f4baa 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.70 \ No newline at end of file +0.0.71-SNAPSHOT \ No newline at end of file From 018371d90cec2abd0fdc06abee9d3e8c21bb1c62 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 13 Feb 2020 20:00:15 +0100 Subject: [PATCH 129/152] sci: check if arity --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index c1752524..0fdb743a 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit c1752524724e6e9e47b0c44fd7b77c831ceff740 +Subproject commit 0fdb743ad833d74fb86bed1ae2eacaa9d883f652 From e98a92c2834570449f405449978c1777641052e1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sun, 16 Feb 2020 12:54:54 +0100 Subject: [PATCH 130/152] Sci: reify core vars, implement repl/doc (#265) --- sci | 2 +- script/lib_tests/spartan_spec_test | 6 +- src/babashka/impl/repl.clj | 2 +- src/babashka/main.clj | 313 ++++++++++++++--------------- test/babashka/main_test.clj | 8 + 5 files changed, 169 insertions(+), 162 deletions(-) diff --git a/sci b/sci index 0fdb743a..57209f08 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 0fdb743ad833d74fb86bed1ae2eacaa9d883f652 +Subproject commit 57209f08d18ee168dbc1dca26e60b4958add3c0c diff --git a/script/lib_tests/spartan_spec_test b/script/lib_tests/spartan_spec_test index 5afe02ef..617368d9 100755 --- a/script/lib_tests/spartan_spec_test +++ b/script/lib_tests/spartan_spec_test @@ -5,7 +5,7 @@ set -eo pipefail export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {spartan.spec {:git/url "https://github.com/borkdude/spartan.spec" :sha "16f7eec4b6589c77c96c9fcf989f78fffcee7c4c"}}}' -Spath) ./bb -e " -(require '[spartan.spec :as s]) -(s/explain (s/cat :i int? :s string?) [1 :foo]) -(s/conform (s/cat :i int? :s string?) [1 \"foo\"]) +(time (require '[spartan.spec :as s])) +(time (s/explain (s/cat :i int? :s string?) [1 :foo])) +(time (s/conform (s/cat :i int? :s string?) [1 \"foo\"])) " diff --git a/src/babashka/impl/repl.clj b/src/babashka/impl/repl.clj index e83f56de..12fb91a6 100644 --- a/src/babashka/impl/repl.clj +++ b/src/babashka/impl/repl.clj @@ -31,7 +31,7 @@ (sio/println "Use :repl/quit or :repl/exit to quit the REPL.") (sio/println "Clojure rocks, Bash reaches.") (sio/println) - (eval-form sci-ctx '(require '[clojure.repl :refer [dir]])))) + (eval-form sci-ctx '(require '[clojure.repl :refer [dir doc]])))) :read (or read (fn [_request-prompt request-exit] ;; (prn "PEEK" @sci/in (r/peek-char @sci/in)) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 9f49f98c..7cf6c8c9 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -25,12 +25,14 @@ [sci.core :as sci] [sci.impl.interpreter :refer [eval-string*]] [sci.impl.opts :as sci-opts] - [sci.impl.vars :as vars]) + [sci.impl.vars :as vars] + [sci.impl.unrestrict :refer [*unrestricted*]]) (:gen-class)) -(sci/alter-var-root sci/in (constantly *in*)) -(sci/alter-var-root sci/out (constantly *out*)) -(sci/alter-var-root sci/err (constantly *err*)) +(binding [*unrestricted* true] + (sci/alter-var-root sci/in (constantly *in*)) + (sci/alter-var-root sci/out (constantly *out*)) + (sci/alter-var-root sci/err (constantly *err*))) (set! *warn-on-reflection* true) ;; To detect problems when generating the image, run: @@ -217,10 +219,6 @@ Everything after that is bound to *command-line-args*.")) (defn exit [n] (throw (ex-info "" {:bb/exit-code n}))) -;; (sci/set-var-root! sci/*in* *in*) -;; (sci/set-var-root! sci/*out* *out*) -;; (sci/set-var-root! sci/*err* *err*) - (def aliases '{tools.cli 'clojure.tools.cli edn clojure.edn @@ -283,155 +281,156 @@ Everything after that is bound to *command-line-args*.")) (handle-pipe!) #_(binding [*out* *err*] (prn "M" (meta (get bindings 'future)))) - (sci/with-bindings {reflection-var false - vars/current-ns (vars/->SciNamespace 'user)} - (let [t0 (System/currentTimeMillis) - {:keys [:version :shell-in :edn-in :shell-out :edn-out - :help? :file :command-line-args - :expressions :stream? :time? - :repl :socket-repl - :verbose? :classpath - :main :uberscript] :as _opts} - (parse-opts args) - read-next (fn [*in*] - (if (pipe-signal-received?) - ::EOF - (if stream? - (if shell-in (or (read-line) ::EOF) - (read-edn)) - (delay (cond shell-in - (shell-seq *in*) - edn-in - (edn-seq *in*) - :else - (edn/read *in*)))))) - uberscript-sources (atom ()) - env (atom {}) - classpath (or classpath - (System/getenv "BABASHKA_CLASSPATH")) - _ (when classpath - (add-classpath* classpath)) - load-fn (fn [{:keys [:namespace]}] - (when-let [{:keys [:loader]} @cp-state] - (let [res (cp/source-for-namespace loader namespace nil)] - (when uberscript (swap! uberscript-sources conj (:source res))) - res))) - _ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file)))) - ctx {:aliases aliases - :namespaces (-> namespaces - (assoc 'clojure.core - (assoc core-extras - '*command-line-args* - (sci/new-dynamic-var '*command-line-args* command-line-args) - '*warn-on-reflection* reflection-var)) - (assoc-in ['clojure.java.io 'resource] - #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) - :bindings bindings - :env env - :features #{:bb} - :classes classes/class-map - :imports '{ArithmeticException java.lang.ArithmeticException - AssertionError java.lang.AssertionError - Boolean java.lang.Boolean - Class java.lang.Class - Double java.lang.Double - Exception java.lang.Exception - IllegalArgumentException java.lang.IllegalArgumentException - Integer java.lang.Integer - File java.io.File - Long java.lang.Long - Math java.lang.Math - Object java.lang.Object - ProcessBuilder java.lang.ProcessBuilder - String java.lang.String - StringBuilder java.lang.StringBuilder - System java.lang.System - Thread java.lang.Thread - Throwable java.lang.Throwable} - :load-fn load-fn - :dry-run uberscript} - ctx (addons/future ctx) - sci-ctx (sci-opts/init ctx) - _ (vreset! common/ctx sci-ctx) - _ (swap! (:env sci-ctx) - (fn [env] - (update-in env [:namespaces 'clojure.core] assoc - 'eval #(eval* sci-ctx %) - 'load-file #(load-file* sci-ctx %)))) - _ (swap! (:env sci-ctx) - (fn [env] - (assoc-in env [:namespaces 'clojure.main 'repl] - (fn [& opts] - (let [opts (apply hash-map opts)] - (repl/start-repl! sci-ctx opts)))))) - preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) - [expressions exit-code] - (cond expressions [expressions nil] - main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" - main)] nil] - file (try [[(read-file file)] nil] - (catch Exception e - (error-handler* e verbose?)))) - expression (str/join " " expressions) ;; this might mess with the locations... - exit-code - ;; handle preloads - (if exit-code exit-code - (do (when preloads - (try - (eval-string* sci-ctx preloads) - (catch Throwable e - (error-handler* e verbose?)))) - nil)) - exit-code - (or exit-code - (second - (cond version - [(print-version) 0] - help? - [(print-help) 0] - repl [(repl/start-repl! sci-ctx) 0] - socket-repl [(start-socket-repl! socket-repl sci-ctx) 0] - (not (str/blank? expression)) - (try - (loop [in (read-next *in*)] - (let [_ (swap! env update-in [:namespaces 'user] - assoc (with-meta '*input* - (when-not stream? - {:sci.impl/deref! true})) - (sci/new-dynamic-var '*input* in))] - (if (identical? ::EOF in) - [nil 0] ;; done streaming - (let [res [(let [res (eval-string* sci-ctx expression)] - (when (some? res) - (if-let [pr-f (cond shell-out println - edn-out prn)] - (if (coll? res) - (doseq [l res - :while (not (pipe-signal-received?))] - (pr-f l)) - (pr-f res)) - (prn res)))) 0]] - (if stream? - (recur (read-next *in*)) - res))))) - (catch Throwable e - (error-handler* e verbose?))) - uberscript [nil 0] - :else [(repl/start-repl! sci-ctx) 0])) - 1) - t1 (System/currentTimeMillis)] - (flush) - (when uberscript - uberscript - (let [uberscript-out uberscript] - (spit uberscript-out "") ;; reset file - (doseq [s @uberscript-sources] - (spit uberscript-out s :append true)) - (spit uberscript-out preloads :append true) - (spit uberscript-out expression :append true))) - (when time? (binding [*out* *err*] - (println "bb took" (str (- t1 t0) "ms.")))) - exit-code))) + (binding [*unrestricted* true] + (sci/binding [reflection-var false + vars/current-ns (vars/->SciNamespace 'user)] + (let [t0 (System/currentTimeMillis) + {:keys [:version :shell-in :edn-in :shell-out :edn-out + :help? :file :command-line-args + :expressions :stream? :time? + :repl :socket-repl + :verbose? :classpath + :main :uberscript] :as _opts} + (parse-opts args) + read-next (fn [*in*] + (if (pipe-signal-received?) + ::EOF + (if stream? + (if shell-in (or (read-line) ::EOF) + (read-edn)) + (delay (cond shell-in + (shell-seq *in*) + edn-in + (edn-seq *in*) + :else + (edn/read *in*)))))) + uberscript-sources (atom ()) + env (atom {}) + classpath (or classpath + (System/getenv "BABASHKA_CLASSPATH")) + _ (when classpath + (add-classpath* classpath)) + load-fn (fn [{:keys [:namespace]}] + (when-let [{:keys [:loader]} @cp-state] + (let [res (cp/source-for-namespace loader namespace nil)] + (when uberscript (swap! uberscript-sources conj (:source res))) + res))) + _ (when file (vars/bindRoot vars/current-file (.getCanonicalPath (io/file file)))) + ctx {:aliases aliases + :namespaces (-> namespaces + (assoc 'clojure.core + (assoc core-extras + '*command-line-args* + (sci/new-dynamic-var '*command-line-args* command-line-args) + '*warn-on-reflection* reflection-var)) + (assoc-in ['clojure.java.io 'resource] + #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) + :bindings bindings + :env env + :features #{:bb} + :classes classes/class-map + :imports '{ArithmeticException java.lang.ArithmeticException + AssertionError java.lang.AssertionError + Boolean java.lang.Boolean + Class java.lang.Class + Double java.lang.Double + Exception java.lang.Exception + IllegalArgumentException java.lang.IllegalArgumentException + Integer java.lang.Integer + File java.io.File + Long java.lang.Long + Math java.lang.Math + Object java.lang.Object + ProcessBuilder java.lang.ProcessBuilder + String java.lang.String + StringBuilder java.lang.StringBuilder + System java.lang.System + Thread java.lang.Thread + Throwable java.lang.Throwable} + :load-fn load-fn + :dry-run uberscript} + ctx (addons/future ctx) + sci-ctx (sci-opts/init ctx) + _ (vreset! common/ctx sci-ctx) + _ (swap! (:env sci-ctx) + (fn [env] + (update-in env [:namespaces 'clojure.core] assoc + 'eval #(eval* sci-ctx %) + 'load-file #(load-file* sci-ctx %)))) + _ (swap! (:env sci-ctx) + (fn [env] + (assoc-in env [:namespaces 'clojure.main 'repl] + (fn [& opts] + (let [opts (apply hash-map opts)] + (repl/start-repl! sci-ctx opts)))))) + preloads (some-> (System/getenv "BABASHKA_PRELOADS") (str/trim)) + [expressions exit-code] + (cond expressions [expressions nil] + main [[(format "(ns user (:require [%1$s])) (apply %1$s/-main *command-line-args*)" + main)] nil] + file (try [[(read-file file)] nil] + (catch Exception e + (error-handler* e verbose?)))) + expression (str/join " " expressions) ;; this might mess with the locations... + exit-code + ;; handle preloads + (if exit-code exit-code + (do (when preloads + (try + (eval-string* sci-ctx preloads) + (catch Throwable e + (error-handler* e verbose?)))) + nil)) + exit-code + (or exit-code + (second + (cond version + [(print-version) 0] + help? + [(print-help) 0] + repl [(repl/start-repl! sci-ctx) 0] + socket-repl [(start-socket-repl! socket-repl sci-ctx) 0] + (not (str/blank? expression)) + (try + (loop [in (read-next *in*)] + (let [_ (swap! env update-in [:namespaces 'user] + assoc (with-meta '*input* + (when-not stream? + {:sci.impl/deref! true})) + (sci/new-dynamic-var '*input* in))] + (if (identical? ::EOF in) + [nil 0] ;; done streaming + (let [res [(let [res (eval-string* sci-ctx expression)] + (when (some? res) + (if-let [pr-f (cond shell-out println + edn-out prn)] + (if (coll? res) + (doseq [l res + :while (not (pipe-signal-received?))] + (pr-f l)) + (pr-f res)) + (prn res)))) 0]] + (if stream? + (recur (read-next *in*)) + res))))) + (catch Throwable e + (error-handler* e verbose?))) + uberscript [nil 0] + :else [(repl/start-repl! sci-ctx) 0])) + 1) + t1 (System/currentTimeMillis)] + (flush) + (when uberscript + uberscript + (let [uberscript-out uberscript] + (spit uberscript-out "") ;; reset file + (doseq [s @uberscript-sources] + (spit uberscript-out s :append true)) + (spit uberscript-out preloads :append true) + (spit uberscript-out expression :append true))) + (when time? (binding [*out* *err*] + (println "bb took" (str (- t1 t0) "ms.")))) + exit-code)))) (defn -main [& args] diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 6b2f2220..795b64f9 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -342,6 +342,14 @@ (is (empty? (bb nil "--uberscript" (.getPath tmp-file) "-e" "(System/exit 1)"))) (is (= "(System/exit 1)" (slurp tmp-file))))) +(deftest unrestricted-access + (testing "babashka is allowed to mess with built-in vars" + (is (= 1 (bb nil " +(def inc2 inc) (alter-var-root #'clojure.core/inc (constantly dec)) +(let [res (inc 2)] + (alter-var-root #'clojure.core/inc (constantly inc2)) + res)"))))) + ;;;; Scratch (comment From 945b2ab68ec4f5f44d280aa384ab988c70550398 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 17 Feb 2020 12:09:34 +0100 Subject: [PATCH 131/152] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a1ab8e5d..0bea4c5e 100644 --- a/README.md +++ b/README.md @@ -718,6 +718,10 @@ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version " " ``` +### [regal](https://github.com/borkdude/regal) + +Reified regular expressions. + #### [spartan.test](https://github.com/borkdude/spartan.test/) A minimal test framework compatible with babashka. This library is deprecated From 1c5ba28932e6cd3fb91315222470f6cda8303029 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 17 Feb 2020 12:11:07 +0100 Subject: [PATCH 132/152] README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bea4c5e..f882284f 100644 --- a/README.md +++ b/README.md @@ -718,7 +718,7 @@ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version " " ``` -### [regal](https://github.com/borkdude/regal) +#### [regal](https://github.com/borkdude/regal) Reified regular expressions. From 29c0f20eadaa449afa8bc1cbc3b577f37c1b21b6 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 17 Feb 2020 18:17:14 +0100 Subject: [PATCH 133/152] Fix tools.cli alias --- src/babashka/main.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 7cf6c8c9..023eb9f2 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -220,7 +220,7 @@ Everything after that is bound to *command-line-args*.")) (throw (ex-info "" {:bb/exit-code n}))) (def aliases - '{tools.cli 'clojure.tools.cli + '{tools.cli clojure.tools.cli edn clojure.edn wait babashka.wait signal babashka.signal From 41b38930567162cafc4cbbf70ad1c8f4e4bfc5df Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Tue, 18 Feb 2020 16:25:04 +0100 Subject: [PATCH 134/152] sci: support metadata on namespaces --- sci | 2 +- src/babashka/impl/clojure/core/server.clj | 2 +- src/babashka/main.clj | 2 +- test/babashka/impl/repl_test.clj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sci b/sci index 57209f08..9c3c6e49 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 57209f08d18ee168dbc1dca26e60b4958add3c0c +Subproject commit 9c3c6e492e100b1b438b9e073f574fc52622c818 diff --git a/src/babashka/impl/clojure/core/server.clj b/src/babashka/impl/clojure/core/server.clj index dd58f737..113934a0 100644 --- a/src/babashka/impl/clojure/core/server.clj +++ b/src/babashka/impl/clojure/core/server.clj @@ -46,7 +46,7 @@ (sci/with-bindings {sci/in in sci/out out sci/err err - vars/current-ns (vars/->SciNamespace 'user)} + vars/current-ns (vars/->SciNamespace 'user nil)} (swap! server assoc-in [:sessions client-id] {}) (apply accept args)) (catch SocketException _disconnect) diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 023eb9f2..36c5af8b 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -283,7 +283,7 @@ Everything after that is bound to *command-line-args*.")) (prn "M" (meta (get bindings 'future)))) (binding [*unrestricted* true] (sci/binding [reflection-var false - vars/current-ns (vars/->SciNamespace 'user)] + vars/current-ns (vars/->SciNamespace 'user nil)] (let [t0 (System/currentTimeMillis) {:keys [:version :shell-in :edn-in :shell-out :edn-out :help? :file :command-line-args diff --git a/test/babashka/impl/repl_test.clj b/test/babashka/impl/repl_test.clj index 3c9cb72f..29a932c8 100644 --- a/test/babashka/impl/repl_test.clj +++ b/test/babashka/impl/repl_test.clj @@ -14,7 +14,7 @@ (vars/bindRoot sci/err *err*) (defn repl! [] - (sci/with-bindings {vars/current-ns (vars/->SciNamespace 'user)} + (sci/with-bindings {vars/current-ns (vars/->SciNamespace 'user nil)} (start-repl! (init {:bindings {'*command-line-args* ["a" "b" "c"]} :env (atom {})})))) From 9ac39dca8b81742ae0b1861b16adf2219354a0ff Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 11:48:46 +0100 Subject: [PATCH 135/152] sci: add ns-imports --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 9c3c6e49..28fbd70e 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 9c3c6e492e100b1b438b9e073f574fc52622c818 +Subproject commit 28fbd70e84e66ba100cdfcf0c0bac292d305c7f9 From 02c53f7b447ac9bc20799bb731589c64664fbca4 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 12:07:50 +0100 Subject: [PATCH 136/152] Revert "sci: add ns-imports" This reverts commit 9ac39dca8b81742ae0b1861b16adf2219354a0ff. --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 28fbd70e..9c3c6e49 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 28fbd70e84e66ba100cdfcf0c0bac292d305c7f9 +Subproject commit 9c3c6e492e100b1b438b9e073f574fc52622c818 From 67653802d6295f9d50adee828a28e364fb54dede Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 17:29:53 +0100 Subject: [PATCH 137/152] sci: fix tag resolution (#269) --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 9c3c6e49..7fb4543c 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 9c3c6e492e100b1b438b9e073f574fc52622c818 +Subproject commit 7fb4543c46171906496c73261eae6077b15d4b76 From 111f344219c04e610e8acf752b282b015012f5c2 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 22:37:52 +0100 Subject: [PATCH 138/152] sci: add ns-map, ns-refers --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index 7fb4543c..cdca1bc2 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit 7fb4543c46171906496c73261eae6077b15d4b76 +Subproject commit cdca1bc28b071163333b6c352430cec544c3c5ae From 643b3a00da43805f4cb971d69390d2496c549ed8 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 22:47:36 +0100 Subject: [PATCH 139/152] Bump deps --- deps.edn | 6 +++--- project.clj | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/deps.edn b/deps.edn index 9f63e001..0b22b062 100644 --- a/deps.edn +++ b/deps.edn @@ -4,10 +4,10 @@ borkdude/edamame {:mvn/version "0.0.10"}, borkdude/graal.locking {:mvn/version "0.0.2"}, borkdude/sci.impl.reflector {:mvn/version "0.0.1"} - org.clojure/core.async {:mvn/version "0.4.500"}, + org.clojure/core.async {:mvn/version "1.0.567"}, org.clojure/tools.cli {:mvn/version "0.4.2"}, - org.clojure/data.csv {:mvn/version "0.1.4"}, - cheshire {:mvn/version "5.9.0"}} + org.clojure/data.csv {:mvn/version "1.0.0"}, + cheshire {:mvn/version "5.10.0"}} :aliases {:main {:main-opts ["-m" "babashka.main"]} :profile diff --git a/project.clj b/project.clj index a399146b..c8e8a43a 100644 --- a/project.clj +++ b/project.clj @@ -14,10 +14,10 @@ [borkdude/edamame "0.0.10"] [borkdude/graal.locking "0.0.2"] [borkdude/sci.impl.reflector "0.0.1"] - [org.clojure/core.async "0.4.500"] + [org.clojure/core.async "1.0.567"] [org.clojure/tools.cli "0.4.2"] - [org.clojure/data.csv "0.1.4"] - [cheshire "5.9.0"]] + [org.clojure/data.csv "1.0.0"] + [cheshire "5.10.0"]] :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"] [com.clojure-goes-fast/clj-async-profiler "0.4.0"]]} :uberjar {:global-vars {*assert* false} From e43727955a2cdabd2bb0189c20dd7f9a18156fc9 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 23:22:49 +0100 Subject: [PATCH 140/152] [#133] add clojure.pprint/pprint backed by fipp --- README.md | 1 + deps.edn | 3 ++- project.clj | 3 ++- src/babashka/main.clj | 8 +++++--- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f882284f..fc373499 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ enumerated explicitly. it is a function. - `clojure.stacktrace` - `clojure.test` +- `clojure.pprint`: `pprint` (currently backed by [fipp](https://github.com/brandonbloom/fipp)'s `fipp.edn/pprint`) - [`clojure.tools.cli`](https://github.com/clojure/tools.cli) aliased as `tools.cli` - [`clojure.data.csv`](https://github.com/clojure/data.csv) aliased as `csv` - [`cheshire.core`](https://github.com/dakrone/cheshire) aliased as `json` diff --git a/deps.edn b/deps.edn index 0b22b062..5b67b4d9 100644 --- a/deps.edn +++ b/deps.edn @@ -7,7 +7,8 @@ org.clojure/core.async {:mvn/version "1.0.567"}, org.clojure/tools.cli {:mvn/version "0.4.2"}, org.clojure/data.csv {:mvn/version "1.0.0"}, - cheshire {:mvn/version "5.10.0"}} + cheshire {:mvn/version "5.10.0"} + fipp {:mvn/version "0.6.22"}} :aliases {:main {:main-opts ["-m" "babashka.main"]} :profile diff --git a/project.clj b/project.clj index c8e8a43a..73f9cf87 100644 --- a/project.clj +++ b/project.clj @@ -17,7 +17,8 @@ [org.clojure/core.async "1.0.567"] [org.clojure/tools.cli "0.4.2"] [org.clojure/data.csv "1.0.0"] - [cheshire "5.10.0"]] + [cheshire "5.10.0"] + [fipp "0.6.22"]] :profiles {:test {:dependencies [[clj-commons/conch "0.9.2"] [com.clojure-goes-fast/clj-async-profiler "0.4.0"]]} :uberjar {:global-vars {*assert* false} diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 36c5af8b..3cbf4259 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -21,12 +21,13 @@ [clojure.edn :as edn] [clojure.java.io :as io] [clojure.string :as str] + [fipp.edn :as fipp] [sci.addons :as addons] [sci.core :as sci] [sci.impl.interpreter :refer [eval-string*]] [sci.impl.opts :as sci-opts] - [sci.impl.vars :as vars] - [sci.impl.unrestrict :refer [*unrestricted*]]) + [sci.impl.unrestrict :refer [*unrestricted*]] + [sci.impl.vars :as vars]) (:gen-class)) (binding [*unrestricted* true] @@ -257,7 +258,8 @@ Everything after that is bound to *command-line-args*.")) 'clojure.main {'demunge demunge} 'clojure.repl {'demunge demunge} 'clojure.test t/clojure-test-namespace - 'babashka.classpath {'add-classpath add-classpath*}}) + 'babashka.classpath {'add-classpath add-classpath*} + 'clojure.pprint {'pprint fipp/pprint}}) (def bindings {'java.lang.System/exit exit ;; override exit, so we have more control From 847a2d192296d9dbb0f901b2ed9af70ee851c74f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 23:27:44 +0100 Subject: [PATCH 141/152] Add docs about binary size --- doc/dev.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/dev.md b/doc/dev.md index 0545a29f..4244149e 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -58,6 +58,10 @@ Keep notes here about how adding libraries and classes to Babashka affects the b +2020/02/19, e43727955a2cdabd2bb0189c20dd7f9a18156fc9 +Added fipp.edn/pprint +40598268 - 39744804 = 853kb added. + 2020/02/09, c8fd1c7931d7842ebaec1fa8faf06d4ab58573bd Added java.lang.BigInteger and java.security.MessageDigest. 39281972 - 39072764 = 209kb added. From 7ad6f3195f20e442f0315c05c35ce020cc4772ea Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Wed, 19 Feb 2020 23:58:49 +0100 Subject: [PATCH 142/152] Lib tests --- .circleci/config.yml | 1 + README.md | 15 ++++++++++----- script/lib_tests/clj_http_lite_test | 8 +++++++- script/lib_tests/clojure_csv_test | 8 +++++++- script/lib_tests/deps_clj_test | 8 +++++++- script/lib_tests/medley_test | 17 +++++++++++++++++ script/lib_tests/regal_test | 13 +++++++++++++ script/lib_tests/spartan_spec_test | 8 +++++++- script/run_lib_tests | 2 ++ src/babashka/main.clj | 2 +- test/babashka/main_test.clj | 5 +++-- 11 files changed, 75 insertions(+), 12 deletions(-) create mode 100755 script/lib_tests/medley_test create mode 100755 script/lib_tests/regal_test diff --git a/.circleci/config.yml b/.circleci/config.yml index cc4f3b34..a7cb1471 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -38,6 +38,7 @@ jobs: name: Run JVM tests command: | script/test + script/run_lib_tests # - run: # name: Run as tools.deps dependency # command: | diff --git a/README.md b/README.md index fc373499..0f70449f 100644 --- a/README.md +++ b/README.md @@ -519,14 +519,19 @@ $ bb script.clj -h ## Reader conditionals -Babashka supports reader conditionals using the `:bb` feature: +Babashka supports reader conditionals by taking either the `:bb` or `:clj` +branch, whichever comes first. NOTE: the `:clj` branch behavior was added in +version 0.0.71, before that version the `:clj` branch was ignored. ``` clojure -$ cat example.clj -#?(:clj (in-ns 'foo) :bb (println "babashka doesn't support in-ns yet!")) +$ bb "#?(:bb :hello :clj :bye)" +:hello -$ ./bb example.clj -babashka doesn't support in-ns yet! +$ bb "#?(:clj :bye :bb :hello)" +:bye + +$ bb "[1 2 #?@(:bb [] :clj [1])]" +[1 2] ``` ## Running tests diff --git a/script/lib_tests/clj_http_lite_test b/script/lib_tests/clj_http_lite_test index 43821d03..8ca39fb0 100755 --- a/script/lib_tests/clj_http_lite_test +++ b/script/lib_tests/clj_http_lite_test @@ -4,7 +4,13 @@ set -eo pipefail export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {clj-http-lite {:git/url "https://github.com/borkdude/clj-http-lite" :sha "f44ebe45446f0f44f2b73761d102af3da6d0a13e"}}}' -Spath) -./bb -e " +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + +$BB_CMD -e " (require '[clj-http.lite.client :as client]) (prn (:status (client/get \"https://www.clojure.org\"))) diff --git a/script/lib_tests/clojure_csv_test b/script/lib_tests/clojure_csv_test index 3b8c6e26..c9b6adf0 100755 --- a/script/lib_tests/clojure_csv_test +++ b/script/lib_tests/clojure_csv_test @@ -4,7 +4,13 @@ set -eo pipefail export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version "RELEASE"}}}' -Spath)" -./bb -e " +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + +$BB_CMD -e " (require '[clojure-csv.core :as csv]) (prn (csv/write-csv (csv/parse-csv \"a,b,c\n1,2,3\"))) " diff --git a/script/lib_tests/deps_clj_test b/script/lib_tests/deps_clj_test index 29d78dc3..d23ae8b6 100755 --- a/script/lib_tests/deps_clj_test +++ b/script/lib_tests/deps_clj_test @@ -2,8 +2,14 @@ set -eo pipefail +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + curl -sL https://raw.githubusercontent.com/borkdude/deps.clj/master/deps.clj -o deps_test.clj chmod +x deps_test.clj -./bb deps_test.clj -Sdescribe +$BB_CMD deps_test.clj -Sdescribe rm deps_test.clj diff --git a/script/lib_tests/medley_test b/script/lib_tests/medley_test new file mode 100755 index 00000000..9bdd30fd --- /dev/null +++ b/script/lib_tests/medley_test @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -eo pipefail + +export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {medley {:git/url "https://github.com/weavejester/medley" :sha "a4e5fb5383f5c0d83cb2d005181a35b76d8a136d"}}}' -Spath)" + +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + +$BB_CMD " +(require '[medley.core :refer [index-by random-uuid]]) +(prn (index-by :id [{:id 1} {:id 2}])) +(prn (random-uuid)) +" diff --git a/script/lib_tests/regal_test b/script/lib_tests/regal_test new file mode 100755 index 00000000..17de219e --- /dev/null +++ b/script/lib_tests/regal_test @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -eo pipefail + +export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {regal {:git/url "https://github.com/lambdaisland/regal" :sha "e179f20a2ec78d47c8c24257e644ac80a70e33cb"}}}' -Spath)" + +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + +$BB_CMD "(require '[lambdaisland.regal :as re]) (re/regex [:range \a \z])" diff --git a/script/lib_tests/spartan_spec_test b/script/lib_tests/spartan_spec_test index 617368d9..0100ca83 100755 --- a/script/lib_tests/spartan_spec_test +++ b/script/lib_tests/spartan_spec_test @@ -4,7 +4,13 @@ set -eo pipefail export BABASHKA_CLASSPATH=$(clojure -Sdeps '{:deps {spartan.spec {:git/url "https://github.com/borkdude/spartan.spec" :sha "16f7eec4b6589c77c96c9fcf989f78fffcee7c4c"}}}' -Spath) -./bb -e " +if [ "$BABASHKA_TEST_ENV" = "native" ]; then + BB_CMD="./bb" +else + BB_CMD="lein bb" +fi + +$BB_CMD -e " (time (require '[spartan.spec :as s])) (time (s/explain (s/cat :i int? :s string?) [1 :foo])) (time (s/conform (s/cat :i int? :s string?) [1 \"foo\"])) diff --git a/script/run_lib_tests b/script/run_lib_tests index e1a95e0b..8e0695a9 100755 --- a/script/run_lib_tests +++ b/script/run_lib_tests @@ -6,3 +6,5 @@ script/lib_tests/clj_http_lite_test script/lib_tests/deps_clj_test script/lib_tests/spartan_spec_test script/lib_tests/clojure_csv_test +script/lib_tests/regal_test +script/lib_tests/medley_test diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 3cbf4259..b0b1a3a2 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -329,7 +329,7 @@ Everything after that is bound to *command-line-args*.")) #(when-let [{:keys [:loader]} @cp-state] (cp/getResource loader % {:url? true})))) :bindings bindings :env env - :features #{:bb} + :features #{:bb :clj} :classes classes/class-map :imports '{ArithmeticException java.lang.ArithmeticException AssertionError java.lang.AssertionError diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 795b64f9..7d0e2e17 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -236,8 +236,9 @@ (is (zero? (bb nil "(try (/ 1 0) (catch ArithmeticException _ 0))")))) (deftest reader-conditionals-test - (is (= :hello (bb nil "#?(:clj (in-ns 'foo)) (println :hello)"))) - (is (= :hello (bb nil "#?(:bb :hello :default :bye)")))) + (is (= :hello (bb nil "#?(:bb :hello :default :bye)"))) + (is (= :hello (bb nil "#?(:clj :hello :bb :bye)"))) + (is (= [1 2] (bb nil "[1 2 #?@(:bb [] :clj [1])]")))) (deftest csv-test (is (= '(["Adult" "87727"] ["Elderly" "43914"] ["Child" "33411"] ["Adolescent" "29849"] From 453d66f0fbae9a2fe3d9b23d982e77d517a3bd82 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 00:29:37 +0100 Subject: [PATCH 143/152] Document breaking change --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index e75cccb9..fff6f671 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## Breaking changes +## v0.0.71 +- #267 Change behavior of reader conditionals: the `:clj` branch is taken when + it occurs before a `:bb` branch. + ## v0.0.44 - 0.0.45 - #173: Rename `*in*` to `*input*` (in the `user` namespace). The reason for this is that itt shadowed `clojure.core/*in*` when used unqualified. From 315db27969ba2230b9ce9a5e2a32c719ca685331 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 00:29:49 +0100 Subject: [PATCH 144/152] v0.0.71 --- resources/BABASHKA_VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index 479f4baa..cf9f1739 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.71-SNAPSHOT \ No newline at end of file +0.0.71 \ No newline at end of file From 133ee70e294e0ac47a66f71161f9626dda47a501 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 09:38:19 +0100 Subject: [PATCH 145/152] Version bump --- resources/BABASHKA_RELEASED_VERSION | 2 +- resources/BABASHKA_VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/resources/BABASHKA_RELEASED_VERSION b/resources/BABASHKA_RELEASED_VERSION index e01df265..cf9f1739 100644 --- a/resources/BABASHKA_RELEASED_VERSION +++ b/resources/BABASHKA_RELEASED_VERSION @@ -1 +1 @@ -0.0.70 \ No newline at end of file +0.0.71 \ No newline at end of file diff --git a/resources/BABASHKA_VERSION b/resources/BABASHKA_VERSION index cf9f1739..75b9c2a8 100644 --- a/resources/BABASHKA_VERSION +++ b/resources/BABASHKA_VERSION @@ -1 +1 @@ -0.0.71 \ No newline at end of file +0.0.72-SNAPSHOT \ No newline at end of file From 868ee1ae54f8a0f67808421b204ad0a79fe11518 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 10:56:07 +0100 Subject: [PATCH 146/152] Update docs --- README.md | 43 ++++++++++++++++++++++++++++++++----- script/lib_tests/regal_test | 2 +- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0f70449f..7e66d02b 100644 --- a/README.md +++ b/README.md @@ -677,10 +677,22 @@ Ran 1 tests containing 0 assertions. {:test 1, :pass 0, :fail 0, :error 0, :type :summary} ``` -#### [medley](https://github.com/borkdude/medley/) +#### [medley](https://github.com/weavejester/medley/) -A fork of [medley](https://github.com/weavejester/medley) made compatible with -babashka. Requires `bb` >= v0.0.58. +Requires `bb` >= v0.0.71. Latest coordinates checked with with bb: + +``` clojure +{:git/url "https://github.com/weavejester" :sha "a4e5fb5383f5c0d83cb2d005181a35b76d8a136d"} +``` + +Example: + +``` shell +$ export BABASHKA_CLASSPATH=$(clojure -Spath -Sdeps '{:deps {medley {:git/url "https://github.com/weavejester" :sha "a4e5fb5383f5c0d83cb2d005181a35b76d8a136d"}}}') + +$ bb -e "(require '[medley.core :as m]) (m/index-by :id [{:id 1} {:id 2}])" +{1 {:id 1}, 2 {:id 2}} +``` #### [clj-http-lite](https://github.com/borkdude/clj-http-lite) @@ -695,7 +707,15 @@ $ bb "(require '[clj-http.lite.client :as client]) (:status (client/get \"https: #### [limit-break](https://github.com/technomancy/limit-break) -A debug REPL library. Example: +A debug REPL library. + +Latest coordinates checked with with bb: + +``` clojure +{:git/url "https://github.com/technomancy/limit-break" :sha "050fcfa0ea29fe3340927533a6fa6fffe23bfc2f" :deps/manifest :deps} +``` + +Example: ``` shell $ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {limit-break {:git/url "https://github.com/technomancy/limit-break" :sha "050fcfa0ea29fe3340927533a6fa6fffe23bfc2f" :deps/manifest :deps}}}' -Spath)" @@ -726,7 +746,20 @@ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version " #### [regal](https://github.com/borkdude/regal) -Reified regular expressions. +Requires `bb` >= v0.0.71. Latest coordinates checked with with bb: + +``` clojure +{:git/url "https://github.com/lambdaisland/regal" :sha "8d300f8e15f43480801766b7762530b6d412c1e6"} +``` + +Example: + +``` shell +$ export BABASHKA_CLASSPATH=$(clojure -Spath -Sdeps '{:deps {regal {:git/url "https://github.com/lambdaisland/regal" :sha "8d300f8e15f43480801766b7762530b6d412c1e6"}}}') + +$ bb -e "(require '[lambdaisland.regal :as regal]) (regal/regex [:* \"ab\"])" +#"(?:\Qab\E)*" +``` #### [spartan.test](https://github.com/borkdude/spartan.test/) diff --git a/script/lib_tests/regal_test b/script/lib_tests/regal_test index 17de219e..76dcca5a 100755 --- a/script/lib_tests/regal_test +++ b/script/lib_tests/regal_test @@ -2,7 +2,7 @@ set -eo pipefail -export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {regal {:git/url "https://github.com/lambdaisland/regal" :sha "e179f20a2ec78d47c8c24257e644ac80a70e33cb"}}}' -Spath)" +export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {regal {:git/url "https://github.com/lambdaisland/regal" :sha "8d300f8e15f43480801766b7762530b6d412c1e6"}}}' -Spath)" if [ "$BABASHKA_TEST_ENV" = "native" ]; then BB_CMD="./bb" From 60ca573397375f407c9814a566bd408ecd600830 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 11:06:03 +0100 Subject: [PATCH 147/152] docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7e66d02b..79087f20 100644 --- a/README.md +++ b/README.md @@ -744,7 +744,7 @@ export BABASHKA_CLASSPATH="$(clojure -Sdeps '{:deps {clojure-csv {:mvn/version " " ``` -#### [regal](https://github.com/borkdude/regal) +#### [regal](https://github.com/lambdaisland/regal) Requires `bb` >= v0.0.71. Latest coordinates checked with with bb: From 576fe1176e62294cb5c1a3bb0cccf157b2975fe1 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 12:20:55 +0100 Subject: [PATCH 148/152] Gallery --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 79087f20..3426a7e7 100644 --- a/README.md +++ b/README.md @@ -947,6 +947,16 @@ See [examples/http_server.clj](https://github.com/borkdude/babashka/blob/master/ Original by [@souenzzo](https://gist.github.com/souenzzo/a959a4c5b8c0c90df76fe33bb7dfe201) +### Print random docstring + +``` shell +$ bb -o "(defmacro random-doc [] (let [sym (-> (ns-publics 'clojure.core) keys rand-nth)] \`(clojure.repl/doc ~sym))) (random-doc)" +------------------------- +clojure.core/keyword? +([x]) + Return true if x is a Keyword +``` + ## Thanks - [adgoji](https://www.adgoji.com/) for financial support From b66c14fec6499ffb93c0fbf2a3d8075a2989aeff Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 12:40:18 +0100 Subject: [PATCH 149/152] doc --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3426a7e7..3654a389 100644 --- a/README.md +++ b/README.md @@ -950,7 +950,7 @@ Original by [@souenzzo](https://gist.github.com/souenzzo/a959a4c5b8c0c90df76fe33 ### Print random docstring ``` shell -$ bb -o "(defmacro random-doc [] (let [sym (-> (ns-publics 'clojure.core) keys rand-nth)] \`(clojure.repl/doc ~sym))) (random-doc)" +$ bb "(defmacro random-doc [] (let [sym (-> (ns-publics 'clojure.core) keys rand-nth)] \`(clojure.repl/doc ~sym))) (random-doc)" ------------------------- clojure.core/keyword? ([x]) From f812868c2b527b13948f6a5b7792d18b89dc785b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 20 Feb 2020 12:45:44 +0100 Subject: [PATCH 150/152] Enhance docstring example --- README.md | 8 +++++--- examples/random_doc.clj | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100755 examples/random_doc.clj diff --git a/README.md b/README.md index 3654a389..c039bd38 100644 --- a/README.md +++ b/README.md @@ -949,12 +949,14 @@ Original by [@souenzzo](https://gist.github.com/souenzzo/a959a4c5b8c0c90df76fe33 ### Print random docstring +See [examples/random_doc.clj](https://github.com/borkdude/babashka/blob/master/examples/random_doc.clj) + ``` shell -$ bb "(defmacro random-doc [] (let [sym (-> (ns-publics 'clojure.core) keys rand-nth)] \`(clojure.repl/doc ~sym))) (random-doc)" +$ examples/random_doc.clj ------------------------- -clojure.core/keyword? +clojure.core/ffirst ([x]) - Return true if x is a Keyword + Same as (first (first x)) ``` ## Thanks diff --git a/examples/random_doc.clj b/examples/random_doc.clj new file mode 100755 index 00000000..c30c4214 --- /dev/null +++ b/examples/random_doc.clj @@ -0,0 +1,11 @@ +#!/usr/bin/env bb + +(require '[clojure.repl]) + +(defmacro random-doc [] + (let [sym (-> (ns-publics 'clojure.core) keys rand-nth)] + (if (:doc (meta (resolve sym))) + `(clojure.repl/doc ~sym) + `(random-doc)))) + +(random-doc) From ee522ccecbb72d4a82dee37d22e4e1205dd0d06b Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 21 Feb 2020 12:15:47 +0100 Subject: [PATCH 151/152] Add link to juxt blog post --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c039bd38..00c3e269 100644 --- a/README.md +++ b/README.md @@ -769,6 +769,7 @@ since babashka v0.0.68 which has `clojure.test` built-in. ### Blogs +- [Babashka: a quick example](https://juxt.pro/blog/posts/babashka.html) by Malcolm Sparks - [Clojure Start Time in 2019](https://stuartsierra.com/2019/12/21/clojure-start-time-in-2019) by Stuart Sierra - [Advent of Random Hacks](https://lambdaisland.com/blog/2019-12-19-advent-of-parens-19-advent-of-random-hacks) From 7b30927a12608df6fe02fdd522c077fd9fe3eadd Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Fri, 21 Feb 2020 19:45:20 +0100 Subject: [PATCH 152/152] sci: add when-some and if-some --- sci | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sci b/sci index cdca1bc2..eebb4566 160000 --- a/sci +++ b/sci @@ -1 +1 @@ -Subproject commit cdca1bc28b071163333b6c352430cec544c3c5ae +Subproject commit eebb456628beb2ac0d1e31c2be46ee0683b9ee7a