Merge pull request #1776 from jcrossley3/1721
A more efficient alternative fix for #1721
This commit is contained in:
commit
1296288b59
2 changed files with 39 additions and 25 deletions
|
@ -1,8 +1,7 @@
|
||||||
(ns leiningen.clean
|
(ns leiningen.clean
|
||||||
"Remove all files from project's target-path."
|
"Remove all files from project's target-path."
|
||||||
(:require [clojure.java.io :as io]
|
(:require [clojure.java.io :as io]
|
||||||
[leiningen.core.utils :as utils]
|
[leiningen.core.utils :as utils])
|
||||||
[leiningen.core.project :as project])
|
|
||||||
(:import [java.io IOException]))
|
(:import [java.io IOException]))
|
||||||
|
|
||||||
(defn real-directory?
|
(defn real-directory?
|
||||||
|
@ -80,26 +79,24 @@
|
||||||
(error-msg
|
(error-msg
|
||||||
(format "Deleting non-target project paths [\"%s\"] is not allowed." clean-target)))))))
|
(format "Deleting non-target project paths [\"%s\"] is not allowed." clean-target)))))))
|
||||||
|
|
||||||
(defn clean-targets
|
(defn- with-parent-target-path
|
||||||
"Return a seq of the project's clean targets."
|
"Assoc the :target-path sans the profile suffix, if any format
|
||||||
|
specifier is detected in the raw :target-path"
|
||||||
[project]
|
[project]
|
||||||
(for [target-key (:clean-targets project)]
|
(if-let [tp (->> project meta :without-profiles :target-path (re-find #"(.*?)/[^/]*%") second)]
|
||||||
(when-let [target (cond (vector? target-key) (get-in project target-key)
|
(assoc project :target-path (if (.isAbsolute (io/file tp))
|
||||||
(keyword? target-key) (target-key project)
|
tp
|
||||||
(string? target-key) target-key)]
|
(str (io/file (:root project) tp))))
|
||||||
(flatten [target]))))
|
project))
|
||||||
|
|
||||||
(defn clean
|
(defn clean
|
||||||
"Removes all files from paths in clean-targets for a project, and for all
|
"Removes all files from paths in clean-targets for a project"
|
||||||
of its profiles."
|
|
||||||
[project]
|
[project]
|
||||||
(doseq [targets (->> (project/read-profiles project)
|
(let [project (with-parent-target-path project)]
|
||||||
keys
|
(doseq [target-key (:clean-targets project)]
|
||||||
(map vector)
|
(when-let [target (cond (vector? target-key) (get-in project target-key)
|
||||||
(map (partial project/set-profiles project))
|
(keyword? target-key) (target-key project)
|
||||||
(cons project)
|
(string? target-key) target-key)]
|
||||||
(mapcat clean-targets)
|
(doseq [f (flatten [target])]
|
||||||
distinct)]
|
(sanity-check project f)
|
||||||
(doseq [f targets]
|
(delete-file-recursively f :silently))))))
|
||||||
(sanity-check project f)
|
|
||||||
(delete-file-recursively f :silently))))
|
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
(:use [clojure.test]
|
(:use [clojure.test]
|
||||||
[clojure.java.io :only [file make-parents writer]]
|
[clojure.java.io :only [file make-parents writer]]
|
||||||
[leiningen.clean :only [clean]]
|
[leiningen.clean :only [clean]]
|
||||||
[leiningen.test.helper :only [sample-project]]))
|
[leiningen.test.helper :only [sample-project]])
|
||||||
|
(:require [leiningen.core.project :as project]))
|
||||||
|
|
||||||
(def target-1 (:target-path sample-project))
|
(def target-1 (:target-path sample-project))
|
||||||
(def target-2 (str (file (:root sample-project) "target-2")))
|
(def target-2 (str (file (:root sample-project) "target-2")))
|
||||||
|
@ -28,9 +29,9 @@
|
||||||
|
|
||||||
(defn assert-cleaned
|
(defn assert-cleaned
|
||||||
"Asserts that the mock was called for the given target path."
|
"Asserts that the mock was called for the given target path."
|
||||||
[test-path]
|
[path]
|
||||||
(is (some #(= test-path (first %)) @delete-calls)
|
(is (some (comp #(.startsWith path %) first) @delete-calls)
|
||||||
(format "delete-files-recursively was not called for %s" test-path)))
|
(format "delete-files-recursively was not called for %s" path)))
|
||||||
|
|
||||||
(defn relative-to-absolute-project-path
|
(defn relative-to-absolute-project-path
|
||||||
"Converts a relative path to an absolute path within the sample project"
|
"Converts a relative path to an absolute path within the sample project"
|
||||||
|
@ -123,3 +124,19 @@
|
||||||
:clean-targets ^{:protect false} [test-dir])]
|
:clean-targets ^{:protect false} [test-dir])]
|
||||||
(clean modified-project)
|
(clean modified-project)
|
||||||
(assert-cleaned test-dir)))))
|
(assert-cleaned test-dir)))))
|
||||||
|
|
||||||
|
(deftest spliced-target-paths
|
||||||
|
(let [p (-> (project/make {:root "/a/b/c" :target-path "foo/bar/%s"})
|
||||||
|
(project/set-profiles [:dev]))]
|
||||||
|
(is (= "/a/b/c/foo/bar/dev" (:target-path p)))
|
||||||
|
(clean p)
|
||||||
|
(assert-cleaned "/a/b/c/foo/bar/dev")))
|
||||||
|
|
||||||
|
(deftest absolute-spliced-target-path
|
||||||
|
(let [p (-> (project/make {:root "/a/b/c"
|
||||||
|
:target-path "/foo/bar/%s"
|
||||||
|
:clean-targets ^{:protect false} [:target-path]})
|
||||||
|
(project/set-profiles [:dev]))]
|
||||||
|
(is (= "/foo/bar/dev" (:target-path p)))
|
||||||
|
(clean p)
|
||||||
|
(assert-cleaned "/foo/bar/dev")))
|
||||||
|
|
Loading…
Reference in a new issue