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]
|
2015-11-08 13:33:07 +00:00
|
|
|
[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")
|
|
|
|
|
2015-11-08 13:33:07 +00:00
|
|
|
(defn path
|
|
|
|
"Creates path from given parts, ignore empty elements"
|
|
|
|
[& path-parts]
|
|
|
|
(->> path-parts
|
|
|
|
(remove s/blank?)
|
|
|
|
(s/join "/")
|
|
|
|
(#(s/replace % #"/+" "/"))))
|
|
|
|
|
2015-01-08 04:54:58 +00:00
|
|
|
(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]
|
2015-01-01 08:49:03 +00:00
|
|
|
(-> resource io/resource io/file))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2014-12-27 07:38:25 +00:00
|
|
|
(defn ignore [ignored-files]
|
2015-09-14 10:06:35 +00:00
|
|
|
(fn [^java.io.File file]
|
2014-12-27 07:38:25 +00:00
|
|
|
(let [name (.getName file)
|
|
|
|
matches (map #(re-find % name) ignored-files)]
|
|
|
|
(not (some seq matches)))))
|
|
|
|
|
2015-01-11 19:20:10 +00:00
|
|
|
(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]
|
2015-01-11 19:20:10 +00:00
|
|
|
(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)))))
|
2015-01-11 19:20:10 +00:00
|
|
|
[]))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(defn create-folder [folder]
|
2016-02-12 02:04:56 +00:00
|
|
|
(let [loc (io/file (path public folder))]
|
2014-12-04 16:38:48 +00:00
|
|
|
(when-not (.exists loc)
|
|
|
|
(.mkdirs loc))))
|
|
|
|
|
2015-11-08 13:33:07 +00:00
|
|
|
(defn create-file [file data]
|
2016-02-12 02:04:56 +00:00
|
|
|
(spit (path public file) data))
|
|
|
|
|
|
|
|
(defn create-file-recursive [file data]
|
|
|
|
(create-folder (.getParent (io/file file)))
|
|
|
|
(create-file file data))
|
2015-11-08 13:33:07 +00:00
|
|
|
|
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))))
|
|
|
|
|
2015-01-08 04:54:58 +00:00
|
|
|
(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)
|
2015-01-08 04:54:58 +00:00
|
|
|
files (.listFiles (io/file src) filename-filter)]
|
2015-09-14 10:06:35 +00:00
|
|
|
(doseq [^java.io.File f files]
|
2015-01-08 04:54:58 +00:00
|
|
|
(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)
|
2015-11-08 13:33:07 +00:00
|
|
|
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))
|
2015-01-08 04:54:58 +00:00
|
|
|
(copy-dir src target ignored-files)
|
2014-12-04 16:38:48 +00:00
|
|
|
:else
|
|
|
|
(fs/copy src target)))))
|
2015-06-14 15:16:35 +00:00
|
|
|
|
|
|
|
(defn copy-resources-from-theme
|
|
|
|
"Copy resources from theme"
|
|
|
|
[config]
|
|
|
|
(let [theme-path (str "themes/" (:theme config))]
|
|
|
|
(copy-resources
|
|
|
|
(merge config
|
|
|
|
{:resources [(str theme-path "/css")
|
|
|
|
(str theme-path "/js")
|
|
|
|
(str theme-path "/html/404.html")]}))))
|