From: Jonathan Rockway Date: Thu, 25 Jun 2009 18:32:36 +0000 (-0700) Subject: Tag plugins that can take cmdline args with Role::CmdlineArgs; add test X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2828ce0c05df0079ece7c5ffd7c5767d3b666b78;p=gitmo%2FMooseX-Runnable.git Tag plugins that can take cmdline args with Role::CmdlineArgs; add test --- diff --git a/lib/MooseX/Runnable/Invocation.pm b/lib/MooseX/Runnable/Invocation.pm index 8ff092d..644d246 100644 --- a/lib/MooseX/Runnable/Invocation.pm +++ b/lib/MooseX/Runnable/Invocation.pm @@ -45,13 +45,17 @@ sub BUILD { Class::MOP::load_class( $plugin ); - my $args = eval { - $plugin->_build_initargs_from_cmdline( - @{$self->plugins->{$orig}}, - ); - }; - if($@ && $plugin->can('_build_initargs_from_cmdline')){ - confess "Error building initargs for $plugin: $@"; + my $args; + if($plugin->meta->does_role('MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs')){ + $args = eval { + $plugin->_build_initargs_from_cmdline( + @{$self->plugins->{$orig}}, + ); + }; + + if($@) { + confess "Error building initargs for $plugin: $@"; + } } $plugin->meta->apply( diff --git a/lib/MooseX/Runnable/Invocation/Plugin/Debug.pm b/lib/MooseX/Runnable/Invocation/Plugin/Debug.pm index 4d1ce96..55f80b3 100644 --- a/lib/MooseX/Runnable/Invocation/Plugin/Debug.pm +++ b/lib/MooseX/Runnable/Invocation/Plugin/Debug.pm @@ -1,6 +1,8 @@ package MooseX::Runnable::Invocation::Plugin::Debug; use Moose::Role; +with 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs'; + # this is an example to cargo-cult, rather than a useful feature :) has 'debug_prefix' => ( is => 'ro', diff --git a/lib/MooseX/Runnable/Invocation/Plugin/Restart.pm b/lib/MooseX/Runnable/Invocation/Plugin/Restart.pm index 424cefe..3c98714 100644 --- a/lib/MooseX/Runnable/Invocation/Plugin/Restart.pm +++ b/lib/MooseX/Runnable/Invocation/Plugin/Restart.pm @@ -4,7 +4,8 @@ use MooseX::Types::Moose qw(Str); use AnyEvent; use namespace::autoclean; -with 'MooseX::Runnable::Invocation::Plugin::Restart::Base'; +with 'MooseX::Runnable::Invocation::Plugin::Restart::Base', + 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs'; has 'completion_condvar' => ( is => 'ro', diff --git a/lib/MooseX/Runnable/Invocation/Plugin/Restart/Auto.pm b/lib/MooseX/Runnable/Invocation/Plugin/Restart/Auto.pm index a10c5b9..210b833 100644 --- a/lib/MooseX/Runnable/Invocation/Plugin/Restart/Auto.pm +++ b/lib/MooseX/Runnable/Invocation/Plugin/Restart/Auto.pm @@ -4,6 +4,7 @@ use MooseX::Types; use MooseX::Types::Moose qw(ArrayRef RegexpRef Any Str); use MooseX::Types::Path::Class qw(Dir); use File::ChangeNotify; +use namespace::autoclean; # coerce ArrayRef[Dir], from ArrayRef[Any], via {[ # map { warn $_; Path::Class::dir($_) } @$_, @@ -11,9 +12,9 @@ use File::ChangeNotify; coerce RegexpRef, from Str, via { qr/$_/i }; -use namespace::autoclean; -with 'MooseX::Runnable::Invocation::Plugin::Restart::Base'; +with 'MooseX::Runnable::Invocation::Plugin::Restart::Base', + 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs'; has 'watch_regexp' => ( is => 'ro', @@ -85,7 +86,6 @@ sub run_parent_loop { while(1){ my @events = $self->watcher->wait_for_events(); $self->restart; - sleep .5; # "debounce" the notifier } } diff --git a/lib/MooseX/Runnable/Invocation/Plugin/Role/CmdlineArgs.pm b/lib/MooseX/Runnable/Invocation/Plugin/Role/CmdlineArgs.pm new file mode 100644 index 0000000..d3afed5 --- /dev/null +++ b/lib/MooseX/Runnable/Invocation/Plugin/Role/CmdlineArgs.pm @@ -0,0 +1,6 @@ +package MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs; +use Moose::Role; + +requires '_build_initargs_from_cmdline'; + +1; diff --git a/t/invocation-plugin-initargs.t b/t/invocation-plugin-initargs.t new file mode 100644 index 0000000..376335d --- /dev/null +++ b/t/invocation-plugin-initargs.t @@ -0,0 +1,44 @@ +use strict; +use warnings; +use Test::Exception; +use Test::More tests => 4; + +use MooseX::Runnable::Invocation; + +my $initargs; + +{ package Class; + use Moose; + with 'MooseX::Runnable'; + sub run { 42 } +} + +{ package Plugin; + use Moose::Role; + with 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs'; + + has 'init' => ( is => 'ro', required => 1 ); + + sub _build_initargs_from_cmdline { + my $class = shift; + $initargs = join ',', @_; + return { init => 'args' }; + } +} + +my $i; +lives_ok { + $i = MooseX::Runnable::Invocation->new( + class => 'Class', + plugins => { + '+Plugin' => [qw/foo bar baz/], + }, + ); +} 'created invocation without dying'; + +ok $i, 'created invocation ok'; +ok $i->run, 'ran ok'; +is $initargs, 'foo,bar,baz', 'got initargs'; + + +