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