]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
sequences: swap stack arguments for start/start*/subseq?.
[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 -- ? ) ".." 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 : class-description. ( word -- )
189     drop
190     "{ $class-description \"\" } ;" print ;
191
192 : symbol-description. ( word -- )
193     drop
194     "{ $var-description \"\" } ;" print ;
195
196 : $description. ( word -- )
197     drop
198     "{ $description \"\" } ;" print ;
199
200 : docs-body. ( word/symbol -- )
201     {
202         { [ dup class? ] [ class-description. ] }
203         { [ dup symbol? ] [ symbol-description. ] }
204         [ [ $values. ] [ $description. ] bi ]
205     } cond ;
206
207 : docs-header. ( word -- )
208     "HELP: " write name>> print ;
209
210 : (help.) ( word -- )
211     [ docs-header. ] [ docs-body. ] bi ;
212
213 : interesting-words ( vocab -- array )
214     vocab-words
215     [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
216     natural-sort ;
217
218 : interesting-words. ( vocab -- )
219     interesting-words [ (help.) nl ] each ;
220
221 : docs-file-string ( vocab -- str2 )
222     [
223         {
224             [ "IN: " write print nl ]
225             [ interesting-words. ]
226             [
227                 [ "ARTICLE: " write unparse dup write bl print ]
228                 [ "{ $vocab-link " write pprint " }" print ] bi
229                 ";" print nl
230             ]
231             [ "ABOUT: " write unparse print ]
232         } cleave
233     ] with-string-writer ;
234
235 : write-using ( vocab -- )
236     "USING:" write
237     using get members
238     { "help.markup" "help.syntax" } append natural-sort remove
239     [ bl write ] each
240     " ;" print ;
241
242 : set-scaffold-docs-file ( vocab path -- )
243     utf8 <file-writer> [
244         scaffold-copyright
245         [ docs-file-string ] [ write-using ] bi
246         write
247     ] with-output-stream ;
248
249 : with-scaffold ( quot -- )
250     [ HS{ } clone using ] dip with-variable ; inline
251
252 : link-vocab ( vocab -- )
253     check-vocab
254     "Edit documentation: " write
255     "-docs.factor" vocab/suffix>path <pathname> . ;
256
257 PRIVATE>
258
259 : help. ( word -- )
260     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
261
262 : scaffold-docs ( vocab -- )
263     ensure-vocab-exists
264     [
265         dup "-docs.factor" vocab/suffix>path scaffolding? [
266             set-scaffold-docs-file
267         ] [
268             2drop
269         ] if
270     ] with-scaffold ;
271
272 : scaffold-undocumented ( string -- )
273     [ interesting-words. ] [ link-vocab ] bi ;
274
275 : scaffold-authors ( vocab -- )
276     "authors.txt" developer-name get scaffold-metadata ;
277
278 : scaffold-tags ( vocab tags -- )
279     [ "tags.txt" ] dip scaffold-metadata ;
280
281 : scaffold-summary ( vocab summary -- )
282     [ "summary.txt" ] dip scaffold-metadata ;
283
284 : scaffold-platforms ( vocab platforms -- )
285     [ "platforms.txt" ] dip scaffold-metadata ;
286
287 : scaffold-vocab ( vocab-root string -- )
288     {
289         [ scaffold-directory ]
290         [ scaffold-main ]
291         [ nip require ]
292         [ nip scaffold-authors ]
293     } 2cleave ;
294
295 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
296
297 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
298
299 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
300
301 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
302
303 <PRIVATE
304
305 : tests-file-string ( vocab -- string )
306     [
307         scaffold-copyright
308         "USING: tools.test " write dup write " ;" print
309         "IN: " write write ".tests" print
310     ] with-string-writer ;
311
312 : set-scaffold-tests-file ( vocab path -- )
313     [ tests-file-string ] dip utf8 set-file-contents ;
314
315 PRIVATE>
316
317 : scaffold-tests ( vocab -- )
318     ensure-vocab-exists
319     dup "-tests.factor" vocab/suffix>path
320     scaffolding? [
321         set-scaffold-tests-file
322     ] [
323         2drop
324     ] if ;
325
326 SYMBOL: nested-examples
327
328 : example-using ( using -- )
329     " " join "example-using" [
330         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
331             "${example-indent}\"Example:\"
332 ${example-indent}{ $example \"USING: ${example-using} ;\"
333 ${example-indent}    \"\"
334 ${example-indent}    \"\"
335 ${example-indent}}
336 "
337             interpolate
338         ] with-variable
339     ] with-variable ;
340
341 : n-examples-using ( n using -- )
342     '[ _ example-using ] times ;
343
344 : scaffold-n-examples ( n word -- )
345     vocabulary>> "prettyprint" 2array
346     [ t nested-examples ] 2dip
347     '[
348         "{ $examples" print
349         _ _ n-examples-using
350         "}" print
351     ] with-variable ;
352
353 : scaffold-examples ( word -- )
354     2 swap scaffold-n-examples ;
355
356 : touch. ( path -- )
357     [ touch-file ]
358     [ "Click to edit: " write <pathname> . ] bi ;
359
360 : scaffold-rc ( path -- )
361     [ home ] dip append-path touch. ;
362
363 : scaffold-factor-boot-rc ( -- )
364     ".factor-boot-rc" scaffold-rc ;
365
366 : scaffold-factor-rc ( -- )
367     ".factor-rc" scaffold-rc ;
368
369 : scaffold-mason-rc ( -- )
370     ".factor-mason-rc" scaffold-rc ;
371
372 : scaffold-factor-roots ( -- )
373     ".factor-roots" scaffold-rc ;
374
375 HOOK: scaffold-emacs os ( -- )
376
377 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;