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