Added new function get-resources-recursive in classpath_able_io.clj

This commit is contained in:
Clemens Geibel 2020-02-11 16:50:34 +01:00
parent 7198ac493b
commit 59206d4545
2 changed files with 37 additions and 10 deletions

View file

@ -152,7 +152,7 @@
(jar/jar-file-for-resource resource))))) (jar/jar-file-for-resource resource)))))
(.list (.toFile (:java-path resource))))) (.list (.toFile (:java-path resource)))))
(defn get-resources-recursive ;:- [Resource] (defn get-resources-recursive-old ;:- [Resource]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
@ -187,16 +187,42 @@
; TODO: rename? Allow base-path to be ""? ; TODO: rename? Allow base-path to be ""?
; base-path must not be "" ; base-path must not be ""
(defn get-resources-recursive-new ;:- [Resource] (defn get-resources-recursive ;:- [Resource]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
(let [fs-resources (fs/get-resources fs-prefix base-path paths) (let [fs-resources (if from-fs
cp-resources (cp/get-resources base-path paths)]) (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] (defn get-resource-paths-recursive ;:- [VirtualPath]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix

View file

@ -28,11 +28,12 @@
(map ftt/filter-object (map ftt/filter-object
(sut/get-resources-recursive "not-existing" "dummy" ["dummy_from_jar"])))) (sut/get-resources-recursive "not-existing" "dummy" ["dummy_from_jar"]))))
(is (= (is (=
[{:virtual-path "dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file} [{:virtual-path "dummy2/dummy2_from_jar", :source-type :java-classpath-jar, :resource-type :file}
{:virtual-path "dummy_from_fs", :source-type :filesystem, :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", :source-type :filesystem, :resource-type :dir}
{:virtual-path "dummy2/dummy2_from_fs", :source-type :filesystem, :resource-type :file} {:virtual-path "dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file}
{:virtual-path "dummy2/dummy_common", :source-type :filesystem, :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 (map ftt/filter-object
(sut/get-resources-recursive "fs_root" "dummy" ["dummy_from_jar" "dummy_from_fs" "dummy2"])))) (sut/get-resources-recursive "fs_root" "dummy" ["dummy_from_jar" "dummy_from_fs" "dummy2"]))))
(is (= (is (=
@ -67,7 +68,7 @@
"./js/subdir/test.js"] "./js/subdir/test.js"]
(sort (map :virtual-path (sort (map :virtual-path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["."])))))) "test-resources" "templates/themes/bootstrap4-test" ["."]))))))
(deftest test-distinct-resources-by-path (deftest test-distinct-resources-by-path
(is (= [{:virtual-path "pages/test"} (is (= [{:virtual-path "pages/test"}