Last active
September 1, 2021 19:07
-
-
Save pithyless/20939406045e2bb2ccd8a64075b458cb to your computer and use it in GitHub Desktop.
A CLJC version of potemkin/import-vars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns my.util.namespace | |
#?(:cljs (:require-macros [my.util.namespace])) | |
(:require | |
#?(:clj [potemkin :as potemkin]))) | |
(defmacro cljs-import-vars [& syms] | |
(let [unravel (fn unravel [x] | |
(if (sequential? x) | |
(->> x | |
rest | |
(mapcat unravel) | |
(map | |
#(symbol | |
(str (first x) | |
(when-let [n (namespace %)] | |
(str "." n))) | |
(name %)))) | |
[x])) | |
syms (mapcat unravel syms)] | |
`(do | |
~@(map | |
(fn [sym] | |
`(def ~(symbol (name sym)) ~sym)) | |
syms)))) | |
(defmacro clj-import-vars [& syms] | |
`(potemkin/import-vars ~@syms)) | |
(defn cljs-env? | |
"https://github.com/Prismatic/schema/blob/master/src/clj/schema/macros.clj" | |
[env] (boolean (:ns env))) | |
(defmacro import-vars | |
[& syms] | |
(let [cljs? (cljs-env? &env)] | |
(if cljs? | |
`(cljs-import-vars ~@syms) | |
`(clj-import-vars ~@syms)))) | |
(comment | |
;; Importing macros: | |
#?(:clj (import-vars [ns | |
some-macro some-macro2])) | |
;; Importing vars: | |
(import-vars [ns | |
some-def some-defn]) | |
) |
@mjmeintjes - This code is not mine, so I'm not licensing it. As far as I can tell, I probably borrowed parts of this from:
- https://github.com/rfkm/zou/blob/master/common/src/zou/util/namespace.cljc
- https://github.com/plumatic/schema/blob/master/src/clj/schema/macros.clj
- https://github.com/clj-commons/potemkin/blob/master/src/potemkin/namespaces.clj
I can't say for certain, since this is an old gist and I'm not using this code in production anywhere. (Slight off topic, but I've stopped trying to use potemkin-style importing in my codebases, because there is a lot of friction with tooling like clj-kondo, clojure-lsp, etc.) All those mentioned projects are licensed EPL and/or MIT, for what it's worth. Good luck!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the license on this code? Can I include it in a library?