1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs io.files io.pathnames io.directories
4 io.encodings.utf8 hashtables kernel namespaces sequences
5 vocabs.loader vocabs.metadata io combinators calendar accessors
6 math.parser io.streams.string ui.tools.operations quotations
7 strings arrays prettyprint words vocabs sorting sets classes
8 math alien urls splitting ascii combinators.short-circuit timers
9 words.symbol system summary ;
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" }
138 { "alist" "an array of key/value pairs" }
139 { "keys" sequence } { "values" sequence }
140 { "class" class } { "tuple" tuple }
144 : add-using ( object -- )
145 vocabulary>> using get [ adjoin ] [ drop ] if* ;
147 : ($values.) ( array -- )
150 dup array? [ first ] when
153 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
155 drop unparse write bl null pprint
164 : ?print-nl ( seq1 seq2 -- )
165 [ empty? ] either? [ nl ] unless ;
167 : $values. ( word -- )
168 "declared-effect" word-prop [
169 [ in>> ] [ out>> ] bi
170 2dup [ empty? ] both? [
173 [ members ] dip over diff
175 [ drop 4bl ($values.) ]
177 [ nip 4bl ($values.) ] 2tri
182 : symbol-description. ( word -- )
184 "{ $var-description \"\" } ;" print ;
186 : $description. ( word -- )
188 "{ $description \"\" } ;" print ;
190 : docs-body. ( word/symbol -- )
194 [ $values. ] [ $description. ] bi
197 : docs-header. ( word -- )
198 "HELP: " write name>> print ;
200 : (help.) ( word -- )
201 [ docs-header. ] [ docs-body. ] bi ;
203 : interesting-words ( vocab -- array )
205 [ { [ "help" word-prop ] [ predicate? ] } 1|| not ] filter
208 : interesting-words. ( vocab -- )
209 interesting-words [ (help.) nl ] each ;
211 : docs-file-string ( vocab -- str2 )
214 [ "IN: " write print nl ]
215 [ interesting-words. ]
217 [ "ARTICLE: " write unparse dup write bl print ]
218 [ "{ $vocab-link " write pprint " }" print ] bi
221 [ "ABOUT: " write unparse print ]
223 ] with-string-writer ;
225 : write-using ( vocab -- )
228 { "help.markup" "help.syntax" } append natural-sort remove
232 : set-scaffold-docs-file ( vocab path -- )
235 [ docs-file-string ] [ write-using ] bi
237 ] with-output-stream ;
239 : with-scaffold ( quot -- )
240 [ HS{ } clone using ] dip with-variable ; inline
242 : link-vocab ( vocab -- )
244 "Edit documentation: " write
245 "-docs.factor" vocab/suffix>path <pathname> . ;
250 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
252 : scaffold-docs ( vocab -- )
255 dup "-docs.factor" vocab/suffix>path scaffolding? [
256 set-scaffold-docs-file
262 : scaffold-undocumented ( string -- )
263 [ interesting-words. ] [ link-vocab ] bi ;
265 : scaffold-authors ( vocab -- )
266 "authors.txt" developer-name get scaffold-metadata ;
268 : scaffold-tags ( vocab tags -- )
269 [ "tags.txt" ] dip scaffold-metadata ;
271 : scaffold-summary ( vocab summary -- )
272 [ "summary.txt" ] dip scaffold-metadata ;
274 : scaffold-platforms ( vocab platforms -- )
275 [ "platforms.txt" ] dip scaffold-metadata ;
277 : scaffold-vocab ( vocab-root string -- )
279 [ scaffold-directory ]
282 [ nip scaffold-authors ]
285 : scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
287 : scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
289 : scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
291 : scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
295 : tests-file-string ( vocab -- string )
298 "USING: tools.test " write dup write " ;" print
299 "IN: " write write ".tests" print
300 ] with-string-writer ;
302 : set-scaffold-tests-file ( vocab path -- )
303 [ tests-file-string ] dip utf8 set-file-contents ;
307 : scaffold-tests ( vocab -- )
309 dup "-tests.factor" vocab/suffix>path
311 set-scaffold-tests-file
316 SYMBOL: examples-flag
320 "{ $example \"\" \"USING: prettyprint ;\""
324 } [ examples-flag get [ 4bl ] when print ] each ;
335 [ "Click to edit: " write <pathname> . ] bi ;
337 : scaffold-rc ( path -- )
338 [ home ] dip append-path touch. ;
340 : scaffold-factor-boot-rc ( -- )
341 ".factor-boot-rc" scaffold-rc ;
343 : scaffold-factor-rc ( -- )
344 ".factor-rc" scaffold-rc ;
346 : scaffold-factor-roots ( -- )
347 ".factor-roots" scaffold-rc ;
349 HOOK: scaffold-emacs os ( -- )
351 M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;