1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs byte-arrays calendar
4 classes classes.error combinators combinators.short-circuit
5 continuations eval hashtables help.markup interpolate io
6 io.directories io.encodings.utf8 io.files io.pathnames
7 io.streams.string kernel math math.parser namespaces prettyprint
8 quotations sequences sequences.extras sets sorting splitting
9 strings system timers unicode urls vocabs vocabs.loader
10 vocabs.metadata words words.symbol ;
13 SYMBOL: developer-name
16 ERROR: not-a-vocab-root string ;
18 ERROR: vocab-must-not-exist string ;
22 : vocab-root? ( string -- ? )
23 trim-tail-separators vocab-roots get member? ;
25 : ensure-vocab-exists ( string -- string )
26 dup lookup-vocab [ no-vocab ] unless ;
28 : check-vocab-root ( string -- string )
29 dup vocab-root? [ not-a-vocab-root ] unless ;
31 : check-vocab-root/name ( vocab-root string -- vocab-root string )
32 [ check-vocab-root ] [ check-vocab-name ] bi* ;
34 : replace-vocab-separators ( vocab -- path )
35 path-separator first CHAR: . associate substitute ;
37 : vocab-root/vocab>path ( vocab-root vocab -- path )
39 [ ] [ replace-vocab-separators ] bi* append-path ;
41 : vocab>path ( vocab -- path )
42 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
44 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
45 [ vocab-root/vocab>path ] dip append-path ;
47 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
48 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
50 : vocab/file>path ( vocab file -- path )
51 [ vocab>path ] dip append-path ;
53 : vocab/suffix>path ( vocab suffix -- path )
54 [ vocab>path dup file-name append-path ] dip append ;
56 : directory-exists ( path -- )
57 "Not creating a directory, it already exists: " write print ;
59 : scaffold-directory ( vocab-root vocab -- )
61 dup file-exists? [ directory-exists ] [ make-directories ] if ;
63 : not-scaffolding ( path -- path )
64 "Not creating scaffolding for " write dup <pathname> . ;
66 : scaffolding ( path -- path )
67 "Creating scaffolding for " write dup <pathname> . ;
69 : scaffolding? ( path -- path ? )
70 dup file-exists? [ not-scaffolding f ] [ scaffolding t ] if ;
72 : scaffold-copyright ( -- )
73 "! Copyright (C) " write now year>> number>string write
74 developer-name get [ "Your name" ] unless* bl write "." print
75 "! See http://factorcode.org/license.txt for BSD license." print ;
77 : main-file-string ( vocab -- string )
82 ] with-string-writer ;
84 : set-scaffold-main-file ( vocab path -- )
85 [ main-file-string 1array ] dip utf8 set-file-lines ;
87 : scaffold-main ( vocab-root vocab -- )
88 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
89 set-scaffold-main-file
94 : scaffold-metadata ( vocab file contents -- )
95 [ ensure-vocab-exists ] 2dip
97 [ vocab/file>path ] dip 1array swap scaffolding? [
106 : lookup-type ( string -- object/string ? )
108 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
116 { "hashtable" hashtable }
118 { "ch" "a character" }
121 { "byte-array" byte-array }
123 { "duration" duration }
124 { "path" "a pathname string" }
125 { "vocab" "a vocabulary specifier" }
126 { "vocab-root" "a vocabulary root string" }
128 { "sequence" sequence }
135 { "exemplar" object }
137 { "alist" "an array of key/value pairs" }
139 { "values" sequence }
143 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
145 GENERIC: add-using ( object -- )
147 M: array add-using [ add-using ] each ;
149 M: string add-using drop ;
152 vocabulary>> using get [ adjoin ] [ drop ] if* ;
154 : ($values.) ( array -- )
159 dup array? [ first ] when
162 [ [ pprint ] [ add-using ] bi ] bi*
164 drop unparse write bl object pprint
171 : ?print-nl ( seq1 seq2 -- )
172 [ empty? ] either? [ nl ] unless ;
174 : $values. ( word -- )
175 "declared-effect" word-prop [
176 [ in>> ] [ out>> ] bi
177 2dup [ empty? ] both? [
180 [ members ] dip over diff
184 [ nip ($values.) ] 2tri
189 : error-description. ( word -- )
191 "{ $description \"Throws " write
192 name>> dup a/an write " \" { $link " write
193 write " } \" error.\" }" print
194 ] bi "{ $error-description \"\" } ;" print ;
196 : class-description. ( word -- )
197 drop "{ $class-description \"\" } ;" print ;
199 : symbol-description. ( word -- )
200 drop "{ $var-description \"\" } ;" print ;
202 : $description. ( word -- )
203 drop "{ $description \"\" } ;" print ;
205 : docs-body. ( word/symbol -- )
207 { [ dup error-class? ] [ error-description. ] }
208 { [ dup class? ] [ class-description. ] }
209 { [ dup symbol? ] [ symbol-description. ] }
210 [ [ $values. ] [ $description. ] bi ]
213 : docs-header. ( word -- )
214 "HELP: " write name>> print ;
216 : (help.) ( word -- )
217 [ docs-header. ] [ docs-body. ] bi ;
219 : interesting-words ( vocab -- array )
221 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
224 : interesting-words. ( vocab -- )
225 interesting-words [ (help.) nl ] each ;
227 : docs-file-string ( vocab -- str2 )
230 [ "IN: " write print nl ]
231 [ interesting-words. ]
233 [ "ARTICLE: " write unparse dup write bl print ]
234 [ "{ $vocab-link " write pprint " }" print ] bi
237 [ "ABOUT: " write unparse print ]
239 ] with-string-writer ;
241 : write-using ( vocab -- )
244 { "help.markup" "help.syntax" } append natural-sort remove
248 : set-scaffold-docs-file ( vocab path -- )
251 [ docs-file-string ] [ write-using ] bi
253 ] with-output-stream ;
255 : with-scaffold ( quot -- )
256 [ HS{ } clone using ] dip with-variable ; inline
258 : link-vocab ( vocab -- )
259 ".private" ?tail drop
261 "Edit documentation: " write
262 "-docs.factor" vocab/suffix>path <pathname> . ;
267 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
269 : scaffold-docs ( vocab -- )
272 dup "-docs.factor" vocab/suffix>path scaffolding? [
273 set-scaffold-docs-file
279 : scaffold-undocumented ( string -- )
280 [ interesting-words. ] [ link-vocab ] bi ;
282 : scaffold-authors ( vocab -- )
283 "authors.txt" developer-name get scaffold-metadata ;
285 : scaffold-tags ( vocab tags -- )
286 [ "tags.txt" ] dip scaffold-metadata ;
288 : scaffold-summary ( vocab summary -- )
289 [ "summary.txt" ] dip scaffold-metadata ;
291 : scaffold-platforms ( vocab platforms -- )
292 [ "platforms.txt" ] dip scaffold-metadata ;
294 : delete-from-root-cache ( string -- )
295 root-cache get delete-at ;
297 : scaffold-vocab-in ( vocab-root string -- )
298 dup delete-from-root-cache
300 [ scaffold-directory ]
303 [ nip scaffold-authors ]
306 : scaffold-core ( string -- )
307 "resource:core" swap scaffold-vocab-in ;
309 : scaffold-basis ( string -- )
310 "resource:basis" swap scaffold-vocab-in ;
312 : scaffold-extra ( string -- )
313 "resource:extra" swap scaffold-vocab-in ;
315 : scaffold-work ( string -- )
316 "resource:work" swap scaffold-vocab-in ;
318 : scaffold-vocab ( string -- )
319 "Choose a vocabulary root:" vocab-roots get
320 '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
321 swap scaffold-vocab-in ;
325 : tests-file-string ( vocab -- string )
328 "USING: tools.test " write dup write " ;" print
329 "IN: " write write ".tests" print
330 ] with-string-writer ;
332 : set-scaffold-tests-file ( vocab path -- )
333 [ tests-file-string ] dip utf8 set-file-contents ;
337 : scaffold-tests ( vocab -- )
339 dup "-tests.factor" vocab/suffix>path
341 set-scaffold-tests-file
346 SYMBOL: nested-examples
348 : example-using ( using -- )
349 join-words "example-using" [
350 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
351 "${example-indent}\"Example:\"
352 ${example-indent}{ $example \"USING: ${example-using} ;\"
353 ${example-indent} \"\"
354 ${example-indent} \"\"
361 : n-examples-using ( n using -- )
362 '[ _ example-using ] times ;
364 : scaffold-n-examples ( n word -- )
365 vocabulary>> "prettyprint" 2array
366 [ t nested-examples ] 2dip
373 : scaffold-examples ( word -- )
374 2 swap scaffold-n-examples ;
376 : scaffold-file ( path -- )
378 [ "Click to edit: " write <pathname> . ] bi ;
380 : scaffold-rc ( path -- )
381 home prepend-path scaffold-file ;
383 : scaffold-factor-boot-rc ( -- )
384 ".factor-boot-rc" scaffold-rc ;
386 : scaffold-factor-rc ( -- )
387 ".factor-rc" scaffold-rc ;
389 : scaffold-mason-rc ( -- )
390 ".factor-mason-rc" scaffold-rc ;
392 : scaffold-factor-roots ( -- )
393 ".factor-roots" scaffold-rc ;
395 : make-unit-test ( answer code -- str )
397 prepend-lines-with-spaces
400 prepend-lines-with-spaces
401 "[\n" "\n] unit-test\n" surround
404 : scaffold-unit-test ( -- str/f )
405 read-contents dup "" = [
408 [ eval( -- x ) unparse ] keep make-unit-test
411 : scaffold-unit-tests ( -- str )
412 [ scaffold-unit-test ] loop>array "\n\n" join ;
414 HOOK: scaffold-emacs os ( -- )
416 M: unix scaffold-emacs ".emacs" scaffold-rc ;