Commit 8fa87936312fb02c4022bc93778fc4d7ffc2276a

Thomas de Grivel 2013-12-17T18:15:30

Provide asset observers for caching. Added ASSET-WRITE-DATE and ASSET-SOURCES, and unexported PREPROCESS-ASSET.

diff --git a/asset.lisp b/asset.lisp
index cd1d698..0366ef4 100644
--- a/asset.lisp
+++ b/asset.lisp
@@ -29,13 +29,18 @@
 	       :type string)
    (source-ext :initarg :source-ext
 	       :reader asset-source-ext
-	       :type keyword)))
+	       :type keyword)
+   (sources :initarg :sources
+	    :reader asset-sources
+	    :type list)))
 
 (defgeneric asset-ext (asset))
 (defgeneric asset-url (asset))
 (defgeneric asset-path (asset))
 (defgeneric asset-source-path (asset))
 (defgeneric asset-include (output context asset &key &allow-other-keys))
+
+(defgeneric asset-write-date (asset))
 (defgeneric compile-asset (asset output))
 
 ;;  Base implementation
@@ -62,6 +67,13 @@
     (ignore-errors (format stream "~S" (asset-path asset)))
     (ignore-errors (format stream " ~S" (asset-source-path asset)))))
 
+(defmethod slot-unbound (class (asset asset) (slot (eql 'sources)))
+  (setf (slot-value asset 'sources) (list asset)))
+
+(defmethod asset-write-date ((asset asset))
+  (loop for a in (asset-sources asset)
+     maximize (file-write-date (asset-source-path a))))
+
 (defmethod compile-asset ((asset asset) (output stream))
   (let ((path (asset-source-path asset)))
     (msg "CP ~A" path)
diff --git a/find.lisp b/find.lisp
index 7c394d9..c07fca0 100644
--- a/find.lisp
+++ b/find.lisp
@@ -148,9 +148,15 @@
 (defmethod asset-path ((spec string))
   (asset-path (find-asset spec)))
 
+(defmethod asset-write-date ((spec string))
+  (asset-write-date (find-asset spec)))
+
 (defmethod asset-source-path ((spec string))
   (asset-source-path (find-asset spec)))
 
+(defmethod asset-sources ((spec string))
+  (asset-sources (find-asset spec)))
+
 (defmethod asset-include (output
 			  context
 			  (spec string)
diff --git a/package.lisp b/package.lisp
index c5353bc..faec8d8 100644
--- a/package.lisp
+++ b/package.lisp
@@ -40,6 +40,8 @@
    ;;  Observers
    #:asset-path
    #:asset-url
+   #:asset-sources
+   #:asset-write-date
    #:assets-dirs
    #:find-asset
    #:find-assets-from-spec
@@ -47,7 +49,6 @@
    #:locate-precompiled-assets
    ;;  Rendering
    #:process-asset
-   #:preprocess-asset
    #:asset-include
    ;;  Precompile
    #:debug-msg
diff --git a/preprocess.lisp b/preprocess.lisp
index afc8361..371a7c7 100644
--- a/preprocess.lisp
+++ b/preprocess.lisp
@@ -83,19 +83,21 @@
 
 (defgeneric preprocess-asset (asset))
 
-(defmethod preprocess-asset ((asset asset))
+(defmethod preprocess-asset ((asset preprocessed-asset))
   (nreverse (preprocess/asset asset nil)))
 
-(defmethod preprocess-asset ((spec string))
-  (preprocess-asset (find-asset spec)))
+(defmethod slot-unbound (class
+			 (asset preprocessed-asset)
+			 (slot (eql 'sources)))
+  (setf (slot-value asset 'sources) (preprocess-asset asset)))
 
 ;;  Compile preprocessed assets
 
 (defmethod compile-asset ((asset preprocessed-asset) (output stream))
   (let ((assets (preprocess-asset asset)))
-    (dolist (a assets)
-      (msg "P ~A" (asset-source-path a))
-      (process-asset a output))))
+    (loop for a in assets
+       do (msg "P ~A" (asset-source-path a))
+       do (process-asset a output))))
 
 (defmethod compile-asset ((asset preprocessed-asset) (output pathname))
   (ensure-directories-exist output)