1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs io.files io.pathnames io.directories
4 io.encodings.utf8 hashtables kernel namespaces sequences
5 vocabs.loader vocabs.metadata io combinators calendar accessors
6 math.parser io.streams.string ui.tools.operations quotations
7 strings arrays prettyprint words vocabs sorting sets classes
8 math alien urls splitting ascii combinators.short-circuit alarms
9 words.symbol system summary ;
12 SYMBOL: developer-name
15 ERROR: not-a-vocab-root string ;
16 ERROR: vocab-name-contains-separator path ;
17 ERROR: vocab-name-contains-dot path ;
18 ERROR: bad-developer-name name ;
20 M: bad-developer-name summary
21 drop "Developer name must be a string." ;
25 : vocab-root? ( string -- ? ) vocab-roots get member? ;
27 : contains-dot? ( string -- ? ) ".." swap subseq? ;
29 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
31 : ensure-vocab-exists ( string -- string )
32 dup vocabs member? [ no-vocab ] unless ;
34 : check-vocab-name ( string -- string )
36 [ contains-dot? [ vocab-name-contains-dot ] when ]
37 [ contains-separator? [ vocab-name-contains-separator ] when ] tri ;
39 : check-root ( string -- string )
40 dup vocab-root? [ not-a-vocab-root ] unless ;
42 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
43 [ check-root ] [ check-vocab-name ] bi* ;
45 : replace-vocab-separators ( vocab -- path )
46 path-separator first CHAR: . associate substitute ; inline
48 : vocab-root/vocab>path ( vocab-root vocab -- path )
49 check-vocab-root/vocab
50 [ ] [ replace-vocab-separators ] bi* append-path ;
52 : vocab>path ( vocab -- path )
54 [ find-vocab-root ] keep vocab-root/vocab>path ;
56 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
57 [ vocab-root/vocab>path ] dip append-path ;
59 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
60 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
62 : vocab/suffix>path ( vocab suffix -- path )
63 [ vocab>path dup file-name append-path ] dip append ;
65 : directory-exists ( path -- )
66 "Not creating a directory, it already exists: " write print ;
68 : scaffold-directory ( vocab-root vocab -- )
70 dup exists? [ directory-exists ] [ make-directories ] if ;
72 : not-scaffolding ( path -- path )
73 "Not creating scaffolding for " write dup <pathname> . ;
75 : scaffolding ( path -- path )
76 "Creating scaffolding for " write dup <pathname> . ;
78 : scaffolding? ( path -- path ? )
79 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
81 : scaffold-copyright ( -- )
82 "! Copyright (C) " write now year>> number>string write
83 developer-name get [ "Your name" ] unless* bl write "." print
84 "! See http://factorcode.org/license.txt for BSD license." print ;
86 : main-file-string ( vocab -- string )
91 ] with-string-writer ;
93 : set-scaffold-main-file ( vocab path -- )
94 [ main-file-string ] dip utf8 set-file-contents ;
96 : scaffold-main ( vocab-root vocab -- )
97 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
98 set-scaffold-main-file
103 : scaffold-authors ( vocab-root vocab -- )
105 "authors.txt" vocab-root/vocab/file>path scaffolding? [
106 developer-name get swap utf8 set-file-contents
114 : lookup-type ( string -- object/string ? )
115 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
117 { "object" object } { "obj" object }
122 { "hashtable" hashtable }
124 { "ch" "a character" }
128 { "duration" duration }
129 { "path" "a pathname string" }
130 { "vocab" "a vocabulary specifier" }
131 { "vocab-root" "a vocabulary root string" }
135 { "alist" "an array of key/value pairs" }
136 { "keys" sequence } { "values" sequence }
137 { "class" class } { "tuple" tuple }
141 : add-using ( object -- )
142 vocabulary>> using get [ conjoin ] [ drop ] if* ;
144 : ($values.) ( array -- )
147 dup array? [ first ] when
150 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
152 drop unparse write bl null pprint
161 : $values. ( word -- )
162 "declared-effect" word-prop [
163 [ in>> ] [ out>> ] bi
164 2dup [ empty? ] bi@ and [
169 [ [ nl 4bl ($values.) ] unless-empty ] bi*
174 : symbol-description. ( word -- )
176 "{ $var-description \"\" } ;" print ;
178 : $description. ( word -- )
180 "{ $description \"\" } ;" print ;
182 : docs-body. ( word/symbol -- )
186 [ $values. ] [ $description. ] bi
189 : docs-header. ( word -- )
190 "HELP: " write name>> print ;
192 : (help.) ( word -- )
193 [ docs-header. ] [ docs-body. ] bi ;
195 : interesting-words ( vocab -- array )
197 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
200 : interesting-words. ( vocab -- )
201 interesting-words [ (help.) nl ] each ;
203 : docs-file-string ( vocab -- str2 )
206 [ "IN: " write print nl ]
207 [ interesting-words. ]
209 [ "ARTICLE: " write unparse dup write bl print ]
210 [ "{ $vocab-link " write pprint " }" print ] bi
213 [ "ABOUT: " write unparse print ]
215 ] with-string-writer ;
217 : write-using ( vocab -- )
220 { "help.markup" "help.syntax" } append natural-sort remove
224 : set-scaffold-docs-file ( vocab path -- )
227 [ docs-file-string ] [ write-using ] bi
229 ] with-output-stream ;
231 : with-scaffold ( quot -- )
232 [ H{ } clone using ] dip with-variable ; inline
234 : link-vocab ( vocab -- )
236 "Edit documentation: " write
237 "-docs.factor" vocab/suffix>path <pathname> . ;
242 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
244 : scaffold-help ( vocab -- )
247 dup "-docs.factor" vocab/suffix>path scaffolding? [
248 set-scaffold-docs-file
254 : scaffold-undocumented ( string -- )
255 [ interesting-words. ] [ link-vocab ] bi ;
257 : scaffold-vocab ( vocab-root string -- )
259 [ scaffold-directory ]
265 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
267 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
269 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
271 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
275 : tests-file-string ( vocab -- string )
278 "USING: tools.test " write dup write " ;" print
279 "IN: " write write ".tests" print
280 ] with-string-writer ;
282 : set-scaffold-tests-file ( vocab path -- )
283 [ tests-file-string ] dip utf8 set-file-contents ;
287 : scaffold-tests ( vocab -- )
289 dup "-tests.factor" vocab/suffix>path
291 set-scaffold-tests-file
296 SYMBOL: examples-flag
300 "{ $example \"\" \"USING: prettyprint ;\""
304 } [ examples-flag get [ 4bl ] when print ] each ;
315 [ "Click to edit: " write <pathname> . ] bi ;
317 : scaffold-rc ( path -- )
318 [ home ] dip append-path touch. ;
320 : scaffold-factor-boot-rc ( -- )
321 os windows? "factor-boot-rc" ".factor-boot-rc" ? scaffold-rc ;
323 : scaffold-factor-rc ( -- )
324 os windows? "factor-rc" ".factor-rc" ? scaffold-rc ;
327 HOOK: scaffold-emacs os ( -- )
329 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;