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