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