Merge pull request #1776 from jcrossley3/1721

A more efficient alternative fix for #1721
This commit is contained in:
Phil Hagelberg 2014-12-05 08:55:07 -08:00
commit 1296288b59
2 changed files with 39 additions and 25 deletions

View file

@ -1,8 +1,7 @@
(ns leiningen.clean
"Remove all files from project's target-path."
(:require [clojure.java.io :as io]
[leiningen.core.utils :as utils]
[leiningen.core.project :as project])
[leiningen.core.utils :as utils])
(:import [java.io IOException]))
(defn real-directory?
@ -80,26 +79,24 @@
(error-msg
(format "Deleting non-target project paths [\"%s\"] is not allowed." clean-target)))))))
(defn clean-targets
"Return a seq of the project's clean targets."
(defn- with-parent-target-path
"Assoc the :target-path sans the profile suffix, if any format
specifier is detected in the raw :target-path"
[project]
(for [target-key (:clean-targets project)]
(when-let [target (cond (vector? target-key) (get-in project target-key)
(keyword? target-key) (target-key project)
(string? target-key) target-key)]
(flatten [target]))))
(if-let [tp (->> project meta :without-profiles :target-path (re-find #"(.*?)/[^/]*%") second)]
(assoc project :target-path (if (.isAbsolute (io/file tp))
tp
(str (io/file (:root project) tp))))
project))
(defn clean
"Removes all files from paths in clean-targets for a project, and for all
of its profiles."
"Removes all files from paths in clean-targets for a project"
[project]
(doseq [targets (->> (project/read-profiles project)
keys
(map vector)
(map (partial project/set-profiles project))
(cons project)
(mapcat clean-targets)
distinct)]
(doseq [f targets]
(sanity-check project f)
(delete-file-recursively f :silently))))
(let [project (with-parent-target-path project)]
(doseq [target-key (:clean-targets project)]
(when-let [target (cond (vector? target-key) (get-in project target-key)
(keyword? target-key) (target-key project)
(string? target-key) target-key)]
(doseq [f (flatten [target])]
(sanity-check project f)
(delete-file-recursively f :silently))))))

View file

@ -2,7 +2,8 @@
(:use [clojure.test]
[clojure.java.io :only [file make-parents writer]]
[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-2 (str (file (:root sample-project) "target-2")))
@ -28,9 +29,9 @@
(defn assert-cleaned
"Asserts that the mock was called for the given target path."
[test-path]
(is (some #(= test-path (first %)) @delete-calls)
(format "delete-files-recursively was not called for %s" test-path)))
[path]
(is (some (comp #(.startsWith path %) first) @delete-calls)
(format "delete-files-recursively was not called for %s" path)))
(defn relative-to-absolute-project-path
"Converts a relative path to an absolute path within the sample project"
@ -123,3 +124,19 @@
:clean-targets ^{:protect false} [test-dir])]
(clean modified-project)
(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")))