Permalink
Browse files

Implement require function.

  • Loading branch information...
1 parent 8baf1ca commit 498da660f411e6871360ecd39a6ecebaef663dad Swarm Coders committed Nov 2, 2012
Showing with 39 additions and 19 deletions.
  1. +23 −17 src/meta/verse.clj
  2. +5 −2 test/metaverse/core_test.clj
  3. +11 −0 test/metaverse/requirer.clj
View
40 src/meta/verse.clj
@@ -1,19 +1,3 @@
-;; (ns clojure.core)
-
-;; (defn versioned-lib [lib rev]
-;; (if (or (nil? rev) (= :head rev))
-;; lib
-;; ))
-
-;; (defn- versioned-require [[lib & {:as opts}]]
-;; (load (root-resource (versioned-lib lib (:rev opts)))))
-
-;; (defn- ns-clause [clauses])
-
-;; (defmacro ns- [name & clauses]
-;; `(ns ~name
-;; ~@(map ns-clause clauses)))
-
(ns meta.verse
(:refer-clojure :exclude [load require ns])
(:require [clojure.java.io :as io]
@@ -27,8 +11,11 @@
(defonce ccl (.getContextClassLoader (Thread/currentThread)))
+(defn qualify [lib checksum]
+ (symbol (str lib "." checksum)))
+
(defn transform [ns-form checksum]
- (let [transformed-ns (symbol (str (second ns-form) "." checksum))]
+ (let [transformed-ns (qualify (second ns-form) checksum)]
`(ns ~transformed-ns ~@(drop 2 ns-form))))
(defn read-all [resource]
@@ -51,7 +38,26 @@
(doseq [form transformed]
(eval form)))))
+;; for debugging
(defn pprint [lib]
(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)
+
+;; (cons clause-type args)))
+
+;; (defmacro ns [name & clauses]
+;; `(do (ns ~name
+;; ~@(map ns-clause clauses))
+;; ~@(versioned-requires )))
View
7 test/metaverse/core_test.clj
@@ -1,7 +1,10 @@
(ns metaverse.core-test
(:require [metaverse.loader :as l]
- ;; [metaverse.requirer :as r]
+ [metaverse.requirer :as r]
[clojure.test :refer :all]))
(deftest loader-test
- (is (= [:original :alternate] (l/get-abcs))))
+ (is (= [:original :alternate] (l/get-abcs))))
+
+(deftest requirer-test
+ (is (= [:original :alternate] (r/get-abcs))))
View
11 test/metaverse/requirer.clj
@@ -0,0 +1,11 @@
+(ns metaverse.requirer
+ (:require [meta.verse :as v]))
+
+(v/require '[sample.a :as original
+ :rev "b3c57239c7035149a511ff1baa067c52a948e254"])
+
+(v/require '[sample.a :as alternate
+ :rev "fd869f8b8cee8667e5a5b51487f87aa5d608f8d4"])
+
+(defn get-abcs []
+ [(original/abc) (alternate/abc)])

0 comments on commit 498da66

Please sign in to comment.