From b4943e1caa68824a603febf8d5b9caad3db9c571 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 24 Dec 2010 21:24:34 -0800 Subject: [PATCH] Allow :clean-non-project-classes to be a seq of regexes. --- NEWS | 4 ++++ sample.project.clj | 4 +++- src/leiningen/compile.clj | 15 +++++++++++---- test/test_compile.clj | 17 ++++++++++++++++- .../checkouts/sample2/src/sample2/alt.clj | 1 + .../checkouts/sample2/src/sample2/core.clj | 1 + 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 test_projects/sample/checkouts/sample2/src/sample2/alt.clj diff --git a/NEWS b/NEWS index ce0bdaccc..119c96f2c 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ Leiningen NEWS -- history of user-visible changes = 1.4.2 / ??? +* Allow a seq of regexes in :clean-non-project-classes for more flexibility. + +* Fix a bug where the first argument to run would be parsed wrong. (Alex Osborne) + * Use JVM_OPTS environment variable instead of JAVA_OPTS, though the latter is still supported for backwards-compatibility. diff --git a/sample.project.clj b/sample.project.clj index 3ff905f03..c8630935d 100644 --- a/sample.project.clj +++ b/sample.project.clj @@ -43,7 +43,9 @@ :disable-implicit-clean false ;; Delete .class files that do not have a correspoinding package in ;; the src/ directory. Workaround for Clojure bug CLJ-322. Causes problems - ;; with protocols in upstream libraries; false by default. + ;; with protocols in upstream libraries; false by default. Set to + ;; true to delete all non-project classes or set to a seq of regexes + ;; to only delete class files that match one of the regexes. :clean-non-project-classes true ;; Load these namespaces on startup to pick up hooks from them. Hooks ;; generally come from plugins, but may be included in your project source. diff --git a/src/leiningen/compile.clj b/src/leiningen/compile.clj index d200178f9..d912eb752 100644 --- a/src/leiningen/compile.clj +++ b/src/leiningen/compile.clj @@ -211,17 +211,24 @@ (:compile-path project) source-path))))) -(defn- keep-class? [project f] +(defn- class-in-project? [project f] (or (has-source-package? project f (:source-path project)) (has-source-package? project f (:java-source-path project)) (.exists (file (str (.replace (.getParent f) (:compile-path project) (:source-path project)) ".clj"))))) -(defn delete-non-project-classes [project] +(defn- blacklisted-class? [project f] + ;; true indicates all non-project classes are blacklisted + (or (true? (:clean-non-project-classes project)) + (let [relative (subs (.getAbsolutePath f) (count (:root project)))] + (some #(re-find % relative) (:clean-non-project-classes project))))) + +(defn clean-non-project-classes [project] (when (:clean-non-project-classes project) (doseq [f (file-seq (file (:compile-path project))) - :when (and (.isFile f) (not (keep-class? project f)))] + :when (and (.isFile f) (not (class-in-project? project f)) + (blacklisted-class? project f))] (.delete f)))) (defn- status [code msg] @@ -251,7 +258,7 @@ those given as command-line arguments." (clojure.core/compile namespace#)))) (success "Compilation succeeded.") (failure "Compilation failed.")) - (finally (delete-non-project-classes project)))) + (finally (clean-non-project-classes project)))) (success "All namespaces already :aot compiled.")) (success "No namespaces to :aot compile listed in project.clj."))) ([project & namespaces] diff --git a/test/test_compile.clj b/test/test_compile.clj index 887dfa0f5..8187a2f38 100644 --- a/test/test_compile.clj +++ b/test/test_compile.clj @@ -40,7 +40,22 @@ #"nom\$_main__\d+.class" #"nom.class" #"nom__init.class"]] (is (some (partial re-find r) classes) (format "missing %s" r)))) (is (not (.exists (file "test_projects" "sample" - "classes" "sample2" "core.class"))))) + "classes" "sample2" "core.class")))) + (is (not (.exists (file "test_projects" "sample" + "classes" "sample2" "alt.class"))))) + +(deftest test-cleared-transitive-aot-by-regexes + (is (zero? (compile (assoc (make-project "test_projects/sample") + :clean-non-project-classes [#"core"])))) + (let [classes (seq (.list (file "test_projects" "sample" + "classes" "nom" "nom")))] + (doseq [r [#"nom\$fn__\d+.class" #"nom\$loading__\d+__auto____\d+.class" + #"nom\$_main__\d+.class" #"nom.class" #"nom__init.class"]] + (is (some (partial re-find r) classes) (format "missing %s" r)))) + (is (not (.exists (file "test_projects" "sample" + "classes" "sample2" "core.class")))) + (is (.exists (file "test_projects" "sample" "classes" + "sample2" "alt__init.class")))) (deftest test-spaces-in-project-path (binding [leiningen.compile/get-raw-input-args diff --git a/test_projects/sample/checkouts/sample2/src/sample2/alt.clj b/test_projects/sample/checkouts/sample2/src/sample2/alt.clj new file mode 100644 index 000000000..648393eaf --- /dev/null +++ b/test_projects/sample/checkouts/sample2/src/sample2/alt.clj @@ -0,0 +1 @@ +(ns sample2.alt) diff --git a/test_projects/sample/checkouts/sample2/src/sample2/core.clj b/test_projects/sample/checkouts/sample2/src/sample2/core.clj index 0cf18b7c0..83833c86c 100644 --- a/test_projects/sample/checkouts/sample2/src/sample2/core.clj +++ b/test_projects/sample/checkouts/sample2/src/sample2/core.clj @@ -1,2 +1,3 @@ (ns sample2.core + (:require sample2.alt) (:gen-class))