Permalink
Browse files

Implement ns-.

  • Loading branch information...
1 parent 498da66 commit 3af8cf30c3abf92c49ee7fa8610f60004f4b945f Swarm Coders committed Nov 2, 2012
Showing with 58 additions and 20 deletions.
  1. +34 −19 src/meta/verse.clj
  2. +5 −1 test/metaverse/core_test.clj
  3. +3 −0 test/metaverse/loader.clj
  4. +13 −0 test/metaverse/nser.clj
  5. +3 −0 test/metaverse/requirer.clj
View
@@ -16,7 +16,7 @@
(defn transform [ns-form checksum]
(let [transformed-ns (qualify (second ns-form) checksum)]
- `(ns ~transformed-ns ~@(drop 2 ns-form))))
+ `(clojure.core/ns ~transformed-ns ~@(drop 2 ns-form))))
(defn read-all [resource]
(let [reader (LineNumberingPushbackReader. (io/reader resource))
@@ -27,8 +27,11 @@
(defn transformed-sources [lib]
(let [matches (enumeration-seq (.findResources ccl lib))]
(for [match matches
- :let [checksum (sha1 (.getBytes (slurp match)))
- [ns-form & body] (read-all match) ]]
+ :let [checksum (sha1 (.getBytes (slurp match)))
+ ;; TODO: the body must be *read* in the ns of the
+ ;; first element
+ [ns-form & body] (binding [*ns* (the-ns 'user)]
+ (read-all match))]]
(cons (transform ns-form checksum) body))))
(defn load [lib]
@@ -43,21 +46,33 @@
(doseq [transformed (transformed-sources lib)]
(pp/pprint transformed)))
-(defn require [[lib & {:as opts}]]
- (let [qualified-lib (qualify lib (:rev opts))]
- (load (str (#'clojure.core/root-resource lib) ".clj"))
- (when-let [as (:as opts)]
- (alias as qualified-lib))
- (dosync
- (commute @#'clojure.core/*loaded-libs* conj qualified-lib))
- qualified-lib))
-
-;; (defn- ns-clause [[clause-type & args]]
-;; (if (= :require clause-type)
+(defn require [[lib & {:as opts} :as orig-args]]
+ (if-let [rev (:rev opts)]
+ (let [qualified-lib (qualify lib rev)]
+ ;; TODO: check *loaded-libs*, don't reload unless necessary
+ (load (str (subs (#'clojure.core/root-resource lib) 1) ".clj"))
+ (when-let [as (:as opts)]
+ (ns-unalias *ns* as)
+ (alias as qualified-lib))
+ ;; TODO: we might need two concepts of "loaded" for this
+ (dosync
+ (commute @#'clojure.core/*loaded-libs* conj qualified-lib))
+ qualified-lib)
+ (clojure.core/require orig-args)))
+
+(defn- ns-clause [[clause-type & args]]
+ (if (= :require clause-type)
-;; (cons clause-type args)))
+ (cons clause-type args)))
+
+(defn- require? [[clause-type & _]]
+ (= clause-type :require))
+
+(defn- versioned-require [[_ & subclauses]]
+ (for [subclause subclauses]
+ `(require '~subclause)))
-;; (defmacro ns [name & clauses]
-;; `(do (ns ~name
-;; ~@(map ns-clause clauses))
-;; ~@(versioned-requires )))
+(defmacro ns- [name & clauses]
+ (let [[requires others] ((juxt filter remove) require? clauses)]
+ `(do (clojure.core/ns ~name ~@others)
+ ~@(mapcat versioned-require requires))))
@@ -1,10 +1,14 @@
(ns metaverse.core-test
(:require [metaverse.loader :as l]
[metaverse.requirer :as r]
+ [metaverse.nser :as n]
[clojure.test :refer :all]))
(deftest loader-test
(is (= [:original :alternate] (l/get-abcs))))
(deftest requirer-test
- (is (= [:original :alternate] (r/get-abcs))))
+ (is (= [:original :alternate] (r/get-abcs))))
+
+(deftest ns-test
+ (is (= [:original :alternate] (n/get-abcs))))
@@ -1,6 +1,9 @@
(ns metaverse.loader
(:require [meta.verse :as v]))
+(remove-ns 'sample.a.b3c57239c7035149a511ff1baa067c52a948e254)
+(remove-ns 'sample.a.fd869f8b8cee8667e5a5b51487f87aa5d608f8d4)
+
(v/load "sample/a.clj")
(defn get-abcs []
View
@@ -0,0 +1,13 @@
+(remove-ns 'sample.a.b3c57239c7035149a511ff1baa067c52a948e254)
+(remove-ns 'sample.a.fd869f8b8cee8667e5a5b51487f87aa5d608f8d4)
+
+(meta.verse/ns-
+ metaverse.nser
+ (:require [clojure.set :as s]
+ [sample.a :as original
+ :rev "b3c57239c7035149a511ff1baa067c52a948e254"]
+ [sample.a :as alternate
+ :rev "fd869f8b8cee8667e5a5b51487f87aa5d608f8d4"]))
+
+(defn get-abcs []
+ [(original/abc) (alternate/abc)])
@@ -1,6 +1,9 @@
(ns metaverse.requirer
(:require [meta.verse :as v]))
+(remove-ns 'sample.a.b3c57239c7035149a511ff1baa067c52a948e254)
+(remove-ns 'sample.a.fd869f8b8cee8667e5a5b51487f87aa5d608f8d4)
+
(v/require '[sample.a :as original
:rev "b3c57239c7035149a511ff1baa067c52a948e254"])

0 comments on commit 3af8cf3

Please sign in to comment.