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