Skip to content

Commit

Permalink
fix a few more issues with the deprecation of Mojo::Server::Morbo::check
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 8, 2016
1 parent 4cdc302 commit 1a1f107
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 34 deletions.
9 changes: 6 additions & 3 deletions Changes
@@ -1,7 +1,10 @@

6.55 2016-03-07
- Added modified_files method to Mojo::Server::Morbo.
- Deprecated Mojo::Server::Morbo::check.
6.55 2016-03-08
- Deprecated Mojo::Server::Morbo::check in favor of
Mojo::Server::Morbo::modified_files. (leejo, nugged)
- Added modified_files method to Mojo::Server::Morbo. (leejo, nugged)
- Fixed a bug where Morbo would restart more than once if multiple files
changed at the same time. (leejo, nugged)

6.54 2016-03-06
- Deprecated Mojo::Template::build and Mojo::Template::compile.
Expand Down
44 changes: 21 additions & 23 deletions lib/Mojo/Server/Morbo.pm
Expand Up @@ -5,25 +5,24 @@ use Mojo::Base -base;
# effects of sudden, intense global warming.
# Morbo: Morbo is pleased but sticky."
use Mojo::Server::Daemon;
use Mojo::Util qw(files deprecated);
use Mojo::Util qw(deprecated files);
use POSIX 'WNOHANG';

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

sub modified_files {
my $self = shift;
my @files;
for (map { -f $_ && -r _ ? $_ : files $_ } @{$self->watch}) {
push @files, $_ if $self->_check($_);
}
return [@files];
}

# DEPRECATED!
sub check {
deprecated 'Mojo::Server::Morbo::check is DEPRECATED';
return (@{$_[0]->modified_files // []})[0];
deprecated 'Mojo::Server::Morbo::check is DEPRECATED'
. ' in favor of Mojo::Server::Morbo::modified_files';
return shift->modified_files->[0];
}

sub modified_files {
my $self = shift;
my @files = grep { $self->_check($_) }
map { -f $_ && -r _ ? $_ : files $_ } @{$self->watch};
return \@files;
}

sub run {
Expand Down Expand Up @@ -59,15 +58,11 @@ sub _check {
sub _manage {
my $self = shift;

if (my @files = @{$self->modified_files//[]}) {
if($ENV{MORBO_VERBOSE}) {
if(@files == 1) {
say "File @{[$files[0]]} changed, restarting.";
}
else {
say scalar(@files).qq{ files changed, restarting.};
}
}
if (my @files = @{$self->modified_files}) {
say @files == 1
? "File @{[$files[0]]} changed, restarting."
: "@{[scalar @files]} files changed, restarting."
if $ENV{MORBO_DEBUG};
kill 'TERM', $self->{worker} if $self->{worker};
$self->{modified} = 1;
}
Expand Down Expand Up @@ -171,8 +166,11 @@ the following new ones.
my $files = $morbo->modified_files;
Check if files from L</"watch"> have been modified since last check and return
an array ref of file names, or C<undef> if there have been no changes.
Check if files from L</"watch"> have been modified since the last check and
return an array reference with the results.
# All files that have been modified
say for @{$morbo->modified_files};
=head2 run
Expand Down
4 changes: 4 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -791,6 +791,8 @@ Andreas Koenig
Andrew Fresh
Andrew Nugged
Andrey Khozov
Andrey Kuzmin
Expand Down Expand Up @@ -905,6 +907,8 @@ Klaus S. Madsen
Lars Balker Rasmussen
Lee Johnson
Leon Brocard
Magnus Holm
Expand Down
15 changes: 7 additions & 8 deletions t/mojo/morbo.t
Expand Up @@ -25,7 +25,7 @@ my $script = catfile $dir, 'myapp.pl';
my $subdir = catdir $dir, 'test', 'stuff';
mkpath $subdir;
my $morbo = Mojo::Server::Morbo->new(watch => [$subdir, $script]);
ok !@{$morbo->modified_files}, 'file has not changed';
is_deeply $morbo->modified_files, [], 'no files have changed';
spurt <<EOF, $script;
use Mojolicious::Lite;
Expand Down Expand Up @@ -67,7 +67,7 @@ get '/hello' => {text => 'Hello World!'};
app->start;
EOF
is $morbo->modified_files->[0], $script, 'file has changed';
is_deeply $morbo->modified_files, [$script], 'file has changed';
ok((stat $script)[9] > $mtime, 'modify time has changed');
is((stat $script)[7], $size, 'still equal size');
sleep 3;
Expand All @@ -86,7 +86,7 @@ is $tx->res->body, 'Hello World!', 'right content';

# Update script without changing mtime
($size, $mtime) = (stat $script)[7, 9];
ok !@{$morbo->modified_files}, 'file has not changed';
is_deeply $morbo->modified_files, [], 'no files have changed';
spurt <<EOF, $script;
use Mojolicious::Lite;
Expand All @@ -97,7 +97,7 @@ get '/hello' => {text => 'Hello!'};
app->start;
EOF
utime $mtime, $mtime, $script;
is $morbo->modified_files->[0], $script, 'file has changed';
is_deeply $morbo->modified_files, [$script], 'file has changed';
ok((stat $script)[9] == $mtime, 'modify time has not changed');
isnt((stat $script)[7], $size, 'size has changed');
sleep 3;
Expand All @@ -115,12 +115,11 @@ is $tx->res->code, 200, 'right status';
is $tx->res->body, 'Hello!', 'right content';

# New file(s)
ok !@{$morbo->modified_files}, 'directory has not changed';
is_deeply $morbo->modified_files, [], 'directory has not changed';
my @new = map { catfile $subdir, "$_.txt" } qw/test testing/;
spurt 'whatever', $_ for @new;
my %modified_files = map { $_ => 1 } @{$morbo->modified_files};
ok $modified_files{$_}, 'directory has changed' for @new;
ok !@{$morbo->modified_files}, 'directory has not changed again';
is_deeply $morbo->modified_files, \@new, 'two files have changed';
is_deeply $morbo->modified_files, [], 'directory has not changed again';

# Stop
kill 'INT', $pid;
Expand Down
1 change: 1 addition & 0 deletions t/pod_coverage.t
Expand Up @@ -11,6 +11,7 @@ my %RULES = (
'Mojo::IOLoop' => {also_private => ['multi_accept']},
'Mojo::IOLoop::Server' => {also_private => ['multi_accept']},
'Mojo::Server::Daemon' => {also_private => ['multi_accept']},
'Mojo::Server::Morbo' => {also_private => ['check']},
'Mojo::Template' => {also_private => ['build', 'compile', 'interpret']},
'Mojo::Transaction::WebSocket' =>
{also_private => [qw(build_frame parse_frame)]},
Expand Down

0 comments on commit 1a1f107

Please sign in to comment.