From be641e770d4107e3412e4630eddfde1369f2993c Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 6 May 2011 20:05:55 -0700 Subject: [PATCH] Un-generalize code injection. Use for Hooke only. There's too much opportunity for collision to make this a generalized feature; let's just use it to get the six forms required for add-hook and thus retest/test selectors. --- src/leiningen/compile.clj | 26 ++++++++------------------ src/leiningen/test.clj | 2 +- test/leiningen/test/compile.clj | 4 ++++ 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/leiningen/compile.clj b/src/leiningen/compile.clj index 72409837..bbcb7af9 100644 --- a/src/leiningen/compile.clj +++ b/src/leiningen/compile.clj @@ -20,9 +20,6 @@ (def *skip-auto-compile* false) -(def ^{:doc "A list of namespaces to inject into project subprocesses."} - injected-namespaces (atom ['robert.hooke])) - (defn- regex? "Returns true if we have regex class" [str-or-re] @@ -130,21 +127,15 @@ (defn- get-jvm-args [project] (concat (get-input-args) (:jvm-opts project) (:jvm-opts (user-settings)))) -(defn- injected-namespace-forms [ns-name] - (with-open [rdr (-> ns-name ns->path resource reader PushbackReader.)] - (doall (take-while #(not= ::done %) - (repeatedly #(read rdr false ::done)))))) - -(defn- get-injected-form [injected-namespaces] - ;; Note: there's a posibility of conflicts here if a project - ;; requires a newer (say) hooke than Leiningen's, but since this - ;; injection doesn't affect clojure.core/*loaded-libs*, if the - ;; project requires hooke, it will load over the injected copy. - (mapcat injected-namespace-forms injected-namespaces)) +(defn- injected-forms [] + (with-open [rdr (-> "robert/hooke.clj" resource reader PushbackReader.)] + `(do (ns ~'leiningen.util.injected) + ~@(doall (take 6 (rest (repeatedly #(read rdr))))) + (ns ~'user)))) (defn get-readable-form [java project form init] (let [form `(do ~init - ~@(get-injected-form @injected-namespaces) + ~(injected-forms) (set! ~'*warn-on-reflection* ~(:warn-on-reflection project)) ~form)] @@ -165,9 +156,8 @@ ;; TODO: split this function up (defn eval-in-project "Executes form in an isolated classloader with the classpath and compile path - set correctly for the project. Pass in a handler function to have it called - with the java task right before executing if you need to customize any of its - properties (classpath, library-path, etc)." + set correctly for the project. If the form depends on any requires, put them + in the init arg to avoid the Gilardi Scenario: http://technomancy.us/143" [project form & [handler skip-auto-compile init]] (when skip-auto-compile (println "WARNING: eval-in-project's skip-auto-compile arg is deprecated.")) diff --git a/src/leiningen/test.clj b/src/leiningen/test.clj index 2765c032..5aa49dc9 100644 --- a/src/leiningen/test.clj +++ b/src/leiningen/test.clj @@ -10,7 +10,7 @@ (defn- form-for-hook-selectors [selectors] `(when (seq ~selectors) - (robert.hooke/add-hook + (leiningen.util.injected/add-hook (resolve 'clojure.test/test-var) (fn test-var-with-selector [test-var# var#] (when (reduce #(or %1 (%2 (assoc (meta var#) ::var var#))) diff --git a/test/leiningen/test/compile.clj b/test/leiningen/test/compile.clj index e72da3a6..3c655e4f 100644 --- a/test/leiningen/test/compile.clj +++ b/test/leiningen/test/compile.clj @@ -64,3 +64,7 @@ (delete-file-recursively (file (:root tricky-name-project) "classes") :silent) (is (zero? (compile tricky-name-project))) (is (empty? (.list (file (:root tricky-name-project) "classes"))))) + +(deftest test-injection + (is (zero? (eval-in-project sample-project + '#'leiningen.util.injected/add-hook))))