refactor short-path -> virtual-path

This commit is contained in:
jem 2020-01-24 13:22:19 +01:00
parent 35b58279a1
commit 0bc1af2719
3 changed files with 44 additions and 44 deletions

View file

@ -21,15 +21,15 @@
(def Prefix s/Str) (def Prefix s/Str)
(def ResourceUri s/Any) ; java.net.URI (def JavaUri s/Any) ; java.net.URI
(def ShortPath s/Str) (def VirtualPath s/Str)
(def JavaPath s/Any) ; java.nio.Path (def JavaPath s/Any) ; java.nio.Path
(def Resource (def Resource
{:short-path ShortPath {:virtual-path VirtualPath
:uri ResourceUri :uri JavaUri
:java-path JavaPath :java-path JavaPath
:source-type SourceType :source-type SourceType
:resource-type ResourceType}) :resource-type ResourceType})
@ -38,20 +38,20 @@
(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 create-resource :- Resource
([short-path :- ShortPath ([virtual-path :- VirtualPath
uri :- ResourceUri uri :- JavaUri
java-path :- JavaPath java-path :- JavaPath
source-type :- SourceType source-type :- SourceType
resource-type :- ResourceType] resource-type :- ResourceType]
{:short-path short-path {:virtual-path virtual-path
:uri uri :uri uri
:java-path java-path :java-path java-path
:source-type source-type :source-type source-type
:resource-type resource-type}) :resource-type resource-type})
([short-path :- ShortPath ([virtual-path :- VirtualPath
java-path :- JavaPath java-path :- JavaPath
source-type :- SourceType] source-type :- SourceType]
{:short-path short-path {:virtual-path virtual-path
:uri (.toUri java-path) :uri (.toUri java-path)
:java-path java-path :java-path java-path
:source-type source-type :source-type source-type
@ -90,7 +90,7 @@
(#(st/replace % #"/+" "/")))) (#(st/replace % #"/+" "/"))))
(s/defn init-file-system (s/defn init-file-system
[resource-uri :- ResourceUri] [resource-uri :- JavaUri]
(let [filesystem-uri (URI. (first (st/split (.toString resource-uri) #"!")))] (let [filesystem-uri (URI. (first (st/split (.toString resource-uri) #"!")))]
(try (try
(FileSystems/getFileSystem filesystem-uri) (FileSystems/getFileSystem filesystem-uri)
@ -99,7 +99,7 @@
; 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 :- ShortPath] [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))] ; check if contains jar:
(when (= (.getScheme resource-uri) "jar") (when (= (.getScheme resource-uri) "jar")
@ -110,7 +110,7 @@
nil))) nil)))
(s/defn path-from-fs ; :- JavaPath (s/defn path-from-fs ; :- JavaPath
[full-path :- ShortPath] [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
(when (Files/exists path-from-fs no-link-option) (when (Files/exists path-from-fs no-link-option)
@ -120,8 +120,8 @@
(defn resource-from-cp-or-fs ; :- Resource (defn resource-from-cp-or-fs ; :- Resource
[fs-prefix ; :- Prefix [fs-prefix ; :- Prefix
base-path ; :- ShortPath base-path ; :- VirtualPath
resource-path ; :- ShortPath 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}}]
@ -144,8 +144,8 @@
(defn path-from-cp-or-fs ; :- JavaPath (defn path-from-cp-or-fs ; :- JavaPath
[fs-prefix ; :- Prefix [fs-prefix ; :- Prefix
base-path ; :- ShortPath base-path ; :- VirtualPath
resource-path; :- ShortPath 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}}]
@ -158,8 +158,8 @@
(defn get-resources-recursive ;:- [Resource] (defn get-resources-recursive ;:- [Resource]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- ShortPath base-path ;:- VirtualPath
paths ;:- [ShortPath] 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}}]
@ -190,14 +190,14 @@
result)))) result))))
result))) result)))
(defn get-resource-paths-recursive ;:- [ShortPath] (defn get-resource-paths-recursive ;:- [VirtualPath]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- ShortPath base-path ;:- VirtualPath
paths ;:- [ShortPath] 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}}]
(map #(:short-path %) (map #(:virtual-path %)
(get-resources-recursive (get-resources-recursive
fs-prefix base-path paths fs-prefix base-path paths
:from-cp from-cp :from-cp from-cp
@ -205,23 +205,23 @@
; TODO: Add files to keep ; TODO: Add files to keep
(s/defn delete-resource-recursive! (s/defn delete-resource-recursive!
[short-path :- s/Str] [virtual-path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (get-resource-paths-recursive (reverse (get-resource-paths-recursive
(str (user-dir) "/") (str (user-dir) "/")
short-path virtual-path
[""] [""]
:from-cp false))] :from-cp false))]
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(Files/delete (absolut-path short-path resource-path)) (Files/delete (absolut-path virtual-path resource-path))
))) )))
; TODO: add ignore patterns filtering ; TODO: add ignore patterns filtering
(defn copy-resources! (defn copy-resources!
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- ShortPath base-path ;:- VirtualPath
source-paths ;:- [ShortPath] source-paths ;:- [VirtualPath]
target-path ;:- ShortPath target-path ;:- VirtualPath
ignore-patterns ;:- s/Str ignore-patterns ;:- s/Str
] ]
(let [resource-paths (let [resource-paths
@ -243,11 +243,11 @@
(defn distinct-resources-by-path (defn distinct-resources-by-path
[resources] [resources]
(loop [paths (set (map :short-path resources)) (loop [paths (set (map :virtual-path resources))
resources resources resources resources
acc []] acc []]
(cond (empty? resources) acc (cond (empty? resources) acc
(contains? paths (:short-path (first resources))) (recur (disj paths (:short-path (first resources))) (contains? paths (:virtual-path (first resources))) (recur (disj paths (:virtual-path (first resources)))
(rest resources) (rest resources)
(conj acc (first resources))) (conj acc (first resources)))
:else (recur paths (rest resources) acc)))) :else (recur paths (rest resources) acc))))

View file

@ -52,7 +52,7 @@
(some? (sut/resource-from-cp-or-fs (some? (sut/resource-from-cp-or-fs
"./not-existing-so-load-from-cp" "" ".gitkeep"))) "./not-existing-so-load-from-cp" "" ".gitkeep")))
(is (= (is (=
{:short-path "js/subdir" {:virtual-path "js/subdir"
:source-type :classpath :source-type :classpath
:resource-type :dir} :resource-type :dir}
(ftt/filter-object (ftt/filter-object
@ -70,12 +70,12 @@
; add test here ; add test here
; TODO: fix dir.list on jar ; TODO: fix dir.list on jar
(is (= (is (=
[{:short-path "dummy", :source-type :classpath, :resource-type :dir} [{:virtual-path "dummy", :source-type :classpath, :resource-type :dir}
{:short-path "dummy/dummy_from_jar", :source-type :classpath, :resource-type :file}] {:virtual-path "dummy/dummy_from_jar", :source-type :classpath, :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 (=
[{:short-path "js/dummy.js" [{:virtual-path "js/dummy.js"
:source-type :classpath :source-type :classpath
:resource-type :file}] :resource-type :file}]
(map ftt/filter-object (map ftt/filter-object
@ -89,7 +89,7 @@
["js/subdir" ["js/subdir"
"js/subdir/subdummy.js" "js/subdir/subdummy.js"
"js/subdir/test.js"] "js/subdir/test.js"]
(sort (map :short-path (sort (map :virtual-path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["js/subdir"]))))) "" "templates/themes/bootstrap4-test" ["js/subdir"])))))
(is (= (is (=
@ -104,18 +104,18 @@
"./js/subdir" "./js/subdir"
"./js/subdir/subdummy.js" "./js/subdir/subdummy.js"
"./js/subdir/test.js"] "./js/subdir/test.js"]
(sort (map :short-path (sort (map :virtual-path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["."])))))) "" "templates/themes/bootstrap4-test" ["."]))))))
(deftest test-distinct-resources-by-path (deftest test-distinct-resources-by-path
(is (= [{:short-path "pages/test"} (is (= [{:virtual-path "pages/test"}
{:short-path "pages/test1"} {:virtual-path "pages/test1"}
{:short-path "pages/test2"}] {:virtual-path "pages/test2"}]
(sut/distinct-resources-by-path [{:short-path "pages/test"} (sut/distinct-resources-by-path [{:virtual-path "pages/test"}
{:short-path "pages/test1"} {:virtual-path "pages/test1"}
{:short-path "pages/test2"} {:virtual-path "pages/test2"}
{:short-path "pages/test1"}])))) {:virtual-path "pages/test1"}]))))
(deftest test-filter-for-ignore-patterns (deftest test-filter-for-ignore-patterns
(is (= (is (=

View file

@ -19,6 +19,6 @@
(defn filter-object (defn filter-object
[e] [e]
{:short-path (:short-path e) {:virtual-path (:virtual-path e)
:source-type (:source-type e) :source-type (:source-type e)
:resource-type (:resource-type e)}) :resource-type (:resource-type e)})