]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/scaffold/scaffold.factor
tools.scaffold: make sure vocab does not exist.
[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 } { "values" sequence }
136         { "class" class } { "tuple" tuple }
137         { "url" url }
138     } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
139
140 GENERIC: add-using ( object -- )
141
142 M: array add-using [ add-using ] each ;
143
144 M: string add-using drop ;
145
146 M: object add-using ( object -- )
147     vocabulary>> using get [ adjoin ] [ drop ] if* ;
148
149 : ($values.) ( array -- )
150     [
151         "    " write
152         [ bl ] [
153             "{ " write
154             dup array? [ first ] when
155             dup lookup-type [
156                 [ unparse write bl ]
157                 [ [ pprint ] [ add-using ] bi ] bi*
158             ] [
159                 drop unparse write bl null pprint
160                 null add-using
161             ] if
162             " }" write
163         ] interleave
164     ] unless-empty ;
165
166 : ?print-nl ( seq1 seq2 -- )
167     [ empty? ] either? [ nl ] unless ;
168
169 : $values. ( word -- )
170     "declared-effect" word-prop [
171         [ in>> ] [ out>> ] bi
172         2dup [ empty? ] both? [
173             2drop
174         ] [
175             [ members ] dip over diff
176             "{ $values" print
177             [ drop ($values.) ]
178             [ ?print-nl ]
179             [ nip ($values.) ] 2tri
180             nl "}" print
181         ] if
182     ] when* ;
183
184 : class-description. ( word -- )
185     drop "{ $class-description \"\" } ;" print ;
186
187 : symbol-description. ( word -- )
188     drop "{ $var-description \"\" } ;" print ;
189
190 : $description. ( word -- )
191     drop "{ $description \"\" } ;" print ;
192
193 : docs-body. ( word/symbol -- )
194     {
195         { [ dup class? ] [ class-description. ] }
196         { [ dup symbol? ] [ symbol-description. ] }
197         [ [ $values. ] [ $description. ] bi ]
198     } cond ;
199
200 : docs-header. ( word -- )
201     "HELP: " write name>> print ;
202
203 : (help.) ( word -- )
204     [ docs-header. ] [ docs-body. ] bi ;
205
206 : interesting-words ( vocab -- array )
207     vocab-words
208     [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
209     natural-sort ;
210
211 : interesting-words. ( vocab -- )
212     interesting-words [ (help.) nl ] each ;
213
214 : docs-file-string ( vocab -- str2 )
215     [
216         {
217             [ "IN: " write print nl ]
218             [ interesting-words. ]
219             [
220                 [ "ARTICLE: " write unparse dup write bl print ]
221                 [ "{ $vocab-link " write pprint " }" print ] bi
222                 ";" print nl
223             ]
224             [ "ABOUT: " write unparse print ]
225         } cleave
226     ] with-string-writer ;
227
228 : write-using ( vocab -- )
229     "USING:" write
230     using get members
231     { "help.markup" "help.syntax" } append natural-sort remove
232     [ bl write ] each
233     " ;" print ;
234
235 : set-scaffold-docs-file ( vocab path -- )
236     utf8 <file-writer> [
237         scaffold-copyright
238         [ docs-file-string ] [ write-using ] bi
239         write
240     ] with-output-stream ;
241
242 : with-scaffold ( quot -- )
243     [ HS{ } clone using ] dip with-variable ; inline
244
245 : link-vocab ( vocab -- )
246     check-vocab
247     "Edit documentation: " write
248     "-docs.factor" vocab/suffix>path <pathname> . ;
249
250 PRIVATE>
251
252 : help. ( word -- )
253     [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
254
255 : scaffold-docs ( vocab -- )
256     ensure-vocab-exists
257     [
258         dup "-docs.factor" vocab/suffix>path scaffolding? [
259             set-scaffold-docs-file
260         ] [
261             2drop
262         ] if
263     ] with-scaffold ;
264
265 : scaffold-undocumented ( string -- )
266     [ interesting-words. ] [ link-vocab ] bi ;
267
268 : scaffold-authors ( vocab -- )
269     "authors.txt" developer-name get scaffold-metadata ;
270
271 : scaffold-tags ( vocab tags -- )
272     [ "tags.txt" ] dip scaffold-metadata ;
273
274 : scaffold-summary ( vocab summary -- )
275     [ "summary.txt" ] dip scaffold-metadata ;
276
277 : scaffold-platforms ( vocab platforms -- )
278     [ "platforms.txt" ] dip scaffold-metadata ;
279
280 : scaffold-vocab ( vocab-root string -- )
281     check-vocab-exists {
282         [ scaffold-directory ]
283         [ scaffold-main ]
284         [ nip require ]
285         [ nip scaffold-authors ]
286     } 2cleave ;
287
288 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
289
290 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
291
292 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
293
294 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
295
296 <PRIVATE
297
298 : tests-file-string ( vocab -- string )
299     [
300         scaffold-copyright
301         "USING: tools.test " write dup write " ;" print
302         "IN: " write write ".tests" print
303     ] with-string-writer ;
304
305 : set-scaffold-tests-file ( vocab path -- )
306     [ tests-file-string ] dip utf8 set-file-contents ;
307
308 PRIVATE>
309
310 : scaffold-tests ( vocab -- )
311     ensure-vocab-exists
312     dup "-tests.factor" vocab/suffix>path
313     scaffolding? [
314         set-scaffold-tests-file
315     ] [
316         2drop
317     ] if ;
318
319 SYMBOL: nested-examples
320
321 : example-using ( using -- )
322     " " join "example-using" [
323         nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
324             "${example-indent}\"Example:\"
325 ${example-indent}{ $example \"USING: ${example-using} ;\"
326 ${example-indent}    \"\"
327 ${example-indent}    \"\"
328 ${example-indent}}
329 "
330             interpolate
331         ] with-variable
332     ] with-variable ;
333
334 : n-examples-using ( n using -- )
335     '[ _ example-using ] times ;
336
337 : scaffold-n-examples ( n word -- )
338     vocabulary>> "prettyprint" 2array
339     [ t nested-examples ] 2dip
340     '[
341         "{ $examples" print
342         _ _ n-examples-using
343         "}" print
344     ] with-variable ;
345
346 : scaffold-examples ( word -- )
347     2 swap scaffold-n-examples ;
348
349 : touch. ( path -- )
350     [ touch-file ]
351     [ "Click to edit: " write <pathname> . ] bi ;
352
353 : scaffold-rc ( path -- )
354     [ home ] dip append-path touch. ;
355
356 : scaffold-factor-boot-rc ( -- )
357     ".factor-boot-rc" scaffold-rc ;
358
359 : scaffold-factor-rc ( -- )
360     ".factor-rc" scaffold-rc ;
361
362 : scaffold-mason-rc ( -- )
363     ".factor-mason-rc" scaffold-rc ;
364
365 : scaffold-factor-roots ( -- )
366     ".factor-roots" scaffold-rc ;
367
368 HOOK: scaffold-emacs os ( -- )
369
370 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;