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 file-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 file-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 }
136 { "exemplar" object }
138 { "alist" "an array of key/value pairs" }
140 { "values" sequence }
144 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
146 GENERIC: add-using ( object -- )
148 M: array add-using [ add-using ] each ;
150 M: string add-using drop ;
153 vocabulary>> using get [ adjoin ] [ drop ] if* ;
155 : ($values.) ( array -- )
160 dup array? [ first ] when
163 [ [ pprint ] [ add-using ] bi ] bi*
165 drop unparse write bl object pprint
172 : ?print-nl ( seq1 seq2 -- )
173 [ empty? ] either? [ nl ] unless ;
175 : $values. ( word -- )
176 "declared-effect" word-prop [
177 [ in>> ] [ out>> ] bi
178 2dup [ empty? ] both? [
181 [ members ] dip over diff
185 [ nip ($values.) ] 2tri
190 : error-description. ( word -- )
192 "{ $description \"Throws " write
193 name>> dup a/an write " \" { $link " write
194 write " } \" error.\" }" print
195 ] bi "{ $error-description \"\" } ;" print ;
197 : class-description. ( word -- )
198 drop "{ $class-description \"\" } ;" print ;
200 : symbol-description. ( word -- )
201 drop "{ $var-description \"\" } ;" print ;
203 : $description. ( word -- )
204 drop "{ $description \"\" } ;" print ;
206 : docs-body. ( word/symbol -- )
208 { [ dup error-class? ] [ error-description. ] }
209 { [ dup class? ] [ class-description. ] }
210 { [ dup symbol? ] [ symbol-description. ] }
211 [ [ $values. ] [ $description. ] bi ]
214 : docs-header. ( word -- )
215 "HELP: " write name>> print ;
217 : (help.) ( word -- )
218 [ docs-header. ] [ docs-body. ] bi ;
220 : interesting-words ( vocab -- array )
222 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
225 : interesting-words. ( vocab -- )
226 interesting-words [ (help.) nl ] each ;
228 : docs-file-string ( vocab -- str2 )
231 [ "IN: " write print nl ]
232 [ interesting-words. ]
234 [ "ARTICLE: " write unparse dup write bl print ]
235 [ "{ $vocab-link " write pprint " }" print ] bi
238 [ "ABOUT: " write unparse print ]
240 ] with-string-writer ;
242 : write-using ( vocab -- )
245 { "help.markup" "help.syntax" } append natural-sort remove
249 : set-scaffold-docs-file ( vocab path -- )
252 [ docs-file-string ] [ write-using ] bi
254 ] with-output-stream ;
256 : with-scaffold ( quot -- )
257 [ HS{ } clone using ] dip with-variable ; inline
259 : link-vocab ( vocab -- )
260 ".private" ?tail drop
262 "Edit documentation: " write
263 "-docs.factor" vocab/suffix>path <pathname> . ;
268 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
270 : scaffold-docs ( vocab -- )
273 dup "-docs.factor" vocab/suffix>path scaffolding? [
274 set-scaffold-docs-file
280 : scaffold-undocumented ( string -- )
281 [ interesting-words. ] [ link-vocab ] bi ;
283 : scaffold-authors ( vocab -- )
284 "authors.txt" developer-name get scaffold-metadata ;
286 : scaffold-tags ( vocab tags -- )
287 [ "tags.txt" ] dip scaffold-metadata ;
289 : scaffold-summary ( vocab summary -- )
290 [ "summary.txt" ] dip scaffold-metadata ;
292 : scaffold-platforms ( vocab platforms -- )
293 [ "platforms.txt" ] dip scaffold-metadata ;
295 : delete-from-root-cache ( string -- )
296 root-cache get delete-at ;
298 : scaffold-vocab-in ( vocab-root string -- )
299 dup delete-from-root-cache
301 [ scaffold-directory ]
304 [ nip scaffold-authors ]
307 : scaffold-core ( string -- )
308 "resource:core" swap scaffold-vocab-in ;
310 : scaffold-basis ( string -- )
311 "resource:basis" swap scaffold-vocab-in ;
313 : scaffold-extra ( string -- )
314 "resource:extra" swap scaffold-vocab-in ;
316 : scaffold-work ( string -- )
317 "resource:work" swap scaffold-vocab-in ;
319 : scaffold-vocab ( string -- )
320 "Choose a vocabulary root:" vocab-roots get
321 '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
322 swap scaffold-vocab-in ;
326 : tests-file-string ( vocab -- string )
329 "USING: tools.test " write dup write " ;" print
330 "IN: " write write ".tests" print
331 ] with-string-writer ;
333 : set-scaffold-tests-file ( vocab path -- )
334 [ tests-file-string ] dip utf8 set-file-contents ;
338 : scaffold-tests ( vocab -- )
340 dup "-tests.factor" vocab/suffix>path
342 set-scaffold-tests-file
347 SYMBOL: nested-examples
349 : example-using ( using -- )
350 join-words "example-using" [
351 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
352 "${example-indent}\"Example:\"
353 ${example-indent}{ $example \"USING: ${example-using} ;\"
354 ${example-indent} \"\"
355 ${example-indent} \"\"
362 : n-examples-using ( n using -- )
363 '[ _ example-using ] times ;
365 : scaffold-n-examples ( n word -- )
366 vocabulary>> "prettyprint" 2array
367 [ t nested-examples ] 2dip
374 : scaffold-examples ( word -- )
375 2 swap scaffold-n-examples ;
377 : scaffold-file ( path -- )
379 [ "Click to edit: " write <pathname> . ] bi ;
381 : scaffold-rc ( path -- )
382 home prepend-path scaffold-file ;
384 : scaffold-factor-boot-rc ( -- )
385 ".factor-boot-rc" scaffold-rc ;
387 : scaffold-factor-rc ( -- )
388 ".factor-rc" scaffold-rc ;
390 : scaffold-mason-rc ( -- )
391 ".factor-mason-rc" scaffold-rc ;
393 : scaffold-factor-roots ( -- )
394 ".factor-roots" scaffold-rc ;
396 HOOK: scaffold-emacs os ( -- )
398 M: unix scaffold-emacs ".emacs" scaffold-rc ;