From 1a93bb1adf36ae331977e52a7b1d995ed3f60489 Mon Sep 17 00:00:00 2001 From: Yannick Scherer Date: Sun, 15 Feb 2015 13:43:40 +0100 Subject: [PATCH] remove 'potemkin' dependency. (see #26) --- project.clj | 3 +- src/rewrite_clj/node.clj | 2 +- src/rewrite_clj/node/coerce.clj | 2 +- src/rewrite_clj/node/protocols.clj | 2 +- src/rewrite_clj/potemkin.clj | 139 +++++++++++++++++++++++++++++ src/rewrite_clj/zip.clj | 4 +- 6 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 src/rewrite_clj/potemkin.clj diff --git a/project.clj b/project.clj index fb9d2f7c..2fd36edb 100644 --- a/project.clj +++ b/project.clj @@ -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"] diff --git a/src/rewrite_clj/node.clj b/src/rewrite_clj/node.clj index 18cfbb72..68246d92 100644 --- a/src/rewrite_clj/node.clj +++ b/src/rewrite_clj/node.clj @@ -17,7 +17,7 @@ token uneval whitespace] - [potemkin :refer [import-vars]])) + [rewrite-clj.potemkin :refer [import-vars]])) ;; ## API Facade diff --git a/src/rewrite_clj/node/coerce.clj b/src/rewrite_clj/node/coerce.clj index 1d2ac880..bb5ac3f6 100644 --- a/src/rewrite_clj/node/coerce.clj +++ b/src/rewrite_clj/node/coerce.clj @@ -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 diff --git a/src/rewrite_clj/node/protocols.clj b/src/rewrite_clj/node/protocols.clj index 4b7d159e..fa28d642 100644 --- a/src/rewrite_clj/node/protocols.clj +++ b/src/rewrite_clj/node/protocols.clj @@ -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 diff --git a/src/rewrite_clj/potemkin.clj b/src/rewrite_clj/potemkin.clj new file mode 100644 index 00000000..9aa56b6c --- /dev/null +++ b/src/rewrite_clj/potemkin.clj @@ -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#)))) diff --git a/src/rewrite_clj/zip.clj b/src/rewrite_clj/zip.clj index 9286d914..4ed64f0a 100644 --- a/src/rewrite_clj/zip.clj +++ b/src/rewrite_clj/zip.clj @@ -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