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.categories urls vocabs
9 vocabs.loader vocabs.metadata words words.symbol ;
10 FROM: sets => members ;
13 SYMBOL: developer-name
16 ERROR: not-a-vocab-root string ;
20 : vocab-root? ( string -- ? )
22 vocab-roots get member? ;
24 : contains-dot? ( string -- ? ) ".." swap subseq? ;
26 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
28 : ensure-vocab-exists ( string -- string )
29 dup loaded-vocab-names member? [ no-vocab ] unless ;
31 : check-root ( string -- string )
32 dup vocab-root? [ not-a-vocab-root ] unless ;
34 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
35 [ check-root ] [ check-vocab-name ] bi* ;
37 : replace-vocab-separators ( vocab -- path )
38 path-separator first CHAR: . associate substitute ; inline
40 : vocab-root/vocab>path ( vocab-root vocab -- path )
41 check-vocab-root/vocab
42 [ ] [ replace-vocab-separators ] bi* append-path ;
44 : vocab>path ( vocab -- path )
46 [ find-vocab-root ] keep vocab-root/vocab>path ;
48 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
49 [ vocab-root/vocab>path ] dip append-path ;
51 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
52 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
54 : vocab/file>path ( vocab file -- path )
55 [ vocab>path ] dip append-path ;
57 : vocab/suffix>path ( vocab suffix -- path )
58 [ vocab>path dup file-name append-path ] dip append ;
60 : directory-exists ( path -- )
61 "Not creating a directory, it already exists: " write print ;
63 : scaffold-directory ( vocab-root vocab -- )
65 dup exists? [ directory-exists ] [ make-directories ] if ;
67 : not-scaffolding ( path -- path )
68 "Not creating scaffolding for " write dup <pathname> . ;
70 : scaffolding ( path -- path )
71 "Creating scaffolding for " write dup <pathname> . ;
73 : scaffolding? ( path -- path ? )
74 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
76 : scaffold-copyright ( -- )
77 "! Copyright (C) " write now year>> number>string write
78 developer-name get [ "Your name" ] unless* bl write "." print
79 "! See http://factorcode.org/license.txt for BSD license." print ;
81 : main-file-string ( vocab -- string )
86 ] with-string-writer ;
88 : set-scaffold-main-file ( vocab path -- )
89 [ main-file-string 1array ] dip utf8 set-file-lines ;
91 : scaffold-main ( vocab-root vocab -- )
92 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
93 set-scaffold-main-file
98 : scaffold-metadata ( vocab file contents -- )
99 [ ensure-vocab-exists ] 2dip
101 [ vocab/file>path ] dip 1array swap scaffolding? [
110 : lookup-type ( string -- object/string ? )
112 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
120 { "hashtable" hashtable }
122 { "ch" "a character" }
125 { "byte-array" byte-array }
127 { "duration" duration }
128 { "path" "a pathname string" }
129 { "vocab" "a vocabulary specifier" }
130 { "vocab-root" "a vocabulary root string" }
132 { "sequence" sequence }
134 { "exemplar" object }
136 { "alist" "an array of key/value pairs" }
137 { "keys" sequence } { "values" sequence }
138 { "class" class } { "tuple" tuple }
140 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
142 GENERIC: add-using ( object -- )
144 M: array add-using [ add-using ] each ;
146 M: string add-using drop ;
148 M: object add-using ( object -- )
149 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 null 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 : symbol-description. ( word -- )
191 "{ $var-description \"\" } ;" print ;
193 : $description. ( word -- )
195 "{ $description \"\" } ;" print ;
197 : docs-body. ( word/symbol -- )
201 [ $values. ] [ $description. ] bi
204 : docs-header. ( word -- )
205 "HELP: " write name>> print ;
207 : (help.) ( word -- )
208 [ docs-header. ] [ docs-body. ] bi ;
210 : interesting-words ( vocab -- array )
212 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
215 : interesting-words. ( vocab -- )
216 interesting-words [ (help.) nl ] each ;
218 : docs-file-string ( vocab -- str2 )
221 [ "IN: " write print nl ]
222 [ interesting-words. ]
224 [ "ARTICLE: " write unparse dup write bl print ]
225 [ "{ $vocab-link " write pprint " }" print ] bi
228 [ "ABOUT: " write unparse print ]
230 ] with-string-writer ;
232 : write-using ( vocab -- )
235 { "help.markup" "help.syntax" } append natural-sort remove
239 : set-scaffold-docs-file ( vocab path -- )
242 [ docs-file-string ] [ write-using ] bi
244 ] with-output-stream ;
246 : with-scaffold ( quot -- )
247 [ HS{ } clone using ] dip with-variable ; inline
249 : link-vocab ( vocab -- )
251 "Edit documentation: " write
252 "-docs.factor" vocab/suffix>path <pathname> . ;
257 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
259 : scaffold-docs ( vocab -- )
262 dup "-docs.factor" vocab/suffix>path scaffolding? [
263 set-scaffold-docs-file
269 : scaffold-undocumented ( string -- )
270 [ interesting-words. ] [ link-vocab ] bi ;
272 : scaffold-authors ( vocab -- )
273 "authors.txt" developer-name get scaffold-metadata ;
275 : scaffold-tags ( vocab tags -- )
276 [ "tags.txt" ] dip scaffold-metadata ;
278 : scaffold-summary ( vocab summary -- )
279 [ "summary.txt" ] dip scaffold-metadata ;
281 : scaffold-platforms ( vocab platforms -- )
282 [ "platforms.txt" ] dip scaffold-metadata ;
284 : scaffold-vocab ( vocab-root string -- )
286 [ scaffold-directory ]
289 [ nip scaffold-authors ]
292 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
294 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
296 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
298 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
302 : tests-file-string ( vocab -- string )
305 "USING: tools.test " write dup write " ;" print
306 "IN: " write write ".tests" print
307 ] with-string-writer ;
309 : set-scaffold-tests-file ( vocab path -- )
310 [ tests-file-string ] dip utf8 set-file-contents ;
314 : scaffold-tests ( vocab -- )
316 dup "-tests.factor" vocab/suffix>path
318 set-scaffold-tests-file
323 SYMBOL: nested-examples
325 : example-using ( using -- )
326 " " join "example-using" [
327 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
328 """${example-indent}"Example:"
329 ${example-indent}{ $example "USING: ${example-using} ;"
338 : n-examples-using ( n using -- )
339 '[ _ example-using ] times ;
341 : scaffold-n-examples ( n word -- )
342 vocabulary>> "prettyprint" 2array
343 [ t nested-examples ] 2dip
350 : scaffold-examples ( word -- )
351 2 swap scaffold-n-examples ;
355 [ "Click to edit: " write <pathname> . ] bi ;
357 : scaffold-rc ( path -- )
358 [ home ] dip append-path touch. ;
360 : scaffold-factor-boot-rc ( -- )
361 ".factor-boot-rc" scaffold-rc ;
363 : scaffold-factor-rc ( -- )
364 ".factor-rc" scaffold-rc ;
366 : scaffold-mason-rc ( -- )
367 ".factor-mason-rc" scaffold-rc ;
369 : scaffold-factor-roots ( -- )
370 ".factor-roots" scaffold-rc ;
372 HOOK: scaffold-emacs os ( -- )
374 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;