factor/basis/tools/scaffold/scaffold.factor

373 lines
9.9 KiB
Factor
Raw Normal View History

2008-09-03 20:43:36 -04:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien arrays assocs byte-arrays calendar
classes combinators combinators.short-circuit fry hashtables
help.markup interpolate io io.directories io.encodings.utf8
io.files io.pathnames io.streams.string kernel math math.parser
namespaces prettyprint quotations sequences sets sorting
splitting strings system timers unicode urls vocabs
vocabs.loader vocabs.metadata words words.symbol ;
2008-09-03 20:43:36 -04:00
IN: tools.scaffold
SYMBOL: developer-name
SYMBOL: using
2008-09-03 20:43:36 -04:00
ERROR: not-a-vocab-root string ;
ERROR: vocab-must-not-exist string ;
2008-09-04 02:50:26 -04:00
<PRIVATE
2008-10-21 22:03:37 -04:00
: vocab-root? ( string -- ? )
trim-tail-separators vocab-roots get member? ;
2008-10-21 22:03:37 -04:00
: ensure-vocab-exists ( string -- string )
dup loaded-vocab-names member? [ no-vocab ] unless ;
: check-root ( string -- string )
dup vocab-root? [ not-a-vocab-root ] unless ;
2008-09-03 20:43:36 -04:00
: check-vocab-root/vocab ( vocab-root string -- vocab-root string )
[ check-root ] [ check-vocab-name ] bi* ;
: check-vocab-exists ( string -- string )
dup vocab-exists? [ vocab-must-not-exist ] when ;
: replace-vocab-separators ( vocab -- path )
path-separator first CHAR: . associate substitute ; inline
: vocab-root/vocab>path ( vocab-root vocab -- path )
check-vocab-root/vocab
[ ] [ replace-vocab-separators ] bi* append-path ;
: vocab>path ( vocab -- path )
check-vocab [ find-vocab-root ] keep vocab-root/vocab>path ;
: vocab-root/vocab/file>path ( vocab-root vocab file -- path )
[ vocab-root/vocab>path ] dip append-path ;
: vocab-root/vocab/suffix>path ( vocab-root vocab suffix -- path )
[ vocab-root/vocab>path dup file-name append-path ] dip append ;
: vocab/file>path ( vocab file -- path )
[ vocab>path ] dip append-path ;
: vocab/suffix>path ( vocab suffix -- path )
[ vocab>path dup file-name append-path ] dip append ;
2008-09-03 20:43:36 -04:00
: directory-exists ( path -- )
"Not creating a directory, it already exists: " write print ;
: scaffold-directory ( vocab-root vocab -- )
vocab-root/vocab>path
2008-09-03 20:43:36 -04:00
dup exists? [ directory-exists ] [ make-directories ] if ;
: not-scaffolding ( path -- path )
"Not creating scaffolding for " write dup <pathname> . ;
2008-09-03 20:43:36 -04:00
: scaffolding ( path -- path )
"Creating scaffolding for " write dup <pathname> . ;
2008-09-03 20:43:36 -04:00
: scaffolding? ( path -- path ? )
dup exists? [ not-scaffolding f ] [ scaffolding t ] if ;
2008-09-03 20:43:36 -04:00
: scaffold-copyright ( -- )
"! Copyright (C) " write now year>> number>string write
developer-name get [ "Your name" ] unless* bl write "." print
"! See http://factorcode.org/license.txt for BSD license." print ;
: main-file-string ( vocab -- string )
2008-09-03 20:43:36 -04:00
[
scaffold-copyright
"USING: ;" print
"IN: " write print
] with-string-writer ;
: set-scaffold-main-file ( vocab path -- )
[ main-file-string 1array ] dip utf8 set-file-lines ;
2008-09-03 20:43:36 -04:00
: scaffold-main ( vocab-root vocab -- )
[ ".factor" vocab-root/vocab/suffix>path ] keep swap scaffolding? [
set-scaffold-main-file
] [
2drop
] if ;
2008-09-03 20:43:36 -04:00
: scaffold-metadata ( vocab file contents -- )
[ ensure-vocab-exists ] 2dip
[
[ vocab/file>path ] dip 1array swap scaffolding? [
utf8 set-file-lines
] [
2drop
] if
2008-09-03 20:43:36 -04:00
] [
2drop
] if* ;
2008-09-03 20:43:36 -04:00
: lookup-type ( string -- object/string ? )
"/f" ?tail swap
"new" ?head drop [ { [ CHAR: ' = ] [ digit? ] } 1|| ] trim-tail
2008-09-03 20:43:36 -04:00
H{
{ "object" object }
{ "obj" object }
2008-09-24 20:05:03 -04:00
{ "quot" quotation }
{ "string" string }
2008-09-03 20:43:36 -04:00
{ "str" string }
{ "hash" hashtable }
2008-09-03 20:43:36 -04:00
{ "hashtable" hashtable }
{ "?" boolean }
2008-09-03 20:43:36 -04:00
{ "ch" "a character" }
{ "word" word }
{ "array" array }
{ "byte-array" byte-array }
{ "timer" timer }
2008-09-05 18:41:14 -04:00
{ "duration" duration }
{ "path" "a pathname string" }
{ "vocab" "a vocabulary specifier" }
{ "vocab-root" "a vocabulary root string" }
2008-09-05 18:41:14 -04:00
{ "c-ptr" c-ptr }
{ "sequence" sequence }
2008-09-24 20:05:03 -04:00
{ "seq" sequence }
{ "exemplar" object }
2008-09-24 20:05:03 -04:00
{ "assoc" assoc }
2008-09-05 18:41:14 -04:00
{ "alist" "an array of key/value pairs" }
{ "keys" sequence }
{ "values" sequence }
{ "class" class }
{ "tuple" tuple }
2008-09-24 20:05:03 -04:00
{ "url" url }
} at* [ swap [ \ $maybe swap 2array ] when ] dip ;
2008-09-03 20:43:36 -04:00
GENERIC: add-using ( object -- )
M: array add-using [ add-using ] each ;
M: string add-using drop ;
M: object add-using ( object -- )
2013-03-10 13:02:53 -04:00
vocabulary>> using get [ adjoin ] [ drop ] if* ;
: ($values.) ( array -- )
[
" " write
[ bl ] [
"{ " write
dup array? [ first ] when
dup lookup-type [
[ unparse write bl ]
[ [ pprint ] [ add-using ] bi ] bi*
] [
drop unparse write bl null pprint
null add-using
] if
" }" write
] interleave
] unless-empty ;
2010-05-09 19:52:46 -04:00
: ?print-nl ( seq1 seq2 -- )
2010-08-02 20:50:59 -04:00
[ empty? ] either? [ nl ] unless ;
2008-09-03 20:43:36 -04:00
: $values. ( word -- )
"declared-effect" word-prop [
[ in>> ] [ out>> ] bi
2011-10-15 22:19:44 -04:00
2dup [ empty? ] both? [
2drop
] [
[ members ] dip over diff
"{ $values" print
[ drop ($values.) ]
2010-05-09 19:52:46 -04:00
[ ?print-nl ]
[ nip ($values.) ] 2tri
nl "}" print
] if
2008-09-03 20:43:36 -04:00
] when* ;
: class-description. ( word -- )
drop "{ $class-description \"\" } ;" print ;
: symbol-description. ( word -- )
drop "{ $var-description \"\" } ;" print ;
2008-09-03 20:43:36 -04:00
: $description. ( word -- )
drop "{ $description \"\" } ;" print ;
2008-09-03 20:43:36 -04:00
: docs-body. ( word/symbol -- )
{
{ [ dup class? ] [ class-description. ] }
{ [ dup symbol? ] [ symbol-description. ] }
[ [ $values. ] [ $description. ] bi ]
} cond ;
: docs-header. ( word -- )
2008-09-24 20:05:03 -04:00
"HELP: " write name>> print ;
2008-09-03 20:43:36 -04:00
2009-02-22 11:03:37 -05:00
: (help.) ( word -- )
[ docs-header. ] [ docs-body. ] bi ;
2008-09-03 20:43:36 -04:00
: interesting-words ( vocab -- array )
vocab-words
[ { [ "help" word-prop ] [ predicate? ] } 1|| ] reject
natural-sort ;
: interesting-words. ( vocab -- )
2009-02-22 11:03:37 -05:00
interesting-words [ (help.) nl ] each ;
: docs-file-string ( vocab -- str2 )
2008-09-03 20:43:36 -04:00
[
2008-09-05 20:29:14 -04:00
{
[ "IN: " write print nl ]
[ interesting-words. ]
[
[ "ARTICLE: " write unparse dup write bl print ]
[ "{ $vocab-link " write pprint " }" print ] bi
";" print nl
]
2008-09-05 20:29:14 -04:00
[ "ABOUT: " write unparse print ]
} cleave
2008-09-03 20:43:36 -04:00
] with-string-writer ;
2008-09-24 20:05:03 -04:00
: write-using ( vocab -- )
"USING:" write
2013-03-10 13:02:53 -04:00
using get members
2008-09-24 20:05:03 -04:00
{ "help.markup" "help.syntax" } append natural-sort remove
[ bl write ] each
" ;" print ;
: set-scaffold-docs-file ( vocab path -- )
utf8 <file-writer> [
2008-09-24 20:05:03 -04:00
scaffold-copyright
[ docs-file-string ] [ write-using ] bi
2008-09-24 20:05:03 -04:00
write
] with-output-stream ;
2008-09-03 20:43:36 -04:00
2008-09-04 01:43:18 -04:00
: with-scaffold ( quot -- )
2013-03-10 13:02:53 -04:00
[ HS{ } clone using ] dip with-variable ; inline
: link-vocab ( vocab -- )
check-vocab
"Edit documentation: " write
"-docs.factor" vocab/suffix>path <pathname> . ;
PRIVATE>
2009-02-22 11:03:37 -05:00
: help. ( word -- )
[ (help.) ] [ nl vocabulary>> link-vocab ] bi ;
: scaffold-docs ( vocab -- )
ensure-vocab-exists
2008-09-04 01:43:18 -04:00
[
dup "-docs.factor" vocab/suffix>path scaffolding? [
set-scaffold-docs-file
] [
2drop
] if
2008-09-04 01:43:18 -04:00
] with-scaffold ;
: scaffold-undocumented ( string -- )
[ interesting-words. ] [ link-vocab ] bi ;
2008-09-03 20:43:36 -04:00
: scaffold-authors ( vocab -- )
"authors.txt" developer-name get scaffold-metadata ;
: scaffold-tags ( vocab tags -- )
[ "tags.txt" ] dip scaffold-metadata ;
: scaffold-summary ( vocab summary -- )
[ "summary.txt" ] dip scaffold-metadata ;
: scaffold-platforms ( vocab platforms -- )
[ "platforms.txt" ] dip scaffold-metadata ;
: scaffold-vocab ( vocab-root string -- )
check-vocab-exists {
[ scaffold-directory ]
2008-09-03 20:43:36 -04:00
[ scaffold-main ]
[ nip require ]
[ nip scaffold-authors ]
2008-09-03 20:43:36 -04:00
} 2cleave ;
2008-09-05 18:41:14 -04:00
: scaffold-core ( string -- ) "resource:core" swap scaffold-vocab ;
: scaffold-basis ( string -- ) "resource:basis" swap scaffold-vocab ;
: scaffold-extra ( string -- ) "resource:extra" swap scaffold-vocab ;
: scaffold-work ( string -- ) "resource:work" swap scaffold-vocab ;
<PRIVATE
: tests-file-string ( vocab -- string )
[
scaffold-copyright
"USING: tools.test " write dup write " ;" print
"IN: " write write ".tests" print
] with-string-writer ;
: set-scaffold-tests-file ( vocab path -- )
[ tests-file-string ] dip utf8 set-file-contents ;
PRIVATE>
: scaffold-tests ( vocab -- )
ensure-vocab-exists
dup "-tests.factor" vocab/suffix>path
scaffolding? [
set-scaffold-tests-file
] [
2drop
] if ;
SYMBOL: nested-examples
: example-using ( using -- )
" " join "example-using" [
nested-examples get 4 0 ? CHAR: \s <string> "example-indent" [
"${example-indent}\"Example:\"
${example-indent}{ $example \"USING: ${example-using} ;\"
${example-indent} \"\"
${example-indent} \"\"
${example-indent}}
"
interpolate
] with-variable
] with-variable ;
2008-09-05 18:41:14 -04:00
: n-examples-using ( n using -- )
'[ _ example-using ] times ;
: scaffold-n-examples ( n word -- )
vocabulary>> "prettyprint" 2array
[ t nested-examples ] 2dip
'[
"{ $examples" print
_ _ n-examples-using
2008-09-05 18:41:14 -04:00
"}" print
] with-variable ;
: scaffold-examples ( word -- )
2 swap scaffold-n-examples ;
: touch. ( path -- )
[ touch-file ]
[ "Click to edit: " write <pathname> . ] bi ;
: scaffold-rc ( path -- )
[ home ] dip append-path touch. ;
: scaffold-factor-boot-rc ( -- )
".factor-boot-rc" scaffold-rc ;
: scaffold-factor-rc ( -- )
".factor-rc" scaffold-rc ;
: scaffold-mason-rc ( -- )
".factor-mason-rc" scaffold-rc ;
: scaffold-factor-roots ( -- )
".factor-roots" scaffold-rc ;
HOOK: scaffold-emacs os ( -- )
M: unix scaffold-emacs ( -- ) ".emacs" scaffold-rc ;