diff --git a/src/babashka/main.clj b/src/babashka/main.clj index 1dd1b341..c51806a8 100644 --- a/src/babashka/main.clj +++ b/src/babashka/main.clj @@ -444,14 +444,17 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that (binding [*unrestricted* true] (sci/binding [reflection-var false core/data-readers @core/data-readers] - (let [{:keys [:version :shell-in :edn-in :shell-out :edn-out + (let [{version-opt :version + :keys [:shell-in :edn-in :shell-out :edn-out :help? :file :command-line-args :expressions :stream? :repl :socket-repl :nrepl :verbose? :classpath :main :uberscript :describe?] :as _opts} (parse-opts args) - _ (when main (System/setProperty "babashka.main" main)) + _ (do ;; set properties + (when main (System/setProperty "babashka.main" main)) + (System/setProperty "babashka.version" version)) read-next (fn [*in*] (if (pipe-signal-received?) ::EOF @@ -532,7 +535,7 @@ If neither -e, -f, or --socket-repl are specified, then the first argument that exit-code (or exit-code (second - (cond version + (cond version-opt [(print-version) 0] help? [(print-help) 0] diff --git a/test-resources/babashka/version.clj b/test-resources/babashka/version.clj new file mode 100644 index 00000000..b4492869 --- /dev/null +++ b/test-resources/babashka/version.clj @@ -0,0 +1,13 @@ +(require '[clojure.string :as str]) + +(def babashka-version (System/getProperty "babashka.version")) +;; e.g. 0.1.3-SNAPSHOT + +(defn compare-version [v] + (nat-int? (compare + (mapv #(Integer. %) + (take 3 (str/split babashka-version #"[\.\-]"))) v))) + +(prn (compare-version [0 1 2])) ;; true +(prn (compare-version [0 1 3])) ;; true +(prn (compare-version [0 1 4])) ;; false diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 56fc7693..5c42ed78 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -485,6 +485,10 @@ (deftest data-diff-test (is (= [[nil 1] [nil 2] [1 nil 2]] (bb nil "(require '[clojure.data :as d]) (d/diff [1 1 2] [1 2 2])")))) +(deftest version-test + (is (= "true\ntrue\nfalse\n" + (test-utils/bb nil (.getPath (io/file "test-resources" "babashka" "version.clj")))))) + ;;;; Scratch (comment