From 59206d45459718c43f2143f422d746a73d15d151 Mon Sep 17 00:00:00 2001 From: Clemens Geibel Date: Tue, 11 Feb 2020 16:50:34 +0100 Subject: [PATCH] Added new function get-resources-recursive in classpath_able_io.clj --- src/cryogen_core/classpath_able_io.clj | 36 +++++++++++++++++--- test/cryogen_core/classpath_able_io_test.clj | 11 +++--- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index be4a822..975c7ae 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -152,7 +152,7 @@ (jar/jar-file-for-resource resource))))) (.list (.toFile (:java-path resource))))) -(defn get-resources-recursive ;:- [Resource] +(defn get-resources-recursive-old ;:- [Resource] [fs-prefix ;:- Prefix base-path ;:- VirtualPath paths ;:- [VirtualPath] @@ -187,16 +187,42 @@ ; TODO: rename? Allow base-path to be ""? ; base-path must not be "" -(defn get-resources-recursive-new ;:- [Resource] +(defn get-resources-recursive ;:- [Resource] [fs-prefix ;:- Prefix base-path ;:- VirtualPath paths ;:- [VirtualPath] & {:keys [from-cp from-fs] :or {from-cp true from-fs true}}] - (let [fs-resources (fs/get-resources fs-prefix base-path paths) - cp-resources (cp/get-resources base-path paths)]) - ) + (let [fs-resources (if from-fs + (fs/get-resources fs-prefix base-path paths) + []) + cp-resources (if from-cp + (cp/get-resources base-path paths) + []) + get-virtual-paths (fn [resources] (map #(:virtual-path %) resources)) + virtual-paths (distinct + (into + (get-virtual-paths fs-resources) + (get-virtual-paths cp-resources))) + get-resource-local (fn [virtual-path resource-list] + (loop [rl resource-list] + (if (empty? rl) + nil + (let [el (first rl)] + (if (= (:virtual-path el) virtual-path) + el + (recur (rest rl))))))) + get-resource-global (fn [virtual-path resources-prio1 resources-prio2] + (let [prio1 (get-resource-local + virtual-path resources-prio1)] + (if (nil? prio1) + (get-resource-local + virtual-path resources-prio2) + prio1)))] + (map + #(get-resource-global % fs-resources cp-resources) + virtual-paths))) (defn get-resource-paths-recursive ;:- [VirtualPath] [fs-prefix ;:- Prefix diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index 48a60eb..295b0b2 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -28,11 +28,12 @@ (map ftt/filter-object (sut/get-resources-recursive "not-existing" "dummy" ["dummy_from_jar"])))) (is (= - [{:virtual-path "dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file} - {:virtual-path "dummy_from_fs", :source-type :filesystem, :resource-type :file} + [{:virtual-path "dummy2/dummy2_from_jar", :source-type :java-classpath-jar, :resource-type :file} + {:virtual-path "dummy2/dummy_common", :source-type :filesystem, :resource-type :file} {:virtual-path "dummy2", :source-type :filesystem, :resource-type :dir} - {:virtual-path "dummy2/dummy2_from_fs", :source-type :filesystem, :resource-type :file} - {:virtual-path "dummy2/dummy_common", :source-type :filesystem, :resource-type :file}] + {:virtual-path "dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file} + {:virtual-path "dummy_from_fs", :source-type :filesystem, :resource-type :file} + {:virtual-path "dummy2/dummy2_from_fs", :source-type :filesystem, :resource-type :file}] (map ftt/filter-object (sut/get-resources-recursive "fs_root" "dummy" ["dummy_from_jar" "dummy_from_fs" "dummy2"])))) (is (= @@ -67,7 +68,7 @@ "./js/subdir/test.js"] (sort (map :virtual-path (sut/get-resources-recursive - "" "templates/themes/bootstrap4-test" ["."])))))) + "test-resources" "templates/themes/bootstrap4-test" ["."])))))) (deftest test-distinct-resources-by-path (is (= [{:virtual-path "pages/test"}