[#538] support top-level requires
This commit is contained in:
parent
4dd102ca19
commit
9fffa80def
7 changed files with 36 additions and 19 deletions
|
|
@ -616,6 +616,10 @@ $ bb my-script.clj
|
||||||
Hello from gist script!
|
Hello from gist script!
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Caveat: building uberscripts works by statically inspecting top-level `ns` forms
|
||||||
|
and `require` forms. The rest of the code is not evaluated. Code that relies on
|
||||||
|
dynamic requires may not work in an uberscript.
|
||||||
|
|
||||||
## Parsing command line arguments
|
## Parsing command line arguments
|
||||||
|
|
||||||
Babashka ships with `clojure.tools.cli`:
|
Babashka ships with `clojure.tools.cli`:
|
||||||
|
|
|
||||||
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit 4f40b916faa394c37447c99da2e9f2605dc62d87
|
Subproject commit 5e3a4d6556bd6fe5523fc0f60b36a9184508c740
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
(ns my.impl
|
(ns my.impl1
|
||||||
(:require [clojure.string]))
|
(:require [clojure.string]))
|
||||||
|
|
||||||
(defn impl-fn
|
(defn impl-fn
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
(ns my.impl2
|
(ns my.impl2
|
||||||
(:require [my.impl :as impl]))
|
(:require [my.impl1 :as impl1]))
|
||||||
|
|
||||||
(def impl-fn impl/impl-fn)
|
(def impl-fn impl1/impl-fn)
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
(ns my.main
|
(ns my.main
|
||||||
(:require [my.impl :as impl] ;; my.impl is already loaded, so it will not be loaded again (normally)
|
(:require [my.impl1 :as impl1] ;; my.impl is already loaded, so it will not be loaded again (normally)
|
||||||
[my.impl2] ;; but my.impl2 also loads my.impl
|
;; but my.impl2 also loads my.impl
|
||||||
[my.impl3 :refer [foo]]))
|
[my.impl2]))
|
||||||
|
|
||||||
|
;; top-level requires are also supported
|
||||||
|
(require '[my.impl3 :refer [foo]])
|
||||||
|
|
||||||
(defn -main [& args]
|
(defn -main [& args]
|
||||||
;; this function is defined non-top-level and may cause problems
|
;; this function is defined non-top-level and may cause problems
|
||||||
(foo)
|
(foo)
|
||||||
;; this should just return args
|
;; this should just return args
|
||||||
(impl/impl-fn args))
|
(impl1/impl-fn args))
|
||||||
|
|
|
||||||
|
|
@ -34,17 +34,6 @@
|
||||||
(is (= "\"my.main2\""
|
(is (= "\"my.main2\""
|
||||||
(str/trim (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2"))))))
|
(str/trim (tu/bb nil "--classpath" "test-resources/babashka/src_for_classpath_test" "-m" "my.main2"))))))
|
||||||
|
|
||||||
(deftest uberscript-test
|
|
||||||
(let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")]
|
|
||||||
(.deleteOnExit 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")))
|
|
||||||
(testing "order of namespaces is correct"
|
|
||||||
(tu/bb nil "--classpath" "test-resources/babashka/uberscript/src" "-m" "my.main" "--uberscript" (.getPath tmp-file))
|
|
||||||
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
|
||||||
(tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))))))
|
|
||||||
|
|
||||||
(deftest error-while-loading-test
|
(deftest error-while-loading-test
|
||||||
(is (true?
|
(is (true?
|
||||||
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
|
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
|
||||||
|
|
|
||||||
21
test/babashka/uberscript_test.clj
Normal file
21
test/babashka/uberscript_test.clj
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
(ns babashka.uberscript-test
|
||||||
|
(:require
|
||||||
|
[babashka.test-utils :as tu]
|
||||||
|
[clojure.edn :as edn]
|
||||||
|
[clojure.test :as t :refer [deftest is testing]]))
|
||||||
|
|
||||||
|
(defn bb [input & args]
|
||||||
|
(edn/read-string (apply tu/bb (when (some? input) (str input)) (map str args))))
|
||||||
|
|
||||||
|
(deftest uberscript-test
|
||||||
|
(let [tmp-file (java.io.File/createTempFile "uberscript" ".clj")]
|
||||||
|
(.deleteOnExit 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")))
|
||||||
|
(testing "order of namespaces is correct"
|
||||||
|
(tu/bb nil "--classpath" "test-resources/babashka/uberscript/src" "-m" "my.main" "--uberscript" (.getPath tmp-file))
|
||||||
|
(spit "/tmp/foo.clj" (slurp (.getPath tmp-file)))
|
||||||
|
(is (= "(\"1\" \"2\" \"3\" \"4\")\n"
|
||||||
|
(tu/bb nil "--file" (.getPath tmp-file) "1" "2" "3" "4"))))))
|
||||||
|
|
||||||
Loading…
Reference in a new issue