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