introduce Resource for path-handling
This commit is contained in:
parent
55479d72e2
commit
23ca87a8cd
2 changed files with 119 additions and 29 deletions
|
@ -11,6 +11,25 @@
|
||||||
[clojure.string :as st]
|
[clojure.string :as st]
|
||||||
[schema.core :as s]))
|
[schema.core :as s]))
|
||||||
|
|
||||||
|
(def SourceType (s/enum :classpath :filesystem))
|
||||||
|
|
||||||
|
(def ResourceType (s/enum :file :dir :unknown))
|
||||||
|
|
||||||
|
(def Prefix s/Str)
|
||||||
|
|
||||||
|
(def Uri s/Any) ; java.net.URI
|
||||||
|
|
||||||
|
(def Path s/Str)
|
||||||
|
|
||||||
|
(def File s/Any) ; java.io.File
|
||||||
|
|
||||||
|
(def Resource
|
||||||
|
{:path Path
|
||||||
|
:uri Uri
|
||||||
|
:file File
|
||||||
|
:source-type SourceType
|
||||||
|
:resource-type ResourceType })
|
||||||
|
|
||||||
(def public "resources/public")
|
(def public "resources/public")
|
||||||
|
|
||||||
(defn path
|
(defn path
|
||||||
|
@ -25,8 +44,31 @@
|
||||||
[ignore-patterns source-list]
|
[ignore-patterns source-list]
|
||||||
(filter #(not (re-matches ignore-patterns %)) source-list))
|
(filter #(not (re-matches ignore-patterns %)) source-list))
|
||||||
|
|
||||||
(defn file-from-cp
|
(s/defn create-resource :- Resource
|
||||||
[resource-path]
|
([path :- Path
|
||||||
|
uri :- Uri
|
||||||
|
file :- File
|
||||||
|
source-type :- SourceType
|
||||||
|
resource-type :- ResourceType]
|
||||||
|
{:path path
|
||||||
|
:uri uri
|
||||||
|
:file file
|
||||||
|
:source-type source-type
|
||||||
|
:resource-type resource-type})
|
||||||
|
([path :- Path
|
||||||
|
file :- File
|
||||||
|
source-type :- SourceType]
|
||||||
|
{:path path
|
||||||
|
:uri (.toURI file)
|
||||||
|
:file file
|
||||||
|
:source-type source-type
|
||||||
|
:resource-type (cond
|
||||||
|
(.isDirectory file) :dir
|
||||||
|
(.isFile file) :file
|
||||||
|
:else :unknown)}))
|
||||||
|
|
||||||
|
(s/defn file-from-cp ; :- File
|
||||||
|
[resource-path :- Path]
|
||||||
(let [file-from-cp (io/file (io/resource resource-path))]
|
(let [file-from-cp (io/file (io/resource resource-path))]
|
||||||
(try
|
(try
|
||||||
(when (.exists file-from-cp)
|
(when (.exists file-from-cp)
|
||||||
|
@ -34,8 +76,9 @@
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
nil))))
|
nil))))
|
||||||
|
|
||||||
(defn file-from-fs
|
(s/defn file-from-fs ; :- File
|
||||||
[fs-prefix resource-path]
|
[fs-prefix :- Prefix
|
||||||
|
resource-path :- Path]
|
||||||
(let [file-from-fs (io/file (str fs-prefix resource-path))]
|
(let [file-from-fs (io/file (str fs-prefix resource-path))]
|
||||||
(try
|
(try
|
||||||
(when (.exists file-from-fs)
|
(when (.exists file-from-fs)
|
||||||
|
@ -43,7 +86,26 @@
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
nil))))
|
nil))))
|
||||||
|
|
||||||
(defn file-from-cp-or-filesystem
|
(defn resource-from-cp-or-filesystem ; :- Resource
|
||||||
|
[fs-prefix ; :- Prefix
|
||||||
|
resource-path ; :- Path
|
||||||
|
& {:keys [from-cp from-fs]
|
||||||
|
:or {from-cp true
|
||||||
|
from-fs true}}]
|
||||||
|
(let [file-from-fs (if from-fs
|
||||||
|
(file-from-fs fs-prefix resource-path)
|
||||||
|
nil)
|
||||||
|
file-from-cp (if from-cp
|
||||||
|
(file-from-cp resource-path)
|
||||||
|
nil)]
|
||||||
|
(cond
|
||||||
|
(some? file-from-fs)
|
||||||
|
(create-resource resource-path file-from-fs :filesystem)
|
||||||
|
(some? file-from-cp)
|
||||||
|
(create-resource resource-path file-from-cp :classpath)
|
||||||
|
:else nil)))
|
||||||
|
|
||||||
|
(defn uri-from-cp-or-filesystem
|
||||||
[fs-prefix resource-path
|
[fs-prefix resource-path
|
||||||
& {:keys [from-cp from-fs]
|
& {:keys [from-cp from-fs]
|
||||||
:or {from-cp true
|
:or {from-cp true
|
||||||
|
@ -58,10 +120,10 @@
|
||||||
from-fs-file
|
from-fs-file
|
||||||
from-cp-file)))
|
from-cp-file)))
|
||||||
|
|
||||||
(defn get-resource-paths-recursive ;:- [s/Str]
|
(defn get-resources-recursive ;:- [Resource]
|
||||||
[fs-prefix ;:- s/Str
|
[fs-prefix ;:- Prefix
|
||||||
base-path ;:- s/Str
|
base-path ;:- Path
|
||||||
paths ;:- [s/Str]
|
paths ;:- [Path]
|
||||||
& {:keys [from-cp from-fs]
|
& {:keys [from-cp from-fs]
|
||||||
:or {from-cp true
|
:or {from-cp true
|
||||||
from-fs true}}]
|
from-fs true}}]
|
||||||
|
@ -70,19 +132,47 @@
|
||||||
(if (not (empty? paths))
|
(if (not (empty? paths))
|
||||||
(do
|
(do
|
||||||
(let [path-to-work-with (first paths)
|
(let [path-to-work-with (first paths)
|
||||||
file-to-work-with (io/file (file-from-cp-or-filesystem
|
resource-to-work-with (resource-from-cp-or-filesystem
|
||||||
fs-prefix
|
fs-prefix
|
||||||
(str base-path "/" path-to-work-with)
|
(str base-path "/" path-to-work-with)
|
||||||
:from-cp from-cp
|
:from-cp from-cp
|
||||||
:from-fs from-fs))
|
:from-fs from-fs)
|
||||||
result (into result [path-to-work-with])]
|
result (into result [path-to-work-with])]
|
||||||
(cond
|
(cond
|
||||||
(nil? file-to-work-with) []
|
(nil? resource-to-work-with) []
|
||||||
(.isFile file-to-work-with) (recur (drop 1 paths) result)
|
(= :file (:resource-type resource-to-work-with)) (recur (drop 1 paths) result)
|
||||||
:else
|
:else
|
||||||
(recur (into (drop 1 paths)
|
(recur (into (drop 1 paths)
|
||||||
(map #(str path-to-work-with "/" %)
|
(map #(str path-to-work-with "/" %)
|
||||||
(.list file-to-work-with)))
|
(.list (:file resource-to-work-with))))
|
||||||
|
result))))
|
||||||
|
result)))
|
||||||
|
|
||||||
|
(defn get-resource-paths-recursive ;:- [Path]
|
||||||
|
[fs-prefix ;:- Prefix
|
||||||
|
base-path ;:- Path
|
||||||
|
paths ;:- [Path]
|
||||||
|
& {:keys [from-cp from-fs]
|
||||||
|
:or {from-cp true
|
||||||
|
from-fs true}}]
|
||||||
|
(loop [paths paths
|
||||||
|
result []]
|
||||||
|
(if (not (empty? paths))
|
||||||
|
(do
|
||||||
|
(let [path-to-work-with (first paths)
|
||||||
|
resource-to-work-with (resource-from-cp-or-filesystem
|
||||||
|
fs-prefix
|
||||||
|
(str base-path "/" path-to-work-with)
|
||||||
|
:from-cp from-cp
|
||||||
|
:from-fs from-fs)
|
||||||
|
result (into result [path-to-work-with])]
|
||||||
|
(cond
|
||||||
|
(nil? resource-to-work-with) []
|
||||||
|
(= :file (:resource-type resource-to-work-with)) (recur (drop 1 paths) result)
|
||||||
|
:else
|
||||||
|
(recur (into (drop 1 paths)
|
||||||
|
(map #(str path-to-work-with "/" %)
|
||||||
|
(.list (:file resource-to-work-with))))
|
||||||
result)
|
result)
|
||||||
)))
|
)))
|
||||||
result)))
|
result)))
|
||||||
|
@ -98,10 +188,10 @@
|
||||||
|
|
||||||
; TODO: add ignore patterns filtering
|
; TODO: add ignore patterns filtering
|
||||||
(defn copy-resources!
|
(defn copy-resources!
|
||||||
[fs-prefix ;:- s/Str
|
[fs-prefix ;:- Prefix
|
||||||
base-path ;:- s/Str
|
base-path ;:- Path
|
||||||
source-paths ;:- [s/Str]
|
source-paths ;:- [Path]
|
||||||
target-path ;:- s/Str
|
target-path ;:- Path
|
||||||
ignore-patterns ;:- s/Str
|
ignore-patterns ;:- s/Str
|
||||||
]
|
]
|
||||||
(let [resource-paths
|
(let [resource-paths
|
||||||
|
@ -111,7 +201,7 @@
|
||||||
source-paths " not found")))
|
source-paths " not found")))
|
||||||
(doseq [resource-path resource-paths]
|
(doseq [resource-path resource-paths]
|
||||||
(let [target-file (io/file target-path resource-path)
|
(let [target-file (io/file target-path resource-path)
|
||||||
source-file (io/file (file-from-cp-or-filesystem
|
source-file (io/file (uri-from-cp-or-filesystem
|
||||||
fs-prefix
|
fs-prefix
|
||||||
(str base-path "/" resource-path)))]
|
(str base-path "/" resource-path)))]
|
||||||
(io/make-parents target-file)
|
(io/make-parents target-file)
|
||||||
|
|
|
@ -25,13 +25,13 @@
|
||||||
(and (verify-file-exists path)
|
(and (verify-file-exists path)
|
||||||
(.isDirectory (io/file path))))
|
(.isDirectory (io/file path))))
|
||||||
|
|
||||||
(deftest test-file-from-cp-or-filesystem
|
(deftest test-uri-from-cp-or-filesystem
|
||||||
(is
|
(is
|
||||||
(some? (sut/file-from-cp-or-filesystem
|
(some? (sut/uri-from-cp-or-filesystem
|
||||||
"./test-resources/"
|
"./test-resources/"
|
||||||
"templates/themes/bootstrap4-test/js")))
|
"templates/themes/bootstrap4-test/js")))
|
||||||
(is
|
(is
|
||||||
(some? (sut/file-from-cp-or-filesystem
|
(some? (sut/uri-from-cp-or-filesystem
|
||||||
"./not-existing-so-load-from-cp" ".gitkeep"))))
|
"./not-existing-so-load-from-cp" ".gitkeep"))))
|
||||||
|
|
||||||
(deftest test-get-resource-paths-recursive
|
(deftest test-get-resource-paths-recursive
|
||||||
|
@ -80,16 +80,16 @@
|
||||||
["file.js"]
|
["file.js"]
|
||||||
(sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"]))))
|
(sut/filter-for-ignore-patterns #".*\.ignore" ["file.js" "file.ignore"]))))
|
||||||
|
|
||||||
(deftest test-file-from-cp
|
(deftest test-uri-from-cp
|
||||||
(is
|
(is
|
||||||
(sut/file-from-cp ".gitkeep")))
|
(sut/file-from-cp ".gitkeep")))
|
||||||
|
|
||||||
(deftest test-file-from-cp-or-filesystem
|
(deftest test-uri-from-cp-or-filesystem
|
||||||
(is
|
(is
|
||||||
(.exists (sut/file-from-cp-or-filesystem
|
(.exists (sut/uri-from-cp-or-filesystem
|
||||||
"./test-resources/" "templates/themes/bootstrap4-test/js")))
|
"./test-resources/" "templates/themes/bootstrap4-test/js")))
|
||||||
(is
|
(is
|
||||||
(.exists (sut/file-from-cp-or-filesystem
|
(.exists (sut/uri-from-cp-or-filesystem
|
||||||
"./" ".gitkeep"))))
|
"./" ".gitkeep"))))
|
||||||
|
|
||||||
(deftest test-copy-resources-from-theme! (is (do
|
(deftest test-copy-resources-from-theme! (is (do
|
||||||
|
|
Loading…
Reference in a new issue