sci: record file metadata on var

This commit is contained in:
Michiel Borkent 2020-01-18 18:25:38 +01:00
parent ef7a75be5b
commit d1aaa97df6
4 changed files with 8 additions and 6 deletions

2
sci

@ -1 +1 @@
Subproject commit 35ed8cced594f2733ca02d9b7c0b2e7a984adfb3
Subproject commit c84dbaa110cf01b56be38f58c5a7f14cc030f90d

View file

@ -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}))))

View file

@ -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"))))

View file

@ -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)))