conversion to Path::Tiny
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Invocation / Plugin / Restart / Auto.pm
CommitLineData
3fe76927 1package MooseX::Runnable::Invocation::Plugin::Restart::Auto;
2use Moose::Role;
3use MooseX::Types;
4use MooseX::Types::Moose qw(ArrayRef RegexpRef Any Str);
29f9c8cc 5use MooseX::Types::Path::Tiny qw(Path);
6use Path::Tiny; # exports path()
3fe76927 7use File::ChangeNotify;
2828ce0c 8use namespace::autoclean;
3fe76927 9
10# coerce ArrayRef[Dir], from ArrayRef[Any], via {[
11# map { warn $_; Path::Class::dir($_) } @$_,
12# ]};
13
14coerce RegexpRef, from Str, via { qr/$_/i };
15
3fe76927 16
2828ce0c 17with 'MooseX::Runnable::Invocation::Plugin::Restart::Base',
18 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs';
3fe76927 19
20has 'watch_regexp' => (
21 is => 'ro',
22 isa => RegexpRef,
23 required => 1,
24 coerce => 1,
25 default => sub { qr/^[^.].+[.]pmc?$/i },
26);
27
28has 'watch_directories' => (
29 is => 'ro',
29f9c8cc 30 isa => ArrayRef[Path],
3fe76927 31 required => 1,
32 coerce => 1,
29f9c8cc 33 default => sub { [path('.')] },
3fe76927 34);
35
36has 'watcher' => (
37 is => 'ro',
38 isa => 'File::ChangeNotify::Watcher',
39 lazy_build => 1,
40);
41
42sub _build_initargs_from_cmdline {
43 my ($self, @args) = @_;
44
45 my $regexp;
46 my @dirs;
47 my $next_type;
48 for my $arg (@args){
49 # if($arg eq '--inc'){
50 # push @dirs, @INC;
51 # }
52 if($arg eq '--dir'){
53 $next_type = 'dir';
54 }
55 elsif($arg eq '--regexp' || $arg eq '--regex'){
56 # i call them regexps, other people call them "regexen" :P
57 $next_type = 'regexp';
58 }
59 elsif($next_type eq 'dir'){
60 push @dirs, $arg;
61 }
62 elsif($next_type eq 'regexp'){
63 $regexp = $arg;
64 }
65 else {
66 confess 'Invalid args passed to Restart::Auto';
67 }
68 }
69 my %result;
29f9c8cc 70 $result{watch_directories} = [map { path($_) } @dirs] if @dirs;
3fe76927 71 $result{watch_regexp} = $regexp if $regexp;
72 return \%result;
73}
74
75sub _build_watcher {
76 my $self = shift;
77 my $w = File::ChangeNotify->instantiate_watcher(
78 directories => [map { $_->stringify } @{$self->watch_directories}],
79 filter => $self->watch_regexp,
80 );
81
82 return $w;
83}
84
85sub run_parent_loop {
86 my $self = shift;
87 while(1){
88 my @events = $self->watcher->wait_for_events();
89 $self->restart;
3fe76927 90 }
91}
92
931;