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