forked from andreer/lispbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asdf-extensions.lisp
262 lines (215 loc) · 9.91 KB
/
asdf-extensions.lisp
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
(defpackage :com.gigamonkeys.asdf-extensions
(:use :cl :asdf)
(:export :register-source-directory))
(in-package :com.gigamonkeys.asdf-extensions)
;;; Method for finding ASD files that doesn't depend on symlinks.
(defvar *top-level-directories* ())
(defun sysdef-crawl-directories (system)
(let ((name (asdf::coerce-name system)))
(block found
(flet ((found-p (file)
(when (and
(equal (pathname-name file) name)
(equal (pathname-type file) "asd"))
(return-from found file))))
(dolist (dir *top-level-directories*)
(walk-directory dir #'found-p))))))
(defun register-source-directory (dir)
(push (pathname-as-directory dir) *top-level-directories*))
(setf *system-definition-search-functions* '(sysdef-crawl-directories))
;;; Build FASLs into a implementation specific directory.
(defparameter *fasl-directory*
(merge-pathnames
(make-pathname
:directory `(:relative ".lispbox" "fasl" ,(swank-loader::unique-dir-name)))
(user-homedir-pathname)))
(defmethod output-files :around ((operation compile-op) (c source-file))
(let ((defaults (merge-pathnames
(make-relative (component-pathname c))
*fasl-directory*)))
(flet ((relocate-fasl (file)
(make-pathname
:type (pathname-type file) :defaults defaults)))
(mapcar #'relocate-fasl (call-next-method)))))
;; Static files
(defmethod output-files :around ((operation compile-op) (c static-file))
(list
(merge-pathnames (make-relative (component-pathname c)) *fasl-directory*)))
(defmethod perform :after ((operation compile-op) (c static-file))
(let ((source-file (component-pathname c))
(output-file (car (output-files operation c))))
(ensure-directories-exist output-file)
(with-open-file (in source-file :element-type '(unsigned-byte 8))
(with-open-file (out output-file :element-type '(unsigned-byte 8) :direction :output :if-exists :supersede)
(loop for octet = (read-byte in nil nil)
while octet do (write-byte octet out))))))
(defun make-relative (pathname)
(make-pathname
:directory (cons :relative (rest (pathname-directory pathname)))
:defaults pathname))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Portable pathnames lib. Unfortunately we can't use ASDF to load it
;;; so we have to inline it here.
(defun list-directory (dirname)
"Return a list of the contents of the directory named by dirname.
Names of subdirectories will be returned in `directory normal
form'. Unlike CL:DIRECTORY, LIST-DIRECTORY does not accept
wildcard pathnames; `dirname' should simply be a pathname that
names a directory. It can be in either file or directory form."
(when (wild-pathname-p dirname)
(error "Can only list concrete directory names."))
(let ((wildcard (directory-wildcard dirname)))
#+(or sbcl cmu lispworks)
;; SBCL, CMUCL, and Lispworks return subdirectories in directory
;; form just the way we want.
(directory wildcard)
#+openmcl
;; OpenMCl by default doesn't return subdirectories at all. But
;; when prodded to do so with the special argument :directories,
;; it returns them in directory form.
(directory wildcard :directories t)
#+allegro
;; Allegro normally return directories in file form but we can
;; change that with the :directories-are-files argument.
(directory wildcard :directories-are-files nil)
#+clisp
;; CLISP has a particularly idiosyncratic view of things. But we
;; can bludgeon even it into doing what we want.
(nconc
;; CLISP won't list files without an extension when :type is
;; wild so we make a special wildcard for it.
(directory wildcard)
;; And CLISP doesn't consider subdirectories to match unless
;; there is a :wild in the directory component.
(directory (clisp-subdirectories-wildcard wildcard)))
#-(or sbcl cmu lispworks openmcl allegro clisp)
(error "list-directory not implemented")))
(defun file-exists-p (pathname)
"Similar to CL:PROBE-FILE except it always returns directory names
in `directory normal form'. Returns truename which will be in
`directory form' if file named is, in fact, a directory."
#+(or sbcl lispworks openmcl)
;; These implementations do "The Right Thing" as far as we are
;; concerned. They return a truename of the file or directory if it
;; exists and the truename of a directory is in directory normal
;; form.
(probe-file pathname)
#+(or allegro cmu)
;; These implementations accept the name of a directory in either
;; form and return the name in the form given. However the name of a
;; file must be given in file form. So we try first with a directory
;; name which will return NIL if either the file doesn't exist at
;; all or exists and is not a directory. Then we try with a file
;; form name.
(or (probe-file (pathname-as-directory pathname))
(probe-file pathname))
#+clisp
;; Once again CLISP takes a particularly unforgiving approach,
;; signalling ERRORs at the slightest provocation.
;; pathname in file form and actually a file -- (probe-file file) ==> truename
;; pathname in file form and doesn't exist -- (probe-file file) ==> NIL
;; pathname in dir form and actually a directory -- (probe-directory file) ==> truename
;; pathname in dir form and doesn't exist -- (probe-directory file) ==> NIL
;; pathname in file form and actually a directory -- (probe-file file) ==> ERROR
;; pathname in dir form and actually a file -- (probe-directory file) ==> ERROR
(or (ignore-errors
;; PROBE-FILE will return the truename if file exists and is a
;; file or NIL if it doesn't exist at all. If it exists but is
;; a directory PROBE-FILE will signal an error which we
;; ignore.
(probe-file (pathname-as-file pathname)))
(ignore-errors
;; PROBE-DIRECTORY returns T if the file exists and is a
;; directory or NIL if it doesn't exist at all. If it exists
;; but is a file, PROBE-DIRECTORY will signal an error.
(let ((directory-form (pathname-as-directory pathname)))
(when (ext:probe-directory directory-form)
directory-form))))
#-(or sbcl cmu lispworks openmcl allegro clisp)
(error "list-directory not implemented"))
(defun directory-wildcard (dirname)
(make-pathname
:name :wild
:type #-clisp :wild #+clisp nil
:defaults (pathname-as-directory dirname)))
#+clisp
(defun clisp-subdirectories-wildcard (wildcard)
(make-pathname
:directory (append (pathname-directory wildcard) (list :wild))
:name nil
:type nil
:defaults wildcard))
(defun directory-pathname-p (p)
"Is the given pathname the name of a directory? This function can
usefully be used to test whether a name returned by LIST-DIRECTORIES
or passed to the function in WALK-DIRECTORY is the name of a directory
in the file system since they always return names in `directory normal
form'."
(flet ((component-present-p (value)
(and value (not (eql value :unspecific)))))
(and
(not (component-present-p (pathname-name p)))
(not (component-present-p (pathname-type p)))
p)))
(defun file-pathname-p (p)
(unless (directory-pathname-p p) p))
(defun pathname-as-directory (name)
"Return a pathname reperesenting the given pathname in
`directory normal form', i.e. with all the name elements in the
directory component and NIL in the name and type components. Can
not be used on wild pathnames because there's not portable way to
convert wildcards in the name and type into a single directory
component. Returns its argument if name and type are both nil or
:unspecific."
(let ((pathname (pathname name)))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames."))
(if (not (directory-pathname-p name))
(make-pathname
:directory (append (or (pathname-directory pathname) (list :relative))
(list (file-namestring pathname)))
:name nil
:type nil
:defaults pathname)
pathname)))
(defun pathname-as-file (name)
"Return a pathname reperesenting the given pathname in `file form',
i.e. with the name elements in the name and type component. Can't
convert wild pathnames because of problems mapping wild directory
component into name and type components. Returns its argument if
it is already in file form."
(let ((pathname (pathname name)))
(when (wild-pathname-p pathname)
(error "Can't reliably convert wild pathnames."))
(if (directory-pathname-p name)
(let* ((directory (pathname-directory pathname))
(name-and-type (pathname (first (last directory)))))
(make-pathname
:directory (butlast directory)
:name (pathname-name name-and-type)
:type (pathname-type name-and-type)
:defaults pathname))
pathname)))
(defun walk-directory (dirname fn &key directories (test (constantly t)))
"Walk a directory invoking `fn' on each pathname found. If `test' is
supplied fn is invoked only on pathnames for which `test' returns
true. If `directories' is t invokes `test' and `fn' on directory
pathnames as well."
(labels
((walk (name)
(cond
((directory-pathname-p name)
(when (and directories (funcall test name))
(funcall fn name))
(dolist (x (list-directory name)) (walk x)))
((funcall test name) (funcall fn name)))))
(walk (pathname-as-directory dirname))))
(defun directory-p (name)
"Is `name' the name of an existing directory."
(let ((truename (file-exists-p name)))
(and truename (directory-pathname-p name))))
(defun file-p (name)
"Is `name' the name of an existing file, i.e. not a directory."
(let ((truename (file-exists-p name)))
(and truename (file-pathname-p name))))
(pushnew :com.gigamonkeys.asdf-extensions *features*)