Permalink
Browse files

Add `lein dieter-precompile` plugin

  • Loading branch information...
1 parent aa2bb17 commit 4abfc0068437f8fa344e6cfebb4694bb89fd6773 @pbiggar pbiggar committed Apr 15, 2012
@@ -0,0 +1,45 @@
+# Dieter precompile - a leiningen plugin
+
+A leiningen plugin for precompiling assets using Dieter, similar to Rails' `rake asset-precompile`.
+
+## Installation
+
+In `:dev-dependencies`, add:
+
+```clojure
+[lein-dieter-precompile "0.1"]
+```
+
+## Usage
+
+Run
+
+```bash
+lein dieter-precompile
+```
+
+and that's all.
+Unless you changed dieter's default settings, in which case you must tell lein-dieter-precompile where to find the settings for your project.
+In which case, add :dieter-options to your `project.clj` file.
+There are a few options for expression :dieter-options:
+
+1.
+ A string containing the name of a var in your project, where the var is expected to hold the dieter-options map:
+
+ ```clojure
+ :dieter-options "circle.http.assets/dieter-options"
+ ```
+
+2.
+ A string containing the name of a function in your project, where the function returns a dieter-optinos map:
+
+ ```clojure
+ :dieter-options "circle.http.assets/dieter-options-fn"
+ ```
+
+3.
+ A map containing the dieter options
+
+ ```clojure
+ :dieter-options {:production true}
+ ```
@@ -0,0 +1,6 @@
+(defproject lein-dieter-precompile "0.1.1"
+ :description "Precompile dieter assets from the command line"
+ :dev-dependencies [[dieter "0.1.3"]
+ [org.clojure/clojure "1.2.1"]]
+ :dependencies [[fs "0.11.1"]]
+ :eval-in-leiningen true)
@@ -0,0 +1,13 @@
+(ns leiningen.dieter-precompile
+ "Precompile dieter assets"
+ (:use [leiningen.compile :only (eval-in-project)]))
+
+
+(defn dieter-precompile
+ [project]
+ (eval-in-project
+ project
+ `(do (leiningen.dieter-precompile-runtime/precompile '~project))
+ nil
+ nil
+ `(require 'leiningen.dieter-precompile-runtime)))
@@ -0,0 +1,52 @@
+(ns leiningen.dieter-precompile-runtime
+ "Precompile dieter assets"
+ (:require [clojure.string :as string]
+ [clojure.java.io :as io]
+ [fs]
+ [dieter.settings]
+ [dieter.core]))
+
+(defn from-ns [ns-string]
+ (let [pair (string/split ns-string #"/")
+ pair (map symbol pair)
+ ns (first pair)
+ sym (second pair)]
+ (require ns)
+ @(ns-resolve ns sym)))
+
+(defn resolve-dieter-options [opt]
+ (cond
+ (map? opt) opt
+ (string? opt) (let [val (from-ns opt)]
+ (cond
+ (map? val) val
+ (fn? val) (val)))))
+
+(defn relative-path [root file]
+ (let [absroot (fs/abspath root)
+ absfile (fs/abspath file)
+ root-length (count absroot)]
+ (.substring absfile (inc root-length))))
+
+(defn precompile [project]
+ (let [opts (->> project
+ :dieter-options
+ resolve-dieter-options
+ (merge dieter.settings/*settings*))
+ asset-root (:asset-root opts)]
+
+ (fs/walk
+ (fs/join asset-root "assets")
+ (fn [root dirs files]
+ (doseq [file files]
+ (try
+ (binding [dieter.settings/*settings* opts]
+ (->> file
+ (fs/join root)
+ (relative-path asset-root)
+ (str "./")
+ (dieter.core/find-and-cache-asset))
+ (println "Built" (fs/join root file)))
+ (catch Exception e
+ (println "Not built" (fs/join root file)))))))
+ nil))

0 comments on commit 4abfc00

Please sign in to comment.