Support reader_tags.clj(c) (#1481)

This commit is contained in:
Michiel Borkent 2023-02-03 21:21:56 +01:00 committed by GitHub
parent ca1e2d7769
commit a19d05b8da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 90 deletions

View file

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

View file

@ -92,7 +92,11 @@
args (concat args [(str "-A:" (str/join ":" (cons ":org.babashka/defaults" aliases)))])
bindings (cond->
{#'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)))
cp (with-out-str (with-bindings bindings
(apply deps/-main args)))

View file

@ -56,6 +56,7 @@
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[edamame.core :as edamame]
[hf.depstar.uberjar :as uberjar]
[sci.addons :as addons]
[sci.core :as sci]
@ -63,6 +64,7 @@
[sci.impl.copy-vars :as sci-copy-vars]
[sci.impl.io :as sio]
[sci.impl.namespaces :as sci-namespaces]
[sci.impl.parser]
[sci.impl.types :as sci-types]
[sci.impl.unrestrict :refer [*unrestricted*]]
[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)
true)" arg)))
[nil 0]
[nil 1]))
,)
[nil 1])))
(defn print-run-help []
(println (str/trim "
@ -423,8 +424,7 @@ Use bb run --help to show this help output.
;; 'fork (sci/copy-var sci/fork sci-ns)
}
'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)
'clojure.data.xml.event @(resolve 'babashka.impl.xml/xml-event-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)]
(recur (next options)
(update opts-map :expressions (fnil conj []) (first options))))
("--main", "-m",)
("--main", "-m")
(let [options (next options)]
(assoc opts-map :main (first options)
:command-line-args (if (= "--" (second options))
(nthrest options 2)
(rest options))))
("--exec", "-x",)
("--exec", "-x")
(let [options (next options)]
(assoc opts-map :exec (first 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-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]
(with-bindings {#'*unrestricted* true
clojure.lang.Compiler/LOADER @cp/the-url-loader}
@ -899,7 +932,8 @@ Use bb run --help to show this help output.
:uberscript uberscript
;; :readers core/data-readers
:reify-fn reify-fn
:proxy-fn proxy-fn}
:proxy-fn proxy-fn
:readers #(readers-fn (common/ctx) %)}
opts (addons/future opts)
sci-ctx (sci/init opts)
_ (ctx-store/reset-ctx! sci-ctx)

View file

@ -0,0 +1 @@
{r/reverse reader/reversev}

View file

@ -0,0 +1 @@
{r/distinct reader/distinctv}

View file

@ -0,0 +1,7 @@
(ns reader)
(defn reversev [data]
(vec (reverse data)))
(defn distinctv [data]
(vec (distinct data)))

View file

@ -82,3 +82,8 @@
(.getResourceAsStream (clojure.lang.RT/baseLoader) \"foo.clj\")
(.getResources (clojure.lang.RT/baseLoader) \"foo.clj\")])")]
(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]]"))))