]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
Merge remote-tracking branch 'origin/html5-force-push'
[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         { "seq" sequence }
131         { "exemplar" object }
132         { "assoc" assoc }
133         { "alist" "an array of key/value pairs" }
134         { "keys" sequence }
135         { "values" sequence }
136         { "class" class }
137         { "tuple" tuple }
138         { "url" url }
139     } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
140
141 GENERIC: add-using ( object -- )
142
143 M: array add-using [ add-using ] each ;
144
145 M: string add-using drop ;
146
147 M: object add-using
148     vocabulary>> using get [ adjoin ] [ drop ] if* ;
149
150 : ($values.) ( array -- )
151     [
152         "    " write
153         [ bl ] [
154             "{ " write
155             dup array? [ first ] when
156             dup lookup-type [
157                 [ unparse write bl ]
158                 [ [ pprint ] [ add-using ] bi ] bi*
159             ] [
160                 drop unparse write bl null pprint
161                 null add-using
162             ] if
163             " }" write
164         ] interleave
165     ] unless-empty ;
166
167 : ?print-nl ( seq1 seq2 -- )
168     [ empty? ] either? [ nl ] unless ;
169
170 : $values. ( word -- )
171     "declared-effect" word-prop [
172         [ in>> ] [ out>> ] bi
173         2dup [ empty? ] both? [
174             2drop
175         ] [
176             [ members ] dip over diff
177             "{ $values" print
178             [ drop ($values.) ]
179             [ ?print-nl ]
180             [ nip ($values.) ] 2tri
181             nl "}" print
182         ] if
183     ] when* ;
184
185 : error-description. ( word -- )
186     [ $values. ] [
187         "{ $description \"Throws " write
188         name>> dup a/an write " \" { $link " write
189         write " } \" error.\" }" print
190     ] bi "{ $error-description \"\" } ;" print ;
191
192 : class-description. ( word -- )
193     drop "{ $class-description \"\" } ;" print ;
194
195 : symbol-description. ( word -- )
196     drop "{ $var-description \"\" } ;" print ;
197
198 : $description. ( word -- )
199     drop "{ $description \"\" } ;" print ;
200
201 : docs-body. ( word/symbol -- )
202     {
203         { [ dup error-class? ] [ error-description. ] }
204         { [ dup class? ] [ class-description. ] }
205         { [ dup symbol? ] [ symbol-description. ] }
206         [ [ $values. ] [ $description. ] bi ]
207     } cond ;
208
209 : docs-header. ( word -- )
210     "HELP: " write name>> print ;
211
212 : (help.) ( word -- )
213     [ docs-header. ] [ docs-body. ] bi ;
214
215 : interesting-words ( vocab -- array )
216     vocab-words
217     [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
218     natural-sort ;
219
220 : interesting-words. ( vocab -- )
221     interesting-words [ (help.) nl ] each ;
222
223 : docs-file-string ( vocab -- str2 )
224     [
225         {
226             [ "IN: " write print nl ]
227             [ interesting-words. ]
228             [
229                 [ "ARTICLE: " write unparse dup write bl print ]
230                 [ "{ $vocab-link " write pprint " }" print ] bi
231                 ";" print nl
232             ]
233             [ "ABOUT: " write unparse print ]
234         } cleave
235     ] with-string-writer ;
236
237 : write-using ( vocab -- )
238     "USING:" write
239     using get members
240     { "help.markup" "help.syntax" } append natural-sort remove
241     [ bl write ] each
242     " ;" print ;
243
244 : set-scaffold-docs-file ( vocab path -- )
245     utf8 <file-writer> [
246         scaffold-copyright
247         [ docs-file-string ] [ write-using ] bi
248         write
249     ] with-output-stream ;
250
251 : with-scaffold ( quot -- )
252     [ HS{ } clone using ] dip with-variable ; inline
253
254 : link-vocab ( vocab -- )
255     check-vocab
256     "Edit documentation: " write
257     "-docs.factor" vocab/suffix>path <pathname> . ;
258
259 PRIVATE>
260
261 : help. ( word -- )
262     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
263
264 : scaffold-docs ( vocab -- )
265     ensure-vocab-exists
266     [
267         dup "-docs.factor" vocab/suffix>path scaffolding? [
268             set-scaffold-docs-file
269         ] [
270             2drop
271         ] if
272     ] with-scaffold ;
273
274 : scaffold-undocumented ( string -- )
275     [ interesting-words. ] [ link-vocab ] bi ;
276
277 : scaffold-authors ( vocab -- )
278     "authors.txt" developer-name get scaffold-metadata ;
279
280 : scaffold-tags ( vocab tags -- )
281     [ "tags.txt" ] dip scaffold-metadata ;
282
283 : scaffold-summary ( vocab summary -- )
284     [ "summary.txt" ] dip scaffold-metadata ;
285
286 : scaffold-platforms ( vocab platforms -- )
287     [ "platforms.txt" ] dip scaffold-metadata ;
288
289 : delete-from-root-cache ( string -- )
290     root-cache get delete-at ;
291
292 : scaffold-vocab-in ( vocab-root string -- )
293     dup delete-from-root-cache
294     {
295         [ scaffold-directory ]
296         [ scaffold-main ]
297         [ nip require ]
298         [ nip scaffold-authors ]
299     } 2cleave ;
300
301 : scaffold-core ( string -- )
302     "resource:core" swap scaffold-vocab-in ;
303
304 : scaffold-basis ( string -- )
305     "resource:basis" swap scaffold-vocab-in ;
306
307 : scaffold-extra ( string -- )
308     "resource:extra" swap scaffold-vocab-in ;
309
310 : scaffold-work ( string -- )
311     "resource:work" swap scaffold-vocab-in  ;
312
313 : scaffold-vocab ( string -- )
314     "Choose a vocabulary root:" vocab-roots get
315     '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
316     swap scaffold-vocab-in ;
317
318 <PRIVATE
319
320 : tests-file-string ( vocab -- string )
321     [
322         scaffold-copyright
323         "USING: tools.test " write dup write " ;" print
324         "IN: " write write ".tests" print
325     ] with-string-writer ;
326
327 : set-scaffold-tests-file ( vocab path -- )
328     [ tests-file-string ] dip utf8 set-file-contents ;
329
330 PRIVATE>
331
332 : scaffold-tests ( vocab -- )
333     ensure-vocab-exists
334     dup "-tests.factor" vocab/suffix>path
335     scaffolding? [
336         set-scaffold-tests-file
337     ] [
338         2drop
339     ] if ;
340
341 SYMBOL: nested-examples
342
343 : example-using ( using -- )
344     join-words "example-using" [
345         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
346             "${example-indent}\"Example:\"
347 ${example-indent}{ $example \"USING: ${example-using} ;\"
348 ${example-indent}    \"\"
349 ${example-indent}    \"\"
350 ${example-indent}}
351 "
352             interpolate
353         ] with-variable
354     ] with-variable ;
355
356 : n-examples-using ( n using -- )
357     '[ _ example-using ] times ;
358
359 : scaffold-n-examples ( n word -- )
360     vocabulary>> "prettyprint" 2array
361     [ t nested-examples ] 2dip
362     '[
363         "{ $examples" print
364         _ _ n-examples-using
365         "}" print
366     ] with-variable ;
367
368 : scaffold-examples ( word -- )
369     2 swap scaffold-n-examples ;
370
371 : scaffold-file ( path -- )
372     [ touch-file ]
373     [ "Click to edit: " write <pathname> . ] bi ;
374
375 : scaffold-rc ( path -- )
376     home prepend-path scaffold-file ;
377
378 : scaffold-factor-boot-rc ( -- )
379     ".factor-boot-rc" scaffold-rc ;
380
381 : scaffold-factor-rc ( -- )
382     ".factor-rc" scaffold-rc ;
383
384 : scaffold-mason-rc ( -- )
385     ".factor-mason-rc" scaffold-rc ;
386
387 : scaffold-factor-roots ( -- )
388     ".factor-roots" scaffold-rc ;
389
390 HOOK: scaffold-emacs os ( -- )
391
392 M: unix scaffold-emacs ".emacs" scaffold-rc ;