Skip to content

Commit

Permalink
add files function to Mojo::Util
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 23, 2016
1 parent c1ef22a commit 17ace53
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

6.48 2016-02-22
6.48 2016-02-23
- Added files function to Mojo::Util.
- Updated jQuery to version 2.2.1.
- Fixed bug where the results of the list_files method in Mojo::Home would
include directories.
Expand Down
17 changes: 3 additions & 14 deletions lib/Mojo/Home.pm
Expand Up @@ -4,10 +4,9 @@ use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Find 'find';
use File::Spec::Functions qw(abs2rel catdir catfile splitdir);
use FindBin;
use Mojo::Util 'class_to_path';
use Mojo::Util qw(class_to_path files);

has parts => sub { [] };

Expand Down Expand Up @@ -40,18 +39,8 @@ sub lib_dir {

sub list_files {
my ($self, $dir) = (shift, shift // '');

my @files;
return \@files unless -d ($dir = catdir @{$self->parts}, split('/', $dir));
find {
wanted => sub {
push @files, join '/', splitdir abs2rel($File::Find::name, $dir)
unless -d $File::Find::name;
},
no_chdir => 1
}, $dir;

return [sort @files];
$dir = catdir @{$self->parts}, split('/', $dir);
return [map { join '/', splitdir abs2rel($_, $dir) } files $dir];
}

sub mojo_lib_dir { catdir dirname(__FILE__), '..' }
Expand Down
16 changes: 3 additions & 13 deletions lib/Mojo/Server/Morbo.pm
Expand Up @@ -4,27 +4,17 @@ use Mojo::Base -base;
# "Linda: With Haley's Comet out of ice, Earth is experiencing the devastating
# effects of sudden, intense global warming.
# Morbo: Morbo is pleased but sticky."
use Mojo::Home;
use Mojo::Server::Daemon;
use Mojo::Util 'files';
use POSIX 'WNOHANG';

has daemon => sub { Mojo::Server::Daemon->new };
has watch => sub { [qw(lib templates)] };

sub check {
my $self = shift;

# Discover files
my @files;
for my $watch (@{$self->watch}) {
if (-d $watch) {
my $home = Mojo::Home->new->parse($watch);
push @files, $home->rel_file($_) for @{$home->list_files};
}
elsif (-r $watch) { push @files, $watch }
}

$self->_check($_) and return $_ for @files;
$self->_check($_) and return $_
for map { -f $_ && -r $_ ? $_ : files $_ } @{$self->watch};
return undef;
}

Expand Down
25 changes: 23 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -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::Find 'find';
use IO::Poll qw(POLLIN POLLPRI);
use List::Util 'min';
use MIME::Base64 qw(decode_base64 encode_base64);
Expand Down Expand Up @@ -53,8 +54,8 @@ my %CACHE;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated dumper encode hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(decode deprecated dumper encode files hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode)
Expand Down Expand Up @@ -113,6 +114,17 @@ sub dumper {

sub encode { _encoding($_[0])->encode("$_[1]") }

sub files {
my $dir = shift;

my @files;
return @files unless -d $dir;
my $wanted = sub { -d $File::Find::name or push @files, $File::Find::name };
find {wanted => $wanted, no_chdir => 1}, $dir;

return sort @files;
}

sub hmac_sha1_sum { hmac_sha1_hex @_ }

sub html_unescape {
Expand Down Expand Up @@ -576,6 +588,15 @@ Dump a Perl data structure with L<Data::Dumper>.
Encode characters to bytes.
=head2 files
my @files = files '/tmp/uploads';
List all files recursively in a directory.
# List all templates
say for files '/home/sri/myapp/templates';
=head2 hmac_sha1_sum
my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -215,8 +215,7 @@ sub warmup {

# Handlers for templates
s/\.(\w+)$// and push @{$templates->{$_}}, $1
for map { sort @{Mojo::Home->new($_)->list_files} } @{$self->paths},
$TEMPLATES;
for map { @{Mojo::Home->new($_)->list_files} } @{$self->paths}, $TEMPLATES;

# Handlers and classes for DATA templates
for my $class (reverse @{$self->classes}) {
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/home.t
Expand Up @@ -4,7 +4,7 @@ BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }

use Test::More;
use Cwd qw(cwd realpath);
use File::Spec::Functions qw(canonpath catdir splitdir);
use File::Spec::Functions qw(canonpath catdir catfile splitdir);
use FindBin;
use List::Util 'first';
use Mojo::HelloWorld;
Expand Down Expand Up @@ -39,10 +39,10 @@ is_deeply [split /\\|\//, catdir(splitdir($FindBin::Bin))],
# Path generation
$home = Mojo::Home->new($FindBin::Bin);
is $home->lib_dir, catdir(splitdir($FindBin::Bin), 'lib'), 'right path';
is $home->rel_file('foo.txt'), catdir(splitdir($FindBin::Bin), 'foo.txt'),
is $home->rel_file('foo.txt'), catfile(splitdir($FindBin::Bin), 'foo.txt'),
'right path';
is $home->rel_file('foo/bar.txt'),
catdir(splitdir($FindBin::Bin), 'foo', 'bar.txt'), 'right path';
catfile(splitdir($FindBin::Bin), 'foo', 'bar.txt'), 'right path';
is $home->rel_dir('foo'), catdir(splitdir($FindBin::Bin), 'foo'), 'right path';
is $home->rel_dir('foo/bar'), catdir(splitdir($FindBin::Bin), 'foo', 'bar'),
'right path';
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/hypnotoad.t
Expand Up @@ -7,7 +7,7 @@ use Test::More;
plan skip_all => 'set TEST_HYPNOTOAD to enable this test (developer only!)'
unless $ENV{TEST_HYPNOTOAD};

use File::Spec::Functions 'catdir';
use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
Expand Down Expand Up @@ -57,8 +57,8 @@ use Mojo::Util qw(slurp spurt);

# Prepare script
my $dir = tempdir CLEANUP => 1;
my $script = catdir $dir, 'myapp.pl';
my $log = catdir $dir, 'mojo.log';
my $script = catfile $dir, 'myapp.pl';
my $log = catfile $dir, 'mojo.log';
my $port1 = Mojo::IOLoop::Server->generate_port;
my $port2 = Mojo::IOLoop::Server->generate_port;
spurt <<EOF, $script;
Expand Down Expand Up @@ -216,7 +216,7 @@ like $log, qr/Starting zero downtime software upgrade/, 'right message';
like $log, qr/Upgrade successful, stopping $old/, 'right message';

sub _pid {
return undef unless open my $file, '<', catdir($dir, 'hypnotoad.pid');
return undef unless open my $file, '<', catfile($dir, 'hypnotoad.pid');
my $pid = <$file>;
chomp $pid;
return $pid;
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/log.t
@@ -1,14 +1,14 @@
use Mojo::Base -strict;

use Test::More;
use File::Spec::Functions 'catdir';
use File::Spec::Functions 'catfile';
use File::Temp 'tempdir';
use Mojo::Log;
use Mojo::Util qw(decode slurp);

# Logging to file
my $dir = tempdir CLEANUP => 1;
my $path = catdir $dir, 'test.log';
my $path = catfile $dir, 'test.log';
my $log = Mojo::Log->new(level => 'error', path => $path);
$log->error('Just works');
$log->fatal('I ♥ Mojolicious');
Expand Down
16 changes: 13 additions & 3 deletions t/mojo/morbo.t
Expand Up @@ -7,7 +7,8 @@ use Test::More;
plan skip_all => 'set TEST_MORBO to enable this test (developer only!)'
unless $ENV{TEST_MORBO};

use File::Spec::Functions 'catdir';
use File::Path 'mkpath';
use File::Spec::Functions qw(catdir catfile);
use File::Temp 'tempdir';
use FindBin;
use IO::Socket::INET;
Expand All @@ -20,8 +21,10 @@ use Socket qw(SO_REUSEPORT SOL_SOCKET);

# Prepare script
my $dir = tempdir CLEANUP => 1;
my $script = catdir $dir, 'myapp.pl';
my $morbo = Mojo::Server::Morbo->new(watch => [$script]);
my $script = catfile $dir, 'myapp.pl';
my $subdir = catdir $dir, 'test', 'stuff';
mkpath $subdir;
my $morbo = Mojo::Server::Morbo->new(watch => [$subdir, $script]);
is $morbo->check, undef, 'file has not changed';
spurt <<EOF, $script;
use Mojolicious::Lite;
Expand Down Expand Up @@ -111,6 +114,13 @@ ok $tx->is_finished, 'transaction is finished';
is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello!', 'right content';

# New file
is $morbo->check, undef, 'directory has not changed';
my $new = catfile $subdir, 'test.txt';
spurt 'whatever', $new;
is $morbo->check, $new, 'directory has changed';
is $morbo->check, undef, 'directory has not changed again';

# Stop
kill 'INT', $pid;
sleep 1 while _port($port);
Expand Down
17 changes: 15 additions & 2 deletions t/mojo/util.t
Expand Up @@ -5,14 +5,14 @@ use lib "$FindBin::Bin/lib";

use Test::More;
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use File::Spec::Functions qw(catdir catfile);
use File::Temp 'tempdir';
use Mojo::ByteStream 'b';
use Mojo::DeprecationTest;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode dumper encode hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(decode dumper encode files hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
Expand Down Expand Up @@ -411,6 +411,19 @@ my $file = catfile $dir, 'test.txt';
spurt "just\nworks!", $file;
is slurp($file), "just\nworks!", 'successful roundtrip';

# files
is_deeply [files('does_not_exist')], [], 'no files';
is_deeply [files('util.t')], [], 'no files';
my $lib = catdir dirname(__FILE__), 'lib', 'Mojo';
my @files = map { catfile $lib, split '/' } (
'BaseTest/Base1.pm', 'BaseTest/Base2.pm',
'BaseTest/Base3.pm', 'DeprecationTest.pm',
'LoaderException.pm', 'LoaderException2.pm',
'LoaderTest/A.pm', 'LoaderTest/B.pm',
'LoaderTest/C.pm'
);
is_deeply [files($lib)], \@files, 'right files';

# steady_time
like steady_time, qr/^[\d.]+$/, 'high resolution time';

Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/app.t
Expand Up @@ -11,7 +11,7 @@ use Test::More;
use FindBin;
use lib "$FindBin::Bin/lib";

use File::Spec::Functions 'catdir';
use File::Spec::Functions 'catfile';
use Mojo::Asset::File;
use Mojo::Date;
use Mojo::IOLoop;
Expand Down Expand Up @@ -389,7 +389,7 @@ like $log, qr/Controller "MojoliciousTest::Another" does not exist/,
$t->app->log->unsubscribe(message => $cb);

# Check Last-Modified header for static files
my $path = catdir($FindBin::Bin, 'public_dev', 'hello.txt');
my $path = catfile($FindBin::Bin, 'public_dev', 'hello.txt');
my $size = Mojo::Asset::File->new(path => $path)->size;
my $mtime
= Mojo::Date->new(Mojo::Asset::File->new(path => $path)->mtime)->to_string;
Expand Down

0 comments on commit 17ace53

Please sign in to comment.