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 -- ? )
21 vocab-roots get member? ;
23 : contains-dot? ( string -- ? ) ".." subseq? ;
25 : contains-separator? ( string -- ? ) [ path-separator? ] any? ;
27 : ensure-vocab-exists ( string -- string )
28 dup loaded-vocab-names member? [ no-vocab ] unless ;
30 : check-root ( string -- string )
31 dup vocab-root? [ 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 : class-description. ( word -- )
190 "{ $class-description \"\" } ;" print ;
192 : symbol-description. ( word -- )
194 "{ $var-description \"\" } ;" print ;
196 : $description. ( word -- )
198 "{ $description \"\" } ;" print ;
200 : docs-body. ( word/symbol -- )
202 { [ dup class? ] [ class-description. ] }
203 { [ dup symbol? ] [ symbol-description. ] }
204 [ [ $values. ] [ $description. ] bi ]
207 : docs-header. ( word -- )
208 "HELP: " write name>> print ;
210 : (help.) ( word -- )
211 [ docs-header. ] [ docs-body. ] bi ;
213 : interesting-words ( vocab -- array )
215 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
218 : interesting-words. ( vocab -- )
219 interesting-words [ (help.) nl ] each ;
221 : docs-file-string ( vocab -- str2 )
224 [ "IN: " write print nl ]
225 [ interesting-words. ]
227 [ "ARTICLE: " write unparse dup write bl print ]
228 [ "{ $vocab-link " write pprint " }" print ] bi
231 [ "ABOUT: " write unparse print ]
233 ] with-string-writer ;
235 : write-using ( vocab -- )
238 { "help.markup" "help.syntax" } append natural-sort remove
242 : set-scaffold-docs-file ( vocab path -- )
245 [ docs-file-string ] [ write-using ] bi
247 ] with-output-stream ;
249 : with-scaffold ( quot -- )
250 [ HS{ } clone using ] dip with-variable ; inline
252 : link-vocab ( vocab -- )
254 "Edit documentation: " write
255 "-docs.factor" vocab/suffix>path <pathname> . ;
260 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
262 : scaffold-docs ( vocab -- )
265 dup "-docs.factor" vocab/suffix>path scaffolding? [
266 set-scaffold-docs-file
272 : scaffold-undocumented ( string -- )
273 [ interesting-words. ] [ link-vocab ] bi ;
275 : scaffold-authors ( vocab -- )
276 "authors.txt" developer-name get scaffold-metadata ;
278 : scaffold-tags ( vocab tags -- )
279 [ "tags.txt" ] dip scaffold-metadata ;
281 : scaffold-summary ( vocab summary -- )
282 [ "summary.txt" ] dip scaffold-metadata ;
284 : scaffold-platforms ( vocab platforms -- )
285 [ "platforms.txt" ] dip scaffold-metadata ;
287 : scaffold-vocab ( vocab-root string -- )
289 [ scaffold-directory ]
292 [ nip scaffold-authors ]
295 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
297 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
299 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
301 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
305 : tests-file-string ( vocab -- string )
308 "USING: tools.test " write dup write " ;" print
309 "IN: " write write ".tests" print
310 ] with-string-writer ;
312 : set-scaffold-tests-file ( vocab path -- )
313 [ tests-file-string ] dip utf8 set-file-contents ;
317 : scaffold-tests ( vocab -- )
319 dup "-tests.factor" vocab/suffix>path
321 set-scaffold-tests-file
326 SYMBOL: nested-examples
328 : example-using ( using -- )
329 " " join "example-using" [
330 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
331 "${example-indent}\"Example:\"
332 ${example-indent}{ $example \"USING: ${example-using} ;\"
333 ${example-indent} \"\"
334 ${example-indent} \"\"
341 : n-examples-using ( n using -- )
342 '[ _ example-using ] times ;
344 : scaffold-n-examples ( n word -- )
345 vocabulary>> "prettyprint" 2array
346 [ t nested-examples ] 2dip
353 : scaffold-examples ( word -- )
354 2 swap scaffold-n-examples ;
358 [ "Click to edit: " write <pathname> . ] bi ;
360 : scaffold-rc ( path -- )
361 [ home ] dip append-path touch. ;
363 : scaffold-factor-boot-rc ( -- )
364 ".factor-boot-rc" scaffold-rc ;
366 : scaffold-factor-rc ( -- )
367 ".factor-rc" scaffold-rc ;
369 : scaffold-mason-rc ( -- )
370 ".factor-mason-rc" scaffold-rc ;
372 : scaffold-factor-roots ( -- )
373 ".factor-roots" scaffold-rc ;
375 HOOK: scaffold-emacs os ( -- )
377 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;