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