-
-
Save anonymous/9361af3ad5d786c10179 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojo/Home.pm b/lib/Mojo/Home.pm | |
index bcc09e6..f6f883f 100644 | |
--- a/lib/Mojo/Home.pm | |
+++ b/lib/Mojo/Home.pm | |
@@ -38,9 +38,9 @@ sub lib_dir { | |
} | |
sub list_files { | |
- my ($self, $dir) = (shift, shift // ''); | |
+ my ($self, $dir, $options) = (shift, shift // '', shift); | |
$dir = catdir @{$self->parts}, split('/', $dir); | |
- return [map { join '/', splitdir abs2rel($_, $dir) } files $dir]; | |
+ return [map { join '/', splitdir abs2rel($_, $dir) } files $dir, $options]; | |
} | |
sub mojo_lib_dir { catdir dirname(__FILE__), '..' } | |
@@ -111,12 +111,25 @@ Path to C<lib> directory of application. | |
my $files = $home->list_files; | |
my $files = $home->list_files('foo/bar'); | |
+ my $files = $home->list_files('foo/bar', {hidden => 1}); | |
Portably list all files recursively in directory relative to the home directory. | |
# List layouts | |
say for @{$home->list_files('templates/layouts')}; | |
+These options are currently available: | |
+ | |
+=over 2 | |
+ | |
+=item hidden | |
+ | |
+ hidden => 1 | |
+ | |
+Include hidden files and directories. | |
+ | |
+=back | |
+ | |
=head2 mojo_lib_dir | |
my $path = $home->mojo_lib_dir; | |
diff --git a/lib/Mojo/Util.pm b/lib/Mojo/Util.pm | |
index 033874e..3ef1fb2 100644 | |
--- a/lib/Mojo/Util.pm | |
+++ b/lib/Mojo/Util.pm | |
@@ -7,6 +7,7 @@ use Digest::MD5 qw(md5 md5_hex); | |
use Digest::SHA qw(hmac_sha1_hex sha1 sha1_hex); | |
use Encode 'find_encoding'; | |
use Exporter 'import'; | |
+use File::Basename 'basename'; | |
use File::Find 'find'; | |
use IO::Poll qw(POLLIN POLLPRI); | |
use List::Util 'min'; | |
@@ -127,10 +128,16 @@ sub dumper { | |
sub encode { _encoding($_[0])->encode("$_[1]") } | |
sub files { | |
- my $dir = shift; | |
+ my ($dir, $options) = (shift, shift // {}); | |
+ | |
my @files; | |
- my $wanted = sub { -d $File::Find::name or push @files, $File::Find::name }; | |
+ my $wanted = sub { | |
+ my $path = $File::Find::name; | |
+ if (-d $path) { $File::Find::prune = 1 if _ignore($options, $path) } | |
+ elsif (!_ignore($options, $path)) { push @files, $path } | |
+ }; | |
find {wanted => $wanted, no_chdir => 1}, $dir if -d $dir; | |
+ | |
return sort @files; | |
} | |
@@ -419,6 +426,8 @@ sub _header { | |
return [@part ? (@tree, \@part) : @tree]; | |
} | |
+sub _ignore { !$_[0]{hidden} && basename($_[1]) =~ /^\./ } | |
+ | |
sub _options { | |
# Hash or name (one) | |
@@ -586,12 +595,25 @@ Encode characters to bytes. | |
=head2 files | |
my @files = files '/tmp/uploads'; | |
+ my @files = files '/tmp/uploads', {hidden => 1}; | |
List all files recursively in a directory. | |
# List all templates | |
say for files '/home/sri/myapp/templates'; | |
+These options are currently available: | |
+ | |
+=over 2 | |
+ | |
+=item hidden | |
+ | |
+ hidden => 1 | |
+ | |
+Include hidden files and directories. | |
+ | |
+=back | |
+ | |
=head2 hmac_sha1_sum | |
my $checksum = hmac_sha1_sum $bytes, 'passw0rd'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment