2014-12-05 15:56:40 +00:00
|
|
|
(ns cryogen-core.sass
|
2015-02-21 21:56:38 +00:00
|
|
|
(:require [clojure.java.shell :as shell]
|
2015-01-01 08:49:03 +00:00
|
|
|
[clojure.java.io :as io]
|
2015-02-21 22:07:41 +00:00
|
|
|
[text-decoration.core :refer :all]
|
2015-01-08 04:54:58 +00:00
|
|
|
[cryogen-core.io :refer [ignore match-re-filter]]))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2015-02-21 21:56:38 +00:00
|
|
|
(defmacro sh
|
|
|
|
[& args]
|
|
|
|
(let [valid-args (remove nil? args)]
|
|
|
|
`(shell/sh ~@valid-args)))
|
|
|
|
|
2014-12-04 16:38:48 +00:00
|
|
|
(defn sass-installed?
|
|
|
|
"Checks for the installation of Sass."
|
2016-12-17 10:29:16 +00:00
|
|
|
[path-sass]
|
|
|
|
(= 0 (:exit (sh path-sass "--version"))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
2015-02-21 21:56:38 +00:00
|
|
|
(defn compass-installed?
|
2016-12-02 00:55:47 +00:00
|
|
|
"Checks for the installation of Compass."
|
2016-12-17 10:29:16 +00:00
|
|
|
[path-compass]
|
2016-12-02 00:55:47 +00:00
|
|
|
(try
|
2016-12-17 10:29:16 +00:00
|
|
|
(= 0 (:exit (sh path-compass "--version")))
|
2016-12-02 00:55:47 +00:00
|
|
|
(catch java.io.IOException _
|
|
|
|
false)))
|
2015-02-21 21:56:38 +00:00
|
|
|
|
2014-12-04 16:38:48 +00:00
|
|
|
(defn find-sass-files
|
2015-01-01 08:49:03 +00:00
|
|
|
"Given a Diretory, gets files, Filtered to those having scss or sass
|
|
|
|
extention. Ignores files matching any ignored regexps."
|
2015-02-21 23:23:05 +00:00
|
|
|
[base-dir dir ignored-files]
|
2015-09-15 16:24:51 +00:00
|
|
|
(let [^java.io.FilenameFilter filename-filter (match-re-filter #"(?i:s[ca]ss$)")]
|
2015-02-21 23:23:05 +00:00
|
|
|
(->> (.listFiles (io/file base-dir dir) filename-filter)
|
2015-09-14 10:06:35 +00:00
|
|
|
(filter #(not (.isDirectory ^java.io.File %)))
|
2015-01-01 08:49:03 +00:00
|
|
|
(filter (ignore ignored-files))
|
2015-09-14 10:06:35 +00:00
|
|
|
(map #(.getName ^java.io.File %)))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(defn compile-sass-file!
|
|
|
|
"Given a sass file which might be in src-sass directory,
|
|
|
|
output the resulting css in dest-sass. All error handling is
|
|
|
|
done by sh / launching the sass command."
|
2015-02-21 23:23:05 +00:00
|
|
|
[{:keys [src-sass
|
|
|
|
dest-sass
|
2016-12-17 10:29:16 +00:00
|
|
|
path-sass
|
|
|
|
path-compass
|
2015-02-21 23:23:05 +00:00
|
|
|
base-dir]}]
|
|
|
|
(shell/with-sh-dir base-dir
|
2016-12-17 10:29:16 +00:00
|
|
|
(if (compass-installed? path-compass)
|
|
|
|
(sh path-sass "--compass" "--update" (str src-sass ":" dest-sass))
|
|
|
|
(sh path-sass "--update" (str src-sass ":" dest-sass)))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(defn compile-sass->css!
|
|
|
|
"Given a directory src-sass, looks for all sass files and compiles them into
|
|
|
|
dest-sass. Prompts you to install sass if he finds sass files and can't find
|
|
|
|
the command. Shows you any problems it comes across when compiling. "
|
2015-02-21 22:15:40 +00:00
|
|
|
[{:keys [src-sass
|
|
|
|
dest-sass
|
2016-12-17 10:29:16 +00:00
|
|
|
path-sass
|
2015-02-21 23:23:05 +00:00
|
|
|
ignored-files
|
|
|
|
base-dir] :as opts}]
|
|
|
|
(when-let [sass-files (seq (find-sass-files base-dir src-sass ignored-files))]
|
2016-12-17 10:29:16 +00:00
|
|
|
(if (sass-installed? path-sass)
|
2014-12-04 16:38:48 +00:00
|
|
|
;; I found sass files,
|
|
|
|
;; If sass is installed
|
2015-02-21 23:23:05 +00:00
|
|
|
(do
|
2016-12-02 03:36:25 +00:00
|
|
|
(println "\t" (cyan src-sass) "-->" (cyan dest-sass))
|
2015-02-21 23:23:05 +00:00
|
|
|
(let [result (compile-sass-file! opts)]
|
|
|
|
(if (zero? (:exit result))
|
|
|
|
;; no problems in sass compilation
|
|
|
|
(println "Successfully compiled sass files")
|
|
|
|
;; else I show the error
|
|
|
|
(println (red (:err result))
|
|
|
|
(red (:out result))))))
|
|
|
|
;; Else I prompt to install Sass
|
|
|
|
(println "Sass seems not to be installed, but you have scss / sass files in "
|
|
|
|
src-sass
|
|
|
|
" - You might want to install it here: sass-lang.com"))))
|