]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
tools.scaffold: fix "protocols.foobar" case.
[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 hashtables help.markup interpolate io io.directories
7 io.encodings.utf8 io.files io.pathnames io.streams.string kernel
8 math math.parser math.ranges namespaces prettyprint quotations
9 sequences sets sorting splitting strings system timers unicode
10 urls vocabs vocabs.loader vocabs.loader.private vocabs.metadata
11 words 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 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 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 <PRIVATE
314
315 : find-vocab-root-for  ( string -- vocab-root/f )
316     "." split dup length [1,b) [ head "." join ] with map {
317         [ [ find-vocab-root ] map-find-last drop ]
318         [ [ "." "/" replace find-root-for ] map-find-last drop ]
319     } 1|| ;
320
321 PRIVATE>
322
323 : scaffold-vocab ( string -- )
324     [ find-vocab-root-for ] [ scaffold-vocab-in ] bi ;
325
326 <PRIVATE
327
328 : tests-file-string ( vocab -- string )
329     [
330         scaffold-copyright
331         "USING: tools.test " write dup write " ;" print
332         "IN: " write write ".tests" print
333     ] with-string-writer ;
334
335 : set-scaffold-tests-file ( vocab path -- )
336     [ tests-file-string ] dip utf8 set-file-contents ;
337
338 PRIVATE>
339
340 : scaffold-tests ( vocab -- )
341     ensure-vocab-exists
342     dup "-tests.factor" vocab/suffix>path
343     scaffolding? [
344         set-scaffold-tests-file
345     ] [
346         2drop
347     ] if ;
348
349 SYMBOL: nested-examples
350
351 : example-using ( using -- )
352     " " join "example-using" [
353         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
354             "${example-indent}\"Example:\"
355 ${example-indent}{ $example \"USING: ${example-using} ;\"
356 ${example-indent}    \"\"
357 ${example-indent}    \"\"
358 ${example-indent}}
359 "
360             interpolate
361         ] with-variable
362     ] with-variable ;
363
364 : n-examples-using ( n using -- )
365     '[ _ example-using ] times ;
366
367 : scaffold-n-examples ( n word -- )
368     vocabulary>> "prettyprint" 2array
369     [ t nested-examples ] 2dip
370     '[
371         "{ $examples" print
372         _ _ n-examples-using
373         "}" print
374     ] with-variable ;
375
376 : scaffold-examples ( word -- )
377     2 swap scaffold-n-examples ;
378
379 : scaffold-file ( path -- )
380     [ touch-file ]
381     [ "Click to edit: " write <pathname> . ] bi ;
382
383 : scaffold-rc ( path -- )
384     [ home ] dip append-path scaffold-file ;
385
386 : scaffold-factor-boot-rc ( -- )
387     ".factor-boot-rc" scaffold-rc ;
388
389 : scaffold-factor-rc ( -- )
390     ".factor-rc" scaffold-rc ;
391
392 : scaffold-mason-rc ( -- )
393     ".factor-mason-rc" scaffold-rc ;
394
395 : scaffold-factor-roots ( -- )
396     ".factor-roots" scaffold-rc ;
397
398 HOOK: scaffold-emacs os ( -- )
399
400 M: unix scaffold-emacs ".emacs" scaffold-rc ;