cryogen-core/src/cryogen_core/io.clj

95 lines
3.1 KiB
Clojure
Raw Normal View History

2014-12-05 15:56:40 +00:00
(ns cryogen-core.io
2014-12-14 18:37:59 +00:00
(:require [clojure.java.io :as io]
[clojure.string :as s]
2014-12-14 18:37:59 +00:00
[me.raynes.fs :as fs]))
2014-12-04 16:38:48 +00:00
(def public "resources/public")
(defn path
2019-12-20 14:55:53 +00:00
"Creates path from given parts, ignore empty elements"
[& path-parts]
(->> path-parts
(remove s/blank?)
(s/join "/")
(#(s/replace % #"/+" "/"))))
(defn re-filter [bool-fn re & other-res]
(let [res (conj other-res re)]
(reify java.io.FilenameFilter
(accept [this _ name]
(bool-fn (some #(re-find % name) res))))))
(def match-re-filter (partial re-filter some?))
(def reject-re-filter (partial re-filter nil?))
2014-12-04 16:38:48 +00:00
(defn get-resource [resource]
(-> resource io/resource io/file))
2014-12-04 16:38:48 +00:00
(defn ignore [ignored-files]
2015-09-14 10:06:35 +00:00
(fn [^java.io.File file]
(let [name (.getName file)
matches (map #(re-find % name) ignored-files)]
(not (some seq matches)))))
(defn find-assets
"Find all assets in the given root directory (f) and the given file
extension (ext) ignoring any files that match the given (ignored-files).
First make sure that the root directory exists, if yes: process as normal;
if no, return empty vector."
2015-09-14 10:06:35 +00:00
[f ^String ext ignored-files]
(if-let [root (get-resource f)]
(->> (get-resource f)
file-seq
(filter (ignore ignored-files))
2015-09-14 10:06:35 +00:00
(filter (fn [^java.io.File file] (-> file .getName (.endsWith ext)))))
[]))
2014-12-04 16:38:48 +00:00
(defn create-folder [folder]
(let [loc (io/file (path public folder))]
2014-12-04 16:38:48 +00:00
(when-not (.exists loc)
(.mkdirs loc))))
(defn create-file [file data]
(spit (path public file) data))
(defn create-file-recursive [file data]
(create-folder (.getParent (io/file file)))
(create-file file data))
2014-12-04 16:38:48 +00:00
(defn wipe-public-folder [keep-files]
(let [filenamefilter (reify java.io.FilenameFilter (accept [this _ filename] (not (some #{filename} keep-files))))]
2014-12-14 18:37:59 +00:00
(doseq [path (.listFiles (io/file public) filenamefilter)]
2014-12-04 16:38:48 +00:00
(fs/delete-dir path))))
(defn copy-dir [src target ignored-files]
(fs/mkdirs target)
2015-09-15 16:24:51 +00:00
(let [^java.io.FilenameFilter filename-filter (apply reject-re-filter ignored-files)
files (.listFiles (io/file src) filename-filter)]
2015-09-14 10:06:35 +00:00
(doseq [^java.io.File f files]
(let [out (io/file target (.getName f))]
(if (.isDirectory f)
(copy-dir f out ignored-files)
(io/copy f out))))))
(defn copy-resources [{:keys [blog-prefix resources ignored-files]}]
2014-12-04 16:38:48 +00:00
(doseq [resource resources]
(let [src (str "resources/templates/" resource)
target (path public blog-prefix (fs/base-name resource))]
2014-12-04 16:38:48 +00:00
(cond
2014-12-14 18:37:59 +00:00
(not (.exists (io/file src)))
2014-12-04 16:38:48 +00:00
(throw (IllegalArgumentException. (str "resource " src " not found")))
2014-12-14 18:37:59 +00:00
(.isDirectory (io/file src))
(copy-dir src target ignored-files)
2014-12-04 16:38:48 +00:00
:else
(fs/copy src target)))))
(defn copy-resources-from-theme
"Copy resources from theme"
[config]
(let [theme-path (str "themes/" (:theme config))]
(copy-resources
2019-12-20 14:55:53 +00:00
(merge config
{:resources [(str theme-path "/css")
(str theme-path "/js")
(str theme-path "/html/404.html")]}))))