diff --git a/lib/File/Copy.pm b/lib/File/Copy.pm new file mode 100644 index 0000000..71fe5bd --- /dev/null +++ b/lib/File/Copy.pm @@ -0,0 +1,34 @@ +use v6; + +module File::Copy; + +sub cp(Str $from, Str $to) is export { + my $f1 = open $from, :r, :bin; + my $f2 = open $to, :w, :bin; + $f2.write($f1.read(4096)) until $f1.eof; + $f1.close; + $f2.close; +} + +=begin pod + +=head1 NAME + +File::Copy -- copy files + +=head1 SYNOPSIS + + use File::Copy; + + cp 'source', 'destination'; + +=head1 DESCRIPTION + +C exports just one subroutine, cp taking two string +parameters: source and destination. If something comes wrong, the +internal open() or write() calls will die, C has no special +error reporting. + +=end pod + +# vim: ft=perl6 diff --git a/lib/File/Find.pm b/lib/File/Find.pm new file mode 100644 index 0000000..2c4356f --- /dev/null +++ b/lib/File/Find.pm @@ -0,0 +1,125 @@ +use v6; + +module File::Find; + +class File::Find::Result is Cool { + has $.dir; + has $.name; + + method Str { + $.dir ~ '/' ~ $.name + } +} + +sub checkrules ($elem, %opts) { + if %opts.defined { + given %opts { + when Regex { + return False unless $elem ~~ %opts + } + when Str { + return False unless $elem.name ~~ %opts + } + default { + die "name attribute has to be either Regex or Str" + } + } + } + if %opts.defined { + given %opts { + when 'dir' { + return False unless $elem.IO ~~ :d + } + when 'file' { + return False unless $elem.IO ~~ :f + } + when 'symlink' { + return False unless $elem.IO ~~ :l + } + default { + die "type attribute has to be dir, file or symlink"; + } + } + } + return True +} + +sub find (:$dir!, :$name, :$type) is export { + my @targets = dir($dir).map: { + File::Find::Result.new(dir => $dir, name => $_); + }; + my $list = gather while @targets { + my $elem = @targets.shift; + take $elem if checkrules($elem, { :$name, :$type }); + if $elem.IO ~~ :d { + for dir($elem) -> $file { + @targets.push( + File::Find::Result.new(dir => $elem, name => $file) + ); + } + } + } + return $list; +} + +=begin pod + +=head1 NAME + +File::Find - Get a lazy list of a directory tree + +=head1 SYNOPSIS + + use File::Find; + + my @list := find(dir => 'foo'); + say @list[0..3]; + + my $list = find(dir => 'foo'); + say $list[0..3]; + +=head1 DESCRIPTION + +C allows you to get the contents of the given directory, +recursively. The only exported function, C, generates a lazy +list of files in given directory. Every element of the list is a +C object, described below. +C takes one (or more) named arguments. The C argument +is mandatory, and sets the directory C will traverse. +There are also few optional arguments. If more than one is passed, +all of them must match for a file to be returned. + +=head2 name + +Specify a name of the file C is ought to look for. If you +pass a string here, C will return only the files with the given +name. When passing a regex, only the files with path matching the +pattern will be returned. + +=head2 type + +Given a type, C will only return files being the given type. +The available types are C, C or C. + +=head1 File::Find::Result + +C object acts like a normal string, having two +additional accessors, C and C, holding the directory +the file is in and the filename respectively. + +=head1 Perl 5's File::Find + +Please note, that this module is not trying to be the verbatim port of +Perl 5's File::Find module. Its interface is closer to Perl 5's +File::Find::Rule, and its features are planned to be similar one day. + +=head1 CAVEATS + +List assignment is eager in Perl 6, so if You assign C result +to an array, the elements will be copied and the laziness will be +spoiled. For a proper lazy list, use either binding (C<:=>) or assign +a result to a scalar value (see SYNOPSIS). + +=end pod + +# vim: ft=perl6 diff --git a/lib/File/Mkdir.pm b/lib/File/Mkdir.pm new file mode 100644 index 0000000..61c04a5 --- /dev/null +++ b/lib/File/Mkdir.pm @@ -0,0 +1,37 @@ +use v6; + +module File::Mkdir; + +multi sub mkdir(Str $name, $mode = 0o777, :$p!) is export { + for [\~] $name.split('/').map({"$_/"}) { + mkdir($_) unless .IO.d + } +} + +=begin pod + +=head1 NAME + +File::Mkdir -- provides recursive mkdir + +=head1 SYNOPSIS + + use File::Mkdir; + + # special mkdir exported in File::Mkdir + mkdir '/some/directory/tree', :p; + # just a casual, built-in mkdir + mkdir 'directory'; + +=head1 DESCRIPTION + +C provides an mkdir variant, which, when provided the :p +parameter, will create the directory tree recursively. For example, +calling C will create the foo directory (unless +it alredy exists), then the foo/bar directory (unless it exists). +The standard Perl 6 C is still available, and will be called +when the :p parameter is not passed. + +=end pod + +# vim: ft=perl6 diff --git a/lib/File/Tools.pm b/lib/File/Tools.pm new file mode 100644 index 0000000..defe486 --- /dev/null +++ b/lib/File/Tools.pm @@ -0,0 +1,15 @@ +module File::Tools:; + +=begin pod + +This is a distribution for file-related utilities, including: + +=item File::Copy +=item File::Find +=item File::Mkdir + +Please refer to their documentation for more information. + +=end pod + +# vim: ft=perl6