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]
|
|
|
|
[me.raynes.fs :as fs]))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(def public "resources/public")
|
|
|
|
|
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]
|
|
|
|
(fn [file]
|
|
|
|
(let [name (.getName file)
|
|
|
|
matches (map #(re-find % name) ignored-files)]
|
|
|
|
(not (some seq matches)))))
|
|
|
|
|
|
|
|
(defn find-assets [f ext ignored-files]
|
2014-12-04 16:38:48 +00:00
|
|
|
(->> (get-resource f)
|
|
|
|
file-seq
|
2014-12-27 07:38:25 +00:00
|
|
|
(filter (ignore ignored-files))
|
2014-12-04 16:38:48 +00:00
|
|
|
(filter (fn [file] (-> file .getName (.endsWith ext))))))
|
|
|
|
|
|
|
|
(defn create-folder [folder]
|
2014-12-14 18:37:59 +00:00
|
|
|
(let [loc (io/file (str public folder))]
|
2014-12-04 16:38:48 +00:00
|
|
|
(when-not (.exists loc)
|
|
|
|
(.mkdirs loc))))
|
|
|
|
|
|
|
|
(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))))
|
|
|
|
|
2014-12-05 17:17:44 +00:00
|
|
|
(defn copy-images-from-markdown-folders [{:keys [blog-prefix]}]
|
2014-12-14 18:37:59 +00:00
|
|
|
(doseq [asset (fs/find-files "resources/templates/md" #".+(jpg|jpeg|png|gif)")]
|
|
|
|
(io/copy asset (io/file (str public blog-prefix "/img/" (.getName asset))))))
|
2014-12-05 17:17:44 +00:00
|
|
|
|
2015-01-08 04:54:58 +00:00
|
|
|
(defn copy-dir [src target ignored-files]
|
|
|
|
(fs/mkdirs target)
|
|
|
|
(let [filename-filter (apply reject-re-filter ignored-files)
|
|
|
|
files (.listFiles (io/file src) filename-filter)]
|
|
|
|
(doseq [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)
|
2015-01-01 08:49:03 +00:00
|
|
|
target (str public blog-prefix "/" 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)))))
|