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 ;
12 SYMBOL: developer-name
15 ERROR: not-a-vocab-root string ;
19 : vocab-root? ( string -- ? )
21 vocab-roots get member? ;
23 : contains-dot? ( string -- ? ) ".." swap subseq? ;
25 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
27 : ensure-vocab-exists ( string -- string )
28 dup loaded-vocab-names member? [ throw-no-vocab ] unless ;
30 : check-root ( string -- string )
31 dup vocab-root? [ throw-not-a-vocab-root ] unless ;
33 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
34 [ check-root ] [ check-vocab-name ] bi* ;
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 )
45 [ find-vocab-root ] keep vocab-root/vocab>path ;
47 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
48 [ vocab-root/vocab>path ] dip append-path ;
50 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
51 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
53 : vocab/file>path ( vocab file -- path )
54 [ vocab>path ] dip append-path ;
56 : vocab/suffix>path ( vocab suffix -- path )
57 [ vocab>path dup file-name append-path ] dip append ;
59 : directory-exists ( path -- )
60 "Not creating a directory, it already exists: " write print ;
62 : scaffold-directory ( vocab-root vocab -- )
64 dup exists? [ directory-exists ] [ make-directories ] if ;
66 : not-scaffolding ( path -- path )
67 "Not creating scaffolding for " write dup <pathname> . ;
69 : scaffolding ( path -- path )
70 "Creating scaffolding for " write dup <pathname> . ;
72 : scaffolding? ( path -- path ? )
73 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
75 : scaffold-copyright ( -- )
76 "! Copyright (C) " write now year>> number>string write
77 developer-name get [ "Your name" ] unless* bl write "." print
78 "! See http://factorcode.org/license.txt for BSD license." print ;
80 : main-file-string ( vocab -- string )
85 ] with-string-writer ;
87 : set-scaffold-main-file ( vocab path -- )
88 [ main-file-string 1array ] dip utf8 set-file-lines ;
90 : scaffold-main ( vocab-root vocab -- )
91 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
92 set-scaffold-main-file
97 : scaffold-metadata ( vocab file contents -- )
98 [ ensure-vocab-exists ] 2dip
100 [ vocab/file>path ] dip 1array swap scaffolding? [
109 : lookup-type ( string -- object/string ? )
111 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
119 { "hashtable" hashtable }
121 { "ch" "a character" }
124 { "byte-array" byte-array }
126 { "duration" duration }
127 { "path" "a pathname string" }
128 { "vocab" "a vocabulary specifier" }
129 { "vocab-root" "a vocabulary root string" }
131 { "sequence" sequence }
133 { "exemplar" object }
135 { "alist" "an array of key/value pairs" }
136 { "keys" sequence } { "values" sequence }
137 { "class" class } { "tuple" tuple }
139 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
141 GENERIC: add-using ( object -- )
143 M: array add-using [ add-using ] each ;
145 M: string add-using drop ;
147 M: object add-using ( object -- )
148 vocabulary>> using get [ adjoin ] [ drop ] if* ;
153 : ($values.) ( array -- )
158 dup array? [ first ] when
161 [ [ pprint ] [ add-using ] bi ] bi*
163 drop unparse write bl null pprint
170 : ?print-nl ( seq1 seq2 -- )
171 [ empty? ] either? [ nl ] unless ;
173 : $values. ( word -- )
174 "declared-effect" word-prop [
175 [ in>> ] [ out>> ] bi
176 2dup [ empty? ] both? [
179 [ members ] dip over diff
183 [ nip ($values.) ] 2tri
188 : symbol-description. ( word -- )
190 "{ $var-description \"\" } ;" print ;
192 : $description. ( word -- )
194 "{ $description \"\" } ;" print ;
196 : docs-body. ( word/symbol -- )
200 [ $values. ] [ $description. ] bi
203 : docs-header. ( word -- )
204 "HELP: " write name>> print ;
206 : (help.) ( word -- )
207 [ docs-header. ] [ docs-body. ] bi ;
209 : interesting-words ( vocab -- array )
211 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
214 : interesting-words. ( vocab -- )
215 interesting-words [ (help.) nl ] each ;
217 : docs-file-string ( vocab -- str2 )
220 [ "IN: " write print nl ]
221 [ interesting-words. ]
223 [ "ARTICLE: " write unparse dup write bl print ]
224 [ "{ $vocab-link " write pprint " }" print ] bi
227 [ "ABOUT: " write unparse print ]
229 ] with-string-writer ;
231 : write-using ( vocab -- )
234 { "help.markup" "help.syntax" } append natural-sort remove
238 : set-scaffold-docs-file ( vocab path -- )
241 [ docs-file-string ] [ write-using ] bi
243 ] with-output-stream ;
245 : with-scaffold ( quot -- )
246 [ HS{ } clone using ] dip with-variable ; inline
248 : link-vocab ( vocab -- )
250 "Edit documentation: " write
251 "-docs.factor" vocab/suffix>path <pathname> . ;
256 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
258 : scaffold-docs ( vocab -- )
261 dup "-docs.factor" vocab/suffix>path scaffolding? [
262 set-scaffold-docs-file
268 : scaffold-undocumented ( string -- )
269 [ interesting-words. ] [ link-vocab ] bi ;
271 : scaffold-authors ( vocab -- )
272 "authors.txt" developer-name get scaffold-metadata ;
274 : scaffold-tags ( vocab tags -- )
275 [ "tags.txt" ] dip scaffold-metadata ;
277 : scaffold-summary ( vocab summary -- )
278 [ "summary.txt" ] dip scaffold-metadata ;
280 : scaffold-platforms ( vocab platforms -- )
281 [ "platforms.txt" ] dip scaffold-metadata ;
283 : scaffold-vocab ( vocab-root string -- )
285 [ scaffold-directory ]
288 [ nip scaffold-authors ]
291 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
293 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
295 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
297 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
301 : tests-file-string ( vocab -- string )
304 "USING: tools.test " write dup write " ;" print
305 "IN: " write write ".tests" print
306 ] with-string-writer ;
308 : set-scaffold-tests-file ( vocab path -- )
309 [ tests-file-string ] dip utf8 set-file-contents ;
313 : scaffold-tests ( vocab -- )
315 dup "-tests.factor" vocab/suffix>path
317 set-scaffold-tests-file
322 SYMBOL: nested-examples
324 : example-using ( using -- )
325 " " join "example-using" [
326 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
327 "${example-indent}\"Example:\"
328 ${example-indent}{ $example \"USING: ${example-using} ;\"
329 ${example-indent} \"\"
330 ${example-indent} \"\"
337 : n-examples-using ( n using -- )
338 '[ _ example-using ] times ;
340 : scaffold-n-examples ( n word -- )
341 vocabulary>> "prettyprint" 2array
342 [ t nested-examples ] 2dip
349 : scaffold-examples ( word -- )
350 2 swap scaffold-n-examples ;
354 [ "Click to edit: " write <pathname> . ] bi ;
356 : scaffold-rc ( path -- )
357 [ home ] dip append-path touch. ;
359 : scaffold-factor-boot-rc ( -- )
360 ".factor-boot-rc" scaffold-rc ;
362 : scaffold-factor-rc ( -- )
363 ".factor-rc" scaffold-rc ;
365 : scaffold-mason-rc ( -- )
366 ".factor-mason-rc" scaffold-rc ;
368 : scaffold-factor-roots ( -- )
369 ".factor-roots" scaffold-rc ;
371 HOOK: scaffold-emacs os ( -- )
373 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;