Skip to content

Commit

Permalink
Allow :clean-non-project-classes to be a seq of regexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Dec 25, 2010
1 parent 665ab9a commit b4943e1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 3 additions & 1 deletion sample.project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions src/leiningen/compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
17 changes: 16 additions & 1 deletion test/test_compile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test_projects/sample/checkouts/sample2/src/sample2/alt.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns sample2.alt)
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(ns sample2.core
(:require sample2.alt)
(:gen-class))

0 comments on commit b4943e1

Please sign in to comment.