differntiate classpath type better
This commit is contained in:
parent
62839f5d3c
commit
75b887d326
2 changed files with 62 additions and 40 deletions
|
@ -15,18 +15,12 @@
|
|||
[java.nio.file.attribute FileAttribute]))
|
||||
|
||||
; -------------------- 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 Prefix s/Str)
|
||||
|
||||
(def JavaUri s/Any) ; java.net.URI
|
||||
|
||||
(def VirtualPath s/Str)
|
||||
|
||||
(def JavaPath s/Any) ; java.nio.Path
|
||||
|
||||
(def Resource
|
||||
{:virtual-path VirtualPath
|
||||
:source-type SourceType
|
||||
|
@ -37,29 +31,39 @@
|
|||
; ----------------------- Domain functions ------------------------
|
||||
(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
|
||||
uri :- JavaUri
|
||||
java-path :- JavaPath
|
||||
source-type :- SourceType
|
||||
resource-type :- ResourceType]
|
||||
{:virtual-path virtual-path
|
||||
:java-uri uri
|
||||
java-path :- JavaPath]
|
||||
{:virtual-path virtual-path
|
||||
:java-uri (.toUri java-path)
|
||||
:java-path java-path
|
||||
:source-type source-type
|
||||
: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
|
||||
:source-type :filesystem
|
||||
:resource-type (cond
|
||||
(Files/isDirectory java-path no-link-option) :dir
|
||||
(Files/isRegularFile java-path no-link-option) :file
|
||||
: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
|
||||
[resource :- Resource]
|
||||
(= :file (:resource-type resource)))
|
||||
|
@ -98,18 +102,18 @@
|
|||
(FileSystems/newFileSystem filesystem-uri {})))))
|
||||
|
||||
; contains either a jar or a file
|
||||
(s/defn path-from-cp ; :- JavaPath
|
||||
(s/defn path-from-cp ;:- JavaPath
|
||||
[resource-path :- VirtualPath]
|
||||
(try
|
||||
(let [resource-uri (.toURI (io/resource resource-path))] ; check if contains jar:
|
||||
(when (= (.getScheme resource-uri) "jar")
|
||||
(let [resource-uri (.toURI (io/resource resource-path))]
|
||||
(when (is-from-classpath-jar? resource-uri)
|
||||
(init-file-system resource-uri))
|
||||
(when (Files/exists (Paths/get resource-uri) no-link-option)
|
||||
(Paths/get resource-uri)))
|
||||
(catch Exception e
|
||||
nil)))
|
||||
|
||||
(s/defn path-from-fs ; :- JavaPath
|
||||
(s/defn path-from-fs ;:- JavaPath
|
||||
[full-path :- VirtualPath]
|
||||
(let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile
|
||||
(try
|
||||
|
@ -118,10 +122,10 @@
|
|||
(catch Exception e
|
||||
nil))))
|
||||
|
||||
(defn resource-from-cp-or-fs ; :- Resource
|
||||
[fs-prefix ; :- Prefix
|
||||
base-path ; :- VirtualPath
|
||||
resource-path ; :- VirtualPath
|
||||
(defn resource-from-cp-or-fs ;:- Resource
|
||||
[fs-prefix ;:- Prefix
|
||||
base-path ;:- VirtualPath
|
||||
resource-path ;:- VirtualPath
|
||||
& {:keys [from-cp from-fs]
|
||||
:or {from-cp true
|
||||
from-fs true}}]
|
||||
|
@ -137,9 +141,9 @@
|
|||
nil)]
|
||||
(cond
|
||||
(some? path-from-fs)
|
||||
(create-resource resource-path path-from-fs :filesystem)
|
||||
(create-fs-resource resource-path path-from-fs)
|
||||
(some? path-from-cp)
|
||||
(create-resource resource-path path-from-cp :classpath)
|
||||
(create-cp-resource resource-path path-from-cp)
|
||||
:else nil)))
|
||||
|
||||
(defn path-from-cp-or-fs ; :- JavaPath
|
||||
|
@ -156,6 +160,10 @@
|
|||
(when (some? 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]
|
||||
[fs-prefix ;:- Prefix
|
||||
base-path ;:- VirtualPath
|
||||
|
@ -186,7 +194,7 @@
|
|||
(recur (into (drop 1 paths)
|
||||
(map #(str path-to-work-with "/" %)
|
||||
; 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)))
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
(some? (sut/resource-from-cp-or-fs
|
||||
"not-existing-filesystem-path"
|
||||
""
|
||||
"dummy")))
|
||||
"dummy")))
|
||||
(is
|
||||
(Files/exists
|
||||
(:java-path
|
||||
|
@ -53,7 +53,7 @@
|
|||
"./not-existing-so-load-from-cp" "" ".gitkeep")))
|
||||
(is (=
|
||||
{:virtual-path "js/subdir"
|
||||
:source-type :classpath
|
||||
:source-type :java-classpath-filesystem
|
||||
:resource-type :dir}
|
||||
(ftt/filter-object
|
||||
(sut/resource-from-cp-or-fs
|
||||
|
@ -61,6 +61,20 @@
|
|||
"templates/themes/bootstrap4-test"
|
||||
"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
|
||||
(is (=
|
||||
[]
|
||||
|
@ -70,13 +84,13 @@
|
|||
; add test here
|
||||
; TODO: fix dir.list on jar
|
||||
(is (=
|
||||
[{:virtual-path "dummy", :source-type :classpath, :resource-type :dir}
|
||||
{:virtual-path "dummy/dummy_from_jar", :source-type :classpath, :resource-type :file}]
|
||||
[{:virtual-path "dummy", :source-type :java-classpath-jar, :resource-type :dir}
|
||||
{:virtual-path "dummy/dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file}]
|
||||
(map ftt/filter-object
|
||||
(sut/get-resources-recursive "not-existing" "" ["dummy"]))))
|
||||
(is (=
|
||||
[{:virtual-path "js/dummy.js"
|
||||
:source-type :classpath
|
||||
:source-type :java-classpath-filesystem
|
||||
:resource-type :file}]
|
||||
(map ftt/filter-object
|
||||
(sut/get-resources-recursive
|
||||
|
|
Loading…
Reference in a new issue