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