]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
tools.scaffold: Make a better scaffold-examples word in hopes it will cause more...
[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 calendar classes
4 combinators combinators.short-circuit fry hashtables interpolate
5 io io.directories io.encodings.utf8 io.files io.pathnames
6 io.streams.string kernel math math.parser namespaces prettyprint
7 quotations sequences sets sorting splitting strings system
8 timers unicode.categories urls vocabs vocabs.loader
9 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     "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
119     H{
120         { "object" object } { "obj" object }
121         { "quot" quotation }
122         { "string" string }
123         { "str" string }
124         { "hash" hashtable }
125         { "hashtable" hashtable }
126         { "?" boolean }
127         { "ch" "a character" }
128         { "word" word }
129         { "array" array }
130         { "timers" timer }
131         { "duration" duration }
132         { "path" "a pathname string" }
133         { "vocab" "a vocabulary specifier" }
134         { "vocab-root" "a vocabulary root string" }
135         { "c-ptr" c-ptr }
136         { "seq" sequence }
137         { "assoc" assoc }
138         { "alist" "an array of key/value pairs" }
139         { "keys" sequence } { "values" sequence }
140         { "class" class } { "tuple" tuple }
141         { "url" url }
142     } at* ;
143
144 : add-using ( object -- )
145     vocabulary>> using get [ adjoin ] [ drop ] if* ;
146
147 : 4bl ( -- )
148     "    " write ; inline
149
150 : ($values.) ( array -- )
151     [
152         4bl
153         [ bl ] [
154             "{ " write
155             dup array? [ first ] when
156             dup lookup-type [
157                 [ unparse write bl ]
158                 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] 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 : symbol-description. ( word -- )
186     drop
187     "{ $var-description \"\" } ;" print ;
188
189 : $description. ( word -- )
190     drop
191     "{ $description \"\" } ;" print ;
192
193 : docs-body. ( word/symbol -- )
194     dup symbol? [
195         symbol-description.
196     ] [
197         [ $values. ] [ $description. ] bi
198     ] if ;
199
200 : docs-header. ( word -- )
201     "HELP: " write name>> print ;
202
203 : (help.) ( word -- )
204     [ docs-header. ] [ docs-body. ] bi ;
205
206 : interesting-words ( vocab -- array )
207     words
208     [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
209     natural-sort ;
210
211 : interesting-words. ( vocab -- )
212     interesting-words [ (help.) nl ] each ;
213
214 : docs-file-string ( vocab -- str2 )
215     [
216         {
217             [ "IN: " write print nl ]
218             [ interesting-words. ]
219             [
220                 [ "ARTICLE: " write unparse dup write bl print ]
221                 [ "{ $vocab-link " write pprint " }" print ] bi
222                 ";" print nl
223             ]
224             [ "ABOUT: " write unparse print ]
225         } cleave
226     ] with-string-writer ;
227
228 : write-using ( vocab -- )
229     "USING:" write
230     using get members
231     { "help.markup" "help.syntax" } append natural-sort remove
232     [ bl write ] each
233     " ;" print ;
234
235 : set-scaffold-docs-file ( vocab path -- )
236     utf8 <file-writer> [
237         scaffold-copyright
238         [ docs-file-string ] [ write-using ] bi
239         write
240     ] with-output-stream ;
241
242 : with-scaffold ( quot -- )
243     [ HS{ } clone using ] dip with-variable ; inline
244
245 : link-vocab ( vocab -- )
246     check-vocab
247     "Edit documentation: " write
248     "-docs.factor" vocab/suffix>path <pathname> . ;
249
250 PRIVATE>
251
252 : help. ( word -- )
253     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
254
255 : scaffold-docs ( vocab -- )
256     ensure-vocab-exists
257     [
258         dup "-docs.factor" vocab/suffix>path scaffolding? [
259             set-scaffold-docs-file
260         ] [
261             2drop
262         ] if
263     ] with-scaffold ;
264
265 : scaffold-undocumented ( string -- )
266     [ interesting-words. ] [ link-vocab ] bi ;
267
268 : scaffold-authors ( vocab -- )
269     "authors.txt" developer-name get scaffold-metadata ;
270
271 : scaffold-tags ( vocab tags -- )
272     [ "tags.txt" ] dip scaffold-metadata ;
273
274 : scaffold-summary ( vocab summary -- )
275     [ "summary.txt" ] dip scaffold-metadata ;
276
277 : scaffold-platforms ( vocab platforms -- )
278     [ "platforms.txt" ] dip scaffold-metadata ;
279
280 : scaffold-vocab ( vocab-root string -- )
281     {
282         [ scaffold-directory ]
283         [ scaffold-main ]
284         [ nip require ]
285         [ nip scaffold-authors ]
286     } 2cleave ;
287
288 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
289
290 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
291
292 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
293
294 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
295
296 <PRIVATE
297
298 : tests-file-string ( vocab -- string )
299     [
300         scaffold-copyright
301         "USING: tools.test " write dup write " ;" print
302         "IN: " write write ".tests" print
303     ] with-string-writer ;
304
305 : set-scaffold-tests-file ( vocab path -- )
306     [ tests-file-string ] dip utf8 set-file-contents ;
307
308 PRIVATE>
309
310 : scaffold-tests ( vocab -- )
311     ensure-vocab-exists
312     dup "-tests.factor" vocab/suffix>path
313     scaffolding? [
314         set-scaffold-tests-file
315     ] [
316         2drop
317     ] if ;
318
319 SYMBOL: nested-examples
320
321 : example-using ( using -- )
322     " " join "example-using" [
323         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
324             """${example-indent}"Example:"
325 ${example-indent}{ $example "USING: ${example-using} ;"
326 ${example-indent}    ""
327 ${example-indent}    ""
328 ${example-indent}}
329 """
330             interpolate
331         ] with-variable
332     ] with-variable ;
333
334 : n-examples-using ( n using -- )
335     '[ _ example-using ] times ;
336
337 : scaffold-n-examples ( n word -- )
338     vocabulary>> "prettyprint" 2array
339     [ t nested-examples ] 2dip
340     '[
341         "{ $examples" print
342         _ _ n-examples-using
343         "}" print
344     ] with-variable ;
345
346 : scaffold-examples ( word -- )
347     2 swap scaffold-n-examples ;
348
349 : touch. ( path -- )
350     [ touch-file ]
351     [ "Click to edit: " write <pathname> . ] bi ;
352
353 : scaffold-rc ( path -- )
354     [ home ] dip append-path touch. ;
355
356 : scaffold-factor-boot-rc ( -- )
357     ".factor-boot-rc" scaffold-rc ;
358
359 : scaffold-factor-rc ( -- )
360     ".factor-rc" scaffold-rc ;
361
362 : scaffold-mason-rc ( -- )
363     ".factor-mason-rc" scaffold-rc ;
364
365 : scaffold-factor-roots ( -- )
366     ".factor-roots" scaffold-rc ;
367
368 HOOK: scaffold-emacs os ( -- )
369
370 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;