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 ;
17 ERROR: vocab-name-contains-separator path ;
18 ERROR: vocab-name-contains-dot path ;
22 : vocab-root? ( string -- ? )
24 vocab-roots get member? ;
26 : contains-dot? ( string -- ? ) ".." swap subseq? ;
28 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
30 : ensure-vocab-exists ( string -- string )
31 dup vocabs member? [ no-vocab ] unless ;
33 : check-vocab-name ( string -- string )
35 [ contains-dot? [ vocab-name-contains-dot ] when ]
36 [ contains-separator? [ vocab-name-contains-separator ] when ] tri ;
38 : check-root ( string -- string )
39 dup vocab-root? [ not-a-vocab-root ] unless ;
41 : check-vocab-root/vocab ( vocab-root string -- vocab-root string )
42 [ check-root ] [ check-vocab-name ] bi* ;
44 : replace-vocab-separators ( vocab -- path )
45 path-separator first CHAR: . associate substitute ; inline
47 : vocab-root/vocab>path ( vocab-root vocab -- path )
48 check-vocab-root/vocab
49 [ ] [ replace-vocab-separators ] bi* append-path ;
51 : vocab>path ( vocab -- path )
53 [ find-vocab-root ] keep vocab-root/vocab>path ;
55 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
56 [ vocab-root/vocab>path ] dip append-path ;
58 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
59 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
61 : vocab/file>path ( vocab file -- path )
62 [ vocab>path ] dip append-path ;
64 : vocab/suffix>path ( vocab suffix -- path )
65 [ vocab>path dup file-name append-path ] dip append ;
67 : directory-exists ( path -- )
68 "Not creating a directory, it already exists: " write print ;
70 : scaffold-directory ( vocab-root vocab -- )
72 dup exists? [ directory-exists ] [ make-directories ] if ;
74 : not-scaffolding ( path -- path )
75 "Not creating scaffolding for " write dup <pathname> . ;
77 : scaffolding ( path -- path )
78 "Creating scaffolding for " write dup <pathname> . ;
80 : scaffolding? ( path -- path ? )
81 dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
83 : scaffold-copyright ( -- )
84 "! Copyright (C) " write now year>> number>string write
85 developer-name get [ "Your name" ] unless* bl write "." print
86 "! See http://factorcode.org/license.txt for BSD license." print ;
88 : main-file-string ( vocab -- string )
93 ] with-string-writer ;
95 : set-scaffold-main-file ( vocab path -- )
96 [ main-file-string 1array ] dip utf8 set-file-lines ;
98 : scaffold-main ( vocab-root vocab -- )
99 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
100 set-scaffold-main-file
105 : scaffold-metadata ( vocab file contents -- )
106 [ ensure-vocab-exists ] 2dip
108 [ vocab/file>path ] dip 1array swap scaffolding? [
117 : lookup-type ( string -- object/string ? )
119 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
127 { "hashtable" hashtable }
129 { "ch" "a character" }
132 { "byte-array" byte-array }
134 { "duration" duration }
135 { "path" "a pathname string" }
136 { "vocab" "a vocabulary specifier" }
137 { "vocab-root" "a vocabulary root string" }
139 { "sequence" sequence }
141 { "exemplar" object }
143 { "alist" "an array of key/value pairs" }
144 { "keys" sequence } { "values" sequence }
145 { "class" class } { "tuple" tuple }
147 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
149 : add-using ( object -- )
150 vocabulary>> using get [ adjoin ] [ drop ] if* ;
155 : ($values.) ( array -- )
160 dup array? [ first ] when
163 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
165 drop unparse write bl null pprint
172 : ?print-nl ( seq1 seq2 -- )
173 [ empty? ] either? [ nl ] unless ;
175 : $values. ( word -- )
176 "declared-effect" word-prop [
177 [ in>> ] [ out>> ] bi
178 2dup [ empty? ] both? [
181 [ members ] dip over diff
185 [ nip ($values.) ] 2tri
190 : symbol-description. ( word -- )
192 "{ $var-description \"\" } ;" print ;
194 : $description. ( word -- )
196 "{ $description \"\" } ;" print ;
198 : docs-body. ( word/symbol -- )
202 [ $values. ] [ $description. ] bi
205 : docs-header. ( word -- )
206 "HELP: " write name>> print ;
208 : (help.) ( word -- )
209 [ docs-header. ] [ docs-body. ] bi ;
211 : interesting-words ( vocab -- array )
213 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
216 : interesting-words. ( vocab -- )
217 interesting-words [ (help.) nl ] each ;
219 : docs-file-string ( vocab -- str2 )
222 [ "IN: " write print nl ]
223 [ interesting-words. ]
225 [ "ARTICLE: " write unparse dup write bl print ]
226 [ "{ $vocab-link " write pprint " }" print ] bi
229 [ "ABOUT: " write unparse print ]
231 ] with-string-writer ;
233 : write-using ( vocab -- )
236 { "help.markup" "help.syntax" } append natural-sort remove
240 : set-scaffold-docs-file ( vocab path -- )
243 [ docs-file-string ] [ write-using ] bi
245 ] with-output-stream ;
247 : with-scaffold ( quot -- )
248 [ HS{ } clone using ] dip with-variable ; inline
250 : link-vocab ( vocab -- )
252 "Edit documentation: " write
253 "-docs.factor" vocab/suffix>path <pathname> . ;
258 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
260 : scaffold-docs ( vocab -- )
263 dup "-docs.factor" vocab/suffix>path scaffolding? [
264 set-scaffold-docs-file
270 : scaffold-undocumented ( string -- )
271 [ interesting-words. ] [ link-vocab ] bi ;
273 : scaffold-authors ( vocab -- )
274 "authors.txt" developer-name get scaffold-metadata ;
276 : scaffold-tags ( vocab tags -- )
277 [ "tags.txt" ] dip scaffold-metadata ;
279 : scaffold-summary ( vocab summary -- )
280 [ "summary.txt" ] dip scaffold-metadata ;
282 : scaffold-platforms ( vocab platforms -- )
283 [ "platforms.txt" ] dip scaffold-metadata ;
285 : scaffold-vocab ( vocab-root string -- )
287 [ scaffold-directory ]
290 [ nip scaffold-authors ]
293 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
295 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
297 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
299 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
303 : tests-file-string ( vocab -- string )
306 "USING: tools.test " write dup write " ;" print
307 "IN: " write write ".tests" print
308 ] with-string-writer ;
310 : set-scaffold-tests-file ( vocab path -- )
311 [ tests-file-string ] dip utf8 set-file-contents ;
315 : scaffold-tests ( vocab -- )
317 dup "-tests.factor" vocab/suffix>path
319 set-scaffold-tests-file
324 SYMBOL: nested-examples
326 : example-using ( using -- )
327 " " join "example-using" [
328 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
329 """${example-indent}"Example:"
330 ${example-indent}{ $example "USING: ${example-using} ;"
339 : n-examples-using ( n using -- )
340 '[ _ example-using ] times ;
342 : scaffold-n-examples ( n word -- )
343 vocabulary>> "prettyprint" 2array
344 [ t nested-examples ] 2dip
351 : scaffold-examples ( word -- )
352 2 swap scaffold-n-examples ;
356 [ "Click to edit: " write <pathname> . ] bi ;
358 : scaffold-rc ( path -- )
359 [ home ] dip append-path touch. ;
361 : scaffold-factor-boot-rc ( -- )
362 ".factor-boot-rc" scaffold-rc ;
364 : scaffold-factor-rc ( -- )
365 ".factor-rc" scaffold-rc ;
367 : scaffold-mason-rc ( -- )
368 ".factor-mason-rc" scaffold-rc ;
370 : scaffold-factor-roots ( -- )
371 ".factor-roots" scaffold-rc ;
373 HOOK: scaffold-emacs os ( -- )
375 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;