1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien arrays assocs byte-arrays calendar
5 classes classes.error combinators combinators.short-circuit
6 continuations hashtables help.markup interpolate io
7 io.directories io.encodings.utf8 io.files io.pathnames
8 io.streams.string kernel math math.parser namespaces prettyprint
9 quotations sequences sets sorting splitting strings system
10 timers unicode urls vocabs vocabs.loader vocabs.metadata words
14 SYMBOL: developer-name
17 ERROR: not-a-vocab-root string ;
19 ERROR: vocab-must-not-exist string ;
23 : vocab-root? ( string -- ? )
24 trim-tail-separators vocab-roots get member? ;
26 : ensure-vocab-exists ( string -- string )
27 dup lookup-vocab [ no-vocab ] unless ;
29 : check-vocab-root ( string -- string )
30 dup vocab-root? [ not-a-vocab-root ] unless ;
32 : check-vocab-root/name ( vocab-root string -- vocab-root string )
33 [ check-vocab-root ] [ check-vocab-name ] bi* ;
35 : replace-vocab-separators ( vocab -- path )
36 path-separator first CHAR: . associate substitute ;
38 : vocab-root/vocab>path ( vocab-root vocab -- path )
40 [ ] [ replace-vocab-separators ] bi* append-path ;
42 : vocab>path ( vocab -- path )
43 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
45 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
46 [ vocab-root/vocab>path ] dip append-path ;
48 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
49 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
51 : vocab/file>path ( vocab file -- path )
52 [ vocab>path ] dip append-path ;
54 : vocab/suffix>path ( vocab suffix -- path )
55 [ vocab>path dup file-name append-path ] dip append ;
57 : directory-exists ( path -- )
58 "Not creating a directory, it already exists: " write print ;
60 : scaffold-directory ( vocab-root vocab -- )
62 dup exists? [ directory-exists ] [ make-directories ] if ;
64 : not-scaffolding ( path -- path )
65 "Not creating scaffolding for " write dup <pathname> . ;
67 : scaffolding ( path -- path )
68 "Creating scaffolding for " write dup <pathname> . ;
70 : scaffolding? ( path -- path ? )
71 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
73 : scaffold-copyright ( -- )
74 "! Copyright (C) " write now year>> number>string write
75 developer-name get [ "Your name" ] unless* bl write "." print
76 "! See http://factorcode.org/license.txt for BSD license." print ;
78 : main-file-string ( vocab -- string )
83 ] with-string-writer ;
85 : set-scaffold-main-file ( vocab path -- )
86 [ main-file-string 1array ] dip utf8 set-file-lines ;
88 : scaffold-main ( vocab-root vocab -- )
89 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
90 set-scaffold-main-file
95 : scaffold-metadata ( vocab file contents -- )
96 [ ensure-vocab-exists ] 2dip
98 [ vocab/file>path ] dip 1array swap scaffolding? [
107 : lookup-type ( string -- object/string ? )
109 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
117 { "hashtable" hashtable }
119 { "ch" "a character" }
122 { "byte-array" byte-array }
124 { "duration" duration }
125 { "path" "a pathname string" }
126 { "vocab" "a vocabulary specifier" }
127 { "vocab-root" "a vocabulary root string" }
129 { "sequence" sequence }
131 { "exemplar" object }
133 { "alist" "an array of key/value pairs" }
135 { "values" sequence }
139 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
141 GENERIC: add-using ( object -- )
143 M: array add-using [ add-using ] each ;
145 M: string add-using drop ;
148 vocabulary>> using get [ adjoin ] [ drop ] if* ;
150 : ($values.) ( array -- )
155 dup array? [ first ] when
158 [ [ pprint ] [ add-using ] bi ] bi*
160 drop unparse write bl null pprint
167 : ?print-nl ( seq1 seq2 -- )
168 [ empty? ] either? [ nl ] unless ;
170 : $values. ( word -- )
171 "declared-effect" word-prop [
172 [ in>> ] [ out>> ] bi
173 2dup [ empty? ] both? [
176 [ members ] dip over diff
180 [ nip ($values.) ] 2tri
185 : error-description. ( word -- )
187 "{ $description \"Throws " write
188 name>> dup a/an write " \" { $link " write
189 write " } \" error.\" }" print
190 ] bi "{ $error-description \"\" } ;" print ;
192 : class-description. ( word -- )
193 drop "{ $class-description \"\" } ;" print ;
195 : symbol-description. ( word -- )
196 drop "{ $var-description \"\" } ;" print ;
198 : $description. ( word -- )
199 drop "{ $description \"\" } ;" print ;
201 : docs-body. ( word/symbol -- )
203 { [ dup error-class? ] [ error-description. ] }
204 { [ dup class? ] [ class-description. ] }
205 { [ dup symbol? ] [ symbol-description. ] }
206 [ [ $values. ] [ $description. ] bi ]
209 : docs-header. ( word -- )
210 "HELP: " write name>> print ;
212 : (help.) ( word -- )
213 [ docs-header. ] [ docs-body. ] bi ;
215 : interesting-words ( vocab -- array )
217 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
220 : interesting-words. ( vocab -- )
221 interesting-words [ (help.) nl ] each ;
223 : docs-file-string ( vocab -- str2 )
226 [ "IN: " write print nl ]
227 [ interesting-words. ]
229 [ "ARTICLE: " write unparse dup write bl print ]
230 [ "{ $vocab-link " write pprint " }" print ] bi
233 [ "ABOUT: " write unparse print ]
235 ] with-string-writer ;
237 : write-using ( vocab -- )
240 { "help.markup" "help.syntax" } append natural-sort remove
244 : set-scaffold-docs-file ( vocab path -- )
247 [ docs-file-string ] [ write-using ] bi
249 ] with-output-stream ;
251 : with-scaffold ( quot -- )
252 [ HS{ } clone using ] dip with-variable ; inline
254 : link-vocab ( vocab -- )
256 "Edit documentation: " write
257 "-docs.factor" vocab/suffix>path <pathname> . ;
262 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
264 : scaffold-docs ( vocab -- )
267 dup "-docs.factor" vocab/suffix>path scaffolding? [
268 set-scaffold-docs-file
274 : scaffold-undocumented ( string -- )
275 [ interesting-words. ] [ link-vocab ] bi ;
277 : scaffold-authors ( vocab -- )
278 "authors.txt" developer-name get scaffold-metadata ;
280 : scaffold-tags ( vocab tags -- )
281 [ "tags.txt" ] dip scaffold-metadata ;
283 : scaffold-summary ( vocab summary -- )
284 [ "summary.txt" ] dip scaffold-metadata ;
286 : scaffold-platforms ( vocab platforms -- )
287 [ "platforms.txt" ] dip scaffold-metadata ;
289 : delete-from-root-cache ( string -- )
290 root-cache get delete-at ;
292 : scaffold-vocab-in ( vocab-root string -- )
293 dup delete-from-root-cache
295 [ scaffold-directory ]
298 [ nip scaffold-authors ]
301 : scaffold-core ( string -- )
302 "resource:core" swap scaffold-vocab-in ;
304 : scaffold-basis ( string -- )
305 "resource:basis" swap scaffold-vocab-in ;
307 : scaffold-extra ( string -- )
308 "resource:extra" swap scaffold-vocab-in ;
310 : scaffold-work ( string -- )
311 "resource:work" swap scaffold-vocab-in ;
313 : scaffold-vocab ( string -- )
314 "Choose a vocabulary root:" vocab-roots get
315 '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
316 swap scaffold-vocab-in ;
320 : tests-file-string ( vocab -- string )
323 "USING: tools.test " write dup write " ;" print
324 "IN: " write write ".tests" print
325 ] with-string-writer ;
327 : set-scaffold-tests-file ( vocab path -- )
328 [ tests-file-string ] dip utf8 set-file-contents ;
332 : scaffold-tests ( vocab -- )
334 dup "-tests.factor" vocab/suffix>path
336 set-scaffold-tests-file
341 SYMBOL: nested-examples
343 : example-using ( using -- )
344 " " join "example-using" [
345 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
346 "${example-indent}\"Example:\"
347 ${example-indent}{ $example \"USING: ${example-using} ;\"
348 ${example-indent} \"\"
349 ${example-indent} \"\"
356 : n-examples-using ( n using -- )
357 '[ _ example-using ] times ;
359 : scaffold-n-examples ( n word -- )
360 vocabulary>> "prettyprint" 2array
361 [ t nested-examples ] 2dip
368 : scaffold-examples ( word -- )
369 2 swap scaffold-n-examples ;
371 : scaffold-file ( path -- )
373 [ "Click to edit: " write <pathname> . ] bi ;
375 : scaffold-rc ( path -- )
376 [ home ] dip append-path scaffold-file ;
378 : scaffold-factor-boot-rc ( -- )
379 ".factor-boot-rc" scaffold-rc ;
381 : scaffold-factor-rc ( -- )
382 ".factor-rc" scaffold-rc ;
384 : scaffold-mason-rc ( -- )
385 ".factor-mason-rc" scaffold-rc ;
387 : scaffold-factor-roots ( -- )
388 ".factor-roots" scaffold-rc ;
390 HOOK: scaffold-emacs os ( -- )
392 M: unix scaffold-emacs ".emacs" scaffold-rc ;