fix tests; make default value for plugins {}
[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
48 my $args = eval {
49 $plugin->_build_initargs_from_cmdline(
50 @{$self->plugins->{$orig}},
51 );
52 };
53 if($@ && $plugin->can('_build_initargs_from_cmdline')){
54 confess "Error building initargs for $plugin: $@";
55 }
56
57 $plugin->meta->apply(
58 $self,
59 defined $args ? (rebless_params => $args) : (),
60 );
61 }
c527660e 62}
63
64sub load_class {
65 my $self = shift;
66 my $class = $self->class;
67
68 Class::MOP::load_class( $class );
69
70 confess 'We can only work with Moose classes with "meta" methods'
71 if !$class->can('meta');
72
73 my $meta = $class->meta;
74
75 confess "The metaclass of $class is not a Moose::Meta::Class, it's $meta"
76 unless $meta->isa('Moose::Meta::Class');
77
78 confess 'MooseX::Runnable can only run classes tagged with '.
79 'the MooseX::Runnable role'
80 unless $meta->does_role('MooseX::Runnable');
81
82 return $meta;
83}
84
85sub apply_scheme {
86 my ($self, $class) = @_;
87
88 my @schemes = grep { defined } map {
89 $self->_convert_role_to_scheme($_)
90 } $class->calculate_all_roles;
91
26041d41 92 eval {
93 foreach my $scheme (@schemes) {
94 $scheme->apply($self);
95 }
96 };
c527660e 97}
98
780724cb 99
100sub _convert_role_to_scheme {
101 my ($self, $role) = @_;
102
103 my $name = $role->name;
104 return if $name =~ /\|/;
105 $name = "MooseX::Runnable::Invocation::Scheme::$name";
106
107 return eval {
108 Class::MOP::load_class($name);
109 warn "$name was loaded OK, but it's not a role!" and return
110 unless $name->meta->isa('Moose::Meta::Role');
111 return $name->meta;
112 };
113}
114
c527660e 115sub validate_class {
116 my ($self, $class) = @_;
117
118 my @bad_attributes = map { $_->name } grep {
0108a926 119 $_->is_required && !($_->has_default || $_->has_builder)
00d7989a 120 } $class->get_all_attributes;
c527660e 121
122 confess
123 'By default, MooseX::Runnable calls the constructor with no'.
124 ' args, but that will result in an error for your class. You'.
125 ' need to provide a MooseX::Runnable::Invocation::Plugin or'.
126 ' ::Scheme for this class that will satisfy the requirements.'.
127 "\n".
128 "The class is @{[$class->name]}, and the required attributes are ".
129 join ', ', map { "'$_'" } @bad_attributes
130 if @bad_attributes;
131
132 return; # return value is meaningless
133}
134
c527660e 135sub create_instance {
136 my ($self, $class, @args) = @_;
137 return ($class->name->new, @args);
138}
139
140sub start_application {
141 my $self = shift;
142 my $instance = shift;
143 my @args = @_;
144
145 return $instance->run(@args);
146}
147
148sub run {
149 my $self = shift;
150 my @args = @_;
151
152 my $class = $self->load_class;
153 $self->apply_scheme($class);
154 $self->validate_class($class);
155 my ($instance, @more_args) = $self->create_instance($class, @args);
156 my $exit_code = $self->start_application($instance, @more_args);
157 return $exit_code;
158}
159
1601;