Skip to content

Commit

Permalink
remove 'potemkin' dependency. (see #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Scherer committed Feb 15, 2015
1 parent 519d557 commit 1a93bb1
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 7 deletions.
3 changes: 1 addition & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/tools.reader "0.8.13"]
[potemkin "0.3.11"]]
[org.clojure/tools.reader "0.8.13"]]
:profiles {:dev {:dependencies [[midje "1.6.3" :exclusions [joda-time]]
[joda-time "2.7"]]
:plugins [[lein-midje "3.1.3"]
Expand Down
2 changes: 1 addition & 1 deletion src/rewrite_clj/node.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
token
uneval
whitespace]
[potemkin :refer [import-vars]]))
[rewrite-clj.potemkin :refer [import-vars]]))

;; ## API Facade

Expand Down
2 changes: 1 addition & 1 deletion src/rewrite_clj/node/coerce.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns ^:no-doc rewrite-clj.node.coerce
(:require [potemkin :refer [defprotocol+]]
(:require [rewrite-clj.potemkin :refer [defprotocol+]]
[rewrite-clj.node
comment forms integer keyword
quote string uneval
Expand Down
2 changes: 1 addition & 1 deletion src/rewrite_clj/node/protocols.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns
^{:added "0.4.0"}
rewrite-clj.node.protocols
(:require [potemkin :refer [defprotocol+]]
(:require [rewrite-clj.potemkin :refer [defprotocol+]]
[clojure.string :as string]))

;; ## Node
Expand Down
139 changes: 139 additions & 0 deletions src/rewrite_clj/potemkin.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
(ns rewrite-clj.potemkin)

;; --- copied from ztellman/potemkin
;;
;; Copyright (c) 2013 Zachary Tellman
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to
;; deal in the Software without restriction, including without limitation the
;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
;; sell copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
;; IN THE SOFTWARE.
;;
;; ---

;; --- potemkin.namespaces

(defn link-vars
"Makes sure that all changes to `src` are reflected in `dst`."
[src dst]
(add-watch src dst
(fn [_ src old new]
(alter-var-root dst (constantly @src))
(alter-meta! dst merge (dissoc (meta src) :name)))))

(defmacro import-fn
"Given a function in another namespace, defines a function with the
same name in the current namespace. Argument lists, doc-strings,
and original line-numbers are preserved."
([sym]
`(import-fn ~sym nil))
([sym name]
(let [vr (resolve sym)
m (meta vr)
n (or name (:name m))
arglists (:arglists m)
protocol (:protocol m)]
(when-not vr
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
(when (:macro m)
(throw (IllegalArgumentException.
(str "Calling import-fn on a macro: " sym))))

`(do
(def ~(with-meta n {:protocol protocol}) (deref ~vr))
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
(link-vars ~vr (var ~n))
~vr))))

(defmacro import-macro
"Given a macro in another namespace, defines a macro with the same
name in the current namespace. Argument lists, doc-strings, and
original line-numbers are preserved."
([sym]
`(import-macro ~sym nil))
([sym name]
(let [vr (resolve sym)
m (meta vr)
n (or name (with-meta (:name m) {}))
arglists (:arglists m)]
(when-not vr
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
(when-not (:macro m)
(throw (IllegalArgumentException.
(str "Calling import-macro on a non-macro: " sym))))
`(do
(def ~n ~(resolve sym))
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
(.setMacro (var ~n))
(link-vars ~vr (var ~n))
~vr))))

(defmacro import-def
"Given a regular def'd var from another namespace, defined a new var with the
same name in the current namespace."
([sym]
`(import-def ~sym nil))
([sym name]
(let [vr (resolve sym)
m (meta vr)
n (or name (:name m))
n (with-meta n (if (:dynamic m) {:dynamic true} {}))
nspace (:ns m)]
(when-not vr
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
`(do
(def ~n @~vr)
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
(link-vars ~vr (var ~n))
~vr))))

(defmacro import-vars
"Imports a list of vars from other namespaces."
[& 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]
(let [vr (resolve sym)
m (meta vr)]
(cond
(:macro m) `(import-macro ~sym)
(:arglists m) `(import-fn ~sym)
:else `(import-def ~sym))))
syms))))

;; --- potemkin.types

(defmacro defprotocol+
"A simpler version of 'potemkin.types/defprotocol+'."
[name & body]
(let [prev-body (-> name resolve meta :potemkin/body)]
(when-not (= prev-body body)
`(let [p# (defprotocol ~name ~@body)]
(alter-meta! (resolve p#) assoc :potemkin/body '~body)
p#))))
4 changes: 2 additions & 2 deletions src/rewrite_clj/zip.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
[whitespace :as ws]]
[rewrite-clj
[parser :as p]
[potemkin :refer [import-vars]]
[node :as node]]
[clojure.zip :as z]
[potemkin :refer [import-vars]]))
[clojure.zip :as z]))

;; ## API Facade

Expand Down

0 comments on commit 1a93bb1

Please sign in to comment.