refactor assets and update triangle path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
diff --git a/assets.lisp b/assets.lisp
index 140861a..40f8781 100644
--- a/assets.lisp
+++ b/assets.lisp
@@ -45,6 +45,7 @@
(null "")
(symbol (str (string-downcase (symbol-name obj))))
(string obj)
+ (pathname (format nil "~A" obj))
(t (string obj)))))
(t
(apply #'concatenate 'string
@@ -54,14 +55,18 @@
(defvar *debug* nil)
-(defvar *assets-dirs*
+(defvar *default-assets-dirs*
'("lib/*/triangle/assets/*/"
+ "lib/triangle/*/triangle/assets/*/"
"app/assets/*/"
"assets/*/"))
-(defvar *precompiled-assets*
- '("all.css"
- "all.js"
+(defparameter *assets-dirs*
+ *default-assets-dirs*)
+
+(defvar *default-precompiled-assets*
+ '("app.css"
+ "app.js"
"**/*.jpeg"
"**/*.jpg"
"**/*.png"
@@ -70,6 +75,15 @@
"**/*.ttf"
"**/*.woff"))
+(defparameter *precompiled-assets*
+ *default-precompiled-assets*)
+
+(defvar *asset-url-prefix* "/assets/")
+
+(defvar *asset-path-prefix* "public/assets/")
+
+;; Config stanzas
+
(defun assets-dir (pathspec)
(let* ((namestring (enough-namestring pathspec))
(path (if (char= #\/ (last-elt namestring))
@@ -80,6 +94,8 @@
(defun precompiled-asset (asset-name)
(pushnew asset-name *precompiled-assets* :test #'string=))
+;; Read config
+
(defun assets-dirs ()
(cache-1 (eq *assets-dirs*)
(directories (reverse *assets-dirs*))))
@@ -106,19 +122,24 @@
(declare (type asset asset))
(let ((name (asset-name asset))
(ext (asset-ext asset)))
- (str "/assets/" name (when ext ".") ext)))
+ (str *asset-url-prefix* name (when ext ".") ext)))
(defun asset-path (asset)
(declare (type asset asset))
(let ((name (asset-name asset))
(ext (asset-ext asset)))
- (str "public/assets/" name (when ext ".") ext)))
+ (str *asset-path-prefix* name (when ext ".") ext)))
(defun asset-source-path (asset)
(declare (type asset asset))
(with-slots (name source-dir source-ext) asset
(str source-dir name (when source-ext ".") source-ext)))
+(defun asset-debug-path (asset)
+ (declare (type asset asset))
+ (with-slots (name source-dir source-ext) asset
+ (str source-dir name ".debug" (when source-ext ".") source-ext)))
+
(defmethod print-object ((asset asset) stream)
(print-unreadable-object (asset stream :type t)
(ignore-errors (format stream "~S" (asset-path asset)))
diff --git a/find.lisp b/find.lisp
index f39e763..2f4fe2c 100644
--- a/find.lisp
+++ b/find.lisp
@@ -44,12 +44,15 @@
(name string)
(ext symbol)
assets)
- (let ((absolute-dir (merge-pathnames dir))
+ (let ((absolute-dir (truename dir))
(assets assets))
- (dolist (path (directory (str dir name (when ext ".") ext)
- :resolve-symlinks nil))
- (let ((name (enough-namestring (make-pathname :type nil :defaults path)
- absolute-dir)))
+ (dolist (path (directory (str dir name (when ext ".") ext)))
+ (let* ((name.ext (enough-namestring (truename path) absolute-dir))
+ (name (if ext
+ (subseq name.ext 0 (- (length name.ext)
+ (length (string ext))
+ 1))
+ name.ext)))
(unless (find-in-assets type dir name ext assets)
(push (make-instance type
:name name
@@ -115,12 +118,20 @@
,@body)))
(defun find-assets-from-spec (spec &optional class assets)
- (with-asset-spec spec (name ext)
- (find-assets (or class (extension-asset-class ext))
- nil name nil assets)))
+ (let ((new-assets (if class
+ (find-assets class nil spec nil assets)
+ assets)))
+ (if (eq assets new-assets)
+ (with-asset-spec spec (name ext)
+ (find-assets (or class (extension-asset-class ext))
+ nil name nil assets))
+ new-assets)))
(defun find-assets-from-specs (specs &optional class assets)
(reduce (lambda (assets spec)
(find-assets-from-spec spec class assets))
specs
:initial-value assets))
+
+(defun find-asset (spec &optional class)
+ (first (find-assets-from-spec spec class)))
diff --git a/lowh.triangle.assets.precompile.asd b/lowh.triangle.assets.precompile.asd
index 836d2e9..55b06c8 100644
--- a/lowh.triangle.assets.precompile.asd
+++ b/lowh.triangle.assets.precompile.asd
@@ -31,7 +31,8 @@
"cl-uglify-js"
"closer-mop"
"exec-js"
- "lowh.triangle.assets")
+ "lowh.triangle.assets"
+ "lowh.triangle.files")
:components
((:module "precompile"
:components
diff --git a/package.lisp b/package.lisp
index 71e5916..024ce4a 100644
--- a/package.lisp
+++ b/package.lisp
@@ -19,19 +19,27 @@
(in-package :cl-user)
(defpackage :lowh.triangle.assets
- (:nicknames :L.>.assets)
- (:use :cl :alexandria :lowh.triangle.files)
+ (:nicknames :L>assets)
+ (:use :cl :alexandria :L>files)
(:export
;; Config
+ #:*debug*
+ #:*asset-url-prefix*
+ #:*asset-path-prefix*
#:*assets-dirs* #:assets-dir
#:*precompiled-assets* #:precompiled-asset
+ ;; Strings
+ #:str
;; Observers
- #:asset-path
+ #:asset-path #:asset-url
#:assets-dirs
+ #:find-asset #:find-assets-from-spec #:find-assets-from-specs
+ #:locate-precompiled-assets
;; Precompile
#:debug-msg
#:msg
#:with-msg-indent
+ #:compile-asset
#:precompile
#:generator
#:generate))
diff --git a/precompile/generate.lisp b/precompile/generate.lisp
index 9048cee..50291d8 100644
--- a/precompile/generate.lisp
+++ b/precompile/generate.lisp
@@ -40,4 +40,5 @@
(defun generate ()
(msg "Generate")
(with-msg-indent (1)
+ (mapc #'generate/file (directory "lib/triangle/*/triangle/assets.lisp"))
(mapc #'generate/file (directory "lib/*/triangle/assets.lisp"))))
diff --git a/precompile/precompile.lisp b/precompile/precompile.lisp
index 2379297..49c64bc 100644
--- a/precompile/precompile.lisp
+++ b/precompile/precompile.lisp
@@ -25,7 +25,7 @@
(defmethod compile-asset ((asset asset) (output pathname))
(ensure-directories-exist output)
(let ((path (asset-source-path asset)))
- (msg "~A" path)
+ (msg "CP ~A" path)
(copy-files path output :replace t :update t)
nil))
@@ -38,8 +38,18 @@
output))
assets))
(with-output-to-file/utf-8 (out output)
- (dolist (a assets)
- (process-asset a out (not (eq asset a))))))))
+ (cond
+ ((find :assets *debug*)
+ (dolist (a assets)
+ (msg "P ~A" (asset-source-path a))
+ (if (eq asset a)
+ (process-asset a out)
+ (progn (include-asset a out)
+ (copy-files (asset-source-path a)
+ (asset-path a))))))
+ (t (dolist (a assets)
+ (msg "P ~A" (asset-source-path a))
+ (process-asset a out))))))))
;; Precompile
diff --git a/precompile/preprocess.lisp b/precompile/preprocess.lisp
index a0e8e58..31ff785 100644
--- a/precompile/preprocess.lisp
+++ b/precompile/preprocess.lisp
@@ -1,6 +1,5 @@
;;
;; Assets - Asset pipeline
-;; Assets - Asset pipeline
;;
;; Copyright 2012 Thomas de Grivel <billitch@gmail.com>
;;
@@ -28,12 +27,12 @@
:initial-value assets))
(defun preprocess/comment (asset comment assets)
- (debug-msg "Comment ~S" comment)
+ #+nil(debug-msg "Comment ~S" comment)
(or (cl-ppcre:register-groups-bind (command arguments)
("^\\W*=\\s*(\\w+)(\\s+\\S+)*\\s*$" comment)
(let ((arg-list (rest (cl-ppcre:split "\\s+" arguments))))
(cond ((string= "require" command)
- (preprocess/require asset arg-list assets))
+ (setf assets (preprocess/require asset arg-list assets)))
(t
(warn "Unknown preprocessor command : ~A ~S"
command arg-list)
@@ -48,8 +47,7 @@
t))
(defun preprocess/stream (asset stream assets &optional stack)
- (let ((assets assets)
- (line (read-line stream nil))
+ (let ((line (read-line stream nil))
start comment end)
(or (when line
(cl-ppcre:register-groups-bind (s c e)
@@ -74,10 +72,10 @@
(defun preprocess/asset (asset assets)
(let ((path (asset-source-path asset)))
- (msg "~A" path)
+ (msg "PP ~A" path)
(with-msg-indent (1)
(with-input-from-file/utf-8 (input path)
(preprocess/stream asset input assets)))))
-(defun preprocess-asset (asset &optional assets)
- (preprocess/asset asset assets))
+(defun preprocess-asset (asset)
+ (nreverse (preprocess/asset asset nil)))
diff --git a/precompile/process.lisp b/precompile/process.lisp
index 384159d..fe5ad8b 100644
--- a/precompile/process.lisp
+++ b/precompile/process.lisp
@@ -18,7 +18,9 @@
(in-package :lowh.triangle.assets)
-(defgeneric process-asset (asset output included-p))
+(defgeneric process-asset (asset output))
+
+(defgeneric include-asset (asset output))
;; CSS
@@ -70,26 +72,43 @@ try {
(exec-js:from-string js :safely nil :out out)))
(defmethod process-asset ((asset css-asset)
- (output stream)
- included-p)
+ (output stream))
(let ((true-assets-dirs (cache-1 (eq *assets-dirs*)
(mapcar #'truename (assets-dirs))))
(path (asset-source-path asset)))
(less path
(list :paths true-assets-dirs :filename path)
- (list :yuicompress t)
+ (list :yuicompress (not *debug*))
output))
(values))
+(defmethod include-asset ((asset css-asset)
+ (output stream))
+ (format output "@import url('~A');~%" (asset-url asset)))
+
;; JS
(defmethod process-asset ((asset js-asset)
- (output stream)
- included-p)
- (write-string (with-input-from-file/utf-8 (js (asset-source-path asset))
- (cl-uglify-js:ast-gen-code (cl-uglify-js:ast-mangle
- (cl-uglify-js:ast-squeeze
- (parse-js:parse-js js)))
- :beautify nil))
- output)
+ (output stream))
+ (with-input-from-file/utf-8 (js (asset-source-path asset))
+ (if (find :js *debug*)
+ (progn (copy-stream js output)
+ (terpri output))
+ (write-string (cl-uglify-js:ast-gen-code (cl-uglify-js:ast-mangle
+ (cl-uglify-js:ast-squeeze
+ (parse-js:parse-js js)))
+ :beautify nil)
+ output)))
(values))
+
+(defmethod include-asset ((asset js-asset)
+ (output stream))
+ (format output "(function () {
+ var head = document.getElementsByTagName('head')[0];
+ var s = document.createElement('script');
+ s.type = 'text/javascript';
+ s.src = ~S;
+ head.appendChild(s);
+})();
+"
+ (asset-url asset)))