2014-12-05 15:56:40 +00:00
|
|
|
(ns cryogen-core.sass
|
2017-01-16 07:37:19 +00:00
|
|
|
(:require [clojure.java.io :as io]
|
|
|
|
[clojure.java.shell :as shell]
|
2015-02-21 22:07:41 +00:00
|
|
|
[text-decoration.core :refer :all]
|
2017-01-16 07:37:19 +00:00
|
|
|
[cryogen-core.io :as cryogen-io]))
|
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."
|
2017-01-16 07:37:19 +00:00
|
|
|
[sass-path]
|
|
|
|
(zero? (:exit (sh sass-path "--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."
|
2017-01-16 07:37:19 +00:00
|
|
|
[compass-path]
|
2016-12-02 00:55:47 +00:00
|
|
|
(try
|
2017-01-16 07:37:19 +00:00
|
|
|
(zero? (:exit (sh compass-path "--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
|
2017-01-16 07:37:19 +00:00
|
|
|
extention. Ignores files matching any ignored regexps."
|
2015-02-21 23:23:05 +00:00
|
|
|
[base-dir dir ignored-files]
|
2017-01-16 07:37:19 +00:00
|
|
|
(let [^java.io.FilenameFilter filename-filter (cryogen-io/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 %)))
|
2017-01-16 07:37:19 +00:00
|
|
|
(filter (cryogen-io/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!
|
2017-01-16 07:37:19 +00:00
|
|
|
"Given a sass file which might be in sass-src directory,
|
|
|
|
output the resulting css in sass-dest. All error handling is
|
|
|
|
done by sh / launching the sass command."
|
|
|
|
[{:keys [sass-src sass-dest sass-path compass-path base-dir]}]
|
2015-02-21 23:23:05 +00:00
|
|
|
(shell/with-sh-dir base-dir
|
2017-01-16 07:37:19 +00:00
|
|
|
(if (compass-installed? compass-path)
|
|
|
|
(sh sass-path "--compass" "--update" (str sass-src ":" sass-dest))
|
|
|
|
(sh sass-path "--update" (str sass-src ":" sass-dest)))))
|
2014-12-04 16:38:48 +00:00
|
|
|
|
|
|
|
(defn compile-sass->css!
|
2017-01-16 07:37:19 +00:00
|
|
|
"Given a directory sass-src, looks for all sass files and compiles them into
|
|
|
|
sass-dest. 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. "
|
|
|
|
[{:keys [sass-src sass-dest sass-path ignored-files base-dir] :as opts}]
|
|
|
|
(when (seq (find-sass-files base-dir sass-src ignored-files))
|
|
|
|
(if (sass-installed? sass-path)
|
2015-02-21 23:23:05 +00:00
|
|
|
(do
|
2017-01-16 07:37:19 +00:00
|
|
|
(println "\t" (cyan sass-src) "-->" (cyan sass-dest))
|
2015-02-21 23:23:05 +00:00
|
|
|
(let [result (compile-sass-file! opts)]
|
|
|
|
(if (zero? (:exit result))
|
|
|
|
(println "Successfully compiled sass files")
|
|
|
|
(println (red (:err result))
|
|
|
|
(red (:out result))))))
|
|
|
|
(println "Sass seems not to be installed, but you have scss / sass files in "
|
2017-01-16 07:37:19 +00:00
|
|
|
sass-src
|
2015-02-21 23:23:05 +00:00
|
|
|
" - You might want to install it here: sass-lang.com"))))
|