Skip to content

Commit

Permalink
add dir option to list_tree method in Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 4, 2017
1 parent bd9288c commit 518b684
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

7.28 2017-03-04
7.28 2017-03-05
- Added copy_to method to Mojo::File.
- Added dir option to list_tree method in Mojo::File.

7.27 2017-02-27
- Added support for UNIX domain sockets. (sri, salva)
Expand Down
32 changes: 18 additions & 14 deletions lib/Mojo/File.pm
Expand Up @@ -10,9 +10,9 @@ use Carp 'croak';
use Cwd 'getcwd';
use Exporter 'import';
use File::Basename ();
use File::Copy ();
use File::Find ();
use File::Path ();
use File::Copy qw(copy move);
use File::Find 'find';
use File::Path ();
use File::Spec::Functions
qw(abs2rel canonpath catfile file_name_is_absolute rel2abs splitdir);
use File::Temp ();
Expand All @@ -27,8 +27,7 @@ sub child { $_[0]->new(@_) }

sub copy_to {
my ($self, $to) = @_;
File::Copy::copy($$self, $to)
or croak qq{Can't copy file "$$self" to "$to": $!};
copy($$self, $to) or croak qq{Can't copy file "$$self" to "$to": $!};
return $self;
}

Expand All @@ -55,14 +54,14 @@ sub list_tree {
# This may break in the future, but is worth it for performance
local $File::Find::skip_pattern = qr/^\./ unless $options->{hidden};

my %files;
my $w = sub { $files{$File::Find::name}++ };
my $p = sub { delete $files{$File::Find::dir} };
File::Find::find {wanted => $w, postprocess => $p, no_chdir => 1}, $$self
if -d $$self;
my %all;
my $wanted = {wanted => sub { $all{$File::Find::name}++ }, no_chdir => 1};
$wanted->{postprocess} = sub { delete $all{$File::Find::dir} }
unless $options->{dir};
find $wanted, $$self if -d $$self;
delete $all{$$self};

return Mojo::Collection->new(map { $self->new(canonpath($_)) }
sort keys %files);
return Mojo::Collection->new(map { $self->new(canonpath $_) } sort keys %all);
}

sub make_path {
Expand All @@ -73,8 +72,7 @@ sub make_path {

sub move_to {
my ($self, $to) = @_;
File::Copy::move($$self, $to)
or croak qq{Can't move file "$$self" to "$to": $!};
move($$self, $to) or croak qq{Can't move file "$$self" to "$to": $!};
return $self;
}

Expand Down Expand Up @@ -304,6 +302,12 @@ These options are currently available:
=over 2
=item dir
dir => 1
Include directories.
=item hidden
hidden => 1
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/file.t
Expand Up @@ -170,6 +170,18 @@ is_deeply path($lib)->list_tree->map('to_string')->to_array, \@files,
'.test/hidden.txt';
is_deeply path($lib)->list_tree({hidden => 1})->map('to_string')->to_array,
[@hidden, @files], 'right files';
my @all = map { path($lib)->child(split '/') } (
'.hidden.txt', '.test',
'.test/hidden.txt', 'BaseTest',
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest', 'LoaderTest/A.pm',
'LoaderTest/B.pm', 'LoaderTest/C.pm',
'TestConnectProxy.pm'
);
is_deeply path($lib)->list_tree({dir => 1, hidden => 1})->map('to_string')
->to_array, [@all], 'right files';

# I/O
$dir = tempdir;
Expand Down

0 comments on commit 518b684

Please sign in to comment.