]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
use reject instead of [ ... not ] filter.
[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 GENERIC: add-using ( object -- )
150
151 M: array add-using [ add-using ] each ;
152
153 M: string add-using drop ;
154
155 M: object add-using ( object -- )
156     vocabulary>> using get [ adjoin ] [ drop ] if* ;
157
158 : 4bl ( -- )
159     "    " write ; inline
160
161 : ($values.) ( array -- )
162     [
163         4bl
164         [ bl ] [
165             "{ " write
166             dup array? [ first ] when
167             dup lookup-type [
168                 [ unparse write bl ]
169                 [ [ pprint ] [ add-using ] bi ] bi*
170             ] [
171                 drop unparse write bl null pprint
172                 null add-using
173             ] if
174             " }" write
175         ] interleave
176     ] unless-empty ;
177
178 : ?print-nl ( seq1 seq2 -- )
179     [ empty? ] either? [ nl ] unless ;
180
181 : $values. ( word -- )
182     "declared-effect" word-prop [
183         [ in>> ] [ out>> ] bi
184         2dup [ empty? ] both? [
185             2drop
186         ] [
187             [ members ] dip over diff
188             "{ $values" print
189             [ drop ($values.) ]
190             [ ?print-nl ]
191             [ nip ($values.) ] 2tri
192             nl "}" print
193         ] if
194     ] when* ;
195
196 : symbol-description. ( word -- )
197     drop
198     "{ $var-description \"\" } ;" print ;
199
200 : $description. ( word -- )
201     drop
202     "{ $description \"\" } ;" print ;
203
204 : docs-body. ( word/symbol -- )
205     dup symbol? [
206         symbol-description.
207     ] [
208         [ $values. ] [ $description. ] bi
209     ] if ;
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     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     {
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 : touch. ( path -- )
361     [ touch-file ]
362     [ "Click to edit: " write <pathname> . ] bi ;
363
364 : scaffold-rc ( path -- )
365     [ home ] dip append-path touch. ;
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 ;