better base-path handling

This commit is contained in:
jem 2020-02-05 18:35:50 +01:00
parent 3bc2035173
commit 53a897d687
2 changed files with 18 additions and 16 deletions

View file

@ -69,15 +69,16 @@
(defn filter-and-remove-for-dir (defn filter-and-remove-for-dir
[base-path-to-filter-for [base-path-to-filter-for
elements-list] elements-list]
(let [norm-path-to-filter-for (str base-path-to-filter-for "/")
start (count norm-path-to-filter-for)]
(map (map
(fn [el] (fn [el]
(if (st/ends-with? el "/") (let [end (if (st/ends-with? el "/") (dec (count el)) (count el))]
(st/join "" (drop-last el)) (subs el start end)))
el))
(filter (filter
(fn [element] (and (st/starts-with? element base-path-to-filter-for) (fn [element] (and (st/starts-with? element norm-path-to-filter-for)
(not (= element base-path-to-filter-for)))) (not (= element norm-path-to-filter-for))))
elements-list))) elements-list))))
(defn jar-file-for-resource (defn jar-file-for-resource
[resource] [resource]
@ -97,24 +98,26 @@
(jar-file-for-resource resource))))) (jar-file-for-resource resource)))))
(defn list-entries-for-dir ;:- [VirtualPath] (defn list-entries-for-dir ;:- [VirtualPath]
[resource ;:- Resource [base-path ;:- VirtualPath
resource ;:- Resource
] ]
(filter-and-remove-for-dir (filter-and-remove-for-dir
(:virtual-path resource) base-path
(list-entries resource))) (list-entries resource)))
(defn get-resources;:- [Resource] (defn get-resources;:- [Resource]
"base-path is sensible for getting the right jar from classpath. So base-path
should be specific enough for the jar desired. Paths must not be empty."
[base-path ;:- VirtualPath [base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
] ]
(let [entry-list (flatten (let [entry-list (flatten
(map (map
(fn [p] (fn [p]
(let [p (if (empty? base-path) p (str base-path "/" p))]
(list-entries-for-dir (list-entries-for-dir
(create-resource p base-path
(path-if-exists p))))) (create-resource p (path-if-exists base-path p))))
paths))] paths))]
(map (fn [entry] (map (fn [entry]
(create-resource entry (path-if-exists entry))) (create-resource entry (path-if-exists base-path entry)))
entry-list))) entry-list)))

View file

@ -28,7 +28,6 @@
(deftest test-get-resources (deftest test-get-resources
(is (is
(= [{:virtual-path "dummy" :source-type :java-classpath-jar :resource-type :dir} (= [{:virtual-path "dummy_from_jar" :source-type :java-classpath-jar :resource-type :file}]
{:virtual-path "dummy/dummy_from_jar" :source-type :java-classpath-jar :resource-type :file}]
(map ftt/filter-object (map ftt/filter-object
(sut/get-resources "" ["dummy"]))))) (sut/get-resources "dummy" ["dummy_from_jar"])))))