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
;;
;; Assets - Asset pipeline
;;
;; Copyright 2012 Thomas de Grivel <billitch@gmail.com>
;;
;; Permission to use, copy, modify, and distribute this software for any
;; purpose with or without fee is hereby granted, provided that the above
;; copyright notice and this permission notice appear in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
;; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
;; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
;; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
;; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
;; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
;; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(in-package :cl-user)
(defpackage :RoL-files
(:nicknames :L>files :lowh.triangle.files)
(:use :cl :alexandria)
(:import-from :cl-ppcre #:nsubseq)
(:export
#:with-output-to-file/utf-8
#:with-input-from-file/utf-8
#:open-temporary-file
#:with-temporary-file
#:file-exists-p
#:file-more-recent-p
#:directories
#:copy-files
#:link-file
#:unlink-file
#:read-into-string
#:regex-stream-lines
#:regex-lines))
(in-package :RoL-files)
;; Pathnames
(defun pathname-string (pathname)
(format nil "~A" (pathname pathname)))
;; With files
(defmacro with-output-to-file/utf-8 ((stream filespec) &body body)
`(with-open-file (,stream ,filespec
:direction :output
:if-exists :supersede
:if-does-not-exist :create
:element-type 'character
:external-format :utf-8)
,@body))
(defmacro with-input-from-file/utf-8 ((stream filespec) &body body)
`(with-open-file (,stream ,filespec
:direction :input
:if-does-not-exist :error
:element-type 'character
:external-format :utf-8)
,@body))
;; Temporary files
(defun open-temporary-file (&key (element-type 'base-char)
(external-format :default)
(prefix "/tmp/.tmp"))
(let ((template (merge-pathnames
(pathname (format nil "~A.XXXXXXXXXXXX" prefix)))))
(multiple-value-bind (fd path) (sb-posix:mkstemp template)
(let* ((path (pathname path))
(file (truename path))
stream)
(unwind-protect
(setf stream
(sb-sys:make-fd-stream fd
:auto-close t
:input t
:output t
:name (format nil "temporary ~D ~A"
fd file)
:pathname (pathname path)
:file file
:element-type element-type
:external-format external-format))
(unless stream
(sb-posix:close fd)))
stream))))
(defun link-file (oldpath newpath)
#+sbcl(sb-posix:link oldpath newpath))
(defun unlink-file (pathname)
#+sbcl(sb-posix:unlink pathname))
(defmacro with-temporary-file ((stream-var &rest options)
&body body)
(let ((g!stream (gensym "STREAM-")))
`(let ((,g!stream (open-temporary-file ,@options)))
(unwind-protect
(let ((,stream-var ,g!stream))
,@body)
(close ,g!stream)
(unlink-file (pathname ,g!stream))))))
;; Stat
(defun file-exists-p (path)
(when (cl-fad:file-exists-p path)
(enough-namestring path)))
(defun file-more-recent-p (a b)
"Return T if A is more recent than B according to FILE-WRITE-DATE.
Return NIL otherwise."
(or (not (file-exists-p b))
(and (file-exists-p a)
(> (file-write-date a) (file-write-date b)))))
(defun directories (list)
(declare (type list list))
(mapcar (lambda (p)
(enough-namestring p
(truename *default-pathname-defaults*)))
(mapcan #'directory list)))
;; Copying
(defun copy-files (from to &key replace update exclude rename)
(ensure-directories-exist to)
(let (copied updated)
(dolist (src (directory from))
(let* ((src (enough-namestring src))
(name (make-pathname :name (pathname-name src)
:type (pathname-type src))))
(unless (etypecase exclude
(sequence (member name exclude :test #'pathname-match-p))
(function (funcall exclude name))
(null nil))
(let ((dest (if (pathname-name to)
to
(let ((renamed
(etypecase rename
(list (cdr
(assoc name rename
:test #'pathname-match-p)))
(function (funcall rename
(format nil "~A" name)))
(null nil))))
(merge-pathnames (or renamed name) to)))))
(unless (and update (not (file-more-recent-p src dest)))
(cl-fad:copy-file src dest :overwrite (or replace update))
(push dest updated))
(push dest copied)))))
(values (nreverse copied) (nreverse updated))))
;; Regex
(defun regex-do-matches (regex string fn)
(cl-ppcre:do-scans (match-start match-end reg-starts reg-ends regex string)
(let ((match (subseq string match-start match-end))
(regs (loop for i below (length reg-starts)
for start = (aref reg-starts i)
for end = (aref reg-ends i)
collect (subseq string start end))))
(apply fn match regs))))
(defun regex% (regex string replace match-fun output)
(cond
(replace (multiple-value-bind (replaced matched)
(cl-ppcre:regex-replace regex string replace)
(when (and match-fun matched)
(funcall match-fun replaced))
(when output
(write-string replaced output)
(terpri output))
replaced))
(match-fun (regex-do-matches regex string match-fun))
(t (warn "Neither replace or match were given to regex."))))
(defun regex-stream-lines (regex input replace match output)
(do ((line #1=(read-line input nil) #1#))
((null line))
(regex% regex line replace match output)))
(defun regex-lines (regex input &key replace match output)
(labels ((resolve (input match output)
(cond ((typep input 'pathname)
(with-input-from-file/utf-8 (stream input)
(resolve stream match output)))
((typep output 'pathname)
(with-output-to-file/utf-8 (stream output)
(resolve input match stream)))
(t
(regex-stream-lines regex input replace match output)))))
(resolve input match output)))
(defun read-into-string (stream)
(with-output-to-string (string)
(let ((buffer (make-string 1024 :initial-element #\#)))
(loop
:for pos = (read-sequence buffer stream)
:while (< 0 pos)
:do (write-sequence buffer string :end pos)))))