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