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