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 ;
12 SYMBOL: developer-name
15 ERROR: not-a-vocab-root string ;
17 ERROR: vocab-must-not-exist string ;
21 : vocab-root? ( string -- ? )
22 trim-tail-separators vocab-roots get member? ;
24 : ensure-vocab-exists ( string -- string )
25 dup lookup-vocab [ no-vocab ] unless ;
27 : check-root ( string -- string )
28 dup vocab-root? [ not-a-vocab-root ] unless ;
30 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
31 [ check-root ] [ check-vocab-name ] bi* ;
33 : replace-vocab-separators ( vocab -- path )
34 path-separator first CHAR: . associate substitute ;
36 : vocab-root/vocab>path ( vocab-root vocab -- path )
37 check-vocab-root/vocab
38 [ ] [ replace-vocab-separators ] bi* append-path ;
40 : vocab>path ( vocab -- path )
41 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
43 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
44 [ vocab-root/vocab>path ] dip append-path ;
46 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
47 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
49 : vocab/file>path ( vocab file -- path )
50 [ vocab>path ] dip append-path ;
52 : vocab/suffix>path ( vocab suffix -- path )
53 [ vocab>path dup file-name append-path ] dip append ;
55 : directory-exists ( path -- )
56 "Not creating a directory, it already exists: " write print ;
58 : scaffold-directory ( vocab-root vocab -- )
60 dup exists? [ directory-exists ] [ make-directories ] if ;
62 : not-scaffolding ( path -- path )
63 "Not creating scaffolding for " write dup <pathname> . ;
65 : scaffolding ( path -- path )
66 "Creating scaffolding for " write dup <pathname> . ;
68 : scaffolding? ( path -- path ? )
69 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
71 : scaffold-copyright ( -- )
72 "! Copyright (C) " write now year>> number>string write
73 developer-name get [ "Your name" ] unless* bl write "." print
74 "! See http://factorcode.org/license.txt for BSD license." print ;
76 : main-file-string ( vocab -- string )
81 ] with-string-writer ;
83 : set-scaffold-main-file ( vocab path -- )
84 [ main-file-string 1array ] dip utf8 set-file-lines ;
86 : scaffold-main ( vocab-root vocab -- )
87 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
88 set-scaffold-main-file
93 : scaffold-metadata ( vocab file contents -- )
94 [ ensure-vocab-exists ] 2dip
96 [ vocab/file>path ] dip 1array swap scaffolding? [
105 : lookup-type ( string -- object/string ? )
107 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
115 { "hashtable" hashtable }
117 { "ch" "a character" }
120 { "byte-array" byte-array }
122 { "duration" duration }
123 { "path" "a pathname string" }
124 { "vocab" "a vocabulary specifier" }
125 { "vocab-root" "a vocabulary root string" }
127 { "sequence" sequence }
129 { "exemplar" object }
131 { "alist" "an array of key/value pairs" }
133 { "values" sequence }
137 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
139 GENERIC: add-using ( object -- )
141 M: array add-using [ add-using ] each ;
143 M: string add-using drop ;
146 vocabulary>> using get [ adjoin ] [ drop ] if* ;
148 : ($values.) ( array -- )
153 dup array? [ first ] when
156 [ [ pprint ] [ add-using ] bi ] bi*
158 drop unparse write bl null pprint
165 : ?print-nl ( seq1 seq2 -- )
166 [ empty? ] either? [ nl ] unless ;
168 : $values. ( word -- )
169 "declared-effect" word-prop [
170 [ in>> ] [ out>> ] bi
171 2dup [ empty? ] both? [
174 [ members ] dip over diff
178 [ nip ($values.) ] 2tri
183 : error-description. ( word -- )
185 "{ $description \"Throws " write
186 name>> dup a/an write " \" { $link " write
187 write " } \" error.\" }" print
188 ] bi "{ $error-description \"\" } ;" print ;
190 : class-description. ( word -- )
191 drop "{ $class-description \"\" } ;" print ;
193 : symbol-description. ( word -- )
194 drop "{ $var-description \"\" } ;" print ;
196 : $description. ( word -- )
197 drop "{ $description \"\" } ;" print ;
199 : docs-body. ( word/symbol -- )
201 { [ dup error-class? ] [ error-description. ] }
202 { [ dup class? ] [ class-description. ] }
203 { [ dup symbol? ] [ symbol-description. ] }
204 [ [ $values. ] [ $description. ] bi ]
207 : docs-header. ( word -- )
208 "HELP: " write name>> print ;
210 : (help.) ( word -- )
211 [ docs-header. ] [ docs-body. ] bi ;
213 : interesting-words ( vocab -- array )
215 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
218 : interesting-words. ( vocab -- )
219 interesting-words [ (help.) nl ] each ;
221 : docs-file-string ( vocab -- str2 )
224 [ "IN: " write print nl ]
225 [ interesting-words. ]
227 [ "ARTICLE: " write unparse dup write bl print ]
228 [ "{ $vocab-link " write pprint " }" print ] bi
231 [ "ABOUT: " write unparse print ]
233 ] with-string-writer ;
235 : write-using ( vocab -- )
238 { "help.markup" "help.syntax" } append natural-sort remove
242 : set-scaffold-docs-file ( vocab path -- )
245 [ docs-file-string ] [ write-using ] bi
247 ] with-output-stream ;
249 : with-scaffold ( quot -- )
250 [ HS{ } clone using ] dip with-variable ; inline
252 : link-vocab ( vocab -- )
254 "Edit documentation: " write
255 "-docs.factor" vocab/suffix>path <pathname> . ;
260 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
262 : scaffold-docs ( vocab -- )
265 dup "-docs.factor" vocab/suffix>path scaffolding? [
266 set-scaffold-docs-file
272 : scaffold-undocumented ( string -- )
273 [ interesting-words. ] [ link-vocab ] bi ;
275 : scaffold-authors ( vocab -- )
276 "authors.txt" developer-name get scaffold-metadata ;
278 : scaffold-tags ( vocab tags -- )
279 [ "tags.txt" ] dip scaffold-metadata ;
281 : scaffold-summary ( vocab summary -- )
282 [ "summary.txt" ] dip scaffold-metadata ;
284 : scaffold-platforms ( vocab platforms -- )
285 [ "platforms.txt" ] dip scaffold-metadata ;
287 : delete-from-root-cache ( string -- )
288 root-cache get delete-at ;
290 : scaffold-vocab ( vocab-root string -- )
291 dup delete-from-root-cache
293 [ scaffold-directory ]
296 [ nip scaffold-authors ]
299 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
301 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
303 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
305 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
309 : tests-file-string ( vocab -- string )
312 "USING: tools.test " write dup write " ;" print
313 "IN: " write write ".tests" print
314 ] with-string-writer ;
316 : set-scaffold-tests-file ( vocab path -- )
317 [ tests-file-string ] dip utf8 set-file-contents ;
321 : scaffold-tests ( vocab -- )
323 dup "-tests.factor" vocab/suffix>path
325 set-scaffold-tests-file
330 SYMBOL: nested-examples
332 : example-using ( using -- )
333 " " join "example-using" [
334 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
335 "${example-indent}\"Example:\"
336 ${example-indent}{ $example \"USING: ${example-using} ;\"
337 ${example-indent} \"\"
338 ${example-indent} \"\"
345 : n-examples-using ( n using -- )
346 '[ _ example-using ] times ;
348 : scaffold-n-examples ( n word -- )
349 vocabulary>> "prettyprint" 2array
350 [ t nested-examples ] 2dip
357 : scaffold-examples ( word -- )
358 2 swap scaffold-n-examples ;
360 : scaffold-file ( path -- )
362 [ "Click to edit: " write <pathname> . ] bi ;
364 : scaffold-rc ( path -- )
365 [ home ] dip append-path scaffold-file ;
367 : scaffold-factor-boot-rc ( -- )
368 ".factor-boot-rc" scaffold-rc ;
370 : scaffold-factor-rc ( -- )
371 ".factor-rc" scaffold-rc ;
373 : scaffold-mason-rc ( -- )
374 ".factor-mason-rc" scaffold-rc ;
376 : scaffold-factor-roots ( -- )
377 ".factor-roots" scaffold-rc ;
379 HOOK: scaffold-emacs os ( -- )
381 M: unix scaffold-emacs ".emacs" scaffold-rc ;