From 87901a0e9115a0f315fe7d909cc641b4c12a0ce7 Mon Sep 17 00:00:00 2001 From: Jan Krebs Date: Thu, 20 Feb 2020 16:08:38 +0100 Subject: [PATCH] formatting --- src/cryogen_core/classpath_able_io.clj | 121 +++++++++--------- src/cryogen_core/classpath_able_io/cp.clj | 3 +- src/cryogen_core/classpath_able_io/fs.clj | 78 +++++------ src/cryogen_core/classpath_able_io/jar.clj | 18 +-- src/cryogen_core/classpath_able_io/this.clj | 2 +- src/cryogen_core/compiler.clj | 2 +- src/cryogen_core/new_io.clj | 6 +- .../classpath_able_io/cp_test.clj | 1 - .../classpath_able_io/fs_test.clj | 9 +- .../classpath_able_io/jar_test.clj | 9 +- .../classpath_able_io/this_test.clj | 2 +- test/cryogen_core/classpath_able_io_test.clj | 6 +- test/cryogen_core/new_io_test.clj | 10 +- 13 files changed, 131 insertions(+), 136 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 03617de..de4c05d 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -75,68 +75,67 @@ {}) resulting-map (merge cp-resource-map fs-resource-map)] (if (empty? resulting-map) - [] - (vals resulting-map)))) + [] + (vals resulting-map)))) - (defn get-resource-paths-recursive ;:- [VirtualPath] - [fs-prefix ;:- Prefix - base-path ;:- VirtualPath - paths ;:- [VirtualPath] - & {:keys [from-cp from-fs] - :or {from-cp true - from-fs true}}] - (map #(:virtual-path %) - (get-resources - fs-prefix base-path paths - :from-cp from-cp - :from-fs from-fs))) +(defn get-resource-paths-recursive ;:- [VirtualPath] + [fs-prefix ;:- Prefix + base-path ;:- VirtualPath + paths ;:- [VirtualPath] + & {:keys [from-cp from-fs] + :or {from-cp true + from-fs true}}] + (map #(:virtual-path %) + (get-resources + fs-prefix base-path paths + :from-cp from-cp + :from-fs from-fs))) ; TODO: Add files to keep - (s/defn delete-resources! - [virtual-path :- s/Str] - (let [resource-paths - (reverse - (sort - (get-resource-paths-recursive - (str (fs/user-dir) "/") - virtual-path - [""] - :from-cp false)))] - (do - (doseq [resource-path resource-paths] - (Files/delete (fs/absolut-path virtual-path resource-path)))))) - +(s/defn delete-resources! + [virtual-path :- s/Str] + (let [resource-paths + (reverse + (sort + (get-resource-paths-recursive + (str (fs/user-dir) "/") + virtual-path + [""] + :from-cp false)))] + (do + (doseq [resource-path resource-paths] + (Files/delete (fs/absolut-path virtual-path resource-path)))))) + ; TODO: add ignore patterns filtering - (defn copy-resources! - [fs-prefix ;:- Prefix - base-path ;:- VirtualPath - source-paths ;:- [VirtualPath] - target-path ;:- VirtualPath - ignore-patterns ;:- s/Str - ] - (let [resources - (sort - this/compare-resource - (get-resources fs-prefix base-path source-paths))] - (if (empty? resources) - (throw (IllegalArgumentException. (str "resource " base-path ", " - source-paths " not found"))) - (doseq [resource resources] - (let [target-path (fs/absolut-path target-path (:virtual-path resource)) - source-path (:java-path resource)] - (when (this/is-dir? resource) - (Files/createDirectories target-path fs/no-attributes)) - (when (this/is-file? resource) - (Files/copy source-path target-path fs/overwrite-preserve-attributes) - )))))) - - (defn distinct-resources-by-path - [resources] - (loop [paths (set (map :virtual-path resources)) - resources resources - acc []] - (cond (empty? resources) acc - (contains? paths (:virtual-path (first resources))) (recur (disj paths (:virtual-path (first resources))) - (rest resources) - (conj acc (first resources))) - :else (recur paths (rest resources) acc)))) +(defn copy-resources! + [fs-prefix ;:- Prefix + base-path ;:- VirtualPath + source-paths ;:- [VirtualPath] + target-path ;:- VirtualPath + ignore-patterns ;:- s/Str + ] + (let [resources + (sort + this/compare-resource + (get-resources fs-prefix base-path source-paths))] + (if (empty? resources) + (throw (IllegalArgumentException. (str "resource " base-path ", " + source-paths " not found"))) + (doseq [resource resources] + (let [target-path (fs/absolut-path target-path (:virtual-path resource)) + source-path (:java-path resource)] + (when (this/is-dir? resource) + (Files/createDirectories target-path fs/no-attributes)) + (when (this/is-file? resource) + (Files/copy source-path target-path fs/overwrite-preserve-attributes))))))) + +(defn distinct-resources-by-path + [resources] + (loop [paths (set (map :virtual-path resources)) + resources resources + acc []] + (cond (empty? resources) acc + (contains? paths (:virtual-path (first resources))) (recur (disj paths (:virtual-path (first resources))) + (rest resources) + (conj acc (first resources))) + :else (recur paths (rest resources) acc)))) diff --git a/src/cryogen_core/classpath_able_io/cp.clj b/src/cryogen_core/classpath_able_io/cp.clj index de1ddb2..4019ae5 100644 --- a/src/cryogen_core/classpath_able_io/cp.clj +++ b/src/cryogen_core/classpath_able_io/cp.clj @@ -42,8 +42,7 @@ (when (some? java-path) (if is-jar-resource (jar/create-resource virtual-path java-path) - (fs/create-resource virtual-path java-path :java-classpath-filesystem)) - )))) + (fs/create-resource virtual-path java-path :java-classpath-filesystem)))))) (s/defn get-resources ;:- [this/Resource] "base-path is sensible for getting the right jar from classpath. So base-path diff --git a/src/cryogen_core/classpath_able_io/fs.clj b/src/cryogen_core/classpath_able_io/fs.clj index 30bffb0..51be722 100644 --- a/src/cryogen_core/classpath_able_io/fs.clj +++ b/src/cryogen_core/classpath_able_io/fs.clj @@ -15,7 +15,7 @@ (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) (def follow-link-option (into-array LinkOption [])) (def no-attributes (into-array FileAttribute [])) -(def overwrite-preserve-attributes +(def overwrite-preserve-attributes (into-array StandardCopyOption [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING])) @@ -33,11 +33,11 @@ (defn path-if-exists [& path-elements] - (let [path-from-fs + (let [path-from-fs (absolut-path (apply this/virtual-path-from-elements path-elements))] (when (Files/exists path-from-fs follow-link-option) - path-from-fs))) + path-from-fs))) (defn create-resource ([virtual-path @@ -59,40 +59,40 @@ source-type] (create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path) source-type))) - (defn - list-entries-for-dir - [resource] - (.list (.toFile (:java-path resource)))) +(defn + list-entries-for-dir + [resource] + (.list (.toFile (:java-path resource)))) - (defn get-resources ;:- [Resource] - ([fs-prefix ;:- Prefix - base-path ;:- VirtualPath - paths ;:- [VirtualPath] - source-type] - (loop [paths paths - result []] - (if (not (empty? paths)) - (let [path-to-work-with (first paths) - resource-to-work-with (create-resource - fs-prefix - base-path - path-to-work-with - source-type) - result (into result - [resource-to-work-with])] - (cond - (nil? resource-to-work-with) (recur (drop 1 paths) result) - (this/is-file? resource-to-work-with) - (recur (drop 1 paths) result) - (this/is-dir? resource-to-work-with) - (recur (into (drop 1 paths) - (map #(str path-to-work-with "/" %) - (list-entries-for-dir resource-to-work-with))) - result) - :else [])) - (remove nil? result)))) -([fs-prefix ;:- Prefix - base-path ;:- VirtualPath - paths ;:- [VirtualPath] - ] - (get-resources fs-prefix base-path paths :filesystem))) +(defn get-resources ;:- [Resource] + ([fs-prefix ;:- Prefix + base-path ;:- VirtualPath + paths ;:- [VirtualPath] + source-type] + (loop [paths paths + result []] + (if (not (empty? paths)) + (let [path-to-work-with (first paths) + resource-to-work-with (create-resource + fs-prefix + base-path + path-to-work-with + source-type) + result (into result + [resource-to-work-with])] + (cond + (nil? resource-to-work-with) (recur (drop 1 paths) result) + (this/is-file? resource-to-work-with) + (recur (drop 1 paths) result) + (this/is-dir? resource-to-work-with) + (recur (into (drop 1 paths) + (map #(str path-to-work-with "/" %) + (list-entries-for-dir resource-to-work-with))) + result) + :else [])) + (remove nil? result)))) + ([fs-prefix ;:- Prefix + base-path ;:- VirtualPath + paths ;:- [VirtualPath] + ] + (get-resources fs-prefix base-path paths :filesystem))) diff --git a/src/cryogen_core/classpath_able_io/jar.clj b/src/cryogen_core/classpath_able_io/jar.clj index 5736a8c..ebeeebd 100644 --- a/src/cryogen_core/classpath_able_io/jar.clj +++ b/src/cryogen_core/classpath_able_io/jar.clj @@ -70,14 +70,14 @@ elements-list] (let [norm-path-to-filter-for (str base-path-to-filter-for "/") start (count norm-path-to-filter-for)] - (map - (fn [el] - (let [end (if (st/ends-with? el "/") (dec (count el)) (count el))] - (subs el start end))) - (filter - (fn [element] (and (st/starts-with? element norm-path-to-filter-for) - (not (= element norm-path-to-filter-for)))) - elements-list)))) + (map + (fn [el] + (let [end (if (st/ends-with? el "/") (dec (count el)) (count el))] + (subs el start end))) + (filter + (fn [element] (and (st/starts-with? element norm-path-to-filter-for) + (not (= element norm-path-to-filter-for)))) + elements-list)))) (defn filter-and-remove-for-path [path-to-filter-for @@ -119,7 +119,7 @@ [base-path ;:- VirtualPath paths ;:- [VirtualPath] ] - (let [entry-list (flatten + (let [entry-list (flatten (map (fn [p] (filter-and-remove-for-path diff --git a/src/cryogen_core/classpath_able_io/this.clj b/src/cryogen_core/classpath_able_io/this.clj index db6d31e..e854733 100644 --- a/src/cryogen_core/classpath_able_io/this.clj +++ b/src/cryogen_core/classpath_able_io/this.clj @@ -38,7 +38,7 @@ (filter #(not (empty? %)) path-elements))) -(s/defn compare-resource +(s/defn compare-resource [first :- Resource second :- Resource] (compare (:virtual-path first) (:virtual-path second))) diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 3cfe7c5..d6dc776 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -514,7 +514,7 @@ klipsified-pages (map klipsify (read-pages config)) modelled-pages (cond (= page-model :flat) klipsified-pages - (= page-model :hierarchic) + (= page-model :hierarchic) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages)) home-page (->> modelled-pages (filter #(boolean (:home? %))) diff --git a/src/cryogen_core/new_io.clj b/src/cryogen_core/new_io.clj index 47df569..1553a60 100644 --- a/src/cryogen_core/new_io.clj +++ b/src/cryogen_core/new_io.clj @@ -20,9 +20,9 @@ [fs-prefix theme target-path ignore-patterns] (let [theme-path (str "templates/themes/" theme)] (cp-io/copy-resources! fs-prefix theme-path ["css" "js"] - target-path ignore-patterns) + target-path ignore-patterns) (cp-io/copy-resources! fs-prefix (str theme-path "/html") ["404.html"] - target-path ignore-patterns))) + target-path ignore-patterns))) (defn copy-html-from-theme! [fs-prefix theme target-path ignore-patterns] @@ -77,7 +77,7 @@ if no, return empty vector." filter-ext (fn [xs] (filter #(= (get-file-extension-from-resource %) ext) xs))] (->> assets filter-file - filter-ext))) + filter-ext))) ; ; diff --git a/test/cryogen_core/classpath_able_io/cp_test.clj b/test/cryogen_core/classpath_able_io/cp_test.clj index 676f45f..c89b61b 100644 --- a/test/cryogen_core/classpath_able_io/cp_test.clj +++ b/test/cryogen_core/classpath_able_io/cp_test.clj @@ -21,7 +21,6 @@ (is (sut/path-if-exists "dummy_only_in_cp_fs"))) - (deftest should-get-resources-from-jar-and-fs-classpath (is (= [] diff --git a/test/cryogen_core/classpath_able_io/fs_test.clj b/test/cryogen_core/classpath_able_io/fs_test.clj index 59a98d0..a534e54 100644 --- a/test/cryogen_core/classpath_able_io/fs_test.clj +++ b/test/cryogen_core/classpath_able_io/fs_test.clj @@ -23,7 +23,7 @@ (deftest should-return-nil-on-not-existing-path (is - (= nil + (= nil (sut/path-if-exists fs-root "not-existing")))) (deftest should-find-subdir-path @@ -38,12 +38,11 @@ (is (sut/path-if-exists fs-root nil "/dummy/dummy_from_fs"))) - (deftest test-list-entries-for-dir - (is - (= ["dummy2" + (is + (= ["dummy2" "dummy_from_fs"] - (sort + (sort (seq (sut/list-entries-for-dir (sut/create-resource "dummy" (sut/path-if-exists fs-root "dummy") :filesytem))))))) diff --git a/test/cryogen_core/classpath_able_io/jar_test.clj b/test/cryogen_core/classpath_able_io/jar_test.clj index 1e41993..d8e09a8 100644 --- a/test/cryogen_core/classpath_able_io/jar_test.clj +++ b/test/cryogen_core/classpath_able_io/jar_test.clj @@ -16,15 +16,14 @@ (deftest test-is-from-classpath-jar? (is - (sut/is-from-classpath-jar? (.toURI (io/resource "dummy")))) - ) + (sut/is-from-classpath-jar? (.toURI (io/resource "dummy"))))) (deftest test-path-if-exists (is (sut/path-if-exists "dummy/dummy_from_jar"))) - (is - (= nil - (sut/path-if-exists "not-existing"))) +(is + (= nil + (sut/path-if-exists "not-existing"))) (deftest test-get-resources (is diff --git a/test/cryogen_core/classpath_able_io/this_test.clj b/test/cryogen_core/classpath_able_io/this_test.clj index a407db5..8f64cb6 100644 --- a/test/cryogen_core/classpath_able_io/this_test.clj +++ b/test/cryogen_core/classpath_able_io/this_test.clj @@ -18,7 +18,7 @@ (is (= "dummy" (sut/virtual-path-from-elements "dummy"))) - (is + (is (= "dummy" (sut/virtual-path-from-elements "" "dummy" ""))) (is diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index d0a33ea..ca26169 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -21,7 +21,7 @@ "folder21" "folder21/file211" "folder21/file212"] - (sort + (sort (map ftt/filter-path (sut/get-resources "fs_root" @@ -39,8 +39,8 @@ (sort ["dummy2/dummy2_from_jar" "dummy2/dummy_common" "dummy2" "dummy_from_jar" "dummy_from_fs" "dummy2/dummy2_from_fs"]) (sort (map ftt/filter-path - (sut/get-resources "fs_root" "dummy" - ["dummy_from_jar" "dummy_from_fs" "dummy2"]))))) + (sut/get-resources "fs_root" "dummy" + ["dummy_from_jar" "dummy_from_fs" "dummy2"]))))) (is (= [{:virtual-path "js/dummy.js" :source-type :java-classpath-filesystem diff --git a/test/cryogen_core/new_io_test.clj b/test/cryogen_core/new_io_test.clj index 5be6110..dbd5e48 100644 --- a/test/cryogen_core/new_io_test.clj +++ b/test/cryogen_core/new_io_test.clj @@ -31,12 +31,12 @@ "")))))) (deftest test-create-dirs-from-markup-folders! - (is + (is (let [target-tmp "target/tmp-test-create-dirs-from-markup-folders"] (sut/delete-resources! target-tmp) (sut/create-dirs-from-markup-folders! - "./not-existing-get-from-cp" - "test_posts" + "./not-existing-get-from-cp" + "test_posts" "test_pages" target-tmp "") (and (ftt/verify-dir-exists @@ -46,8 +46,8 @@ (ftt/verify-dir-exists (str target-tmp "/test_pages/home")))))) -(deftest test-copy-resources-from-theme! - (is +(deftest test-copy-resources-from-theme! + (is (let [target-tmp "target/tmp-test-copy-resources-from-theme"] (sut/delete-resources! target-tmp) (sut/copy-resources-from-theme! "./" theme target-tmp "")