From 20db9c621f206d092e35417171c80be2e7c1375d Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 25 Feb 2020 11:26:14 +0100 Subject: [PATCH] added filter Co-authored-by: Jan Krebs Co-authored-by: Clemens Geibel --- src/cryogen_core/classpath_able_io.clj | 27 ++++++++++++++++++++------ src/cryogen_core/new_io.clj | 6 +----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index de4c05d..fcf62bc 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -7,7 +7,8 @@ ; You must not remove this notice, or any other, from this software. (ns cryogen-core.classpath-able-io - (:require [cryogen-core.classpath-able-io.fs :as fs] + (:require [clojure.string :as st] + [cryogen-core.classpath-able-io.fs :as fs] [cryogen-core.classpath-able-io.cp :as cp] [cryogen-core.classpath-able-io.this :as this] [schema.core :as s]) @@ -21,10 +22,27 @@ (def JavaPath this/JavaPath) ; java.nio.Path (def Resource this/Resource) +(defn get-file-extension-from-resource + [resource] + (str "." (last (st/split (:virtual-path resource) #"\.")))) + +(defn get-filename-from-resource + [resource] + (last (st/split (:virtual-path resource) #"\/"))) + (defn filter-for-ignore-patterns [ignore-patterns source-list] (filter #(not (re-matches ignore-patterns %)) source-list)) +(defn filter-resources-for-ignore-patterns + [ignore-patterns resources] + (let [files (filter #(= (:resource-type %) :file) resources) + dirs (filter #(= (:resource-type %) :dir) resources) + filtered-files (filter + #(not (re-matches (re-pattern ignore-patterns) (get-filename-from-resource %))) + files)] +(concat dirs filtered-files))) + (defn resource-from-cp-or-fs ;:- Resource [fs-prefix ;:- Prefix base-path ;:- VirtualPath @@ -106,7 +124,6 @@ (doseq [resource-path resource-paths] (Files/delete (fs/absolut-path virtual-path resource-path)))))) -; TODO: add ignore patterns filtering (defn copy-resources! [fs-prefix ;:- Prefix base-path ;:- VirtualPath @@ -114,10 +131,8 @@ target-path ;:- VirtualPath ignore-patterns ;:- s/Str ] - (let [resources - (sort - this/compare-resource - (get-resources fs-prefix base-path source-paths))] + (let [resources (sort this/compare-resource (get-resources fs-prefix base-path source-paths)) + resources (filter-resources-for-ignore-patterns ignore-patterns resources) ] (if (empty? resources) (throw (IllegalArgumentException. (str "resource " base-path ", " source-paths " not found"))) diff --git a/src/cryogen_core/new_io.clj b/src/cryogen_core/new_io.clj index 74cc31f..3300f8a 100644 --- a/src/cryogen_core/new_io.clj +++ b/src/cryogen_core/new_io.clj @@ -62,10 +62,6 @@ (st/join "/") (#(st/replace % #"/+" "/")))) -(defn get-file-extension-from-resource - [resource] - (str "." (last (st/split (:virtual-path resource) #"\.")))) - (defn find-assets "Find all assets in the given root directory (f) and the given file extension (ext) ignoring any files that match the given (ignored-files). @@ -74,7 +70,7 @@ if no, return empty vector." [base-path paths fs-prefix ext ignored-files] (let [assets (cp-io/get-resources fs-prefix base-path paths) filter-file (fn [xs] (filter #(= (:resource-type %) :file) xs)) - filter-ext (fn [xs] (filter #(= (get-file-extension-from-resource %) ext) xs)) + filter-ext (fn [xs] (filter #(= (cp-io/get-file-extension-from-resource %) ext) xs)) cast-file (fn [java-path] (io/as-file (.toString java-path))) get-java-path (fn [map-entry] (cast-file (:java-path map-entry)))] (->> assets