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 combinators combinators.short-circuit fry hashtables
5 help.markup interpolate io io.directories io.encodings.utf8
6 io.files io.pathnames io.streams.string kernel math math.parser
7 namespaces prettyprint quotations sequences sets sorting
8 splitting strings system timers unicode urls vocabs
9 vocabs.loader vocabs.metadata words words.symbol ;
12 SYMBOL: developer-name
15 ERROR: not-a-vocab-root string ;
17 ERROR: vocab-must-not-exist string ;
21 : vocab-root? ( string -- ? )
22 trim-tail-separators vocab-roots get member? ;
24 : ensure-vocab-exists ( string -- string )
25 dup loaded-vocab-names member? [ no-vocab ] unless ;
27 : check-root ( string -- string )
28 dup vocab-root? [ not-a-vocab-root ] unless ;
30 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
31 [ check-root ] [ check-vocab-name ] bi* ;
33 : check-vocab-exists ( string -- string )
34 dup vocab-exists? [ vocab-must-not-exist ] when ;
36 : replace-vocab-separators ( vocab -- path )
37 path-separator first CHAR: . associate substitute ; inline
39 : vocab-root/vocab>path ( vocab-root vocab -- path )
40 check-vocab-root/vocab
41 [ ] [ replace-vocab-separators ] bi* append-path ;
43 : vocab>path ( vocab -- path )
44 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
46 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
47 [ vocab-root/vocab>path ] dip append-path ;
49 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
50 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
52 : vocab/file>path ( vocab file -- path )
53 [ vocab>path ] dip append-path ;
55 : vocab/suffix>path ( vocab suffix -- path )
56 [ vocab>path dup file-name append-path ] dip append ;
58 : directory-exists ( path -- )
59 "Not creating a directory, it already exists: " write print ;
61 : scaffold-directory ( vocab-root vocab -- )
63 dup exists? [ directory-exists ] [ make-directories ] if ;
65 : not-scaffolding ( path -- path )
66 "Not creating scaffolding for " write dup <pathname> . ;
68 : scaffolding ( path -- path )
69 "Creating scaffolding for " write dup <pathname> . ;
71 : scaffolding? ( path -- path ? )
72 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
74 : scaffold-copyright ( -- )
75 "! Copyright (C) " write now year>> number>string write
76 developer-name get [ "Your name" ] unless* bl write "." print
77 "! See http://factorcode.org/license.txt for BSD license." print ;
79 : main-file-string ( vocab -- string )
84 ] with-string-writer ;
86 : set-scaffold-main-file ( vocab path -- )
87 [ main-file-string 1array ] dip utf8 set-file-lines ;
89 : scaffold-main ( vocab-root vocab -- )
90 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
91 set-scaffold-main-file
96 : scaffold-metadata ( vocab file contents -- )
97 [ ensure-vocab-exists ] 2dip
99 [ vocab/file>path ] dip 1array swap scaffolding? [
108 : lookup-type ( string -- object/string ? )
110 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
118 { "hashtable" hashtable }
120 { "ch" "a character" }
123 { "byte-array" byte-array }
125 { "duration" duration }
126 { "path" "a pathname string" }
127 { "vocab" "a vocabulary specifier" }
128 { "vocab-root" "a vocabulary root string" }
130 { "sequence" sequence }
132 { "exemplar" object }
134 { "alist" "an array of key/value pairs" }
135 { "keys" sequence } { "values" sequence }
136 { "class" class } { "tuple" tuple }
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 ;
146 M: object add-using ( object -- )
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 : class-description. ( word -- )
185 drop "{ $class-description \"\" } ;" print ;
187 : symbol-description. ( word -- )
188 drop "{ $var-description \"\" } ;" print ;
190 : $description. ( word -- )
191 drop "{ $description \"\" } ;" print ;
193 : docs-body. ( word/symbol -- )
195 { [ dup class? ] [ class-description. ] }
196 { [ dup symbol? ] [ symbol-description. ] }
197 [ [ $values. ] [ $description. ] bi ]
200 : docs-header. ( word -- )
201 "HELP: " write name>> print ;
203 : (help.) ( word -- )
204 [ docs-header. ] [ docs-body. ] bi ;
206 : interesting-words ( vocab -- array )
208 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
211 : interesting-words. ( vocab -- )
212 interesting-words [ (help.) nl ] each ;
214 : docs-file-string ( vocab -- str2 )
217 [ "IN: " write print nl ]
218 [ interesting-words. ]
220 [ "ARTICLE: " write unparse dup write bl print ]
221 [ "{ $vocab-link " write pprint " }" print ] bi
224 [ "ABOUT: " write unparse print ]
226 ] with-string-writer ;
228 : write-using ( vocab -- )
231 { "help.markup" "help.syntax" } append natural-sort remove
235 : set-scaffold-docs-file ( vocab path -- )
238 [ docs-file-string ] [ write-using ] bi
240 ] with-output-stream ;
242 : with-scaffold ( quot -- )
243 [ HS{ } clone using ] dip with-variable ; inline
245 : link-vocab ( vocab -- )
247 "Edit documentation: " write
248 "-docs.factor" vocab/suffix>path <pathname> . ;
253 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
255 : scaffold-docs ( vocab -- )
258 dup "-docs.factor" vocab/suffix>path scaffolding? [
259 set-scaffold-docs-file
265 : scaffold-undocumented ( string -- )
266 [ interesting-words. ] [ link-vocab ] bi ;
268 : scaffold-authors ( vocab -- )
269 "authors.txt" developer-name get scaffold-metadata ;
271 : scaffold-tags ( vocab tags -- )
272 [ "tags.txt" ] dip scaffold-metadata ;
274 : scaffold-summary ( vocab summary -- )
275 [ "summary.txt" ] dip scaffold-metadata ;
277 : scaffold-platforms ( vocab platforms -- )
278 [ "platforms.txt" ] dip scaffold-metadata ;
280 : scaffold-vocab ( vocab-root string -- )
282 [ scaffold-directory ]
285 [ nip scaffold-authors ]
288 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
290 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
292 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
294 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
298 : tests-file-string ( vocab -- string )
301 "USING: tools.test " write dup write " ;" print
302 "IN: " write write ".tests" print
303 ] with-string-writer ;
305 : set-scaffold-tests-file ( vocab path -- )
306 [ tests-file-string ] dip utf8 set-file-contents ;
310 : scaffold-tests ( vocab -- )
312 dup "-tests.factor" vocab/suffix>path
314 set-scaffold-tests-file
319 SYMBOL: nested-examples
321 : example-using ( using -- )
322 " " join "example-using" [
323 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
324 "${example-indent}\"Example:\"
325 ${example-indent}{ $example \"USING: ${example-using} ;\"
326 ${example-indent} \"\"
327 ${example-indent} \"\"
334 : n-examples-using ( n using -- )
335 '[ _ example-using ] times ;
337 : scaffold-n-examples ( n word -- )
338 vocabulary>> "prettyprint" 2array
339 [ t nested-examples ] 2dip
346 : scaffold-examples ( word -- )
347 2 swap scaffold-n-examples ;
351 [ "Click to edit: " write <pathname> . ] bi ;
353 : scaffold-rc ( path -- )
354 [ home ] dip append-path touch. ;
356 : scaffold-factor-boot-rc ( -- )
357 ".factor-boot-rc" scaffold-rc ;
359 : scaffold-factor-rc ( -- )
360 ".factor-rc" scaffold-rc ;
362 : scaffold-mason-rc ( -- )
363 ".factor-mason-rc" scaffold-rc ;
365 : scaffold-factor-roots ( -- )
366 ".factor-roots" scaffold-rc ;
368 HOOK: scaffold-emacs os ( -- )
370 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;