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 : check-vocab-exists ( string -- string )
34 dup vocab-exists? [ vocab-must-not-exist ] when
35 dup root-cache get delete-at ;
37 : replace-vocab-separators ( vocab -- path )
38 path-separator first CHAR: . associate substitute ;
40 : vocab-root/vocab>path ( vocab-root vocab -- path )
41 check-vocab-root/vocab
42 [ ] [ replace-vocab-separators ] bi* append-path ;
44 : vocab>path ( vocab -- path )
45 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
47 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
48 [ vocab-root/vocab>path ] dip append-path ;
50 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
51 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
53 : vocab/file>path ( vocab file -- path )
54 [ vocab>path ] dip append-path ;
56 : vocab/suffix>path ( vocab suffix -- path )
57 [ vocab>path dup file-name append-path ] dip append ;
59 : directory-exists ( path -- )
60 "Not creating a directory, it already exists: " write print ;
62 : scaffold-directory ( vocab-root vocab -- )
64 dup exists? [ directory-exists ] [ make-directories ] if ;
66 : not-scaffolding ( path -- path )
67 "Not creating scaffolding for " write dup <pathname> . ;
69 : scaffolding ( path -- path )
70 "Creating scaffolding for " write dup <pathname> . ;
72 : scaffolding? ( path -- path ? )
73 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
75 : scaffold-copyright ( -- )
76 "! Copyright (C) " write now year>> number>string write
77 developer-name get [ "Your name" ] unless* bl write "." print
78 "! See http://factorcode.org/license.txt for BSD license." print ;
80 : main-file-string ( vocab -- string )
85 ] with-string-writer ;
87 : set-scaffold-main-file ( vocab path -- )
88 [ main-file-string 1array ] dip utf8 set-file-lines ;
90 : scaffold-main ( vocab-root vocab -- )
91 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
92 set-scaffold-main-file
97 : scaffold-metadata ( vocab file contents -- )
98 [ ensure-vocab-exists ] 2dip
100 [ vocab/file>path ] dip 1array swap scaffolding? [
109 : lookup-type ( string -- object/string ? )
111 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
119 { "hashtable" hashtable }
121 { "ch" "a character" }
124 { "byte-array" byte-array }
126 { "duration" duration }
127 { "path" "a pathname string" }
128 { "vocab" "a vocabulary specifier" }
129 { "vocab-root" "a vocabulary root string" }
131 { "sequence" sequence }
133 { "exemplar" object }
135 { "alist" "an array of key/value pairs" }
137 { "values" sequence }
141 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
143 GENERIC: add-using ( object -- )
145 M: array add-using [ add-using ] each ;
147 M: string add-using drop ;
149 M: object add-using ( object -- )
150 vocabulary>> using get [ adjoin ] [ drop ] if* ;
152 : ($values.) ( array -- )
157 dup array? [ first ] when
160 [ [ pprint ] [ add-using ] bi ] bi*
162 drop unparse write bl null pprint
169 : ?print-nl ( seq1 seq2 -- )
170 [ empty? ] either? [ nl ] unless ;
172 : $values. ( word -- )
173 "declared-effect" word-prop [
174 [ in>> ] [ out>> ] bi
175 2dup [ empty? ] both? [
178 [ members ] dip over diff
182 [ nip ($values.) ] 2tri
187 : error-description. ( word -- )
189 "{ $description \"Throws " write
190 name>> dup a/an write " \" { $link " write
191 write " } \" error.\" }" print
192 ] bi "{ $error-description \"\" } ;" print ;
194 : class-description. ( word -- )
195 drop "{ $class-description \"\" } ;" print ;
197 : symbol-description. ( word -- )
198 drop "{ $var-description \"\" } ;" print ;
200 : $description. ( word -- )
201 drop "{ $description \"\" } ;" print ;
203 : docs-body. ( word/symbol -- )
205 { [ dup error-class? ] [ error-description. ] }
206 { [ dup class? ] [ class-description. ] }
207 { [ dup symbol? ] [ symbol-description. ] }
208 [ [ $values. ] [ $description. ] bi ]
211 : docs-header. ( word -- )
212 "HELP: " write name>> print ;
214 : (help.) ( word -- )
215 [ docs-header. ] [ docs-body. ] bi ;
217 : interesting-words ( vocab -- array )
219 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
222 : interesting-words. ( vocab -- )
223 interesting-words [ (help.) nl ] each ;
225 : docs-file-string ( vocab -- str2 )
228 [ "IN: " write print nl ]
229 [ interesting-words. ]
231 [ "ARTICLE: " write unparse dup write bl print ]
232 [ "{ $vocab-link " write pprint " }" print ] bi
235 [ "ABOUT: " write unparse print ]
237 ] with-string-writer ;
239 : write-using ( vocab -- )
242 { "help.markup" "help.syntax" } append natural-sort remove
246 : set-scaffold-docs-file ( vocab path -- )
249 [ docs-file-string ] [ write-using ] bi
251 ] with-output-stream ;
253 : with-scaffold ( quot -- )
254 [ HS{ } clone using ] dip with-variable ; inline
256 : link-vocab ( vocab -- )
258 "Edit documentation: " write
259 "-docs.factor" vocab/suffix>path <pathname> . ;
264 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
266 : scaffold-docs ( vocab -- )
269 dup "-docs.factor" vocab/suffix>path scaffolding? [
270 set-scaffold-docs-file
276 : scaffold-undocumented ( string -- )
277 [ interesting-words. ] [ link-vocab ] bi ;
279 : scaffold-authors ( vocab -- )
280 "authors.txt" developer-name get scaffold-metadata ;
282 : scaffold-tags ( vocab tags -- )
283 [ "tags.txt" ] dip scaffold-metadata ;
285 : scaffold-summary ( vocab summary -- )
286 [ "summary.txt" ] dip scaffold-metadata ;
288 : scaffold-platforms ( vocab platforms -- )
289 [ "platforms.txt" ] dip scaffold-metadata ;
291 : scaffold-vocab ( vocab-root string -- )
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 ;