Tag plugins that can take cmdline args with Role::CmdlineArgs; add test
Jonathan Rockway [Thu, 25 Jun 2009 18:32:36 +0000 (11:32 -0700)]
lib/MooseX/Runnable/Invocation.pm
lib/MooseX/Runnable/Invocation/Plugin/Debug.pm
lib/MooseX/Runnable/Invocation/Plugin/Restart.pm
lib/MooseX/Runnable/Invocation/Plugin/Restart/Auto.pm
lib/MooseX/Runnable/Invocation/Plugin/Role/CmdlineArgs.pm [new file with mode: 0644]
t/invocation-plugin-initargs.t [new file with mode: 0644]

index 8ff092d..644d246 100644 (file)
@@ -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(
index 4d1ce96..55f80b3 100644 (file)
@@ -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',
index 424cefe..3c98714 100644 (file)
@@ -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',
index a10c5b9..210b833 100644 (file)
@@ -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 (file)
index 0000000..d3afed5
--- /dev/null
@@ -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 (file)
index 0000000..376335d
--- /dev/null
@@ -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';
+
+
+