Support reader_tags.clj(c) (#1481)
This commit is contained in:
parent
ca1e2d7769
commit
a19d05b8da
7 changed files with 143 additions and 90 deletions
|
|
@ -11,6 +11,7 @@ A preview of the next release can be installed from
|
||||||
|
|
||||||
- [#1473](https://github.com/babashka/babashka/issues/1473): make relative paths in bb.edn resolve relative to it ([@lispyclouds](https://github.com/lispyclouds))
|
- [#1473](https://github.com/babashka/babashka/issues/1473): make relative paths in bb.edn resolve relative to it ([@lispyclouds](https://github.com/lispyclouds))
|
||||||
- Compatibility with `clojure.tools.namespace.repl/refresh` and `clojure.java.classpath`
|
- Compatibility with `clojure.tools.namespace.repl/refresh` and `clojure.java.classpath`
|
||||||
|
- Support reading tags from `data_readers.clj` and `data_readers.cljc`
|
||||||
|
|
||||||
## 1.1.172 (2023-01-23)
|
## 1.1.172 (2023-01-23)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,11 @@
|
||||||
args (concat args [(str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))])
|
args (concat args [(str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))])
|
||||||
bindings (cond->
|
bindings (cond->
|
||||||
{#'deps/*env* env
|
{#'deps/*env* env
|
||||||
#'deps/*extra-env* extra-env}
|
#'deps/*extra-env* extra-env
|
||||||
|
#'deps/*exit-fn* (fn
|
||||||
|
([_])
|
||||||
|
([_exit-code msg]
|
||||||
|
(throw (Exception. msg))))}
|
||||||
deps-root (assoc #'deps/*dir* (str deps-root)))
|
deps-root (assoc #'deps/*dir* (str deps-root)))
|
||||||
cp (with-out-str (with-bindings bindings
|
cp (with-out-str (with-bindings bindings
|
||||||
(apply deps/-main args)))
|
(apply deps/-main args)))
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@
|
||||||
[clojure.edn :as edn]
|
[clojure.edn :as edn]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.string :as str]
|
[clojure.string :as str]
|
||||||
|
[edamame.core :as edamame]
|
||||||
[hf.depstar.uberjar :as uberjar]
|
[hf.depstar.uberjar :as uberjar]
|
||||||
[sci.addons :as addons]
|
[sci.addons :as addons]
|
||||||
[sci.core :as sci]
|
[sci.core :as sci]
|
||||||
|
|
@ -63,6 +64,7 @@
|
||||||
[sci.impl.copy-vars :as sci-copy-vars]
|
[sci.impl.copy-vars :as sci-copy-vars]
|
||||||
[sci.impl.io :as sio]
|
[sci.impl.io :as sio]
|
||||||
[sci.impl.namespaces :as sci-namespaces]
|
[sci.impl.namespaces :as sci-namespaces]
|
||||||
|
[sci.impl.parser]
|
||||||
[sci.impl.types :as sci-types]
|
[sci.impl.types :as sci-types]
|
||||||
[sci.impl.unrestrict :refer [*unrestricted*]]
|
[sci.impl.unrestrict :refer [*unrestricted*]]
|
||||||
[sci.impl.vars :as vars])
|
[sci.impl.vars :as vars])
|
||||||
|
|
@ -234,8 +236,7 @@ When no eval opts or subcommand is provided, the implicit subcommand is repl.")
|
||||||
(clojure.repl/doc %1$s)
|
(clojure.repl/doc %1$s)
|
||||||
true)" arg)))
|
true)" arg)))
|
||||||
[nil 0]
|
[nil 0]
|
||||||
[nil 1]))
|
[nil 1])))
|
||||||
,)
|
|
||||||
|
|
||||||
(defn print-run-help []
|
(defn print-run-help []
|
||||||
(println (str/trim "
|
(println (str/trim "
|
||||||
|
|
@ -423,8 +424,7 @@ Use bb run --help to show this help output.
|
||||||
;; 'fork (sci/copy-var sci/fork sci-ns)
|
;; 'fork (sci/copy-var sci/fork sci-ns)
|
||||||
}
|
}
|
||||||
'babashka.cli cli/cli-namespace
|
'babashka.cli cli/cli-namespace
|
||||||
'babashka.http-client http-client-namespace
|
'babashka.http-client http-client-namespace}
|
||||||
}
|
|
||||||
features/xml? (assoc 'clojure.data.xml @(resolve 'babashka.impl.xml/xml-namespace)
|
features/xml? (assoc 'clojure.data.xml @(resolve 'babashka.impl.xml/xml-namespace)
|
||||||
'clojure.data.xml.event @(resolve 'babashka.impl.xml/xml-event-namespace)
|
'clojure.data.xml.event @(resolve 'babashka.impl.xml/xml-event-namespace)
|
||||||
'clojure.data.xml.tree @(resolve 'babashka.impl.xml/xml-tree-namespace))
|
'clojure.data.xml.tree @(resolve 'babashka.impl.xml/xml-tree-namespace))
|
||||||
|
|
@ -650,13 +650,13 @@ Use bb run --help to show this help output.
|
||||||
opts-map (assoc opts-map :prn true)]
|
opts-map (assoc opts-map :prn true)]
|
||||||
(recur (next options)
|
(recur (next options)
|
||||||
(update opts-map :expressions (fnil conj []) (first options))))
|
(update opts-map :expressions (fnil conj []) (first options))))
|
||||||
("--main", "-m",)
|
("--main", "-m")
|
||||||
(let [options (next options)]
|
(let [options (next options)]
|
||||||
(assoc opts-map :main (first options)
|
(assoc opts-map :main (first options)
|
||||||
:command-line-args (if (= "--" (second options))
|
:command-line-args (if (= "--" (second options))
|
||||||
(nthrest options 2)
|
(nthrest options 2)
|
||||||
(rest options))))
|
(rest options))))
|
||||||
("--exec", "-x",)
|
("--exec", "-x")
|
||||||
(let [options (next options)]
|
(let [options (next options)]
|
||||||
(assoc opts-map :exec (first options)
|
(assoc opts-map :exec (first options)
|
||||||
:command-line-args (if (= "--" (second options))
|
:command-line-args (if (= "--" (second options))
|
||||||
|
|
@ -780,6 +780,39 @@ Use bb run --help to show this help output.
|
||||||
env-os-name-present? (not= env-os-name sys-os-name)
|
env-os-name-present? (not= env-os-name sys-os-name)
|
||||||
env-os-arch-present? (not= env-os-arch sys-os-arch))))
|
env-os-arch-present? (not= env-os-arch sys-os-arch))))
|
||||||
|
|
||||||
|
(def seen-urls (atom nil))
|
||||||
|
|
||||||
|
(defn read-data-readers [url]
|
||||||
|
(edamame/parse-string (slurp url)
|
||||||
|
{:read-cond :allow
|
||||||
|
:features #{:bb :clj}
|
||||||
|
:eof nil}))
|
||||||
|
|
||||||
|
(defn readers-fn
|
||||||
|
"Lazy reading of data reader functions"
|
||||||
|
[ctx t]
|
||||||
|
(or (@core/data-readers t)
|
||||||
|
(default-data-readers t)
|
||||||
|
(when (simple-symbol? t)
|
||||||
|
(when-let [the-var (sci/resolve ctx t)]
|
||||||
|
(some-> the-var meta :sci.impl.record/map-constructor)))
|
||||||
|
(when-let [f @sci.impl.parser/default-data-reader-fn]
|
||||||
|
(fn [form]
|
||||||
|
(f t form)))
|
||||||
|
(let [;; urls is a vector for equality check
|
||||||
|
urls (vec (.getURLs ^java.net.URLClassLoader @cp/the-url-loader))
|
||||||
|
parsed-resources (or (get @seen-urls urls)
|
||||||
|
(let [^java.net.URLClassLoader cl @cp/the-url-loader
|
||||||
|
resources (concat (enumeration-seq (.getResources cl "data_readers.clj"))
|
||||||
|
(enumeration-seq (.getResources cl "data_readers.cljc")))
|
||||||
|
parsed-resources (apply merge (map read-data-readers resources))
|
||||||
|
_ (swap! seen-urls assoc urls parsed-resources)]
|
||||||
|
parsed-resources))]
|
||||||
|
(when-let [var-sym (get parsed-resources t)]
|
||||||
|
(when-let [the-var (sci/resolve ctx var-sym)]
|
||||||
|
(sci/eval-form ctx (list 'clojure.core/var-set core/data-readers (list 'quote (assoc @core/data-readers t the-var))))
|
||||||
|
the-var)))))
|
||||||
|
|
||||||
(defn exec [cli-opts]
|
(defn exec [cli-opts]
|
||||||
(with-bindings {#'*unrestricted* true
|
(with-bindings {#'*unrestricted* true
|
||||||
clojure.lang.Compiler/LOADER @cp/the-url-loader}
|
clojure.lang.Compiler/LOADER @cp/the-url-loader}
|
||||||
|
|
@ -899,7 +932,8 @@ Use bb run --help to show this help output.
|
||||||
:uberscript uberscript
|
:uberscript uberscript
|
||||||
;; :readers core/data-readers
|
;; :readers core/data-readers
|
||||||
:reify-fn reify-fn
|
:reify-fn reify-fn
|
||||||
:proxy-fn proxy-fn}
|
:proxy-fn proxy-fn
|
||||||
|
:readers #(readers-fn (common/ctx) %)}
|
||||||
opts (addons/future opts)
|
opts (addons/future opts)
|
||||||
sci-ctx (sci/init opts)
|
sci-ctx (sci/init opts)
|
||||||
_ (ctx-store/reset-ctx! sci-ctx)
|
_ (ctx-store/reset-ctx! sci-ctx)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
{r/reverse reader/reversev}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
{r/distinct reader/distinctv}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
(ns reader)
|
||||||
|
|
||||||
|
(defn reversev [data]
|
||||||
|
(vec (reverse data)))
|
||||||
|
|
||||||
|
(defn distinctv [data]
|
||||||
|
(vec (distinct data)))
|
||||||
|
|
@ -82,3 +82,8 @@
|
||||||
(.getResourceAsStream (clojure.lang.RT/baseLoader) \"foo.clj\")
|
(.getResourceAsStream (clojure.lang.RT/baseLoader) \"foo.clj\")
|
||||||
(.getResources (clojure.lang.RT/baseLoader) \"foo.clj\")])")]
|
(.getResources (clojure.lang.RT/baseLoader) \"foo.clj\")])")]
|
||||||
(is (= [true true true] (edn/read-string results)))))
|
(is (= [true true true] (edn/read-string results)))))
|
||||||
|
|
||||||
|
(deftest reader-tag-test
|
||||||
|
(is (= [[3 2 1] [1 2 3]]
|
||||||
|
(bb nil "--classpath" "test-resources/babashka/src_for_classpath_test"
|
||||||
|
"(require 'reader) [#r/reverse [1 2 3] #r/distinct [1 1 2 3]]"))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue