fix basepath problem

This commit is contained in:
jem 2019-12-16 13:51:41 +01:00
parent 1bdb53d4e3
commit ef171bbd2c
3 changed files with 77 additions and 65 deletions

View file

@ -44,16 +44,27 @@
nil)))) nil))))
(defn file-from-cp-or-filesystem (defn file-from-cp-or-filesystem
[fs-prefix resource-path] [fs-prefix resource-path
(let [from-fs (file-from-fs fs-prefix resource-path)] & {:keys [from-cp from-fs]
(if (some? from-fs) :or {from-cp true
from-fs from-fs true}}]
(file-from-cp resource-path)))) (let [from-fs-file (if from-fs
(file-from-fs fs-prefix resource-path)
nil)
from-cp-file (if from-cp
(file-from-cp resource-path)
nil)]
(if (some? from-fs-file)
from-fs-file
from-cp-file)))
(s/defn get-resource-paths-recursive :- [s/Str] (defn get-resource-paths-recursive ;:- [s/Str]
[fs-prefix :- s/Str [fs-prefix ;:- s/Str
base-path :- s/Str base-path ;:- s/Str
paths :- [s/Str]] paths ;:- [s/Str]
& {:keys [from-cp from-fs]
:or {from-cp true
from-fs true}}]
(loop [paths paths (loop [paths paths
result []] result []]
(if (not (empty? paths)) (if (not (empty? paths))
@ -61,7 +72,9 @@
(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 file-to-work-with (io/file (file-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-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? file-to-work-with) []
@ -77,47 +90,38 @@
(s/defn delete-resource-recursive! (s/defn delete-resource-recursive!
[path :- s/Str] [path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (get-resource-paths-recursive "" path [""]))] (reverse (get-resource-paths-recursive
"" path [""] :from-cp false))]
(println resource-paths) (println resource-paths)
(doseq [path resource-paths] (doseq [path resource-paths]
(io/delete-file path)))) (io/delete-file path))))
(defn copy-file!
[source-file
target-file]
(do (io/make-parents target-file)
(io/copy source-file target-file)))
(defn do-copy!
[source-dir target-dir ignore-patterns]
(loop [source-list (.list source-dir)
file-path-prefix [""]]
(let [f (first source-list)
second? (not (nil? (second source-list)))
target-file (io/file target-dir (str (first file-path-prefix) f))
source-file (io/file source-dir (str (first file-path-prefix) f))]
(if (.isFile source-file)
(do
(copy-file! source-file target-file)
(when second?
(recur (drop 1 source-list) (drop 1 file-path-prefix))))
(when (> (count (.list source-file)) 0)
(recur (concat (.list source-file) (drop 1 source-list))
(concat (repeat (count (.list source-file)) (str (first file-path-prefix) f "/"))
(drop 1 file-path-prefix))))))))
(defn copy-resources! (defn copy-resources!
[fs-prefix source-path target-path ignore-patterns] [fs-prefix ;:- s/Str
(let [source-file (file-from-cp-or-filesystem fs-prefix source-path) base-path ;:- s/Str
target-file (io/file target-path source-path) source-paths ;:- [s/Str]
is-source-dir? (.isDirectory source-file)] target-path ;:- s/Str
(if (nil? source-file) ignore-patterns ;:- s/Str
(throw (IllegalArgumentException. (str "resource " source-path " not found"))) ]
(do-copy! source-file target-file ignore-patterns)))) (let [resource-paths
(get-resource-paths-recursive fs-prefix base-path source-paths)]
(if (empty? resource-paths)
(throw (IllegalArgumentException. (str "resource " resource-paths ", "
source-paths " not found")))
(doseq [resource-path resource-paths]
(let [target-file (io/file target-path resource-path)
source-file (io/file (file-from-cp-or-filesystem
fs-prefix
(str base-path "/" resource-path)))]
(println resource-path)
(println source-file)
(println target-file)
(io/make-parents target-file)
(when (.isFile source-file)
(io/copy source-file target-file)))))))
(defn copy-resources-from-theme! (defn copy-resources-from-theme!
[fs-prefix theme target-path ignore-patterns] [fs-prefix theme target-path ignore-patterns]
(let [theme-path (str "templates/themes/" theme)] (let [theme-path (str "templates/themes/" theme)]
(copy-resources! fs-prefix (str theme-path "/css") target-path ignore-patterns) (copy-resources! fs-prefix theme-path ["css" "js" "html"]
(copy-resources! fs-prefix (str theme-path "/js") target-path ignore-patterns) target-path ignore-patterns)))
(copy-resources! fs-prefix (str theme-path "/html/") target-path ignore-patterns)))

View file

@ -534,7 +534,7 @@
(set-custom-resource-path! (str "file:resources/templates/themes/" theme)) (set-custom-resource-path! (str "file:resources/templates/themes/" theme))
(cryogen-io/wipe-public-folder keep-files) (cryogen-io/wipe-public-folder keep-files)
(println (blue "copying theme resources")) (println (blue "copying theme resources"))
(cp-io/copy-resources-from-theme "resources/templates/themes/" (cp-io/copy-resources-from-theme! "resources/templates/themes/"
theme theme
(cp-io/path "resources/public" blog-prefix) (cp-io/path "resources/public" blog-prefix)
ignored-files) ignored-files)

View file

@ -25,18 +25,34 @@
(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
(is
(some? (sut/file-from-cp-or-filesystem
"./test-resources/"
"templates/themes/bootstrap4-test/js")))
(is
(some? (sut/file-from-cp-or-filesystem
"./not-existing-so-load-from-cp" ".gitkeep"))))
(deftest test-get-resource-paths-recursive (deftest test-get-resource-paths-recursive
(is (= (is (=
[] []
(sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["not-existing"]))) (sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["not-existing"])))
(is (= (is (=
["js/dummy.js"] ["js/dummy.js"]
(sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["js/dummy.js"]))) (sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"])))
(is (=
[]
(sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/dummy.js"]
:from-cp false)))
(is (= (is (=
["js/subdir" ["js/subdir"
"js/subdir/test.js" "js/subdir/test.js"
"js/subdir/subdummy.js"] "js/subdir/subdummy.js"]
(sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["js/subdir"]))) (sut/get-resource-paths-recursive
"" "templates/themes/bootstrap4-test" ["js/subdir"])))
(is (= (is (=
["." ["."
"./css" "./css"
@ -59,14 +75,6 @@
(sut/delete-resource-recursive! (str "target/tmp" target)) (sut/delete-resource-recursive! (str "target/tmp" target))
(not (verify-dir-exists (str "target/tmp" target)))))) (not (verify-dir-exists (str "target/tmp" target))))))
(deftest test-file-from-cp-or-filesystem
(is
(.exists (sut/file-from-cp-or-filesystem
"./test-resources/" "templates/themes/bootstrap4-test/js")))
(is
(.exists (sut/file-from-cp-or-filesystem
"./" ".gitkeep"))))
(deftest test-filter-for-ignore-patterns (deftest test-filter-for-ignore-patterns
(is (= (is (=
["file.js"] ["file.js"]
@ -88,15 +96,15 @@
(sut/delete-resource-recursive! (str "target/tmp" target)) (sut/delete-resource-recursive! (str "target/tmp" target))
(sut/copy-resources-from-theme! "./" theme target "") (sut/copy-resources-from-theme! "./" theme target "")
(and (verify-dir-exists (and (verify-dir-exists
(str target "/templates/themes/bootstrap4-test/js")) (str target "/js"))
(verify-file-exists (verify-file-exists
(str target "/templates/themes/bootstrap4-test/js/dummy.js")) (str target "/js/dummy.js"))
(verify-dir-exists (verify-dir-exists
(str target "/templates/themes/bootstrap4-test/js/subdir")) (str target "/js/subdir"))
(verify-file-exists (verify-file-exists
(str target "/templates/themes/bootstrap4-test/js/subdir/subdummy.js")) (str target "/js/subdir/subdummy.js"))
(verify-file-exists (verify-file-exists
(str target "/templates/themes/bootstrap4-test/css/dummy.css")) (str target "/css/dummy.css"))
(verify-file-exists (verify-file-exists
(str target "/templates/themes/bootstrap4-test/html/404.html")) (str target "/html/404.html"))
)))) ))))