differntiate classpath type better

This commit is contained in:
jem 2020-01-24 18:32:13 +01:00
parent 62839f5d3c
commit 75b887d326
2 changed files with 62 additions and 40 deletions

View file

@ -15,18 +15,12 @@
[java.nio.file.attribute FileAttribute])) [java.nio.file.attribute FileAttribute]))
; -------------------- Domain Definition ------------------------------ ; -------------------- Domain Definition ------------------------------
(def SourceType (s/enum :classpath :filesystem)) (def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem))
(def ResourceType (s/enum :file :dir :unknown)) (def ResourceType (s/enum :file :dir :unknown))
(def Prefix s/Str) (def Prefix s/Str)
(def JavaUri s/Any) ; java.net.URI (def JavaUri s/Any) ; java.net.URI
(def VirtualPath s/Str) (def VirtualPath s/Str)
(def JavaPath s/Any) ; java.nio.Path (def JavaPath s/Any) ; java.nio.Path
(def Resource (def Resource
{:virtual-path VirtualPath {:virtual-path VirtualPath
:source-type SourceType :source-type SourceType
@ -37,29 +31,39 @@
; ----------------------- Domain functions ------------------------ ; ----------------------- Domain functions ------------------------
(def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS]))
(s/defn create-resource :- Resource (s/defn is-from-classpath-jar? :- s/Bool
[uri ;:- JavaUri
]
(= (.getScheme uri) "jar"))
(s/defn create-fs-resource :- Resource
([virtual-path :- VirtualPath ([virtual-path :- VirtualPath
uri :- JavaUri java-path :- JavaPath]
java-path :- JavaPath {:virtual-path virtual-path
source-type :- SourceType :java-uri (.toUri java-path)
resource-type :- ResourceType]
{:virtual-path virtual-path
:java-uri uri
:java-path java-path :java-path java-path
:source-type source-type :source-type :filesystem
:resource-type resource-type})
([virtual-path :- VirtualPath
java-path :- JavaPath
source-type :- SourceType]
{:virtual-path virtual-path
:java-uri (.toUri java-path)
:java-path java-path
:source-type source-type
:resource-type (cond :resource-type (cond
(Files/isDirectory java-path no-link-option) :dir (Files/isDirectory java-path no-link-option) :dir
(Files/isRegularFile java-path no-link-option) :file (Files/isRegularFile java-path no-link-option) :file
:else :unknown)})) :else :unknown)}))
(s/defn create-cp-resource :- Resource
([virtual-path :- VirtualPath
java-path :- JavaPath]
(let [java-uri (.toUri java-path)]
{:virtual-path virtual-path
:java-uri java-uri
:java-path java-path
:source-type (cond (is-from-classpath-jar? java-uri)
:java-classpath-jar
:else :java-classpath-filesystem)
:resource-type (cond
(Files/isDirectory java-path no-link-option) :dir
(Files/isRegularFile java-path no-link-option) :file
:else :unknown)})))
(s/defn is-file? :- s/Bool (s/defn is-file? :- s/Bool
[resource :- Resource] [resource :- Resource]
(= :file (:resource-type resource))) (= :file (:resource-type resource)))
@ -98,18 +102,18 @@
(FileSystems/newFileSystem filesystem-uri {}))))) (FileSystems/newFileSystem filesystem-uri {})))))
; contains either a jar or a file ; contains either a jar or a file
(s/defn path-from-cp ; :- JavaPath (s/defn path-from-cp ;:- JavaPath
[resource-path :- VirtualPath] [resource-path :- VirtualPath]
(try (try
(let [resource-uri (.toURI (io/resource resource-path))] ; check if contains jar: (let [resource-uri (.toURI (io/resource resource-path))]
(when (= (.getScheme resource-uri) "jar") (when (is-from-classpath-jar? resource-uri)
(init-file-system resource-uri)) (init-file-system resource-uri))
(when (Files/exists (Paths/get resource-uri) no-link-option) (when (Files/exists (Paths/get resource-uri) no-link-option)
(Paths/get resource-uri))) (Paths/get resource-uri)))
(catch Exception e (catch Exception e
nil))) nil)))
(s/defn path-from-fs ; :- JavaPath (s/defn path-from-fs ;:- JavaPath
[full-path :- VirtualPath] [full-path :- VirtualPath]
(let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile (let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile
(try (try
@ -118,10 +122,10 @@
(catch Exception e (catch Exception e
nil)))) nil))))
(defn resource-from-cp-or-fs ; :- Resource (defn resource-from-cp-or-fs ;:- Resource
[fs-prefix ; :- Prefix [fs-prefix ;:- Prefix
base-path ; :- VirtualPath base-path ;:- VirtualPath
resource-path ; :- VirtualPath resource-path ;:- 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}}]
@ -137,9 +141,9 @@
nil)] nil)]
(cond (cond
(some? path-from-fs) (some? path-from-fs)
(create-resource resource-path path-from-fs :filesystem) (create-fs-resource resource-path path-from-fs)
(some? path-from-cp) (some? path-from-cp)
(create-resource resource-path path-from-cp :classpath) (create-cp-resource resource-path path-from-cp)
:else nil))) :else nil)))
(defn path-from-cp-or-fs ; :- JavaPath (defn path-from-cp-or-fs ; :- JavaPath
@ -156,6 +160,10 @@
(when (some? resource) (when (some? resource)
(:java-path resource)))) (:java-path resource))))
(s/defn list-entries-for-dir ;:- [VirtualPath]
[resource :- Resource]
(.list (io/file (.toString (:java-path resource))))) ; TODO doesnt work in jars
(defn get-resources-recursive ;:- [Resource] (defn get-resources-recursive ;:- [Resource]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
@ -186,7 +194,7 @@
(recur (into (drop 1 paths) (recur (into (drop 1 paths)
(map #(str path-to-work-with "/" %) (map #(str path-to-work-with "/" %)
; Bsp-Code: https://github.com/clojure/java.classpath/blob/c10fc96a8ff98db4eb925a13ef0f5135ad8dacc6/src/main/clojure/clojure/java/classpath.clj#L50 ; Bsp-Code: https://github.com/clojure/java.classpath/blob/c10fc96a8ff98db4eb925a13ef0f5135ad8dacc6/src/main/clojure/clojure/java/classpath.clj#L50
(.list (io/file (.toString (:java-path resource-to-work-with)))))) ; TODO doesnt work in jars (list-entries-for-dir resource-to-work-with)))
result)))) result))))
result))) result)))

