1 ! Copyright (C) 2008 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays assocs byte-arrays calendar
4 classes classes.error combinators combinators.short-circuit
5 continuations effects eval hashtables help.markup interpolate io
6 io.directories io.encodings.utf8 io.files io.pathnames
7 io.streams.string kernel math math.parser namespaces parser
8 prettyprint prettyprint.config quotations sequences sets sorting
9 splitting strings system timers unicode urls vocabs
10 vocabs.loader vocabs.metadata words words.symbol ;
13 SYMBOL: developer-name
16 ERROR: not-a-vocab-root string ;
18 ERROR: vocab-must-not-exist string ;
22 : vocab-root? ( string -- ? )
23 trim-tail-separators vocab-roots get member? ;
25 : ensure-vocab-exists ( string -- string )
26 dup lookup-vocab [ no-vocab ] unless ;
28 : check-vocab-root ( string -- string )
29 dup vocab-root? [ not-a-vocab-root ] unless ;
31 : check-vocab-root/name ( vocab-root string -- vocab-root string )
32 [ check-vocab-root ] [ check-vocab-name ] bi* ;
34 : replace-vocab-separators ( vocab -- path )
35 path-separator first CHAR: . associate substitute ;
37 : vocab-root/vocab>path ( vocab-root vocab -- path )
39 [ ] [ replace-vocab-separators ] bi* append-path ;
41 : vocab>path ( vocab -- path )
42 check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
44 : vocab-root/vocab/file>path ( vocab-root vocab file -- path )
45 [ vocab-root/vocab>path ] dip append-path ;
47 : vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
48 [ vocab-root/vocab>path dup file-name append-path ] dip append ;
50 : vocab/file>path ( vocab file -- path )
51 [ vocab>path ] dip append-path ;
53 : vocab/suffix>path ( vocab suffix -- path )
54 [ vocab>path dup file-name append-path ] dip append ;
56 : directory-exists ( path -- )
57 "Not creating a directory, it already exists: " write print ;
59 : scaffold-directory ( vocab-root vocab -- )
61 [ directory-exists ] [ make-directories ] if-file-exists ;
63 : not-scaffolding ( path -- path )
64 "Not creating scaffolding for " write dup <pathname> . ;
66 : scaffolding ( path -- path )
67 "Creating scaffolding for " write dup <pathname> . ;
69 : scaffolding? ( path -- path ? )
70 [ not-scaffolding f ] [ scaffolding t ] if-file-exists ;
72 : scaffold-copyright ( -- )
73 "! Copyright (C) " write now year>> number>string write
74 developer-name get [ "Your name" ] unless* bl write "." print
75 "! See https://factorcode.org/license.txt for BSD license." print ;
77 : main-file-string ( vocab -- string )
82 ] with-string-writer ;
84 : set-scaffold-main-file ( vocab path -- )
85 [ main-file-string 1array ] dip utf8 set-file-lines ;
87 : scaffold-main ( vocab-root vocab -- )
88 [ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
89 set-scaffold-main-file
94 : scaffold-metadata ( vocab file contents -- )
95 [ ensure-vocab-exists ] 2dip
97 [ vocab/file>path ] dip 1array swap scaffolding? [
106 : lookup-type ( string -- object/string ? )
108 "new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
116 { "hashtable" hashtable }
118 { "ch" "a character" }
121 { "byte-array" byte-array }
123 { "duration" duration }
124 { "path" "a pathname string" }
125 { "vocab" "a vocabulary specifier" }
126 { "vocab-root" "a vocabulary root string" }
128 { "sequence" sequence }
135 { "exemplar" object }
137 { "alist" "an array of key/value pairs" }
139 { "values" sequence }
143 } at* [ swap [ \ $maybe swap 2array ] when ] dip ;
145 GENERIC: add-using ( object -- )
147 M: array add-using [ add-using ] each ;
149 M: string add-using drop ;
152 vocabulary>> using get [ adjoin ] [ drop ] if* ;
154 : ($values.) ( array -- )
159 dup array? [ first ] when
162 [ [ pprint ] [ add-using ] bi ] bi*
164 drop unparse write bl object pprint
171 : ?print-nl ( seq1 seq2 -- )
172 [ empty? ] either? [ nl ] unless ;
174 : $values. ( word -- )
175 "declared-effect" word-prop [
176 [ in>> ] [ out>> ] bi
177 2dup [ empty? ] both? [
180 [ members ] dip over diff
184 [ nip ($values.) ] 2tri
189 : error-description. ( word -- )
191 "{ $description \"Throws " write
192 name>> dup a/an write " \" { $link " write
193 write " } \" error.\" }" print
194 ] bi "{ $error-description \"\" } ;" print ;
196 : class-description. ( word -- )
197 drop "{ $class-description \"\" } ;" print ;
199 : symbol-description. ( word -- )
200 drop "{ $var-description \"\" } ;" print ;
202 : $description. ( word -- )
203 drop "{ $description \"\" } ;" print ;
205 : docs-body. ( word/symbol -- )
207 { [ dup error-class? ] [ error-description. ] }
208 { [ dup class? ] [ class-description. ] }
209 { [ dup symbol? ] [ symbol-description. ] }
210 [ [ $values. ] [ $description. ] bi ]
213 : docs-header. ( word -- )
214 "HELP: " write name>> print ;
216 : interesting-words ( vocab -- array )
218 [ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
223 : scaffold-word-docs ( word -- )
224 [ docs-header. ] [ docs-body. ] bi ;
228 : interesting-words. ( vocab -- )
229 interesting-words [ scaffold-word-docs nl ] each ;
231 : docs-file-string ( vocab -- str2 )
234 [ "IN: " write print nl ]
235 [ interesting-words. ]
237 [ "ARTICLE: " write unparse dup write bl print ]
238 [ "{ $vocab-link " write pprint " }" print ] bi
241 [ "ABOUT: " write unparse print ]
243 ] with-string-writer ;
245 : write-using ( vocab -- )
248 { "help.markup" "help.syntax" } append sort remove
252 : set-scaffold-docs-file ( vocab path -- )
255 [ docs-file-string ] [ write-using ] bi
257 ] with-output-stream ;
259 : with-scaffold ( quot -- )
260 [ HS{ } clone using ] dip with-variable ; inline
262 : link-vocab ( vocab -- )
263 ".private" ?tail drop
265 "Edit documentation: " write
266 "-docs.factor" vocab/suffix>path <pathname> . ;
271 [ scaffold-word-docs ] [ nl vocabulary>> link-vocab ] bi ;
273 GENERIC: scaffold-docs ( obj -- )
275 M: string scaffold-docs ( vocab -- )
278 dup "-docs.factor" vocab/suffix>path scaffolding? [
279 set-scaffold-docs-file
285 M: sequence scaffold-docs [ scaffold-word-docs nl ] each ;
286 M: word scaffold-docs scaffold-word-docs ;
288 : scaffold-undocumented ( string -- )
289 [ interesting-words. ] [ link-vocab ] bi ;
291 : scaffold-authors ( vocab -- )
292 "authors.txt" developer-name get scaffold-metadata ;
294 : scaffold-tags ( vocab tags -- )
295 [ "tags.txt" ] dip scaffold-metadata ;
297 : scaffold-summary ( vocab summary -- )
298 [ "summary.txt" ] dip scaffold-metadata ;
300 : scaffold-platforms ( vocab platforms -- )
301 [ "platforms.txt" ] dip scaffold-metadata ;
303 : delete-from-root-cache ( string -- )
304 root-cache get delete-at ;
306 : scaffold-vocab-in ( vocab-root string -- )
307 dup delete-from-root-cache
309 [ scaffold-directory ]
312 [ nip scaffold-authors ]
315 : scaffold-core ( string -- )
316 "resource:core" swap scaffold-vocab-in ;
318 : scaffold-basis ( string -- )
319 "resource:basis" swap scaffold-vocab-in ;
321 : scaffold-extra ( string -- )
322 "resource:extra" swap scaffold-vocab-in ;
324 : scaffold-work ( string -- )
325 "resource:work" swap scaffold-vocab-in ;
327 : scaffold-vocab ( string -- )
328 "Choose a vocabulary root:" vocab-roots get
329 '[ [ "Use " prepend ] keep ] { } map>assoc throw-restarts
330 swap scaffold-vocab-in ;
334 : tests-file-string ( vocab -- string )
337 "USING: tools.test " write dup write " ;" print
338 "IN: " write write ".tests" print
339 ] with-string-writer ;
341 : set-scaffold-tests-file ( vocab path -- )
342 [ tests-file-string ] dip utf8 set-file-contents ;
344 : vocab>test-path ( vocab -- string )
345 "-tests.factor" vocab/suffix>path ;
349 : scaffold-tests ( vocab -- )
350 ensure-vocab-exists dup vocab>test-path
352 set-scaffold-tests-file
357 SYMBOL: nested-examples
359 : example-using ( using -- )
360 join-words "example-using" [
361 nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
362 "${example-indent}\"Example:\"
363 ${example-indent}{ $example \"USING: ${example-using} ;\"
364 ${example-indent} \"\"
365 ${example-indent} \"\"
372 : n-examples-using ( n using -- )
373 '[ _ example-using ] times ;
375 : scaffold-n-examples ( n word -- )
376 vocabulary>> "prettyprint" 2array
377 [ t nested-examples ] 2dip
384 : scaffold-examples ( word -- )
385 2 swap scaffold-n-examples ;
387 : scaffold-file ( path -- )
389 [ "Click to edit: " write >pathname . ] bi ;
391 : scaffold-rc ( path -- )
392 home-path scaffold-file ;
394 : scaffold-factor-boot-rc ( -- )
395 ".factor-boot-rc" scaffold-rc ;
397 : scaffold-factor-rc ( -- )
398 ".factor-rc" scaffold-rc ;
400 : scaffold-mason-rc ( -- )
401 ".factor-mason-rc" scaffold-rc ;
403 : scaffold-factor-roots ( -- )
404 ".factor-roots" scaffold-rc ;
406 : make-unit-test ( answer code -- str )
407 split-lines [ " " prepend ] map "\n" join
408 "[\n" "\n] unit-test\n" surround
411 : run-string ( string -- datastack )
412 [ parse-string ] with-file-vocabs V{ } clone swap with-datastack ;
414 : read-unit-test ( -- str/f )
415 read-contents [ f ] [
416 [ run-string [ unparse ] without-limits ] keep
420 : read-unit-tests ( -- str )
421 [ read-unit-test dup ] [ ] produce nip "\n\n" join ;
423 : scaffold-unit-tests ( vocab -- )
424 [ scaffold-tests read-unit-tests ]
425 [ vocab>test-path utf8 [ write ] with-file-appender ] bi ;
427 HOOK: scaffold-emacs os ( -- )
429 M: unix scaffold-emacs ".emacs" scaffold-rc ;