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