[#601] Support clojure.java.io/Coercions
This commit is contained in:
parent
24f6f8f2a8
commit
b3fbe7e12e
2 changed files with 64 additions and 12 deletions
|
|
@ -1,15 +1,67 @@
|
||||||
(ns babashka.impl.clojure.java.io
|
(ns babashka.impl.clojure.java.io
|
||||||
{:no-doc true}
|
{:no-doc true}
|
||||||
(:require [clojure.java.io :as io]))
|
(:require [clojure.java.io :as io]
|
||||||
|
[sci.core :as sci :refer [copy-var]]
|
||||||
|
[sci.impl.types :as types])
|
||||||
|
(:import [java.io File FileInputStream]))
|
||||||
|
|
||||||
|
;;;; datafy
|
||||||
|
|
||||||
|
(defmulti as-file types/type-impl)
|
||||||
|
|
||||||
|
(defmethod as-file :sci.impl.protocols/reified [x]
|
||||||
|
(let [methods (types/getMethods x)]
|
||||||
|
((get methods 'as-file) x)))
|
||||||
|
|
||||||
|
(defmethod as-file :default [x]
|
||||||
|
(io/as-file x))
|
||||||
|
|
||||||
|
(defmulti as-url types/type-impl)
|
||||||
|
|
||||||
|
(defmethod as-file :sci.impl.protocols/reified [x]
|
||||||
|
(let [methods (types/getMethods x)]
|
||||||
|
((get methods 'as-url) x)))
|
||||||
|
|
||||||
|
(defmethod as-url :default [x]
|
||||||
|
(io/as-url x))
|
||||||
|
|
||||||
|
;; patch this impl with new impl of as-file
|
||||||
|
(defn ^String as-relative-path
|
||||||
|
"Take an as-file-able thing and return a string if it is
|
||||||
|
a relative path, else IllegalArgumentException."
|
||||||
|
{:added "1.2"}
|
||||||
|
[x]
|
||||||
|
(let [^File f (as-file x)]
|
||||||
|
(if (.isAbsolute f)
|
||||||
|
(throw (IllegalArgumentException. (str f " is not a relative path")))
|
||||||
|
(.getPath f))))
|
||||||
|
|
||||||
|
;; patch this impl with new impl of as-file and as-relative-path
|
||||||
|
(defn ^java.io.File file
|
||||||
|
"Returns a java.io.File, passing each arg to as-file. Multiple-arg
|
||||||
|
versions treat the first argument as parent and subsequent args as
|
||||||
|
children relative to the parent."
|
||||||
|
{:added "1.2"}
|
||||||
|
([arg]
|
||||||
|
(as-file arg))
|
||||||
|
([parent child]
|
||||||
|
(File. ^File (as-file parent) ^String (as-relative-path child)))
|
||||||
|
([parent child & more]
|
||||||
|
(reduce file (file parent child) more)))
|
||||||
|
|
||||||
|
(def io-ns (sci/create-ns 'clojure.java.io nil))
|
||||||
|
|
||||||
(def io-namespace
|
(def io-namespace
|
||||||
{'as-relative-path io/as-relative-path
|
{'Coercions (sci/new-var 'Coercions {:methods #{'as-file 'as-url}
|
||||||
'as-url io/as-url
|
:ns io-ns})
|
||||||
'copy io/copy
|
'as-relative-path (copy-var as-relative-path io-ns)
|
||||||
'delete-file io/delete-file
|
'as-file as-file
|
||||||
'file io/file
|
'file (copy-var file io-ns)
|
||||||
'input-stream io/input-stream
|
'as-url as-url
|
||||||
'make-parents io/make-parents
|
'copy (copy-var io/copy io-ns)
|
||||||
'output-stream io/output-stream
|
'delete-file (copy-var io/delete-file io-ns)
|
||||||
'reader io/reader
|
'input-stream (copy-var io/input-stream io-ns)
|
||||||
'writer io/writer})
|
'make-parents (copy-var io/make-parents io-ns)
|
||||||
|
'output-stream (copy-var io/output-stream io-ns)
|
||||||
|
'reader (copy-var io/reader io-ns)
|
||||||
|
'writer (copy-var io/writer io-ns)})
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
(println "===" (-> m :var meta :name))
|
(println "===" (-> m :var meta :name))
|
||||||
(println))
|
(println))
|
||||||
|
|
||||||
(defmethod clojure.test/report :end-test-var [m]
|
(defmethod clojure.test/report :end-test-var [_m]
|
||||||
(let [{:keys [:fail :error]} @*report-counters*]
|
(let [{:keys [:fail :error]} @*report-counters*]
|
||||||
(when (and (= "true" (System/getenv "BABASHKA_FAIL_FAST"))
|
(when (and (= "true" (System/getenv "BABASHKA_FAIL_FAST"))
|
||||||
(or (pos? fail) (pos? error)))
|
(or (pos? fail) (pos? error)))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue