diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 3cd4182..9116771 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -44,16 +44,27 @@ nil)))) (defn file-from-cp-or-filesystem - [fs-prefix resource-path] - (let [from-fs (file-from-fs fs-prefix resource-path)] - (if (some? from-fs) - from-fs - (file-from-cp resource-path)))) + [fs-prefix resource-path + & {:keys [from-cp from-fs] + :or {from-cp true + from-fs true}}] + (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] - [fs-prefix :- s/Str - base-path :- s/Str - paths :- [s/Str]] +(defn get-resource-paths-recursive ;:- [s/Str] + [fs-prefix ;:- s/Str + base-path ;:- s/Str + paths ;:- [s/Str] + & {:keys [from-cp from-fs] + :or {from-cp true + from-fs true}}] (loop [paths paths result []] (if (not (empty? paths)) @@ -61,7 +72,9 @@ (let [path-to-work-with (first paths) file-to-work-with (io/file (file-from-cp-or-filesystem 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])] (cond (nil? file-to-work-with) [] @@ -77,47 +90,38 @@ (s/defn delete-resource-recursive! [path :- s/Str] (let [resource-paths - (reverse (get-resource-paths-recursive "" path [""]))] + (reverse (get-resource-paths-recursive + "" path [""] :from-cp false))] (println resource-paths) (doseq [path resource-paths] (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! - [fs-prefix source-path target-path ignore-patterns] - (let [source-file (file-from-cp-or-filesystem fs-prefix source-path) - target-file (io/file target-path source-path) - is-source-dir? (.isDirectory source-file)] - (if (nil? source-file) - (throw (IllegalArgumentException. (str "resource " source-path " not found"))) - (do-copy! source-file target-file ignore-patterns)))) + [fs-prefix ;:- s/Str + base-path ;:- s/Str + source-paths ;:- [s/Str] + target-path ;:- s/Str + ignore-patterns ;:- s/Str + ] + (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! [fs-prefix theme target-path ignore-patterns] (let [theme-path (str "templates/themes/" theme)] - (copy-resources! fs-prefix (str theme-path "/css") target-path ignore-patterns) - (copy-resources! fs-prefix (str theme-path "/js") target-path ignore-patterns) - (copy-resources! fs-prefix (str theme-path "/html/") target-path ignore-patterns))) + (copy-resources! fs-prefix theme-path ["css" "js" "html"] + target-path ignore-patterns))) diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 5f21eb7..1fd1f2f 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -534,10 +534,10 @@ (set-custom-resource-path! (str "file:resources/templates/themes/" theme)) (cryogen-io/wipe-public-folder keep-files) (println (blue "copying theme resources")) - (cp-io/copy-resources-from-theme "resources/templates/themes/" - theme - (cp-io/path "resources/public" blog-prefix) - ignored-files) + (cp-io/copy-resources-from-theme! "resources/templates/themes/" + theme + (cp-io/path "resources/public" blog-prefix) + ignored-files) (println (blue "copying resources")) (cryogen-io/copy-resources config) (copy-resources-from-markup-folders config) diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index a277621..4b3f535 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -25,18 +25,34 @@ (and (verify-file-exists 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 (is (= [] (sut/get-resource-paths-recursive "" "templates/themes/bootstrap4-test" ["not-existing"]))) (is (= ["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 (= ["js/subdir" "js/subdir/test.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 (= ["." "./css" @@ -59,14 +75,6 @@ (sut/delete-resource-recursive! (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 (is (= ["file.js"] @@ -88,15 +96,15 @@ (sut/delete-resource-recursive! (str "target/tmp" target)) (sut/copy-resources-from-theme! "./" theme target "") (and (verify-dir-exists - (str target "/templates/themes/bootstrap4-test/js")) + (str target "/js")) (verify-file-exists - (str target "/templates/themes/bootstrap4-test/js/dummy.js")) + (str target "/js/dummy.js")) (verify-dir-exists - (str target "/templates/themes/bootstrap4-test/js/subdir")) + (str target "/js/subdir")) (verify-file-exists - (str target "/templates/themes/bootstrap4-test/js/subdir/subdummy.js")) + (str target "/js/subdir/subdummy.js")) (verify-file-exists - (str target "/templates/themes/bootstrap4-test/css/dummy.css")) + (str target "/css/dummy.css")) (verify-file-exists - (str target "/templates/themes/bootstrap4-test/html/404.html")) + (str target "/html/404.html")) ))))