]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
tools.scaffold: Use object instead of null and fix sequences.private
[factor.git] / basis / tools / scaffold / scaffold.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3
4 USING: accessors alien arrays assocs byte-arrays calendar
5 classes classes.error combinators combinators.short-circuit
6 continuations hashtables help.markup interpolate io
7 io.directories io.encodings.utf8 io.files io.pathnames
8 io.streams.string kernel math math.parser namespaces prettyprint
9 quotations sequences sets sorting splitting strings system
10 timers unicode urls vocabs vocabs.loader vocabs.metadata words
11 words.symbol ;
12 IN: tools.scaffold
13
14 SYMBOL: developer-name
15 SYMBOL: using
16
17 ERROR: not-a-vocab-root string ;
18
19 ERROR: vocab-must-not-exist string ;
20
21 <PRIVATE
22
23 : vocab-root? ( string -- ? )
24     trim-tail-separators vocab-roots get member? ;
25
26 : ensure-vocab-exists ( string -- string )
27     dup lookup-vocab [ no-vocab ] unless ;
28
29 : check-vocab-root ( string -- string )
30     dup vocab-root? [ not-a-vocab-root ] unless ;
31
32 : check-vocab-root/name ( vocab-root string -- vocab-root string )
33     [ check-vocab-root ] [ check-vocab-name ] bi* ;
34
35 : replace-vocab-separators ( vocab -- path )
36     path-separator first CHAR: . associate substitute ;
37
38 : vocab-root/vocab>path ( vocab-root vocab -- path )
39     check-vocab-root/name
40     [ ] [ replace-vocab-separators ] bi* append-path ;
41
42 : vocab>path ( vocab -- path )
43     check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
44
45 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
46     [ vocab-root/vocab>path ] dip append-path ;
47
48 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
49     [ vocab-root/vocab>path dup file-name append-path ] dip append ;
50
51 : vocab/file>path ( vocab file -- path )
52     [ vocab>path ] dip append-path ;
53
54 : vocab/suffix>path ( vocab suffix -- path )
55     [ vocab>path dup file-name append-path ] dip append ;
56
57 : directory-exists ( path -- )
58     "Not creating a directory, it already exists: " write print ;
59
60 : scaffold-directory ( vocab-root vocab -- )
61     vocab-root/vocab>path
62     dup file-exists? [ directory-exists ] [ make-directories ] if ;
63
64 : not-scaffolding ( path -- path )
65     "Not creating scaffolding for " write dup <pathname> . ;
66
67 : scaffolding ( path -- path )
68     "Creating scaffolding for " write dup <pathname> . ;
69
70 : scaffolding? ( path -- path ? )
71     dup file-exists? [ not-scaffolding f ] [ scaffolding t ] if ;
72
73 : scaffold-copyright ( -- )
74     "! Copyright (C) " write now year>> number>string write
75     developer-name get [ "Your name" ] unless* bl write "." print
76     "! See http://factorcode.org/license.txt for BSD license." print ;
77
78 : main-file-string ( vocab -- string )
79     [
80         scaffold-copyright
81         "USING: ;" print
82         "IN: " write print
83     ] with-string-writer ;
84
85 : set-scaffold-main-file ( vocab path -- )
86     [ main-file-string 1array ] dip utf8 set-file-lines ;
87
88 : scaffold-main ( vocab-root vocab -- )
89     [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
90         set-scaffold-main-file
91     ] [
92         2drop
93     ] if ;
94
95 : scaffold-metadata ( vocab file contents -- )
96     [ ensure-vocab-exists ] 2dip
97     [
98         [ vocab/file>path ] dip 1array swap scaffolding? [
99             utf8 set-file-lines
100         ] [
101             2drop
102         ] if
103     ] [
104         2drop
105     ] if* ;
106
107 : lookup-type ( string -- object/string ? )
108     "/f" ?tail swap
109     "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
110     H{
111         { "object" object }
112         { "obj" object }
113         { "quot" quotation }
114         { "string" string }
115         { "str" string }
116         { "hash" hashtable }
117         { "hashtable" hashtable }
118         { "?" boolean }
119         { "ch" "a character" }
120         { "word" word }
121         { "array" array }
122         { "byte-array" byte-array }
123         { "timer" timer }
124         { "duration" duration }
125         { "path" "a pathname string" }
126         { "vocab" "a vocabulary specifier" }
127         { "vocab-root" "a vocabulary root string" }
128         { "c-ptr" c-ptr }
129         { "sequence" sequence }
130         { "slice" slice }
131         { "from" integer }
132         { "to" integer }
133         { "i" integer }
134         { "n" integer }
135         { "seq" sequence }
136         { "exemplar" object }
137         { "assoc" assoc }
138         { "alist" "an array of key/value pairs" }
139         { "keys" sequence }
140         { "values" sequence }
141         { "class" class }
142         { "tuple" tuple }
143         { "url" url }
144     } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
145
146 GENERIC: add-using ( object -- )
147
148 M: array add-using [ add-using ] each ;
149
150 M: string add-using drop ;
151
152 M: object add-using
153     vocabulary>> using get [ adjoin ] [ drop ] if* ;
154
155 : ($values.) ( array -- )
156     [
157         "    " write
158         [ bl ] [
159             "{ " write
160             dup array? [ first ] when
161             dup lookup-type [
162                 [ unparse write bl ]
163                 [ [ pprint ] [ add-using ] bi ] bi*
164             ] [
165                 drop unparse write bl object pprint
166                 object add-using
167             ] if
168             " }" write
169         ] interleave
170     ] unless-empty ;
171
172 : ?print-nl ( seq1 seq2 -- )
173     [ empty? ] either? [ nl ] unless ;
174
175 : $values. ( word -- )
176     "declared-effect" word-prop [
177         [ in>> ] [ out>> ] bi
178         2dup [ empty? ] both? [
179             2drop
180         ] [
181             [ members ] dip over diff
182             "{ $values" print
183             [ drop ($values.) ]
184             [ ?print-nl ]
185             [ nip ($values.) ] 2tri
186             nl "}" print
187         ] if
188     ] when* ;
189
190 : error-description. ( word -- )
191     [ $values. ] [
192         "{ $description \"Throws " write
193         name>> dup a/an write " \" { $link " write
194         write " } \" error.\" }" print
195     ] bi "{ $error-description \"\" } ;" print ;
196
197 : class-description. ( word -- )
198     drop "{ $class-description \"\" } ;" print ;
199
200 : symbol-description. ( word -- )
201     drop "{ $var-description \"\" } ;" print ;
202
203 : $description. ( word -- )
204     drop "{ $description \"\" } ;" print ;
205
206 : docs-body. ( word/symbol -- )
207     {
208         { [ dup error-class? ] [ error-description. ] }
209         { [ dup class? ] [ class-description. ] }
210         { [ dup symbol? ] [ symbol-description. ] }
211         [ [ $values. ] [ $description. ] bi ]
212     } cond ;
213
214 : docs-header. ( word -- )
215     "HELP: " write name>> print ;
216
217 : (help.) ( word -- )
218     [ docs-header. ] [ docs-body. ] bi ;
219
220 : interesting-words ( vocab -- array )
221     vocab-words
222     [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
223     natural-sort ;
224
225 : interesting-words. ( vocab -- )
226     interesting-words [ (help.) nl ] each ;
227
228 : docs-file-string ( vocab -- str2 )
229     [
230         {
231             [ "IN: " write print nl ]
232             [ interesting-words. ]
233             [
234                 [ "ARTICLE: " write unparse dup write bl print ]
235                 [ "{ $vocab-link " write pprint " }" print ] bi
236                 ";" print nl
237             ]
238             [ "ABOUT: " write unparse print ]
239         } cleave
240     ] with-string-writer ;
241
242 : write-using ( vocab -- )
243     "USING:" write
244     using get members
245     { "help.markup" "help.syntax" } append natural-sort remove
246     [ bl write ] each
247     " ;" print ;
248
249 : set-scaffold-docs-file ( vocab path -- )
250     utf8 <file-writer> [
251         scaffold-copyright
252         [ docs-file-string ] [ write-using ] bi
253         write
254     ] with-output-stream ;
255
256 : with-scaffold ( quot -- )
257     [ HS{ } clone using ] dip with-variable ; inline
258
259 : link-vocab ( vocab -- )
260     ".private" ?tail drop
261     check-vocab
262     "Edit documentation: " write
263     "-docs.factor" vocab/suffix>path <pathname> . ;
264
265 PRIVATE>
266
267 : help. ( word -- )
268     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
269
270 : scaffold-docs ( vocab -- )
271     ensure-vocab-exists
272     [
273         dup "-docs.factor" vocab/suffix>path scaffolding? [
274             set-scaffold-docs-file
275         ] [
276             2drop
277         ] if
278     ] with-scaffold ;
279
280 : scaffold-undocumented ( string -- )
281     [ interesting-words. ] [ link-vocab ] bi ;
282
283 : scaffold-authors ( vocab -- )
284     "authors.txt" developer-name get scaffold-metadata ;
285
286 : scaffold-tags ( vocab tags -- )
287     [ "tags.txt" ] dip scaffold-metadata ;
288
289 : scaffold-summary ( vocab summary -- )
290     [ "summary.txt" ] dip scaffold-metadata ;
291
292 : scaffold-platforms ( vocab platforms -- )
293     [ "platforms.txt" ] dip scaffold-metadata ;
294
295 : delete-from-root-cache ( string -- )
296     root-cache get delete-at ;
297
298 : scaffold-vocab-in ( vocab-root string -- )
299     dup delete-from-root-cache
300     {
301         [ scaffold-directory ]
302         [ scaffold-main ]
303         [ nip require ]
304         [ nip scaffold-authors ]
305     } 2cleave ;
306
307 : scaffold-core ( string -- )
308     "resource:core" swap scaffold-vocab-in ;
309
310 : scaffold-basis ( string -- )
311     "resource:basis" swap scaffold-vocab-in ;
312
313 : scaffold-extra ( string -- )
314     "resource:extra" swap scaffold-vocab-in ;
315
316 : scaffold-work ( string -- )
317     "resource:work" swap scaffold-vocab-in  ;
318
319 : scaffold-vocab ( string -- )
320     "Choose a vocabulary root:" vocab-roots get
321     '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
322     swap scaffold-vocab-in ;
323
324 <PRIVATE
325
326 : tests-file-string ( vocab -- string )
327     [
328         scaffold-copyright
329         "USING: tools.test " write dup write " ;" print
330         "IN: " write write ".tests" print
331     ] with-string-writer ;
332
333 : set-scaffold-tests-file ( vocab path -- )
334     [ tests-file-string ] dip utf8 set-file-contents ;
335
336 PRIVATE>
337
338 : scaffold-tests ( vocab -- )
339     ensure-vocab-exists
340     dup "-tests.factor" vocab/suffix>path
341     scaffolding? [
342         set-scaffold-tests-file
343     ] [
344         2drop
345     ] if ;
346
347 SYMBOL: nested-examples
348
349 : example-using ( using -- )
350     join-words "example-using" [
351         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
352             "${example-indent}\"Example:\"
353 ${example-indent}{ $example \"USING: ${example-using} ;\"
354 ${example-indent}    \"\"
355 ${example-indent}    \"\"
356 ${example-indent}}
357 "
358             interpolate
359         ] with-variable
360     ] with-variable ;
361
362 : n-examples-using ( n using -- )
363     '[ _ example-using ] times ;
364
365 : scaffold-n-examples ( n word -- )
366     vocabulary>> "prettyprint" 2array
367     [ t nested-examples ] 2dip
368     '[
369         "{ $examples" print
370         _ _ n-examples-using
371         "}" print
372     ] with-variable ;
373
374 : scaffold-examples ( word -- )
375     2 swap scaffold-n-examples ;
376
377 : scaffold-file ( path -- )
378     [ touch-file ]
379     [ "Click to edit: " write <pathname> . ] bi ;
380
381 : scaffold-rc ( path -- )
382     home prepend-path scaffold-file ;
383
384 : scaffold-factor-boot-rc ( -- )
385     ".factor-boot-rc" scaffold-rc ;
386
387 : scaffold-factor-rc ( -- )
388     ".factor-rc" scaffold-rc ;
389
390 : scaffold-mason-rc ( -- )
391     ".factor-mason-rc" scaffold-rc ;
392
393 : scaffold-factor-roots ( -- )
394     ".factor-roots" scaffold-rc ;
395
396 HOOK: scaffold-emacs os ( -- )
397
398 M: unix scaffold-emacs ".emacs" scaffold-rc ;