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 timers
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 -- ? )
27 vocab-roots get member? ;
29 : contains-dot? ( string -- ? ) ".." swap subseq? ;
31 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
33 : ensure-vocab-exists ( string -- string )
34 dup vocabs member? [ no-vocab ] unless ;
36 : check-vocab-name ( string -- string )
38 [ contains-dot? [ vocab-name-contains-dot ] when ]
39 [ contains-separator? [ vocab-name-contains-separator ] when ] tri ;
41 : check-root ( string -- string )
42 dup vocab-root? [ not-a-vocab-root ] unless ;
44 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
45 [ check-root ] [ check-vocab-name ] bi* ;
47 : replace-vocab-separators ( vocab -- path )
48 path-separator first CHAR: . associate substitute ; inline
50 : vocab-root/vocab>path ( vocab-root vocab -- path )
51 check-vocab-root/vocab
52 [ ] [ replace-vocab-separators ] bi* append-path ;
54 : vocab>path ( vocab -- path )
56 [ find-vocab-root ] keep vocab-root/vocab>path ;
58 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
59 [ vocab-root/vocab>path ] dip append-path ;
61 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
62 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
64 : vocab/file>path ( vocab file -- path )
65 [ vocab>path ] dip append-path ;
67 : vocab/suffix>path ( vocab suffix -- path )
68 [ vocab>path dup file-name append-path ] dip append ;
70 : directory-exists ( path -- )
71 "Not creating a directory, it already exists: " write print ;
73 : scaffold-directory ( vocab-root vocab -- )
75 dup exists? [ directory-exists ] [ make-directories ] if ;
77 : not-scaffolding ( path -- path )
78 "Not creating scaffolding for " write dup <pathname> . ;
80 : scaffolding ( path -- path )
81 "Creating scaffolding for " write dup <pathname> . ;
83 : scaffolding? ( path -- path ? )
84 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
86 : scaffold-copyright ( -- )
87 "! Copyright (C) " write now year>> number>string write
88 developer-name get [ "Your name" ] unless* bl write "." print
89 "! See http://factorcode.org/license.txt for BSD license." print ;
91 : main-file-string ( vocab -- string )
96 ] with-string-writer ;
98 : set-scaffold-main-file ( vocab path -- )
99 [ main-file-string 1array ] dip utf8 set-file-lines ;
101 : scaffold-main ( vocab-root vocab -- )
102 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
103 set-scaffold-main-file
108 : scaffold-metadata ( vocab file contents -- )
109 [ ensure-vocab-exists ] 2dip
111 [ vocab/file>path ] dip 1array swap scaffolding? [
120 : lookup-type ( string -- object/string ? )
121 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
123 { "object" object } { "obj" object }
128 { "hashtable" hashtable }
130 { "ch" "a character" }
134 { "duration" duration }
135 { "path" "a pathname string" }
136 { "vocab" "a vocabulary specifier" }
137 { "vocab-root" "a vocabulary root string" }
141 { "alist" "an array of key/value pairs" }
142 { "keys" sequence } { "values" sequence }
143 { "class" class } { "tuple" tuple }
147 : add-using ( object -- )
148 vocabulary>> using get [ conjoin ] [ drop ] if* ;
150 : ($values.) ( array -- )
153 dup array? [ first ] when
156 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
158 drop unparse write bl null pprint
167 : $values. ( word -- )
168 "declared-effect" word-prop [
169 [ in>> ] [ out>> ] bi
170 2dup [ empty? ] bi@ and [
175 [ [ nl 4bl ($values.) ] unless-empty ] bi*
180 : symbol-description. ( word -- )
182 "{ $var-description \"\" } ;" print ;
184 : $description. ( word -- )
186 "{ $description \"\" } ;" print ;
188 : docs-body. ( word/symbol -- )
192 [ $values. ] [ $description. ] bi
195 : docs-header. ( word -- )
196 "HELP: " write name>> print ;
198 : (help.) ( word -- )
199 [ docs-header. ] [ docs-body. ] bi ;
201 : interesting-words ( vocab -- array )
203 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
206 : interesting-words. ( vocab -- )
207 interesting-words [ (help.) nl ] each ;
209 : docs-file-string ( vocab -- str2 )
212 [ "IN: " write print nl ]
213 [ interesting-words. ]
215 [ "ARTICLE: " write unparse dup write bl print ]
216 [ "{ $vocab-link " write pprint " }" print ] bi
219 [ "ABOUT: " write unparse print ]
221 ] with-string-writer ;
223 : write-using ( vocab -- )
226 { "help.markup" "help.syntax" } append natural-sort remove
230 : set-scaffold-docs-file ( vocab path -- )
233 [ docs-file-string ] [ write-using ] bi
235 ] with-output-stream ;
237 : with-scaffold ( quot -- )
238 [ H{ } clone using ] dip with-variable ; inline
240 : link-vocab ( vocab -- )
242 "Edit documentation: " write
243 "-docs.factor" vocab/suffix>path <pathname> . ;
248 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
250 : scaffold-help ( vocab -- )
253 dup "-docs.factor" vocab/suffix>path scaffolding? [
254 set-scaffold-docs-file
260 : scaffold-undocumented ( string -- )
261 [ interesting-words. ] [ link-vocab ] bi ;
263 : scaffold-authors ( vocab -- )
264 "authors.txt" developer-name get scaffold-metadata ;
266 : scaffold-tags ( vocab tags -- )
267 [ "tags.txt" ] dip scaffold-metadata ;
269 : scaffold-summary ( vocab summary -- )
270 [ "summary.txt" ] dip scaffold-metadata ;
272 : scaffold-platforms ( vocab platforms -- )
273 [ "platforms.txt" ] dip scaffold-metadata ;
275 : scaffold-vocab ( vocab-root string -- )
277 [ scaffold-directory ]
280 [ nip scaffold-authors ]
283 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
285 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
287 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
289 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
293 : tests-file-string ( vocab -- string )
296 "USING: tools.test " write dup write " ;" print
297 "IN: " write write ".tests" print
298 ] with-string-writer ;
300 : set-scaffold-tests-file ( vocab path -- )
301 [ tests-file-string ] dip utf8 set-file-contents ;
305 : scaffold-tests ( vocab -- )
307 dup "-tests.factor" vocab/suffix>path
309 set-scaffold-tests-file
314 SYMBOL: examples-flag
318 "{ $example \"\" \"USING: prettyprint ;\""
322 } [ examples-flag get [ 4bl ] when print ] each ;
333 [ "Click to edit: " write <pathname> . ] bi ;
335 : scaffold-rc ( path -- )
336 [ home ] dip append-path touch. ;
338 : scaffold-factor-boot-rc ( -- )
339 os windows? "factor-boot-rc" ".factor-boot-rc" ? scaffold-rc ;
341 : scaffold-factor-rc ( -- )
342 os windows? "factor-rc" ".factor-rc" ? scaffold-rc ;
345 HOOK: scaffold-emacs os ( -- )
347 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;