c3786c786644c89c6a0dc8ff7388d57001dc8670
[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 = 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     }
62 }
63
64 sub 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
85 sub 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
92     eval {
93         foreach my $scheme (@schemes) {
94             $scheme->apply($self);
95         }
96     };
97 }
98
99
100 sub _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
115 sub validate_class {
116     my ($self, $class) = @_;
117
118     my @bad_attributes = map { $_->name } grep {
119         $_->is_required && !($_->has_default || $_->has_builder)
120     } $class->get_all_attributes;
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
135 sub create_instance {
136     my ($self, $class, @args) = @_;
137     return ($class->name->new, @args);
138 }
139
140 sub start_application {
141     my $self = shift;
142     my $instance = shift;
143     my @args = @_;
144
145     return $instance->run(@args);
146 }
147
148 sub 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
160 1;