View file

@ -30,9 +30,9 @@
(some? (sut/resource-from-cp-or-fs (some? (sut/resource-from-cp-or-fs
"not-existing-filesystem-path" "not-existing-filesystem-path"
"" ""
"dummy"))) "dummy")))
(is (is
(Files/exists (Files/exists
(:java-path (:java-path
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./test-resources" "./test-resources"
@ -53,7 +53,7 @@
"./not-existing-so-load-from-cp" "" ".gitkeep"))) "./not-existing-so-load-from-cp" "" ".gitkeep")))
(is (= (is (=
{:virtual-path "js/subdir" {:virtual-path "js/subdir"
:source-type :classpath :source-type :java-classpath-filesystem
:resource-type :dir} :resource-type :dir}
(ftt/filter-object (ftt/filter-object
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
@ -61,6 +61,20 @@
"templates/themes/bootstrap4-test" "templates/themes/bootstrap4-test"
"js/subdir"))))) "js/subdir")))))
(deftest test-list-entries-for-dir
(is (= ["subdummy.js", "test.js"]
(seq
(sut/list-entries-for-dir (sut/resource-from-cp-or-fs
"./not-existing-so-load-from-cp"
"templates/themes/bootstrap4-test"
"js/subdir")))))
(is (= ["dummy-from-jar"]
(sut/list-entries-for-dir (sut/resource-from-cp-or-fs
"not-existing-filesystem-path"
""
"dummy")))))
(deftest test-get-resources-recursive (deftest test-get-resources-recursive
(is (= (is (=
[] []
@ -70,13 +84,13 @@
; add test here ; add test here
; TODO: fix dir.list on jar ; TODO: fix dir.list on jar
(is (= (is (=
[{:virtual-path "dummy", :source-type :classpath, :resource-type :dir} [{:virtual-path "dummy", :source-type :java-classpath-jar, :resource-type :dir}
{:virtual-path "dummy/dummy_from_jar", :source-type :classpath, :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-recursive "not-existing" "" ["dummy"])))) (sut/get-resources-recursive "not-existing" "" ["dummy"]))))
(is (= (is (=
[{:virtual-path "js/dummy.js" [{:virtual-path "js/dummy.js"
:source-type :classpath :source-type :java-classpath-filesystem
:resource-type :file}] :resource-type :file}]
(map ftt/filter-object (map ftt/filter-object
(sut/get-resources-recursive (sut/get-resources-recursive