Skip to content

Commit

Permalink
add list method to Mojo::File
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 7, 2017
1 parent 583d564 commit 8978b9f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 14 deletions.
48 changes: 47 additions & 1 deletion lib/Mojo/File.pm
Expand Up @@ -20,7 +20,7 @@ use Scalar::Util 'blessed';

our @EXPORT_OK = ('path', 'tempdir');

sub basename { scalar File::Basename::basename ${$_[0]} }
sub basename { scalar File::Basename::basename ${$_[0]}, @_ }

sub child {
my $self = shift;
Expand All @@ -31,6 +31,19 @@ sub dirname { $_[0]->new(scalar File::Basename::dirname ${$_[0]}) }

sub is_abs { File::Spec->file_name_is_absolute(${shift()}) }

sub list {
my ($self, $options) = (shift, shift // {});

return Mojo::Collection->new unless -d $$self;
opendir(my $dir, $$self);
my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dir;
@files = grep { !/^\./ } @files unless $options->{hidden};
@files = map { File::Spec->catfile($$self, $_) } @files;
@files = grep { !-d } @files unless $options->{dir};

return Mojo::Collection->new(map { $self->new($_) } sort @files);
}

sub list_tree {
my ($self, $options) = (shift, shift // {});

Expand Down Expand Up @@ -170,12 +183,16 @@ L<Mojo::File> implements the following methods.
=head2 basename
my $name = $path->basename;
my $name = $path->basename('.txt');
Return the last level of the path with L<File::Basename> as a string.
# ".vimrc" (on UNIX)
Mojo::File->new('/home/sri/.vimrc')->basename;
# "test" (on UNIX)
Mojo::File->new('/home/sri/test.txt')->basename('.txt');
=head2 child
my $child = $path->child('.vimrc');
Expand Down Expand Up @@ -207,6 +224,35 @@ Check if path is absolute.
# False (on UNIX)
Mojo::File->new('.vimrc')->is_abs;
=head2 list
my $collection = $path->list;
my $collection = $path->list({hidden => 1});
List all files in a directory and return a L<Mojo::Collection> object containing
the results as L<Mojo::File> objects.
# List files
say for Mojo::File->new('/home/sri/myapp')->list->each;
These options are currently available:
=over 2
=item dir
dir => 1
Include directories.
=item hidden
hidden => 1
Include hidden files.
=back
=head2 list_tree
my $collection = $path->list_tree;
Expand Down
14 changes: 4 additions & 10 deletions lib/Mojo/Loader.pm
Expand Up @@ -2,9 +2,8 @@ package Mojo::Loader;
use Mojo::Base -strict;

use Exporter 'import';
use File::Basename 'basename';
use File::Spec::Functions qw(catdir catfile splitdir);
use Mojo::Exception;
use Mojo::File 'path';
use Mojo::Util qw(b64_decode class_to_path);

our @EXPORT_OK
Expand All @@ -21,14 +20,9 @@ sub find_modules {

my %modules;
for my $directory (@INC) {
next unless -d (my $path = catdir $directory, split(/::|'/, $ns));

# List "*.pm" files in directory
opendir(my $dir, $path);
for my $file (grep /\.pm$/, readdir $dir) {
next if -d catfile splitdir($path), $file;
$modules{"${ns}::" . basename $file, '.pm'}++;
}
next unless -d (my $path = path($directory, split(/::|'/, $ns)));
$modules{"${ns}::$_"}++
for $path->list->grep(qr/\.pm$/)->map('basename', '.pm')->each;
}

return sort keys %modules;
Expand Down
26 changes: 23 additions & 3 deletions t/mojo/file.t
Expand Up @@ -54,11 +54,31 @@ ok !-d $subdir, 'directory does not exist anymore';
$subdir->make_path;
ok -d $subdir, 'directory exists';

# List
is_deeply path('does_not_exist')->list->to_array, [], 'no files';
is_deeply path(__FILE__)->list->to_array, [], 'no files';
my $lib = path(__FILE__)->dirname->child('lib', 'Mojo');
my @files = map { path($lib)->child(split '/') }
('DeprecationTest.pm', 'LoaderException.pm', 'LoaderException2.pm');
is_deeply path($lib)->list->map('to_string')->to_array, \@files, 'right files';
unshift @files, $lib->child('.hidden.txt')->to_string;
is_deeply path($lib)->list({hidden => 1})->map('to_string')->to_array, \@files,
'right files';
@files = map { path($lib)->child(split '/') } (
'BaseTest', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest'
);
is_deeply path($lib)->list({dir => 1})->map('to_string')->to_array, \@files,
'right files';
my @hidden = map { path($lib)->child(split '/') } '.hidden.txt', '.test';
is_deeply path($lib)->list({dir => 1, hidden => 1})->map('to_string')->to_array,
[@hidden, @files], 'right files';

# List tree
is_deeply path('does_not_exist')->list_tree->to_array, [], 'no files';
is_deeply path(__FILE__)->list_tree->to_array, [], 'no files';
my $lib = path(__FILE__)->dirname->child('lib', 'Mojo');
my @files = map { path($lib)->child(split '/') } (
@files = map { path($lib)->child(split '/') } (
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
Expand All @@ -67,7 +87,7 @@ my @files = map { path($lib)->child(split '/') } (
);
is_deeply path($lib)->list_tree->map('to_string')->to_array, \@files,
'right files';
my @hidden = map { path($lib)->child(split '/') } '.hidden.txt',
@hidden = map { path($lib)->child(split '/') } '.hidden.txt',
'.test/hidden.txt';
is_deeply path($lib)->list_tree({hidden => 1})->map('to_string')->to_array,
[@hidden, @files], 'right files';
Expand Down

0 comments on commit 8978b9f

Please sign in to comment.