Skip to content

Commit

Permalink
[new] Add experimental load-inline
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Nov 10, 2024
1 parent 336e4a1 commit b6dd2b6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5585,6 +5585,36 @@

;;;;

#?(:clj
(defn ^:no-doc read-resource
"Private, don't use."
[rname]
(if-not [res (jio/resource rname)]
(throw
(ex-info (str "[encore/read-resource] Resource not found on classpath: " rname)
{:rname (typed-val rname)}))

(with-open [reader (java.io.PushbackReader. (jio/reader res))]
(loop [forms []]
(let [form (read reader false ::eof)]
(if (= form ::eof)
forms
(recur (conj forms form)))))))))

#?(:clj
(defmacro ^:no-doc load-inline
"Private, don't use.
Injects Clojure/Script code from named resource on classpath:
#?(:clj (load-inline \"my-code.clj\")
:cljs (load-inline \"my-code.cljs\"))"
[rname] `(do ~@(read-resource rname))))

(comment
(run! eval (read-resource "taoensso/encore/bytes.clj"))
(load-inline "taoensso/encore/bytes.clj"))

;;;;

#?(:clj
(defn get-pom-version
"Returns POM version string for given Maven dependency, or nil."
Expand Down
8 changes: 8 additions & 0 deletions test/taoensso/encore_tests.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@
(is (= (c :=+ 2) 8))
(is (= @c 10))]))

;;;; Loading

#?(:clj (enc/load-inline "taoensso/encore_tests/loadme.clj")
:cljs (enc/load-inline "taoensso/encore_tests/loadme.cljs"))

(deftest _load-inline
[(is (= (get-loaded) #?(:clj :clj, :cljs :cljs)))])

;;;; Reductions

(deftest _reduce-zip
Expand Down
4 changes: 4 additions & 0 deletions test/taoensso/encore_tests/loadme.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

(def loaded :clj)
(defn get-loaded [] loaded)

3 changes: 3 additions & 0 deletions test/taoensso/encore_tests/loadme.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

(def loaded :cljs)
(defn get-loaded [] loaded)

0 comments on commit b6dd2b6

Please sign in to comment.