[#11] implement load-file

This commit is contained in:
Michiel Borkent 2019-08-17 23:44:17 +02:00 committed by GitHub
parent 00db597c7a
commit c876b67f95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 27 deletions

2
sci

@ -1 +1 @@
Subproject commit 1d41480d9dca2cd62f8cd341392919d1b64183ec
Subproject commit 87938f46813e73e0f2d8b5ebf4a421b0f0b75031

View file

@ -76,15 +76,19 @@
-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.
--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]
(let [f (io/file file)]
(if (.exists f)
(as-> (slurp file) x
;; remove shebang
(str/replace x #"^#!.*" "")
(format "(do %s)" x))
(wrap-do x))
(throw (Exception. (str "File does not exist: " file))))))
(defn get-env
@ -138,6 +142,11 @@
(System/setProperty "javax.net.ssl.trustStore" 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
[& args]
#_(binding [*out* *err*]
@ -147,6 +156,7 @@
:help? :file :command-line-args
:expression :stream? :time?] :as _opts}
(parse-opts args)
env (atom {})
exit-code
(or
#_(binding [*out* *err*]
@ -159,7 +169,7 @@
:else
(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?
(if raw-in (or (read-line) ::EOF)
(read-edn))
@ -168,27 +178,30 @@
(parse-shell-string in)
(edn/read-string in)))))]
(loop [in (read-next)]
(if (identical? ::EOF in)
[nil 0] ;; done streaming
(let [res [(do (when-not (or expression file)
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
(let [res (sci/eval-string
expr
{:bindings (assoc bindings
(with-meta '*in*
(when-not stream? {:sci/deref! true})) in
#_(with-meta 'bb/*in*
{:sci/deref! true}) #_do-in
'*command-line-args* command-line-args)})]
(if raw-out
(if (coll? res)
(doseq [l res]
(println l))
(println res))
((if println? println? prn) res)))) 0]]
(if stream?
(recur (read-next))
res)))))
(let [ctx {:bindings (assoc bindings
(with-meta '*in*
(when-not stream? {:sci/deref! true})) in
#_(with-meta 'bb/*in*
{:sci/deref! true}) #_do-in
'*command-line-args* command-line-args)
:env env}
ctx (update ctx :bindings assoc 'load-file #(load-file* ctx %))]
(if (identical? ::EOF in)
[nil 0] ;; done streaming
(let [res [(do (when-not (or expression file)
(throw (Exception. (str args "Babashka expected an expression. Type --help to print help."))))
(let [res (sci/eval-string
expr
ctx)]
(if raw-out
(if (coll? res)
(doseq [l res]
(println l))
(println res))
((if println? println? prn) res)))) 0]]
(if stream?
(recur (read-next))
res))))))
(catch Exception e
(binding [*out* *err*]
(let [d (ex-data e)

View file

@ -86,7 +86,7 @@
(deftest ssl-test
(let [graalvm-home (System/getenv "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\")
(slurp \"https://www.google.com\")"
lib-path))]
@ -99,3 +99,9 @@
(is (= x (test-utils/bb x "--stream" "-io" "*in*"))))
(let [x "f\n\b\n"]
(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)))))))

View file

@ -12,7 +12,7 @@
(if input
(with-in-str input
(apply main/main args))
(apply main/main input args))))]
(apply main/main args))))]
(if-let [err ^String (not-empty (str sw))]
(throw (Exception. err)) res)))
@ -21,7 +21,7 @@
(try (if input
(apply bb (conj (vec args)
{:in input}))
(apply bb input args))
(apply bb args))
(catch Exception e
(let [d (ex-data e)
err-msg (or (:stderr (ex-data e)) "")]