Merge pull request #20 from wjlroe/compile-sass-with-compass

Compile sass with compass when available
This commit is contained in:
Carmen La 2015-02-21 23:34:31 -05:00
commit efdd06ef29
2 changed files with 45 additions and 32 deletions

View file

@ -321,9 +321,10 @@
(rss/make-filtered-channels public config posts-by-tag) (rss/make-filtered-channels public config posts-by-tag)
(println (blue "compiling sass")) (println (blue "compiling sass"))
(sass/compile-sass->css! (sass/compile-sass->css!
(str "resources/templates/" sass-src) {:src-sass sass-src
(str "resources/public" blog-prefix "/" sass-dest) :dest-sass (str "../public" blog-prefix "/" sass-dest)
ignored-files))) :ignored-files ignored-files
:base-dir "resources/templates/"})))
(defn compile-assets-timed [] (defn compile-assets-timed []
(time (time

View file

@ -1,19 +1,30 @@
(ns cryogen-core.sass (ns cryogen-core.sass
(:require [clojure.java.shell :refer [sh]] (:require [clojure.java.shell :as shell]
[clojure.java.io :as io] [clojure.java.io :as io]
[text-decoration.core :refer :all]
[cryogen-core.io :refer [ignore match-re-filter]])) [cryogen-core.io :refer [ignore match-re-filter]]))
(defmacro sh
[& args]
(let [valid-args (remove nil? args)]
`(shell/sh ~@valid-args)))
(defn sass-installed? (defn sass-installed?
"Checks for the installation of Sass." "Checks for the installation of Sass."
[] []
(= 0 (:exit (sh "sass" "--version")))) (= 0 (:exit (sh "sass" "--version"))))
(defn compass-installed?
"Checks for the installation of Compass."
[]
(= 0 (:exit (sh "compass" "--version"))))
(defn find-sass-files (defn find-sass-files
"Given a Diretory, gets files, Filtered to those having scss or sass "Given a Diretory, gets files, Filtered to those having scss or sass
extention. Ignores files matching any ignored regexps." extention. Ignores files matching any ignored regexps."
[dir ignored-files] [base-dir dir ignored-files]
(let [filename-filter (match-re-filter #"(?i:s[ca]ss$)")] (let [filename-filter (match-re-filter #"(?i:s[ca]ss$)")]
(->> (.listFiles (io/file dir) filename-filter) (->> (.listFiles (io/file base-dir dir) filename-filter)
(filter #(not (.isDirectory %))) (filter #(not (.isDirectory %)))
(filter (ignore ignored-files)) (filter (ignore ignored-files))
(map #(.getName %))))) (map #(.getName %)))))
@ -22,36 +33,37 @@
"Given a sass file which might be in src-sass directory, "Given a sass file which might be in src-sass directory,
output the resulting css in dest-sass. All error handling is output the resulting css in dest-sass. All error handling is
done by sh / launching the sass command." done by sh / launching the sass command."
[sass-file [{:keys [src-sass
src-sass dest-sass
dest-sass] base-dir]}]
(sh "sass" (shell/with-sh-dir base-dir
"--update" (sh "sass"
(str src-sass "/" sass-file) "--update"
(str dest-sass "/" ))) (when (compass-installed?) "--compass")
(str src-sass ":" dest-sass))))
(defn compile-sass->css! (defn compile-sass->css!
"Given a directory src-sass, looks for all sass files and compiles them into "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 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. " the command. Shows you any problems it comes across when compiling. "
[src-sass [{:keys [src-sass
dest-sass dest-sass
ignored-files] ignored-files
(let [sass-files (find-sass-files src-sass ignored-files)] base-dir] :as opts}]
(if (seq sass-files) (when-let [sass-files (seq (find-sass-files base-dir src-sass ignored-files))]
(if (sass-installed?)
;; I found sass files, ;; I found sass files,
;; If sass is installed ;; If sass is installed
(if (sass-installed?) (do
;; I compile all files (println "Compiling Sass Files:" src-sass dest-sass)
(doseq [a-file sass-files] (let [result (compile-sass-file! opts)]
(println "Compiling Sass File:" a-file) (if (zero? (:exit result))
(let [result (compile-sass-file! a-file src-sass dest-sass)] ;; no problems in sass compilation
(if (zero? (:exit result)) (println "Successfully compiled sass files")
;; no problems in sass compilation ;; else I show the error
(println "Successfully compiled:" a-file) (println (red (:err result))
;; else I show the error (red (:out result))))))
(println (:err result))))) ;; Else I prompt to install Sass
;; Else I prompt to install Sass (println "Sass seems not to be installed, but you have scss / sass files in "
(println "Sass seems not to be installed, but you have scss / sass files in " src-sass
src-sass " - You might want to install it here: sass-lang.com"))))
" - You might want to install it here: sass-lang.com")))))