]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
tools.scaffold: quick fix for scaffold-vocab bug.
[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 : check-vocab-exists ( string -- string )
34     dup vocab-exists? [ vocab-must-not-exist ] when
35     dup root-cache get delete-at ;
36
37 : replace-vocab-separators ( vocab -- path )
38     path-separator first CHAR: . associate substitute ;
39
40 : vocab-root/vocab>path ( vocab-root vocab -- path )
41     check-vocab-root/vocab
42     [ ] [ replace-vocab-separators ] bi* append-path ;
43
44 : vocab>path ( vocab -- path )
45     check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
46
47 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
48     [ vocab-root/vocab>path ] dip append-path ;
49
50 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
51     [ vocab-root/vocab>path dup file-name append-path ] dip append ;
52
53 : vocab/file>path ( vocab file -- path )
54     [ vocab>path ] dip append-path ;
55
56 : vocab/suffix>path ( vocab suffix -- path )
57     [ vocab>path dup file-name append-path ] dip append ;
58
59 : directory-exists ( path -- )
60     "Not creating a directory, it already exists: " write print ;
61
62 : scaffold-directory ( vocab-root vocab -- )
63     vocab-root/vocab>path
64     dup exists? [ directory-exists ] [ make-directories ] if ;
65
66 : not-scaffolding ( path -- path )
67     "Not creating scaffolding for " write dup <pathname> . ;
68
69 : scaffolding ( path -- path )
70     "Creating scaffolding for " write dup <pathname> . ;
71
72 : scaffolding? ( path -- path ? )
73     dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
74
75 : scaffold-copyright ( -- )
76     "! Copyright (C) " write now year>> number>string write
77     developer-name get [ "Your name" ] unless* bl write "." print
78     "! See http://factorcode.org/license.txt for BSD license." print ;
79
80 : main-file-string ( vocab -- string )
81     [
82         scaffold-copyright
83         "USING: ;" print
84         "IN: " write print
85     ] with-string-writer ;
86
87 : set-scaffold-main-file ( vocab path -- )
88     [ main-file-string 1array ] dip utf8 set-file-lines ;
89
90 : scaffold-main ( vocab-root vocab -- )
91     [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
92         set-scaffold-main-file
93     ] [
94         2drop
95     ] if ;
96
97 : scaffold-metadata ( vocab file contents -- )
98     [ ensure-vocab-exists ] 2dip
99     [
100         [ vocab/file>path ] dip 1array swap scaffolding? [
101             utf8 set-file-lines
102         ] [
103             2drop
104         ] if
105     ] [
106         2drop
107     ] if* ;
108
109 : lookup-type ( string -- object/string ? )
110     "/f" ?tail swap
111     "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
112     H{
113         { "object" object }
114         { "obj" object }
115         { "quot" quotation }
116         { "string" string }
117         { "str" string }
118         { "hash" hashtable }
119         { "hashtable" hashtable }
120         { "?" boolean }
121         { "ch" "a character" }
122         { "word" word }
123         { "array" array }
124         { "byte-array" byte-array }
125         { "timer" timer }
126         { "duration" duration }
127         { "path" "a pathname string" }
128         { "vocab" "a vocabulary specifier" }
129         { "vocab-root" "a vocabulary root string" }
130         { "c-ptr" c-ptr }
131         { "sequence" sequence }
132         { "seq" sequence }
133         { "exemplar" object }
134         { "assoc" assoc }
135         { "alist" "an array of key/value pairs" }
136         { "keys" sequence }
137         { "values" sequence }
138         { "class" class }
139         { "tuple" tuple }
140         { "url" url }
141     } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
142
143 GENERIC: add-using ( object -- )
144
145 M: array add-using [ add-using ] each ;
146
147 M: string add-using drop ;
148
149 M: object add-using ( object -- )
150     vocabulary>> using get [ adjoin ] [ drop ] if* ;
151
152 : ($values.) ( array -- )
153     [
154         "    " write
155         [ bl ] [
156             "{ " write
157             dup array? [ first ] when
158             dup lookup-type [
159                 [ unparse write bl ]
160                 [ [ pprint ] [ add-using ] bi ] bi*
161             ] [
162                 drop unparse write bl null pprint
163                 null add-using
164             ] if
165             " }" write
166         ] interleave
167     ] unless-empty ;
168
169 : ?print-nl ( seq1 seq2 -- )
170     [ empty? ] either? [ nl ] unless ;
171
172 : $values. ( word -- )
173     "declared-effect" word-prop [
174         [ in>> ] [ out>> ] bi
175         2dup [ empty? ] both? [
176             2drop
177         ] [
178             [ members ] dip over diff
179             "{ $values" print
180             [ drop ($values.) ]
181             [ ?print-nl ]
182             [ nip ($values.) ] 2tri
183             nl "}" print
184         ] if
185     ] when* ;
186
187 : error-description. ( word -- )
188     [ $values. ] [
189         "{ $description \"Throws " write
190         name>> dup a/an write " \" { $link " write
191         write " } \" error.\" }" print
192     ] bi "{ $error-description \"\" } ;" print ;
193
194 : class-description. ( word -- )
195     drop "{ $class-description \"\" } ;" print ;
196
197 : symbol-description. ( word -- )
198     drop "{ $var-description \"\" } ;" print ;
199
200 : $description. ( word -- )
201     drop "{ $description \"\" } ;" print ;
202
203 : docs-body. ( word/symbol -- )
204     {
205         { [ dup error-class? ] [ error-description. ] }
206         { [ dup class? ] [ class-description. ] }
207         { [ dup symbol? ] [ symbol-description. ] }
208         [ [ $values. ] [ $description. ] bi ]
209     } cond ;
210
211 : docs-header. ( word -- )
212     "HELP: " write name>> print ;
213
214 : (help.) ( word -- )
215     [ docs-header. ] [ docs-body. ] bi ;
216
217 : interesting-words ( vocab -- array )
218     vocab-words
219     [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
220     natural-sort ;
221
222 : interesting-words. ( vocab -- )
223     interesting-words [ (help.) nl ] each ;
224
225 : docs-file-string ( vocab -- str2 )
226     [
227         {
228             [ "IN: " write print nl ]
229             [ interesting-words. ]
230             [
231                 [ "ARTICLE: " write unparse dup write bl print ]
232                 [ "{ $vocab-link " write pprint " }" print ] bi
233                 ";" print nl
234             ]
235             [ "ABOUT: " write unparse print ]
236         } cleave
237     ] with-string-writer ;
238
239 : write-using ( vocab -- )
240     "USING:" write
241     using get members
242     { "help.markup" "help.syntax" } append natural-sort remove
243     [ bl write ] each
244     " ;" print ;
245
246 : set-scaffold-docs-file ( vocab path -- )
247     utf8 <file-writer> [
248         scaffold-copyright
249         [ docs-file-string ] [ write-using ] bi
250         write
251     ] with-output-stream ;
252
253 : with-scaffold ( quot -- )
254     [ HS{ } clone using ] dip with-variable ; inline
255
256 : link-vocab ( vocab -- )
257     check-vocab
258     "Edit documentation: " write
259     "-docs.factor" vocab/suffix>path <pathname> . ;
260
261 PRIVATE>
262
263 : help. ( word -- )
264     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
265
266 : scaffold-docs ( vocab -- )
267     ensure-vocab-exists
268     [
269         dup "-docs.factor" vocab/suffix>path scaffolding? [
270             set-scaffold-docs-file
271         ] [
272             2drop
273         ] if
274     ] with-scaffold ;
275
276 : scaffold-undocumented ( string -- )
277     [ interesting-words. ] [ link-vocab ] bi ;
278
279 : scaffold-authors ( vocab -- )
280     "authors.txt" developer-name get scaffold-metadata ;
281
282 : scaffold-tags ( vocab tags -- )
283     [ "tags.txt" ] dip scaffold-metadata ;
284
285 : scaffold-summary ( vocab summary -- )
286     [ "summary.txt" ] dip scaffold-metadata ;
287
288 : scaffold-platforms ( vocab platforms -- )
289     [ "platforms.txt" ] dip scaffold-metadata ;
290
291 : scaffold-vocab ( vocab-root string -- )
292     check-vocab-exists {
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 ;