From c0c9115b5fd25f8588426b91083d560a70a167c6 Mon Sep 17 00:00:00 2001 From: jem Date: Mon, 17 Feb 2020 20:08:31 +0100 Subject: [PATCH] improve copy-resources! --- src/cryogen_core/classpath_able_io.clj | 30 ++++++++++----------- src/cryogen_core/classpath_able_io/fs.clj | 7 ++++- src/cryogen_core/classpath_able_io/this.clj | 5 ++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 8a17f7f..8431284 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -12,8 +12,7 @@ [cryogen-core.classpath-able-io.cp :as cp] [cryogen-core.classpath-able-io.this :as this] [schema.core :as s]) - (:import [java.nio.file Files StandardCopyOption] - [java.nio.file.attribute FileAttribute])) + (:import [java.nio.file Files])) (def SourceType this/SourceType) (def ResourceType this/ResourceType) @@ -125,21 +124,20 @@ target-path ;:- VirtualPath ignore-patterns ;:- s/Str ] - (let [resource-paths - (sort (get-resource-paths-recursive fs-prefix base-path source-paths))] - (if (empty? resource-paths) - (throw (IllegalArgumentException. (str "resource " resource-paths ", " + (let [resources + (sort + this/compare-resource + (get-resources-recursive fs-prefix base-path source-paths))] + (if (empty? resources) + (throw (IllegalArgumentException. (str "resource " base-path ", " source-paths " not found"))) - (doseq [resource-path resource-paths] - (let [target-file (fs/absolut-path target-path resource-path) - source-file (path-from-cp-or-fs - fs-prefix - base-path - resource-path)] - (when (Files/isDirectory source-file fs/no-link-option) - (Files/createDirectories target-file (into-array FileAttribute []))) - (when (Files/isRegularFile source-file fs/no-link-option) - (Files/copy source-file target-file (into-array StandardCopyOption [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING])) + (doseq [resource resources] + (let [target-path (fs/absolut-path target-path (:virtual-path resource)) + source-path (:java-path resource)] + (when (this/is-dir? resource) + (Files/createDirectories target-path fs/no-attributes)) + (when (this/is-file? resource) + (Files/copy source-path target-path fs/overwrite-preserve-attributes) )))))) (defn distinct-resources-by-path diff --git a/src/cryogen_core/classpath_able_io/fs.clj b/src/cryogen_core/classpath_able_io/fs.clj index dd3ca21..30bffb0 100644 --- a/src/cryogen_core/classpath_able_io/fs.clj +++ b/src/cryogen_core/classpath_able_io/fs.clj @@ -8,11 +8,16 @@ (ns cryogen-core.classpath-able-io.fs (:require [cryogen-core.classpath-able-io.this :as this]) - (:import [java.nio.file Paths Files LinkOption])) + (:import [java.nio.file Paths Files LinkOption StandardCopyOption] + [java.nio.file.attribute FileAttribute])) ; ----------------------- Domain functions ------------------------ (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) (def follow-link-option (into-array LinkOption [])) +(def no-attributes (into-array FileAttribute [])) +(def overwrite-preserve-attributes + (into-array StandardCopyOption + [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING])) (defn user-dir [] (java.lang.System/getProperty "user.dir")) diff --git a/src/cryogen_core/classpath_able_io/this.clj b/src/cryogen_core/classpath_able_io/this.clj index 86ba05a..db6d31e 100644 --- a/src/cryogen_core/classpath_able_io/this.clj +++ b/src/cryogen_core/classpath_able_io/this.clj @@ -37,3 +37,8 @@ (st/join "/" (filter #(not (empty? %)) path-elements))) + +(s/defn compare-resource + [first :- Resource + second :- Resource] + (compare (:virtual-path first) (:virtual-path second)))