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/file>path ( vocab file -- path )
63 [ vocab>path ] dip append-path ;
65 : vocab/suffix>path ( vocab suffix -- path )
66 [ vocab>path dup file-name append-path ] dip append ;
68 : directory-exists ( path -- )
69 "Not creating a directory, it already exists: " write print ;
71 : scaffold-directory ( vocab-root vocab -- )
73 dup exists? [ directory-exists ] [ make-directories ] if ;
75 : not-scaffolding ( path -- path )
76 "Not creating scaffolding for " write dup <pathname> . ;
78 : scaffolding ( path -- path )
79 "Creating scaffolding for " write dup <pathname> . ;
81 : scaffolding? ( path -- path ? )
82 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
84 : scaffold-copyright ( -- )
85 "! Copyright (C) " write now year>> number>string write
86 developer-name get [ "Your name" ] unless* bl write "." print
87 "! See http://factorcode.org/license.txt for BSD license." print ;
89 : main-file-string ( vocab -- string )
94 ] with-string-writer ;
96 : set-scaffold-main-file ( vocab path -- )
97 [ main-file-string ] dip utf8 set-file-contents ;
99 : scaffold-main ( vocab-root vocab -- )
100 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
101 set-scaffold-main-file
106 : scaffold-metadata ( vocab file contents -- )
107 [ ensure-vocab-exists ] 2dip
109 [ vocab/file>path ] dip swap scaffolding? [
110 utf8 set-file-contents
118 : lookup-type ( string -- object/string ? )
119 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
121 { "object" object } { "obj" object }
126 { "hashtable" hashtable }
128 { "ch" "a character" }
132 { "duration" duration }
133 { "path" "a pathname string" }
134 { "vocab" "a vocabulary specifier" }
135 { "vocab-root" "a vocabulary root string" }
139 { "alist" "an array of key/value pairs" }
140 { "keys" sequence } { "values" sequence }
141 { "class" class } { "tuple" tuple }
145 : add-using ( object -- )
146 vocabulary>> using get [ conjoin ] [ drop ] if* ;
148 : ($values.) ( array -- )
151 dup array? [ first ] when
154 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
156 drop unparse write bl null pprint
165 : $values. ( word -- )
166 "declared-effect" word-prop [
167 [ in>> ] [ out>> ] bi
168 2dup [ empty? ] bi@ and [
173 [ [ nl 4bl ($values.) ] unless-empty ] bi*
178 : symbol-description. ( word -- )
180 "{ $var-description \"\" } ;" print ;
182 : $description. ( word -- )
184 "{ $description \"\" } ;" print ;
186 : docs-body. ( word/symbol -- )
190 [ $values. ] [ $description. ] bi
193 : docs-header. ( word -- )
194 "HELP: " write name>> print ;
196 : (help.) ( word -- )
197 [ docs-header. ] [ docs-body. ] bi ;
199 : interesting-words ( vocab -- array )
201 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
204 : interesting-words. ( vocab -- )
205 interesting-words [ (help.) nl ] each ;
207 : docs-file-string ( vocab -- str2 )
210 [ "IN: " write print nl ]
211 [ interesting-words. ]
213 [ "ARTICLE: " write unparse dup write bl print ]
214 [ "{ $vocab-link " write pprint " }" print ] bi
217 [ "ABOUT: " write unparse print ]
219 ] with-string-writer ;
221 : write-using ( vocab -- )
224 { "help.markup" "help.syntax" } append natural-sort remove
228 : set-scaffold-docs-file ( vocab path -- )
231 [ docs-file-string ] [ write-using ] bi
233 ] with-output-stream ;
235 : with-scaffold ( quot -- )
236 [ H{ } clone using ] dip with-variable ; inline
238 : link-vocab ( vocab -- )
240 "Edit documentation: " write
241 "-docs.factor" vocab/suffix>path <pathname> . ;
246 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
248 : scaffold-help ( vocab -- )
251 dup "-docs.factor" vocab/suffix>path scaffolding? [
252 set-scaffold-docs-file
258 : scaffold-undocumented ( string -- )
259 [ interesting-words. ] [ link-vocab ] bi ;
261 : scaffold-authors ( vocab -- )
262 "authors.txt" developer-name get scaffold-metadata ;
264 : scaffold-tags ( vocab tags -- )
265 [ "tags.txt" ] dip scaffold-metadata ;
267 : scaffold-summary ( vocab summary -- )
268 [ "summary.txt" ] dip scaffold-metadata ;
270 : scaffold-vocab ( vocab-root string -- )
272 [ scaffold-directory ]
275 [ nip scaffold-authors ]
278 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
280 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
282 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
284 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
288 : tests-file-string ( vocab -- string )
291 "USING: tools.test " write dup write " ;" print
292 "IN: " write write ".tests" print
293 ] with-string-writer ;
295 : set-scaffold-tests-file ( vocab path -- )
296 [ tests-file-string ] dip utf8 set-file-contents ;
300 : scaffold-tests ( vocab -- )
302 dup "-tests.factor" vocab/suffix>path
304 set-scaffold-tests-file
309 SYMBOL: examples-flag
313 "{ $example \"\" \"USING: prettyprint ;\""
317 } [ examples-flag get [ 4bl ] when print ] each ;
328 [ "Click to edit: " write <pathname> . ] bi ;
330 : scaffold-rc ( path -- )
331 [ home ] dip append-path touch. ;
333 : scaffold-factor-boot-rc ( -- )
334 os windows? "factor-boot-rc" ".factor-boot-rc" ? scaffold-rc ;
336 : scaffold-factor-rc ( -- )
337 os windows? "factor-rc" ".factor-rc" ? scaffold-rc ;
340 HOOK: scaffold-emacs os ( -- )
342 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;