1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs io.files hashtables kernel namespaces sequences
4 vocabs.loader io combinators io.encodings.utf8 calendar accessors
5 math.parser io.streams.string ui.tools.operations quotations
6 strings arrays prettyprint words vocabs sorting sets
7 classes math alien urls splitting ascii ;
10 SYMBOL: developer-name
13 ERROR: not-a-vocab-root string ;
14 ERROR: vocab-name-contains-separator path ;
15 ERROR: vocab-name-contains-dot path ;
16 ERROR: no-vocab vocab ;
20 : root? ( string -- ? ) vocab-roots get member? ;
22 : contains-dot? ( string -- ? ) ".." swap subseq? ;
24 : contains-separator? ( string -- ? ) [ path-separator? ] contains? ;
26 : check-vocab-name ( string -- string )
27 dup contains-dot? [ vocab-name-contains-dot ] when
28 dup contains-separator? [ vocab-name-contains-separator ] when ;
30 : check-root ( string -- string )
31 dup root? [ not-a-vocab-root ] unless ;
33 : directory-exists ( path -- )
34 "Not creating a directory, it already exists: " write print ;
36 : scaffold-directory ( path -- )
37 dup exists? [ directory-exists ] [ make-directories ] if ;
39 : not-scaffolding ( path -- )
40 "Not creating scaffolding for " write <pathname> . ;
42 : scaffolding ( path -- )
43 "Creating scaffolding for " write <pathname> . ;
45 : (scaffold-path) ( path string -- path )
46 dupd [ file-name ] dip append append-path ;
48 : scaffold-path ( path string -- path ? )
50 dup exists? [ dup not-scaffolding f ] [ dup scaffolding t ] if ;
52 : scaffold-copyright ( -- )
53 "! Copyright (C) " write now year>> number>string write
54 developer-name get [ "Your name" ] unless* bl write "." print
55 "! See http://factorcode.org/license.txt for BSD license." print ;
57 : main-file-string ( vocab -- string )
62 ] with-string-writer ;
64 : set-scaffold-main-file ( path vocab -- )
65 main-file-string swap utf8 set-file-contents ;
67 : scaffold-main ( path vocab -- )
68 [ ".factor" scaffold-path ] dip
69 swap [ set-scaffold-main-file ] [ 2drop ] if ;
71 : tests-file-string ( vocab -- string )
74 "USING: tools.test " write dup write " ;" print
75 "IN: " write write ".tests" print
76 ] with-string-writer ;
78 : set-scaffold-tests-file ( path vocab -- )
79 tests-file-string swap utf8 set-file-contents ;
81 : scaffold-tests ( path vocab -- )
82 [ "-tests.factor" scaffold-path ] dip
83 swap [ set-scaffold-tests-file ] [ 2drop ] if ;
85 : scaffold-authors ( path -- )
86 "authors.txt" append-path dup exists? [
90 developer-name get swap utf8 set-file-contents
93 : lookup-type ( string -- object/string ? )
94 "new" ?head drop [ [ CHAR: ' = ] [ digit? ] bi or ] trim-right
96 { "object" object } { "obj" object }
101 { "hashtable" hashtable }
103 { "ch" "a character" }
106 { "duration" duration }
107 { "path" "a pathname string" }
108 { "vocab" "a vocabulary specifier" }
109 { "vocab-root" "a vocabulary root string" }
113 { "alist" "an array of key/value pairs" }
114 { "keys" sequence } { "values" sequence }
115 { "class" class } { "tuple" tuple }
119 : add-using ( object -- )
120 vocabulary>> using get [ conjoin ] [ drop ] if* ;
122 : ($values.) ( array -- )
125 dup array? [ first ] when
128 [ [ pprint ] [ dup string? [ drop ] [ add-using ] if ] bi ] bi*
130 drop unparse write bl null pprint
136 : $values. ( word -- )
137 "declared-effect" word-prop [
138 [ in>> ] [ out>> ] bi
139 2dup [ empty? ] bi@ and [
143 [ " " write ($values.) ]
144 [ [ nl " " write ($values.) ] unless-empty ] bi*
149 : $description. ( word -- )
151 "{ $description \"\" } ;" print ;
153 : help-header. ( word -- )
154 "HELP: " write name>> print ;
156 : (help.) ( word -- )
157 [ help-header. ] [ $values. ] [ $description. ] tri ;
159 : interesting-words ( vocab -- array )
161 [ [ "help" word-prop ] [ predicate? ] bi or not ] filter
164 : interesting-words. ( vocab -- )
165 interesting-words [ (help.) nl ] each ;
167 : help-file-string ( vocab -- str2 )
170 [ "IN: " write print nl ]
171 [ interesting-words. ]
173 [ "ARTICLE: " write unparse dup write bl print ]
174 [ "{ $vocab-link " write pprint " }" print ] bi
177 [ "ABOUT: " write unparse print ]
179 ] with-string-writer ;
181 : write-using ( vocab -- )
184 { "help.markup" "help.syntax" } append natural-sort remove
188 : set-scaffold-help-file ( path vocab -- )
189 swap utf8 <file-writer> [
191 [ help-file-string ] [ write-using ] bi
193 ] with-output-stream ;
195 : check-scaffold ( vocab-root string -- vocab-root string )
196 [ check-root ] [ check-vocab-name ] bi* ;
198 : vocab>scaffold-path ( vocab-root string -- path )
199 path-separator first CHAR: . associate substitute
202 : prepare-scaffold ( vocab-root string -- string path )
203 check-scaffold [ vocab>scaffold-path ] keep ;
205 : with-scaffold ( quot -- )
206 [ H{ } clone using ] dip with-variable ; inline
208 : check-vocab ( vocab -- vocab )
209 dup find-vocab-root [ no-vocab ] unless ;
213 : link-vocab ( vocab -- )
215 "Edit documentation: " write
217 [ vocab>scaffold-path ] bi
218 "-docs.factor" (scaffold-path) <pathname> . ;
221 [ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
223 : scaffold-help ( string -- )
225 [ find-vocab-root ] [ check-vocab ] bi
227 [ "-docs.factor" scaffold-path ] dip
228 swap [ set-scaffold-help-file ] [ 2drop ] if
231 : scaffold-undocumented ( string -- )
232 [ interesting-words. ] [ link-vocab ] bi ;
234 : scaffold-vocab ( vocab-root string -- )
237 [ drop scaffold-directory ]
240 [ drop scaffold-authors ]
244 SYMBOL: examples-flag
248 "{ $example \"\" \"USING: prettyprint ;\""
252 } [ examples-flag get [ " " write ] when print ] each ;
261 : scaffold-rc ( path -- )
262 [ touch-file ] [ "Click to edit: " write <pathname> . ] bi ;
264 : scaffold-factor-boot-rc ( -- )
265 home ".factor-boot-rc" append-path scaffold-rc ;
267 : scaffold-factor-rc ( -- )
268 home ".factor-rc" append-path scaffold-rc ;