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