Compiler-Parser
view release on metacpan or search on metacpan
t/app/Plack/Loader/Restarter.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Compiler::Lexer;
use Compiler::Parser;
use Compiler::Parser::AST::Renderer;
use Test::Compiler::Parser;
subtest 'parse Plack/Loader/Restarter.pm' => sub {
my $script = do { local $/; <DATA> };
my $tokens = Compiler::Lexer->new('')->tokenize($script);
my $ast = Compiler::Parser->new->parse($tokens);
Compiler::Parser::AST::Renderer->new->render($ast);
node_ok($ast->root, [
Test::Compiler::Parser::package { 'Plack::Loader::Restarter',
},
module { 'strict',
},
module { 'warnings',
},
module { 'parent',
args => reg_prefix { 'qw',
expr => leaf 'Plack::Loader',
},
},
module { 'Plack::Util',
},
module { 'Try::Tiny',
},
function { 'new',
body => [
branch { '=',
left => list { '()',
data => branch { ',',
left => leaf '$class',
right => leaf '$runner',
},
},
right => leaf '@_',
},
function_call { 'bless',
args => [
branch { ',',
left => hash_ref { '{}',
data => branch { '=>',
left => leaf 'watch',
right => array_ref { '[]',
},
},
},
right => leaf '$class',
},
],
},
],
},
function { 'preload_app',
body => [
branch { '=',
left => list { '()',
data => branch { ',',
left => leaf '$self',
right => leaf '$builder',
},
},
right => leaf '@_',
},
branch { '=',
left => branch { '->',
left => leaf '$self',
right => hash_ref { '{}',
data => leaf 'builder',
},
},
right => leaf '$builder',
},
],
},
function { 'watch',
body => [
branch { '=',
left => list { '()',
data => branch { ',',
left => leaf '$self',
right => leaf '@dir',
},
},
right => leaf '@_',
t/app/Plack/Loader/Restarter.t view on Meta::CPAN
foreach_stmt { 'for',
cond => leaf '@restart',
true_stmt => function_call { 'warn',
args => [
leaf '-- $ev->{path} updated.\n',
],
},
itr => leaf '$ev',
},
branch { '->',
left => leaf '$self',
right => function_call { '_kill_child',
args => [
],
},
},
function_call { 'warn',
args => [
leaf 'Successfully killed! Restarting the new server process.\n',
],
},
branch { '->',
left => leaf '$self',
right => function_call { '_fork_and_start',
args => [
list { '()',
data => branch { ',',
left => leaf '$server',
right => leaf '$builder',
},
},
],
},
},
if_stmt { 'unless',
expr => branch { '->',
left => leaf '$self',
right => hash_ref { '{}',
data => leaf 'pid',
},
},
true_stmt => Test::Compiler::Parser::return { 'return',
},
},
],
},
],
},
leaf '1',
]);
};
done_testing;
__DATA__
package Plack::Loader::Restarter;
use strict;
use warnings;
use parent qw(Plack::Loader);
use Plack::Util;
use Try::Tiny;
sub new {
my($class, $runner) = @_;
bless { watch => [] }, $class;
}
sub preload_app {
my($self, $builder) = @_;
$self->{builder} = $builder;
}
sub watch {
my($self, @dir) = @_;
push @{$self->{watch}}, @dir;
}
sub _fork_and_start {
my($self, $server) = @_;
delete $self->{pid}; # re-init in case it's a restart
my $pid = fork;
die "Can't fork: $!" unless defined $pid;
if ($pid == 0) { # child
return $server->run($self->{builder}->());
} else {
$self->{pid} = $pid;
}
}
sub _kill_child {
my $self = shift;
my $pid = $self->{pid} or return;
warn "Killing the existing server (pid:$pid)\n";
kill 'TERM' => $pid;
waitpid($pid, 0);
}
sub valid_file {
my($self, $file) = @_;
# vim temporary file is 4913 to 5036
# http://www.mail-archive.com/vim_dev@googlegroups.com/msg07518.html
if ( $file->{path} =~ m{(\d+)$} && $1 >= 4913 && $1 <= 5036) {
return 0;
}
$file->{path} !~ m!\.(?:git|svn)[/\\]|\.(?:bak|swp|swpx|swx)$|~$|_flymake\.p[lm]$|\.#!;
}
sub run {
my($self, $server, $builder) = @_;
$self->_fork_and_start($server, $builder);
return unless $self->{pid};
require Filesys::Notify::Simple;
my $watcher = Filesys::Notify::Simple->new($self->{watch});
warn "Watching @{$self->{watch}} for file updates.\n";
( run in 0.633 second using v1.01-cache-2.11-cpan-5fbc6bb55f2 )