Tag plugins that can take cmdline args with Role::CmdlineArgs; add test
[gitmo/MooseX-Runnable.git] / lib / MooseX / Runnable / Invocation.pm
1 package MooseX::Runnable::Invocation;
2 use Moose;
3 use MooseX::Types -declare => ['RunnableClass'];
4 use MooseX::Types::Moose qw(Str HashRef ArrayRef);
5 use namespace::autoclean;
6
7 require 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
12 subtype RunnableClass,
13   as Str,
14   where { $_ =~ /^[:A-Za-z_]+$/ };
15
16
17 with 'MooseX::Runnable'; # this class technically follows
18                          # MX::Runnable's protocol
19
20 has 'class' => (
21     is       => 'ro',
22     isa      => RunnableClass,
23     required => 1,
24 );
25
26 has 'plugins' => (
27     is         => 'ro',
28     isa        => HashRef[ArrayRef[Str]],
29     default    => sub { +{} },
30     required   => 1,
31     auto_deref => 1,
32 );
33
34 sub BUILD {
35     my $self = shift;
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;
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             }
59         }
60
61         $plugin->meta->apply(
62             $self,
63             defined $args ? (rebless_params => $args) : (),
64         );
65     }
66 }
67
68 sub 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
89 sub 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
96     eval {
97         foreach my $scheme (@schemes) {
98             $scheme->apply($self);
99         }
100     };
101 }
102
103
104 sub _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
119 sub validate_class {
120     my ($self, $class) = @_;
121
122     my @bad_attributes = map { $_->name } grep {
123         $_->is_required && !($_->has_default || $_->has_builder)
124     } $class->get_all_attributes;
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
139 sub create_instance {
140     my ($self, $class, @args) = @_;
141     return ($class->name->new, @args);
142 }
143
144 sub start_application {
145     my $self = shift;
146     my $instance = shift;
147     my @args = @_;
148
149     return $instance->run(@args);
150 }
151
152 sub 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
164 1;