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 ;
19 : vocab-root? ( string -- ? )
20 trim-tail-separators vocab-roots get member? ;
22 : ensure-vocab-exists ( string -- string )
23 dup loaded-vocab-names member? [ no-vocab ] unless ;
25 : check-root ( string -- string )
26 dup vocab-root? [ not-a-vocab-root ] unless ;
28 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
29 [ check-root ] [ check-vocab-name ] bi* ;
31 : replace-vocab-separators ( vocab -- path )
32 path-separator first CHAR: . associate substitute ; inline
34 : vocab-root/vocab>path ( vocab-root vocab -- path )
35 check-vocab-root/vocab
36 [ ] [ replace-vocab-separators ] bi* append-path ;
38 : vocab>path ( vocab -- path )
39 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
41 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
42 [ vocab-root/vocab>path ] dip append-path ;
44 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
45 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
47 : vocab/file>path ( vocab file -- path )
48 [ vocab>path ] dip append-path ;
50 : vocab/suffix>path ( vocab suffix -- path )
51 [ vocab>path dup file-name append-path ] dip append ;
53 : directory-exists ( path -- )
54 "Not creating a directory, it already exists: " write print ;
56 : scaffold-directory ( vocab-root vocab -- )
58 dup exists? [ directory-exists ] [ make-directories ] if ;
60 : not-scaffolding ( path -- path )
61 "Not creating scaffolding for " write dup <pathname> . ;
63 : scaffolding ( path -- path )
64 "Creating scaffolding for " write dup <pathname> . ;
66 : scaffolding? ( path -- path ? )
67 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
69 : scaffold-copyright ( -- )
70 "! Copyright (C) " write now year>> number>string write
71 developer-name get [ "Your name" ] unless* bl write "." print
72 "! See http://factorcode.org/license.txt for BSD license." print ;
74 : main-file-string ( vocab -- string )
79 ] with-string-writer ;
81 : set-scaffold-main-file ( vocab path -- )
82 [ main-file-string 1array ] dip utf8 set-file-lines ;
84 : scaffold-main ( vocab-root vocab -- )
85 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
86 set-scaffold-main-file
91 : scaffold-metadata ( vocab file contents -- )
92 [ ensure-vocab-exists ] 2dip
94 [ vocab/file>path ] dip 1array swap scaffolding? [
103 : lookup-type ( string -- object/string ? )
105 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
113 { "hashtable" hashtable }
115 { "ch" "a character" }
118 { "byte-array" byte-array }
120 { "duration" duration }
121 { "path" "a pathname string" }
122 { "vocab" "a vocabulary specifier" }
123 { "vocab-root" "a vocabulary root string" }
125 { "sequence" sequence }
127 { "exemplar" object }
129 { "alist" "an array of key/value pairs" }
130 { "keys" sequence } { "values" sequence }
131 { "class" class } { "tuple" tuple }
133 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
135 GENERIC: add-using ( object -- )
137 M: array add-using [ add-using ] each ;
139 M: string add-using drop ;
141 M: object add-using ( object -- )
142 vocabulary>> using get [ adjoin ] [ drop ] if* ;
144 : ($values.) ( array -- )
149 dup array? [ first ] when
152 [ [ pprint ] [ add-using ] bi ] bi*
154 drop unparse write bl null pprint
161 : ?print-nl ( seq1 seq2 -- )
162 [ empty? ] either? [ nl ] unless ;
164 : $values. ( word -- )
165 "declared-effect" word-prop [
166 [ in>> ] [ out>> ] bi
167 2dup [ empty? ] both? [
170 [ members ] dip over diff
174 [ nip ($values.) ] 2tri
179 : class-description. ( word -- )
180 drop "{ $class-description \"\" } ;" print ;
182 : symbol-description. ( word -- )
183 drop "{ $var-description \"\" } ;" print ;
185 : $description. ( word -- )
186 drop "{ $description \"\" } ;" print ;
188 : docs-body. ( word/symbol -- )
190 { [ dup class? ] [ class-description. ] }
191 { [ dup symbol? ] [ symbol-description. ] }
192 [ [ $values. ] [ $description. ] bi ]
195 : docs-header. ( word -- )
196 "HELP: " write name>> print ;
198 : (help.) ( word -- )
199 [ docs-header. ] [ docs-body. ] bi ;
201 : interesting-words ( vocab -- array )
203 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
206 : interesting-words. ( vocab -- )
207 interesting-words [ (help.) nl ] each ;
209 : docs-file-string ( vocab -- str2 )
212 [ "IN: " write print nl ]
213 [ interesting-words. ]
215 [ "ARTICLE: " write unparse dup write bl print ]
216 [ "{ $vocab-link " write pprint " }" print ] bi
219 [ "ABOUT: " write unparse print ]
221 ] with-string-writer ;
223 : write-using ( vocab -- )
226 { "help.markup" "help.syntax" } append natural-sort remove
230 : set-scaffold-docs-file ( vocab path -- )
233 [ docs-file-string ] [ write-using ] bi
235 ] with-output-stream ;
237 : with-scaffold ( quot -- )
238 [ HS{ } clone using ] dip with-variable ; inline
240 : link-vocab ( vocab -- )
242 "Edit documentation: " write
243 "-docs.factor" vocab/suffix>path <pathname> . ;
248 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
250 : scaffold-docs ( vocab -- )
253 dup "-docs.factor" vocab/suffix>path scaffolding? [
254 set-scaffold-docs-file
260 : scaffold-undocumented ( string -- )
261 [ interesting-words. ] [ link-vocab ] bi ;
263 : scaffold-authors ( vocab -- )
264 "authors.txt" developer-name get scaffold-metadata ;
266 : scaffold-tags ( vocab tags -- )
267 [ "tags.txt" ] dip scaffold-metadata ;
269 : scaffold-summary ( vocab summary -- )
270 [ "summary.txt" ] dip scaffold-metadata ;
272 : scaffold-platforms ( vocab platforms -- )
273 [ "platforms.txt" ] dip scaffold-metadata ;
275 : scaffold-vocab ( vocab-root string -- )
277 [ scaffold-directory ]
280 [ nip scaffold-authors ]
283 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
285 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
287 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
289 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
293 : tests-file-string ( vocab -- string )
296 "USING: tools.test " write dup write " ;" print
297 "IN: " write write ".tests" print
298 ] with-string-writer ;
300 : set-scaffold-tests-file ( vocab path -- )
301 [ tests-file-string ] dip utf8 set-file-contents ;
305 : scaffold-tests ( vocab -- )
307 dup "-tests.factor" vocab/suffix>path
309 set-scaffold-tests-file
314 SYMBOL: nested-examples
316 : example-using ( using -- )
317 " " join "example-using" [
318 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
319 "${example-indent}\"Example:\"
320 ${example-indent}{ $example \"USING: ${example-using} ;\"
321 ${example-indent} \"\"
322 ${example-indent} \"\"
329 : n-examples-using ( n using -- )
330 '[ _ example-using ] times ;
332 : scaffold-n-examples ( n word -- )
333 vocabulary>> "prettyprint" 2array
334 [ t nested-examples ] 2dip
341 : scaffold-examples ( word -- )
342 2 swap scaffold-n-examples ;
346 [ "Click to edit: " write <pathname> . ] bi ;
348 : scaffold-rc ( path -- )
349 [ home ] dip append-path touch. ;
351 : scaffold-factor-boot-rc ( -- )
352 ".factor-boot-rc" scaffold-rc ;
354 : scaffold-factor-rc ( -- )
355 ".factor-rc" scaffold-rc ;
357 : scaffold-mason-rc ( -- )
358 ".factor-mason-rc" scaffold-rc ;
360 : scaffold-factor-roots ( -- )
361 ".factor-roots" scaffold-rc ;
363 HOOK: scaffold-emacs os ( -- )
365 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;