formatting

This commit is contained in:
Jan Krebs 2020-02-20 16:08:38 +01:00
parent c6559f8a3e
commit 87901a0e91
13 changed files with 131 additions and 136 deletions

View file

@ -75,68 +75,67 @@
{}) {})
resulting-map (merge cp-resource-map fs-resource-map)] resulting-map (merge cp-resource-map fs-resource-map)]
(if (empty? resulting-map) (if (empty? resulting-map)
[] []
(vals resulting-map)))) (vals resulting-map))))
(defn get-resource-paths-recursive ;:- [VirtualPath] (defn get-resource-paths-recursive ;:- [VirtualPath]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] 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 #(:virtual-path %) (map #(:virtual-path %)
(get-resources (get-resources
fs-prefix base-path paths fs-prefix base-path paths
:from-cp from-cp :from-cp from-cp
:from-fs from-fs))) :from-fs from-fs)))
; TODO: Add files to keep ; TODO: Add files to keep
(s/defn delete-resources! (s/defn delete-resources!
[virtual-path :- s/Str] [virtual-path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (reverse
(sort (sort
(get-resource-paths-recursive (get-resource-paths-recursive
(str (fs/user-dir) "/") (str (fs/user-dir) "/")
virtual-path virtual-path
[""] [""]
:from-cp false)))] :from-cp false)))]
(do (do
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(Files/delete (fs/absolut-path virtual-path resource-path)))))) (Files/delete (fs/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 ;:- VirtualPath base-path ;:- VirtualPath
source-paths ;:- [VirtualPath] source-paths ;:- [VirtualPath]
target-path ;:- VirtualPath target-path ;:- VirtualPath
ignore-patterns ;:- s/Str ignore-patterns ;:- s/Str
] ]
(let [resources (let [resources
(sort (sort
this/compare-resource this/compare-resource
(get-resources fs-prefix base-path source-paths))] (get-resources fs-prefix base-path source-paths))]
(if (empty? resources) (if (empty? resources)
(throw (IllegalArgumentException. (str "resource " base-path ", " (throw (IllegalArgumentException. (str "resource " base-path ", "
source-paths " not found"))) source-paths " not found")))
(doseq [resource resources] (doseq [resource resources]
(let [target-path (fs/absolut-path target-path (:virtual-path resource)) (let [target-path (fs/absolut-path target-path (:virtual-path resource))
source-path (:java-path resource)] source-path (:java-path resource)]
(when (this/is-dir? resource) (when (this/is-dir? resource)
(Files/createDirectories target-path fs/no-attributes)) (Files/createDirectories target-path fs/no-attributes))
(when (this/is-file? resource) (when (this/is-file? resource)
(Files/copy source-path target-path fs/overwrite-preserve-attributes) (Files/copy source-path target-path fs/overwrite-preserve-attributes)))))))
))))))
(defn distinct-resources-by-path
(defn distinct-resources-by-path [resources]
[resources] (loop [paths (set (map :virtual-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 (:virtual-path (first resources))) (recur (disj paths (:virtual-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

@ -42,8 +42,7 @@
(when (some? java-path) (when (some? java-path)
(if is-jar-resource (if is-jar-resource
(jar/create-resource virtual-path java-path) (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] (s/defn get-resources ;:- [this/Resource]
"base-path is sensible for getting the right jar from classpath. So base-path "base-path is sensible for getting the right jar from classpath. So base-path

View file

@ -15,7 +15,7 @@
(def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS]))
(def follow-link-option (into-array LinkOption [])) (def follow-link-option (into-array LinkOption []))
(def no-attributes (into-array FileAttribute [])) (def no-attributes (into-array FileAttribute []))
(def overwrite-preserve-attributes (def overwrite-preserve-attributes
(into-array StandardCopyOption (into-array StandardCopyOption
[StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING])) [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING]))
@ -33,11 +33,11 @@
(defn path-if-exists (defn path-if-exists
[& path-elements] [& path-elements]
(let [path-from-fs (let [path-from-fs
(absolut-path (absolut-path
(apply this/virtual-path-from-elements path-elements))] (apply this/virtual-path-from-elements path-elements))]
(when (Files/exists path-from-fs follow-link-option) (when (Files/exists path-from-fs follow-link-option)
path-from-fs))) path-from-fs)))
(defn create-resource (defn create-resource
([virtual-path ([virtual-path
@ -59,40 +59,40 @@
source-type] source-type]
(create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path) source-type))) (create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path) source-type)))
(defn (defn
list-entries-for-dir list-entries-for-dir
[resource] [resource]
(.list (.toFile (:java-path resource)))) (.list (.toFile (:java-path resource))))
(defn get-resources ;:- [Resource] (defn get-resources ;:- [Resource]
([fs-prefix ;:- Prefix ([fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
source-type] source-type]
(loop [paths paths (loop [paths paths
result []] result []]
(if (not (empty? paths)) (if (not (empty? paths))
(let [path-to-work-with (first paths) (let [path-to-work-with (first paths)
resource-to-work-with (create-resource resource-to-work-with (create-resource
fs-prefix fs-prefix
base-path base-path
path-to-work-with path-to-work-with
source-type) source-type)
result (into result result (into result
[resource-to-work-with])] [resource-to-work-with])]
(cond (cond
(nil? resource-to-work-with) (recur (drop 1 paths) result) (nil? resource-to-work-with) (recur (drop 1 paths) result)
(this/is-file? resource-to-work-with) (this/is-file? resource-to-work-with)
(recur (drop 1 paths) result) (recur (drop 1 paths) result)
(this/is-dir? resource-to-work-with) (this/is-dir? resource-to-work-with)
(recur (into (drop 1 paths) (recur (into (drop 1 paths)
(map #(str path-to-work-with "/" %) (map #(str path-to-work-with "/" %)
(list-entries-for-dir resource-to-work-with))) (list-entries-for-dir resource-to-work-with)))
result) result)
:else [])) :else []))
(remove nil? result)))) (remove nil? result))))
([fs-prefix ;:- Prefix ([fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
] ]
(get-resources fs-prefix base-path paths :filesystem))) (get-resources fs-prefix base-path paths :filesystem)))

View file

@ -70,14 +70,14 @@
elements-list] elements-list]
(let [norm-path-to-filter-for (str base-path-to-filter-for "/") (let [norm-path-to-filter-for (str base-path-to-filter-for "/")
start (count norm-path-to-filter-for)] start (count norm-path-to-filter-for)]
(map (map
(fn [el] (fn [el]
(let [end (if (st/ends-with? el "/") (dec (count el)) (count el))] (let [end (if (st/ends-with? el "/") (dec (count el)) (count el))]
(subs el start end))) (subs el start end)))
(filter (filter
(fn [element] (and (st/starts-with? element norm-path-to-filter-for) (fn [element] (and (st/starts-with? element norm-path-to-filter-for)
(not (= element norm-path-to-filter-for)))) (not (= element norm-path-to-filter-for))))
elements-list)))) elements-list))))
(defn filter-and-remove-for-path (defn filter-and-remove-for-path
[path-to-filter-for [path-to-filter-for
@ -119,7 +119,7 @@
[base-path ;:- VirtualPath [base-path ;:- VirtualPath
paths ;:- [VirtualPath] paths ;:- [VirtualPath]
] ]
(let [entry-list (flatten (let [entry-list (flatten
(map (map
(fn [p] (fn [p]
(filter-and-remove-for-path (filter-and-remove-for-path

View file

@ -38,7 +38,7 @@
(filter #(not (empty? %)) (filter #(not (empty? %))
path-elements))) path-elements)))
(s/defn compare-resource (s/defn compare-resource
[first :- Resource [first :- Resource
second :- Resource] second :- Resource]
(compare (:virtual-path first) (:virtual-path second))) (compare (:virtual-path first) (:virtual-path second)))

View file

@ -514,7 +514,7 @@
klipsified-pages (map klipsify (read-pages config)) klipsified-pages (map klipsify (read-pages config))
modelled-pages (cond modelled-pages (cond
(= page-model :flat) klipsified-pages (= page-model :flat) klipsified-pages
(= page-model :hierarchic) (= page-model :hierarchic)
(hierarchic/build-hierarchic-map page-root-uri klipsified-pages)) (hierarchic/build-hierarchic-map page-root-uri klipsified-pages))
home-page (->> modelled-pages home-page (->> modelled-pages
(filter #(boolean (:home? %))) (filter #(boolean (:home? %)))

View file

@ -20,9 +20,9 @@
[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)]
(cp-io/copy-resources! fs-prefix theme-path ["css" "js"] (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"] (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! (defn copy-html-from-theme!
[fs-prefix theme target-path ignore-patterns] [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))] filter-ext (fn [xs] (filter #(= (get-file-extension-from-resource %) ext) xs))]
(->> assets (->> assets
filter-file filter-file
filter-ext))) filter-ext)))
; ;
; ;

View file

@ -21,7 +21,6 @@
(is (is
(sut/path-if-exists "dummy_only_in_cp_fs"))) (sut/path-if-exists "dummy_only_in_cp_fs")))
(deftest should-get-resources-from-jar-and-fs-classpath (deftest should-get-resources-from-jar-and-fs-classpath
(is (= (is (=
[] []

View file

@ -23,7 +23,7 @@
(deftest should-return-nil-on-not-existing-path (deftest should-return-nil-on-not-existing-path
(is (is
(= nil (= nil
(sut/path-if-exists fs-root "not-existing")))) (sut/path-if-exists fs-root "not-existing"))))
(deftest should-find-subdir-path (deftest should-find-subdir-path
@ -38,12 +38,11 @@
(is (is
(sut/path-if-exists fs-root nil "/dummy/dummy_from_fs"))) (sut/path-if-exists fs-root nil "/dummy/dummy_from_fs")))
(deftest test-list-entries-for-dir (deftest test-list-entries-for-dir
(is (is
(= ["dummy2" (= ["dummy2"
"dummy_from_fs"] "dummy_from_fs"]
(sort (sort
(seq (seq
(sut/list-entries-for-dir (sut/list-entries-for-dir
(sut/create-resource "dummy" (sut/path-if-exists fs-root "dummy") :filesytem))))))) (sut/create-resource "dummy" (sut/path-if-exists fs-root "dummy") :filesytem)))))))

View file

@ -16,15 +16,14 @@
(deftest test-is-from-classpath-jar? (deftest test-is-from-classpath-jar?
(is (is
(sut/is-from-classpath-jar? (.toURI (io/resource "dummy")))) (sut/is-from-classpath-jar? (.toURI (io/resource "dummy")))))
)
(deftest test-path-if-exists (deftest test-path-if-exists
(is (is
(sut/path-if-exists "dummy/dummy_from_jar"))) (sut/path-if-exists "dummy/dummy_from_jar")))
(is (is
(= nil (= nil
(sut/path-if-exists "not-existing"))) (sut/path-if-exists "not-existing")))
(deftest test-get-resources (deftest test-get-resources
(is (is

View file

@ -18,7 +18,7 @@
(is (is
(= "dummy" (= "dummy"
(sut/virtual-path-from-elements "dummy"))) (sut/virtual-path-from-elements "dummy")))
(is (is
(= "dummy" (= "dummy"
(sut/virtual-path-from-elements "" "dummy" ""))) (sut/virtual-path-from-elements "" "dummy" "")))
(is (is

View file

@ -21,7 +21,7 @@
"folder21" "folder21"
"folder21/file211" "folder21/file211"
"folder21/file212"] "folder21/file212"]
(sort (sort
(map ftt/filter-path (map ftt/filter-path
(sut/get-resources (sut/get-resources
"fs_root" "fs_root"
@ -39,8 +39,8 @@
(sort ["dummy2/dummy2_from_jar" "dummy2/dummy_common" "dummy2" "dummy_from_jar" "dummy_from_fs" (sort ["dummy2/dummy2_from_jar" "dummy2/dummy_common" "dummy2" "dummy_from_jar" "dummy_from_fs"
"dummy2/dummy2_from_fs"]) "dummy2/dummy2_from_fs"])
(sort (map ftt/filter-path (sort (map ftt/filter-path
(sut/get-resources "fs_root" "dummy" (sut/get-resources "fs_root" "dummy"
["dummy_from_jar" "dummy_from_fs" "dummy2"]))))) ["dummy_from_jar" "dummy_from_fs" "dummy2"])))))
(is (= (is (=
[{:virtual-path "js/dummy.js" [{:virtual-path "js/dummy.js"
:source-type :java-classpath-filesystem :source-type :java-classpath-filesystem

View file

@ -31,12 +31,12 @@
"")))))) ""))))))
(deftest test-create-dirs-from-markup-folders! (deftest test-create-dirs-from-markup-folders!
(is (is
(let [target-tmp "target/tmp-test-create-dirs-from-markup-folders"] (let [target-tmp "target/tmp-test-create-dirs-from-markup-folders"]
(sut/delete-resources! target-tmp) (sut/delete-resources! target-tmp)
(sut/create-dirs-from-markup-folders! (sut/create-dirs-from-markup-folders!
"./not-existing-get-from-cp" "./not-existing-get-from-cp"
"test_posts" "test_posts"
"test_pages" "test_pages"
target-tmp "") target-tmp "")
(and (ftt/verify-dir-exists (and (ftt/verify-dir-exists
@ -46,8 +46,8 @@
(ftt/verify-dir-exists (ftt/verify-dir-exists
(str target-tmp "/test_pages/home")))))) (str target-tmp "/test_pages/home"))))))
(deftest test-copy-resources-from-theme! (deftest test-copy-resources-from-theme!
(is (is
(let [target-tmp "target/tmp-test-copy-resources-from-theme"] (let [target-tmp "target/tmp-test-copy-resources-from-theme"]
(sut/delete-resources! target-tmp) (sut/delete-resources! target-tmp)
(sut/copy-resources-from-theme! "./" theme target-tmp "") (sut/copy-resources-from-theme! "./" theme target-tmp "")