]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
Merge branch 'master' of git://factorcode.org/git/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 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/suffix>path ( vocab suffix -- path )
67     [ vocab>path dup file-name append-path ] dip append ;
68
69 : directory-exists ( path -- )
70     "Not creating a directory, it already exists: " write print ;
71
72 : scaffold-directory ( vocab-root vocab -- )
73     vocab-root/vocab>path
74     dup exists? [ directory-exists ] [ make-directories ] if ;
75
76 : not-scaffolding ( path -- path )
77     "Not creating scaffolding for " write dup <pathname> . ;
78
79 : scaffolding ( path -- path )
80     "Creating scaffolding for " write dup <pathname> . ;
81
82 : scaffolding? ( path -- path ? )
83     dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
84
85 : scaffold-copyright ( -- )
86     "! Copyright (C) " write now year>> number>string write
87     developer-name get [ "Your name" ] unless* bl write "." print
88     "! See http://factorcode.org/license.txt for BSD license." print ;
89
90 : main-file-string ( vocab -- string )
91     [
92         scaffold-copyright
93         "USING: ;" print
94         "IN: " write print
95     ] with-string-writer ;
96
97 : set-scaffold-main-file ( vocab path -- )
98     [ main-file-string ] dip utf8 set-file-contents ;
99
100 : scaffold-main ( vocab-root vocab -- )
101     [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
102         set-scaffold-main-file
103     ] [
104         2drop
105     ] if ;
106
107 : scaffold-authors ( vocab-root vocab -- )
108     developer-name get [
109         "authors.txt" vocab-root/vocab/file>path scaffolding? [
110             developer-name get swap utf8 set-file-contents
111         ] [
112             drop
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-vocab ( vocab-root string -- )
262     {
263         [ scaffold-directory ]
264         [ scaffold-main ]
265         [ scaffold-authors ]
266         [ nip require ]
267     } 2cleave ;
268
269 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
270
271 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
272
273 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
274
275 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
276
277 <PRIVATE
278
279 : tests-file-string ( vocab -- string )
280     [
281         scaffold-copyright
282         "USING: tools.test " write dup write " ;" print
283         "IN: " write write ".tests" print
284     ] with-string-writer ;
285
286 : set-scaffold-tests-file ( vocab path -- )
287     [ tests-file-string ] dip utf8 set-file-contents ;
288
289 PRIVATE>
290
291 : scaffold-tests ( vocab -- )
292     ensure-vocab-exists
293     dup "-tests.factor" vocab/suffix>path
294     scaffolding? [
295         set-scaffold-tests-file
296     ] [
297         2drop
298     ] if ;
299
300 SYMBOL: examples-flag
301
302 : example ( -- )
303     {
304         "{ $example \"\" \"USING: prettyprint ;\""
305         "           \"\""
306         "           \"\""
307         "}"
308     } [ examples-flag get [ 4bl ] when print ] each ;
309
310 : examples ( n -- )
311     t \ examples-flag [
312         "{ $examples " print
313         [ example ] times
314         "}" print
315     ] with-variable ;
316
317 : touch. ( path -- )
318     [ touch-file ]
319     [ "Click to edit: " write <pathname> . ] bi ;
320
321 : scaffold-rc ( path -- )
322     [ home ] dip append-path touch. ;
323
324 : scaffold-factor-boot-rc ( -- )
325     os windows? "factor-boot-rc" ".factor-boot-rc" ? scaffold-rc ;
326
327 : scaffold-factor-rc ( -- )
328     os windows? "factor-rc" ".factor-rc" ? scaffold-rc ;
329
330
331 HOOK: scaffold-emacs os ( -- )
332
333 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;