2018-07-09 02:33:26 -04:00
|
|
|
! Copyright (C) 2018 Doug Coleman.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2019-01-26 06:49:03 -05:00
|
|
|
USING: base85 combinators command-line escape-strings fry
|
|
|
|
io.backend io.directories io.directories.search
|
|
|
|
io.encodings.binary io.encodings.utf8 io.files io.files.info
|
|
|
|
io.pathnames kernel locals math namespaces sequences
|
|
|
|
sequences.extras splitting ;
|
2018-07-09 02:33:26 -04:00
|
|
|
IN: tools.directory-to-file
|
|
|
|
|
2019-01-26 06:49:03 -05:00
|
|
|
: file-is-text? ( path -- ? )
|
|
|
|
binary file-contents [ 127 < ] all? ;
|
|
|
|
|
|
|
|
: directory-repr ( path -- obj )
|
|
|
|
escape-simplest
|
|
|
|
"DIRECTORY: " prepend ;
|
|
|
|
|
|
|
|
: file-repr ( path string -- obj )
|
|
|
|
[ escape-simplest "FILE:: " prepend ] dip " " glue ;
|
2018-07-09 02:33:26 -04:00
|
|
|
|
2018-12-27 17:56:20 -05:00
|
|
|
:: directory-to-string ( path -- string )
|
|
|
|
path normalize-path
|
2018-07-09 02:33:26 -04:00
|
|
|
[ path-separator = ] trim-tail "/" append
|
2019-01-26 06:49:03 -05:00
|
|
|
[ recursive-directory-files ] keep
|
2018-07-09 02:33:26 -04:00
|
|
|
dup '[
|
|
|
|
[ _ ?head drop ] map
|
2019-01-26 06:49:03 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
{ [ dup file-info directory? ] [ directory-repr ] }
|
|
|
|
{ [ dup file-is-text? ] [ dup utf8 file-contents escape-string file-repr ] }
|
|
|
|
[
|
|
|
|
dup binary file-contents >base85
|
|
|
|
"" like escape-string
|
|
|
|
"base85" prepend file-repr
|
|
|
|
]
|
|
|
|
} cond
|
|
|
|
] map
|
2018-07-09 02:33:26 -04:00
|
|
|
] with-directory
|
2019-01-26 06:49:03 -05:00
|
|
|
"\n\n" join
|
2018-12-27 17:56:20 -05:00
|
|
|
"<DIRECTORY: " path escape-simplest "\n\n" 3append
|
2019-01-26 06:49:03 -05:00
|
|
|
"\n\n;DIRECTORY>" surround ;
|
2018-07-09 02:33:26 -04:00
|
|
|
|
|
|
|
: directory-to-file ( path -- )
|
|
|
|
[ directory-to-string ] keep ".modern" append
|
|
|
|
utf8 set-file-contents ;
|
|
|
|
|
|
|
|
: directory-to-file-main ( -- )
|
|
|
|
command-line get dup length 1 = [ "oops" throw ] unless first
|
|
|
|
directory-to-file ;
|
|
|
|
|
|
|
|
MAIN: directory-to-file-main
|