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 hashtables help.markup interpolate io io.directories
7 io.encodings.utf8 io.files io.pathnames io.streams.string kernel
8 math math.parser math.ranges namespaces prettyprint quotations
9 sequences sets sorting splitting strings system timers unicode
10 urls vocabs vocabs.loader 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 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 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 }
130 { "exemplar" object }
132 { "alist" "an array of key/value pairs" }
134 { "values" sequence }
138 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
140 GENERIC: add-using ( object -- )
142 M: array add-using [ add-using ] each ;
144 M: string add-using drop ;
147 vocabulary>> using get [ adjoin ] [ drop ] if* ;
149 : ($values.) ( array -- )
154 dup array? [ first ] when
157 [ [ pprint ] [ add-using ] bi ] bi*
159 drop unparse write bl null pprint
166 : ?print-nl ( seq1 seq2 -- )
167 [ empty? ] either? [ nl ] unless ;
169 : $values. ( word -- )
170 "declared-effect" word-prop [
171 [ in>> ] [ out>> ] bi
172 2dup [ empty? ] both? [
175 [ members ] dip over diff
179 [ nip ($values.) ] 2tri
184 : error-description. ( word -- )
186 "{ $description \"Throws " write
187 name>> dup a/an write " \" { $link " write
188 write " } \" error.\" }" print
189 ] bi "{ $error-description \"\" } ;" print ;
191 : class-description. ( word -- )
192 drop "{ $class-description \"\" } ;" print ;
194 : symbol-description. ( word -- )
195 drop "{ $var-description \"\" } ;" print ;
197 : $description. ( word -- )
198 drop "{ $description \"\" } ;" print ;
200 : docs-body. ( word/symbol -- )
202 { [ dup error-class? ] [ error-description. ] }
203 { [ dup class? ] [ class-description. ] }
204 { [ dup symbol? ] [ symbol-description. ] }
205 [ [ $values. ] [ $description. ] bi ]
208 : docs-header. ( word -- )
209 "HELP: " write name>> print ;
211 : (help.) ( word -- )
212 [ docs-header. ] [ docs-body. ] bi ;
214 : interesting-words ( vocab -- array )
216 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
219 : interesting-words. ( vocab -- )
220 interesting-words [ (help.) nl ] each ;
222 : docs-file-string ( vocab -- str2 )
225 [ "IN: " write print nl ]
226 [ interesting-words. ]
228 [ "ARTICLE: " write unparse dup write bl print ]
229 [ "{ $vocab-link " write pprint " }" print ] bi
232 [ "ABOUT: " write unparse print ]
234 ] with-string-writer ;
236 : write-using ( vocab -- )
239 { "help.markup" "help.syntax" } append natural-sort remove
243 : set-scaffold-docs-file ( vocab path -- )
246 [ docs-file-string ] [ write-using ] bi
248 ] with-output-stream ;
250 : with-scaffold ( quot -- )
251 [ HS{ } clone using ] dip with-variable ; inline
253 : link-vocab ( vocab -- )
255 "Edit documentation: " write
256 "-docs.factor" vocab/suffix>path <pathname> . ;
261 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
263 : scaffold-docs ( vocab -- )
266 dup "-docs.factor" vocab/suffix>path scaffolding? [
267 set-scaffold-docs-file
273 : scaffold-undocumented ( string -- )
274 [ interesting-words. ] [ link-vocab ] bi ;
276 : scaffold-authors ( vocab -- )
277 "authors.txt" developer-name get scaffold-metadata ;
279 : scaffold-tags ( vocab tags -- )
280 [ "tags.txt" ] dip scaffold-metadata ;
282 : scaffold-summary ( vocab summary -- )
283 [ "summary.txt" ] dip scaffold-metadata ;
285 : scaffold-platforms ( vocab platforms -- )
286 [ "platforms.txt" ] dip scaffold-metadata ;
288 : delete-from-root-cache ( string -- )
289 root-cache get delete-at ;
291 : scaffold-vocab-in ( vocab-root string -- )
292 dup delete-from-root-cache
294 [ scaffold-directory ]
297 [ nip scaffold-authors ]
300 : scaffold-core ( string -- )
301 "resource:core" swap scaffold-vocab-in ;
303 : scaffold-basis ( string -- )
304 "resource:basis" swap scaffold-vocab-in ;
306 : scaffold-extra ( string -- )
307 "resource:extra" swap scaffold-vocab-in ;
309 : scaffold-work ( string -- )
310 "resource:work" swap scaffold-vocab-in ;
314 : find-vocab-root-for ( string -- vocab-root/f )
315 "." split dup length [1,b) [ head "." join ] with map
316 [ find-vocab-root ] map-find-last drop ;
320 : scaffold-vocab ( string -- )
321 [ find-vocab-root-for ] [ scaffold-vocab-in ] bi ;
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 "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 ] dip append-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 HOOK: scaffold-emacs os ( -- )
397 M: unix scaffold-emacs ".emacs" scaffold-rc ;