Tag plugins that can take cmdline args with Role::CmdlineArgs; add test
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Invocation.pm
CommitLineData
c527660e 1package MooseX::Runnable::Invocation;
2use Moose;
3use MooseX::Types -declare => ['RunnableClass'];
a19e41e3 4use MooseX::Types::Moose qw(Str HashRef ArrayRef);
d60733e4 5use namespace::autoclean;
c527660e 6
7require Class::MOP;
8
9# we can't load the class until plugins are loaded,
10# so we have to handle this outside of coerce
11
12subtype RunnableClass,
13 as Str,
14 where { $_ =~ /^[:A-Za-z_]+$/ };
15
c527660e 16
e8020e5e 17with 'MooseX::Runnable'; # this class technically follows
18 # MX::Runnable's protocol
c527660e 19
c527660e 20has 'class' => (
21 is => 'ro',
22 isa => RunnableClass,
23 required => 1,
24);
25
26has 'plugins' => (
27 is => 'ro',
a19e41e3 28 isa => HashRef[ArrayRef[Str]],
528dfa34 29 default => sub { +{} },
c527660e 30 required => 1,
31 auto_deref => 1,
32);
33
34sub BUILD {
35 my $self = shift;
e8020e5e 36
37 # it would be nice to use MX::Object::Pluggable, but our plugins
38 # are too configurable
39
40 my $plugin_ns = 'MooseX::Runnable::Invocation::Plugin::';
41 for my $plugin (keys %{$self->plugins}){
42 my $orig = $plugin;
43 $plugin = "$plugin_ns$plugin" unless $plugin =~ /^[+]/;
44 $plugin =~ s/^[+]//g;
45
46 Class::MOP::load_class( $plugin );
47
2828ce0c 48 my $args;
49 if($plugin->meta->does_role('MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs')){
50 $args = eval {
51 $plugin->_build_initargs_from_cmdline(
52 @{$self->plugins->{$orig}},
53 );
54 };
55
56 if($@) {
57 confess "Error building initargs for $plugin: $@";
58 }
e8020e5e 59 }
60
61 $plugin->meta->apply(
62 $self,
63 defined $args ? (rebless_params => $args) : (),
64 );
65 }
c527660e 66}
67
68sub load_class {
69 my $self = shift;
70 my $class = $self->class;
71
72 Class::MOP::load_class( $class );
73
74 confess 'We can only work with Moose classes with "meta" methods'
75 if !$class->can('meta');
76
77 my $meta = $class->meta;
78
79 confess "The metaclass of $class is not a Moose::Meta::Class, it's $meta"
80 unless $meta->isa('Moose::Meta::Class');
81
82 confess 'MooseX::Runnable can only run classes tagged with '.
83 'the MooseX::Runnable role'
84 unless $meta->does_role('MooseX::Runnable');
85
86 return $meta;
87}
88
89sub apply_scheme {
90 my ($self, $class) = @_;
91
92 my @schemes = grep { defined } map {
93 $self->_convert_role_to_scheme($_)
94 } $class->calculate_all_roles;
95
26041d41 96 eval {
97 foreach my $scheme (@schemes) {
98 $scheme->apply($self);
99 }
100 };
c527660e 101}
102
780724cb 103
104sub _convert_role_to_scheme {
105 my ($self, $role) = @_;
106
107 my $name = $role->name;
108 return if $name =~ /\|/;
109 $name = "MooseX::Runnable::Invocation::Scheme::$name";
110
111 return eval {
112 Class::MOP::load_class($name);
113 warn "$name was loaded OK, but it's not a role!" and return
114 unless $name->meta->isa('Moose::Meta::Role');
115 return $name->meta;
116 };
117}
118
c527660e 119sub validate_class {
120 my ($self, $class) = @_;
121
122 my @bad_attributes = map { $_->name } grep {
0108a926 123 $_->is_required && !($_->has_default || $_->has_builder)
00d7989a 124 } $class->get_all_attributes;
c527660e 125
126 confess
127 'By default, MooseX::Runnable calls the constructor with no'.
128 ' args, but that will result in an error for your class. You'.
129 ' need to provide a MooseX::Runnable::Invocation::Plugin or'.
130 ' ::Scheme for this class that will satisfy the requirements.'.
131 "\n".
132 "The class is @{[$class->name]}, and the required attributes are ".
133 join ', ', map { "'$_'" } @bad_attributes
134 if @bad_attributes;
135
136 return; # return value is meaningless
137}
138
c527660e 139sub create_instance {
140 my ($self, $class, @args) = @_;
141 return ($class->name->new, @args);
142}
143
144sub start_application {
145 my $self = shift;
146 my $instance = shift;
147 my @args = @_;
148
149 return $instance->run(@args);
150}
151
152sub run {
153 my $self = shift;
154 my @args = @_;
155
156 my $class = $self->load_class;
157 $self->apply_scheme($class);
158 $self->validate_class($class);
159 my ($instance, @more_args) = $self->create_instance($class, @args);
160 my $exit_code = $self->start_application($instance, @more_args);
161 return $exit_code;
162}
163
1641;