Skip to content

Commit 1a93bb1

Browse files
author
Yannick Scherer
committed
remove 'potemkin' dependency. (see #26)
1 parent 519d557 commit 1a93bb1

File tree

6 files changed

+145
-7
lines changed

6 files changed

+145
-7
lines changed

project.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
66
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}
77
:dependencies [[org.clojure/clojure "1.6.0"]
8-
[org.clojure/tools.reader "0.8.13"]
9-
[potemkin "0.3.11"]]
8+
[org.clojure/tools.reader "0.8.13"]]
109
:profiles {:dev {:dependencies [[midje "1.6.3" :exclusions [joda-time]]
1110
[joda-time "2.7"]]
1211
:plugins [[lein-midje "3.1.3"]

src/rewrite_clj/node.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
token
1818
uneval
1919
whitespace]
20-
[potemkin :refer [import-vars]]))
20+
[rewrite-clj.potemkin :refer [import-vars]]))
2121

2222
;; ## API Facade
2323

src/rewrite_clj/node/coerce.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns ^:no-doc rewrite-clj.node.coerce
2-
(:require [potemkin :refer [defprotocol+]]
2+
(:require [rewrite-clj.potemkin :refer [defprotocol+]]
33
[rewrite-clj.node
44
comment forms integer keyword
55
quote string uneval

src/rewrite_clj/node/protocols.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns
22
^{:added "0.4.0"}
33
rewrite-clj.node.protocols
4-
(:require [potemkin :refer [defprotocol+]]
4+
(:require [rewrite-clj.potemkin :refer [defprotocol+]]
55
[clojure.string :as string]))
66

77
;; ## Node

src/rewrite_clj/potemkin.clj

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
(ns rewrite-clj.potemkin)
2+
3+
;; --- copied from ztellman/potemkin
4+
;;
5+
;; Copyright (c) 2013 Zachary Tellman
6+
;;
7+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
8+
;; of this software and associated documentation files (the "Software"), to
9+
;; deal in the Software without restriction, including without limitation the
10+
;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
;; sell copies of the Software, and to permit persons to whom the Software is
12+
;; furnished to do so, subject to the following conditions:
13+
;;
14+
;; The above copyright notice and this permission notice shall be included in
15+
;; all copies or substantial portions of the Software.
16+
;;
17+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
;; IN THE SOFTWARE.
24+
;;
25+
;; ---
26+
27+
;; --- potemkin.namespaces
28+
29+
(defn link-vars
30+
"Makes sure that all changes to `src` are reflected in `dst`."
31+
[src dst]
32+
(add-watch src dst
33+
(fn [_ src old new]
34+
(alter-var-root dst (constantly @src))
35+
(alter-meta! dst merge (dissoc (meta src) :name)))))
36+
37+
(defmacro import-fn
38+
"Given a function in another namespace, defines a function with the
39+
same name in the current namespace. Argument lists, doc-strings,
40+
and original line-numbers are preserved."
41+
([sym]
42+
`(import-fn ~sym nil))
43+
([sym name]
44+
(let [vr (resolve sym)
45+
m (meta vr)
46+
n (or name (:name m))
47+
arglists (:arglists m)
48+
protocol (:protocol m)]
49+
(when-not vr
50+
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
51+
(when (:macro m)
52+
(throw (IllegalArgumentException.
53+
(str "Calling import-fn on a macro: " sym))))
54+
55+
`(do
56+
(def ~(with-meta n {:protocol protocol}) (deref ~vr))
57+
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
58+
(link-vars ~vr (var ~n))
59+
~vr))))
60+
61+
(defmacro import-macro
62+
"Given a macro in another namespace, defines a macro with the same
63+
name in the current namespace. Argument lists, doc-strings, and
64+
original line-numbers are preserved."
65+
([sym]
66+
`(import-macro ~sym nil))
67+
([sym name]
68+
(let [vr (resolve sym)
69+
m (meta vr)
70+
n (or name (with-meta (:name m) {}))
71+
arglists (:arglists m)]
72+
(when-not vr
73+
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
74+
(when-not (:macro m)
75+
(throw (IllegalArgumentException.
76+
(str "Calling import-macro on a non-macro: " sym))))
77+
`(do
78+
(def ~n ~(resolve sym))
79+
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
80+
(.setMacro (var ~n))
81+
(link-vars ~vr (var ~n))
82+
~vr))))
83+
84+
(defmacro import-def
85+
"Given a regular def'd var from another namespace, defined a new var with the
86+
same name in the current namespace."
87+
([sym]
88+
`(import-def ~sym nil))
89+
([sym name]
90+
(let [vr (resolve sym)
91+
m (meta vr)
92+
n (or name (:name m))
93+
n (with-meta n (if (:dynamic m) {:dynamic true} {}))
94+
nspace (:ns m)]
95+
(when-not vr
96+
(throw (IllegalArgumentException. (str "Don't recognize " sym))))
97+
`(do
98+
(def ~n @~vr)
99+
(alter-meta! (var ~n) merge (dissoc (meta ~vr) :name))
100+
(link-vars ~vr (var ~n))
101+
~vr))))
102+
103+
(defmacro import-vars
104+
"Imports a list of vars from other namespaces."
105+
[& syms]
106+
(let [unravel (fn unravel [x]
107+
(if (sequential? x)
108+
(->> x
109+
rest
110+
(mapcat unravel)
111+
(map
112+
#(symbol
113+
(str (first x)
114+
(when-let [n (namespace %)]
115+
(str "." n)))
116+
(name %))))
117+
[x]))
118+
syms (mapcat unravel syms)]
119+
`(do
120+
~@(map
121+
(fn [sym]
122+
(let [vr (resolve sym)
123+
m (meta vr)]
124+
(cond
125+
(:macro m) `(import-macro ~sym)
126+
(:arglists m) `(import-fn ~sym)
127+
:else `(import-def ~sym))))
128+
syms))))
129+
130+
;; --- potemkin.types
131+
132+
(defmacro defprotocol+
133+
"A simpler version of 'potemkin.types/defprotocol+'."
134+
[name & body]
135+
(let [prev-body (-> name resolve meta :potemkin/body)]
136+
(when-not (= prev-body body)
137+
`(let [p# (defprotocol ~name ~@body)]
138+
(alter-meta! (resolve p#) assoc :potemkin/body '~body)
139+
p#))))

src/rewrite_clj/zip.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
[whitespace :as ws]]
1616
[rewrite-clj
1717
[parser :as p]
18+
[potemkin :refer [import-vars]]
1819
[node :as node]]
19-
[clojure.zip :as z]
20-
[potemkin :refer [import-vars]]))
20+
[clojure.zip :as z]))
2121

2222
;; ## API Facade
2323

0 commit comments

Comments
 (0)