Skip to content

Instantly share code, notes, and snippets.

@pithyless
Last active September 1, 2021 19:07
Show Gist options
  • Save pithyless/20939406045e2bb2ccd8a64075b458cb to your computer and use it in GitHub Desktop.
Save pithyless/20939406045e2bb2ccd8a64075b458cb to your computer and use it in GitHub Desktop.
A CLJC version of potemkin/import-vars
(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
Copy link

What's the license on this code? Can I include it in a library?

@pithyless
Copy link
Author

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

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