1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs calendar classes
4 combinators combinators.short-circuit fry hashtables interpolate
5 io io.directories io.encodings.utf8 io.files io.pathnames
6 io.streams.string kernel math math.parser namespaces prettyprint
7 quotations sequences sets sorting splitting strings system
8 timers unicode.categories urls vocabs vocabs.loader
9 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 ? )
118 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
120 { "object" object } { "obj" object }
125 { "hashtable" hashtable }
127 { "ch" "a character" }
131 { "duration" duration }
132 { "path" "a pathname string" }
133 { "vocab" "a vocabulary specifier" }
134 { "vocab-root" "a vocabulary root string" }
137 { "exemplar" object }
139 { "alist" "an array of key/value pairs" }
140 { "keys" sequence } { "values" sequence }
141 { "class" class } { "tuple" tuple }
145 : add-using ( object -- )
146 vocabulary>> using get [ adjoin ] [ drop ] if* ;
151 : ($values.) ( array -- )
156 dup array? [ first ] when
159 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
161 drop unparse write bl null pprint
168 : ?print-nl ( seq1 seq2 -- )
169 [ empty? ] either? [ nl ] unless ;
171 : $values. ( word -- )
172 "declared-effect" word-prop [
173 [ in>> ] [ out>> ] bi
174 2dup [ empty? ] both? [
177 [ members ] dip over diff
181 [ nip ($values.) ] 2tri
186 : symbol-description. ( word -- )
188 "{ $var-description \"\" } ;" print ;
190 : $description. ( word -- )
192 "{ $description \"\" } ;" print ;
194 : docs-body. ( word/symbol -- )
198 [ $values. ] [ $description. ] bi
201 : docs-header. ( word -- )
202 "HELP: " write name>> print ;
204 : (help.) ( word -- )
205 [ docs-header. ] [ docs-body. ] bi ;
207 : interesting-words ( vocab -- array )
209 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
212 : interesting-words. ( vocab -- )
213 interesting-words [ (help.) nl ] each ;
215 : docs-file-string ( vocab -- str2 )
218 [ "IN: " write print nl ]
219 [ interesting-words. ]
221 [ "ARTICLE: " write unparse dup write bl print ]
222 [ "{ $vocab-link " write pprint " }" print ] bi
225 [ "ABOUT: " write unparse print ]
227 ] with-string-writer ;
229 : write-using ( vocab -- )
232 { "help.markup" "help.syntax" } append natural-sort remove
236 : set-scaffold-docs-file ( vocab path -- )
239 [ docs-file-string ] [ write-using ] bi
241 ] with-output-stream ;
243 : with-scaffold ( quot -- )
244 [ HS{ } clone using ] dip with-variable ; inline
246 : link-vocab ( vocab -- )
248 "Edit documentation: " write
249 "-docs.factor" vocab/suffix>path <pathname> . ;
254 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
256 : scaffold-docs ( vocab -- )
259 dup "-docs.factor" vocab/suffix>path scaffolding? [
260 set-scaffold-docs-file
266 : scaffold-undocumented ( string -- )
267 [ interesting-words. ] [ link-vocab ] bi ;
269 : scaffold-authors ( vocab -- )
270 "authors.txt" developer-name get scaffold-metadata ;
272 : scaffold-tags ( vocab tags -- )
273 [ "tags.txt" ] dip scaffold-metadata ;
275 : scaffold-summary ( vocab summary -- )
276 [ "summary.txt" ] dip scaffold-metadata ;
278 : scaffold-platforms ( vocab platforms -- )
279 [ "platforms.txt" ] dip scaffold-metadata ;
281 : scaffold-vocab ( vocab-root string -- )
283 [ scaffold-directory ]
286 [ nip scaffold-authors ]
289 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
291 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
293 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
295 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
299 : tests-file-string ( vocab -- string )
302 "USING: tools.test " write dup write " ;" print
303 "IN: " write write ".tests" print
304 ] with-string-writer ;
306 : set-scaffold-tests-file ( vocab path -- )
307 [ tests-file-string ] dip utf8 set-file-contents ;
311 : scaffold-tests ( vocab -- )
313 dup "-tests.factor" vocab/suffix>path
315 set-scaffold-tests-file
320 SYMBOL: nested-examples
322 : example-using ( using -- )
323 " " join "example-using" [
324 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
325 """${example-indent}"Example:"
326 ${example-indent}{ $example "USING: ${example-using} ;"
335 : n-examples-using ( n using -- )
336 '[ _ example-using ] times ;
338 : scaffold-n-examples ( n word -- )
339 vocabulary>> "prettyprint" 2array
340 [ t nested-examples ] 2dip
347 : scaffold-examples ( word -- )
348 2 swap scaffold-n-examples ;
352 [ "Click to edit: " write <pathname> . ] bi ;
354 : scaffold-rc ( path -- )
355 [ home ] dip append-path touch. ;
357 : scaffold-factor-boot-rc ( -- )
358 ".factor-boot-rc" scaffold-rc ;
360 : scaffold-factor-rc ( -- )
361 ".factor-rc" scaffold-rc ;
363 : scaffold-mason-rc ( -- )
364 ".factor-mason-rc" scaffold-rc ;
366 : scaffold-factor-roots ( -- )
367 ".factor-roots" scaffold-rc ;
369 HOOK: scaffold-emacs os ( -- )
371 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;