From 42de012fb41429594ef7513b3990e7558fbb170f Mon Sep 17 00:00:00 2001 From: jem Date: Tue, 17 Dec 2019 18:51:29 +0100 Subject: [PATCH] now using resource type internaly --- src/cryogen_core/classpath_able_io.clj | 60 +++++++++------- test/cryogen_core/classpath_able_io_test.clj | 73 +++++++++++++++----- 2 files changed, 92 insertions(+), 41 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index cbd54c9..254ec4a 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -67,6 +67,10 @@ (.isFile file) :file :else :unknown)})) +(s/defn is-file? :- s/Bool + [resource :- Resource] + (= :file (:resource-type resource))) + (s/defn file-from-cp ; :- File [resource-path :- Path] (let [file-from-cp (io/file (io/resource resource-path))] @@ -86,17 +90,21 @@ (catch Exception e nil)))) -(defn resource-from-cp-or-filesystem ; :- Resource +(defn resource-from-cp-or-fs ; :- Resource [fs-prefix ; :- Prefix + base-path ; :- Path resource-path ; :- Path & {:keys [from-cp from-fs] :or {from-cp true from-fs true}}] - (let [file-from-fs (if from-fs - (file-from-fs fs-prefix resource-path) + (let [full-path (if (empty? base-path) + resource-path + (str base-path "/" resource-path)) + file-from-fs (if from-fs + (file-from-fs fs-prefix full-path) nil) file-from-cp (if from-cp - (file-from-cp resource-path) + (file-from-cp full-path) nil)] (cond (some? file-from-fs) @@ -105,20 +113,19 @@ (create-resource resource-path file-from-cp :classpath) :else nil))) -(defn uri-from-cp-or-filesystem - [fs-prefix resource-path +(defn file-from-cp-or-fs ; :- File + [fs-prefix ; :- Prefix + base-path ; :- Path + resource-path; :- Path & {:keys [from-cp from-fs] :or {from-cp true from-fs true}}] - (let [from-fs-file (if from-fs - (file-from-fs fs-prefix resource-path) - nil) - from-cp-file (if from-cp - (file-from-cp resource-path) - nil)] - (if (some? from-fs-file) - from-fs-file - from-cp-file))) + (let [resource (resource-from-cp-or-fs + fs-prefix base-path resource-path + :from-cp from-cp + :from-fs from-fs)] + (when (some? resource) + (:file resource)))) (defn get-resources-recursive ;:- [Resource] [fs-prefix ;:- Prefix @@ -132,15 +139,18 @@ (if (not (empty? paths)) (do (let [path-to-work-with (first paths) - resource-to-work-with (resource-from-cp-or-filesystem + resource-to-work-with (resource-from-cp-or-fs fs-prefix - (str base-path "/" path-to-work-with) + base-path + path-to-work-with :from-cp from-cp :from-fs from-fs) - result (into result [path-to-work-with])] + result (into result + [resource-to-work-with])] (cond (nil? resource-to-work-with) [] - (= :file (:resource-type resource-to-work-with)) (recur (drop 1 paths) result) + (is-file? resource-to-work-with) + (recur (drop 1 paths) result) :else (recur (into (drop 1 paths) (map #(str path-to-work-with "/" %) @@ -160,9 +170,10 @@ (if (not (empty? paths)) (do (let [path-to-work-with (first paths) - resource-to-work-with (resource-from-cp-or-filesystem + resource-to-work-with (resource-from-cp-or-fs fs-prefix - (str base-path "/" path-to-work-with) + base-path + path-to-work-with :from-cp from-cp :from-fs from-fs) result (into result [path-to-work-with])] @@ -201,9 +212,10 @@ source-paths " not found"))) (doseq [resource-path resource-paths] (let [target-file (io/file target-path resource-path) - source-file (io/file (uri-from-cp-or-filesystem - fs-prefix - (str base-path "/" resource-path)))] + source-file (io/file (file-from-cp-or-fs + fs-prefix + base-path + resource-path))] (io/make-parents target-file) (when (.isFile source-file) (io/copy source-file target-file))))))) diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index 513a452..e0da43a 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -25,14 +25,65 @@ (and (verify-file-exists path) (.isDirectory (io/file path)))) -(deftest test-uri-from-cp-or-filesystem +(defn filter-object + [e] + {:path (:path e) + :source-type (:source-type e) + :resource-type (:resource-type e)}) + +(deftest test-uri-from-cp (is - (some? (sut/uri-from-cp-or-filesystem + (sut/file-from-cp ".gitkeep"))) + +(deftest test-resource-from-cp-or-fs + (is + (.exists + (:file + (sut/resource-from-cp-or-fs + "./test-resources/" + "templates/themes/bootstrap4-test" + "js")))) + (is + (.exists + (:file + (sut/resource-from-cp-or-fs + "./" "" ".gitkeep")))) + (is + (some? (sut/resource-from-cp-or-fs "./test-resources/" - "templates/themes/bootstrap4-test/js"))) + "templates/themes/bootstrap4-test" + "js"))) (is - (some? (sut/uri-from-cp-or-filesystem - "./not-existing-so-load-from-cp" ".gitkeep")))) + (some? (sut/resource-from-cp-or-fs + "./not-existing-so-load-from-cp" "" ".gitkeep"))) + (is (= + {:path "js/subdir" + :source-type :classpath + :resource-type :dir} + (filter-object + (sut/resource-from-cp-or-fs + "./not-existing-so-load-from-cp" + "templates/themes/bootstrap4-test" + "js/subdir"))))) + +(deftest test-get-resources-recursive + (is (= + [] + (sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["not-existing"]))) + (is (= + [{:path "js/dummy.js" + :source-type :classpath + :resource-type :file}] + (map filter-object + (sut/get-resources-recursive + "" "templates/themes/bootstrap4-test" ["js/dummy.js"])))) + (is (= + ["js/subdir" + "js/subdir/test.js" + "js/subdir/subdummy.js"] + (map #(:path %) + (sut/get-resources-recursive + "" "templates/themes/bootstrap4-test" ["js/subdir"]))))) (deftest test-get-resource-paths-recursive (is (= @@ -80,18 +131,6 @@ ["file.js"] (sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"])))) -(deftest test-uri-from-cp - (is - (sut/file-from-cp ".gitkeep"))) - -(deftest test-uri-from-cp-or-filesystem - (is - (.exists (sut/uri-from-cp-or-filesystem - "./test-resources/" "templates/themes/bootstrap4-test/js"))) - (is - (.exists (sut/uri-from-cp-or-filesystem - "./" ".gitkeep")))) - (deftest test-copy-resources-from-theme! (is (do (sut/delete-resource-recursive! (str "target/tmp" target)) (sut/copy-resources-from-theme! "./" theme target "")