[#11] implement load-file
This commit is contained in:
parent
00db597c7a
commit
c876b67f95
4 changed files with 46 additions and 27 deletions
2
sci
2
sci
|
|
@ -1 +1 @@
|
||||||
Subproject commit 1d41480d9dca2cd62f8cd341392919d1b64183ec
|
Subproject commit 87938f46813e73e0f2d8b5ebf4a421b0f0b75031
|
||||||
|
|
@ -76,15 +76,19 @@
|
||||||
-io: combination of -i and -o.
|
-io: combination of -i and -o.
|
||||||
--stream: stream over lines or EDN values from stdin. Combined with -i *in* becomes a single line per iteration.
|
--stream: stream over lines or EDN values from stdin. Combined with -i *in* becomes a single line per iteration.
|
||||||
--file or -f: read expressions from file instead of argument wrapped in an implicit do.
|
--file or -f: read expressions from file instead of argument wrapped in an implicit do.
|
||||||
|
--time: print execution time before exiting.
|
||||||
"))
|
"))
|
||||||
|
|
||||||
|
(defn wrap-do [s]
|
||||||
|
(format "(do %s)" s))
|
||||||
|
|
||||||
(defn read-file [file]
|
(defn read-file [file]
|
||||||
(let [f (io/file file)]
|
(let [f (io/file file)]
|
||||||
(if (.exists f)
|
(if (.exists f)
|
||||||
(as-> (slurp file) x
|
(as-> (slurp file) x
|
||||||
;; remove shebang
|
;; remove shebang
|
||||||
(str/replace x #"^#!.*" "")
|
(str/replace x #"^#!.*" "")
|
||||||
(format "(do %s)" x))
|
(wrap-do x))
|
||||||
(throw (Exception. (str "File does not exist: " file))))))
|
(throw (Exception. (str "File does not exist: " file))))))
|
||||||
|
|
||||||
(defn get-env
|
(defn get-env
|
||||||
|
|
@ -138,6 +142,11 @@
|
||||||
(System/setProperty "javax.net.ssl.trustStore" ca-certs)
|
(System/setProperty "javax.net.ssl.trustStore" ca-certs)
|
||||||
(System/setProperty "javax.net.ssl.tru stAnchors" ca-certs)))
|
(System/setProperty "javax.net.ssl.tru stAnchors" ca-certs)))
|
||||||
|
|
||||||
|
(defn load-file* [ctx file]
|
||||||
|
(let [s (slurp file)
|
||||||
|
s (wrap-do s)]
|
||||||
|
(sci/eval-string s ctx)))
|
||||||
|
|
||||||
(defn main
|
(defn main
|
||||||
[& args]
|
[& args]
|
||||||
#_(binding [*out* *err*]
|
#_(binding [*out* *err*]
|
||||||
|
|
@ -147,6 +156,7 @@
|
||||||
:help? :file :command-line-args
|
:help? :file :command-line-args
|
||||||
:expression :stream? :time?] :as _opts}
|
:expression :stream? :time?] :as _opts}
|
||||||
(parse-opts args)
|
(parse-opts args)
|
||||||
|
env (atom {})
|
||||||
exit-code
|
exit-code
|
||||||
(or
|
(or
|
||||||
#_(binding [*out* *err*]
|
#_(binding [*out* *err*]
|
||||||
|
|
@ -159,7 +169,7 @@
|
||||||
:else
|
:else
|
||||||
(try
|
(try
|
||||||
|
|
||||||
(let [expr (if file (read-file file) (format "(do %s)" expression))
|
(let [expr (if file (read-file file) (wrap-do expression))
|
||||||
read-next #(if stream?
|
read-next #(if stream?
|
||||||
(if raw-in (or (read-line) ::EOF)
|
(if raw-in (or (read-line) ::EOF)
|
||||||
(read-edn))
|
(read-edn))
|
||||||
|
|
@ -168,27 +178,30 @@
|
||||||
(parse-shell-string in)
|
(parse-shell-string in)
|
||||||
(edn/read-string in)))))]
|
(edn/read-string in)))))]
|
||||||
(loop [in (read-next)]
|
(loop [in (read-next)]
|
||||||
(if (identical? ::EOF in)
|
(let [ctx {:bindings (assoc bindings
|
||||||
[nil 0] ;; done streaming
|
(with-meta '*in*
|
||||||
(let [res [(do (when-not (or expression file)
|
(when-not stream? {:sci/deref! true})) in
|
||||||
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
|
#_(with-meta 'bb/*in*
|
||||||
(let [res (sci/eval-string
|
{:sci/deref! true}) #_do-in
|
||||||
expr
|
'*command-line-args* command-line-args)
|
||||||
{:bindings (assoc bindings
|
:env env}
|
||||||
(with-meta '*in*
|
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))]
|
||||||
(when-not stream? {:sci/deref! true})) in
|
(if (identical? ::EOF in)
|
||||||
#_(with-meta 'bb/*in*
|
[nil 0] ;; done streaming
|
||||||
{:sci/deref! true}) #_do-in
|
(let [res [(do (when-not (or expression file)
|
||||||
'*command-line-args* command-line-args)})]
|
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
|
||||||
(if raw-out
|
(let [res (sci/eval-string
|
||||||
(if (coll? res)
|
expr
|
||||||
(doseq [l res]
|
ctx)]
|
||||||
(println l))
|
(if raw-out
|
||||||
(println res))
|
(if (coll? res)
|
||||||
((if println? println? prn) res)))) 0]]
|
(doseq [l res]
|
||||||
(if stream?
|
(println l))
|
||||||
(recur (read-next))
|
(println res))
|
||||||
res)))))
|
((if println? println? prn) res)))) 0]]
|
||||||
|
(if stream?
|
||||||
|
(recur (read-next))
|
||||||
|
res))))))
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(binding [*out* *err*]
|
(binding [*out* *err*]
|
||||||
(let [d (ex-data e)
|
(let [d (ex-data e)
|
||||||
|
|
|
||||||
|
|
@ -86,7 +86,7 @@
|
||||||
(deftest ssl-test
|
(deftest ssl-test
|
||||||
(let [graalvm-home (System/getenv "GRAALVM_HOME")
|
(let [graalvm-home (System/getenv "GRAALVM_HOME")
|
||||||
lib-path (format "%1$s/jre/lib:%1$s/jre/lib/amd64" graalvm-home)
|
lib-path (format "%1$s/jre/lib:%1$s/jre/lib/amd64" graalvm-home)
|
||||||
_ (prn "lib-path" lib-path)
|
;; _ (prn "lib-path" lib-path)
|
||||||
resp (bb nil (format "(System/setProperty \"java.library.path\" \"%s\")
|
resp (bb nil (format "(System/setProperty \"java.library.path\" \"%s\")
|
||||||
(slurp \"https://www.google.com\")"
|
(slurp \"https://www.google.com\")"
|
||||||
lib-path))]
|
lib-path))]
|
||||||
|
|
@ -99,3 +99,9 @@
|
||||||
(is (= x (test-utils/bb x "--stream" "-io" "*in*"))))
|
(is (= x (test-utils/bb x "--stream" "-io" "*in*"))))
|
||||||
(let [x "f\n\b\n"]
|
(let [x "f\n\b\n"]
|
||||||
(is (= x (test-utils/bb x "--stream" "-io" "(subs *in* 0 1)")))))
|
(is (= x (test-utils/bb x "--stream" "-io" "(subs *in* 0 1)")))))
|
||||||
|
|
||||||
|
(deftest load-file-test
|
||||||
|
(let [tmp (java.io.File/createTempFile "script" ".clj")]
|
||||||
|
(spit tmp "(defn foo [x y] (+ x y)) (defn bar [x y] (* x y))")
|
||||||
|
(is (= "120\n" (test-utils/bb nil (format "(load-file \"%s\") (bar (foo 10 30) 3)"
|
||||||
|
(.getPath tmp)))))))
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
(if input
|
(if input
|
||||||
(with-in-str input
|
(with-in-str input
|
||||||
(apply main/main args))
|
(apply main/main args))
|
||||||
(apply main/main input args))))]
|
(apply main/main args))))]
|
||||||
(if-let [err ^String (not-empty (str sw))]
|
(if-let [err ^String (not-empty (str sw))]
|
||||||
(throw (Exception. err)) res)))
|
(throw (Exception. err)) res)))
|
||||||
|
|
||||||
|
|
@ -21,7 +21,7 @@
|
||||||
(try (if input
|
(try (if input
|
||||||
(apply bb (conj (vec args)
|
(apply bb (conj (vec args)
|
||||||
{:in input}))
|
{:in input}))
|
||||||
(apply bb input args))
|
(apply bb args))
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(let [d (ex-data e)
|
(let [d (ex-data e)
|
||||||
err-msg (or (:stderr (ex-data e)) "")]
|
err-msg (or (:stderr (ex-data e)) "")]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue