1 package MooseX::Runnable::Invocation;
3 use MooseX::Types -declare => ['RunnableClass'];
4 use MooseX::Types::Moose qw(Str HashRef ArrayRef);
5 use List::MoreUtils qw(uniq);
6 use Params::Util qw(_CLASS);
7 use namespace::autoclean;
11 # we can't load the class until plugins are loaded,
12 # so we have to handle this outside of coerce
14 subtype RunnableClass,
19 with 'MooseX::Runnable'; # this class technically follows
20 # MX::Runnable's protocol
30 isa => HashRef[ArrayRef[Str]],
31 default => sub { +{} },
39 # it would be nice to use MX::Object::Pluggable, but our plugins
40 # are too configurable
42 my $plugin_ns = 'MooseX::Runnable::Invocation::Plugin::';
43 for my $plugin (keys %{$self->plugins}){
45 $plugin = "$plugin_ns$plugin" unless $plugin =~ /^[+]/;
48 Class::MOP::load_class( $plugin );
50 my $does_cmdline = $plugin->meta->
51 does_role('MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs');
56 $plugin->_build_initargs_from_cmdline(
57 @{$self->plugins->{$orig}},
62 confess "Error building initargs for $plugin: $@";
65 elsif(!$does_cmdline && scalar @{$self->plugins->{$orig}} > 0){
66 confess "You supplied arguments to the $orig plugin, but it".
67 " does not know how to accept them. Perhaps the plugin".
68 " should consume the".
69 " 'MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs'".
75 defined $args ? (rebless_params => $args) : (),
82 my $class = $self->class;
84 Class::MOP::load_class( $class );
86 confess 'We can only work with Moose classes with "meta" methods'
87 if !$class->can('meta');
89 my $meta = $class->meta;
91 confess "The metaclass of $class is not a Moose::Meta::Class, it's $meta"
92 unless $meta->isa('Moose::Meta::Class');
94 confess 'MooseX::Runnable can only run classes tagged with '.
95 'the MooseX::Runnable role'
96 unless $meta->does_role('MooseX::Runnable');
102 my ($self, $class) = @_;
104 my @schemes = grep { defined } map {
105 eval { $self->_convert_role_to_scheme($_) }
107 eval { $_->meta->calculate_all_roles };
108 } $class->linearized_isa;
111 foreach my $scheme (uniq @schemes) {
112 $scheme->apply($self);
118 sub _convert_role_to_scheme {
119 my ($self, $role) = @_;
121 my $name = $role->name;
122 return if $name =~ /\|/;
123 $name = "MooseX::Runnable::Invocation::Scheme::$name";
126 Class::MOP::load_class($name);
127 warn "$name was loaded OK, but it's not a role!" and return
128 unless $name->meta->isa('Moose::Meta::Role');
134 my ($self, $class) = @_;
136 my @bad_attributes = map { $_->name } grep {
137 $_->is_required && !($_->has_default || $_->has_builder)
138 } $class->get_all_attributes;
141 'By default, MooseX::Runnable calls the constructor with no'.
142 ' args, but that will result in an error for your class. You'.
143 ' need to provide a MooseX::Runnable::Invocation::Plugin or'.
144 ' ::Scheme for this class that will satisfy the requirements.'.
146 "The class is @{[$class->name]}, and the required attributes are ".
147 join ', ', map { "'$_'" } @bad_attributes
150 return; # return value is meaningless
153 sub create_instance {
154 my ($self, $class, @args) = @_;
155 return ($class->name->new, @args);
158 sub start_application {
160 my $instance = shift;
163 return $instance->run(@args);
170 my $class = $self->load_class;
171 $self->apply_scheme($class);
172 $self->validate_class($class);
173 my ($instance, @more_args) = $self->create_instance($class, @args);
174 my $exit_code = $self->start_application($instance, @more_args);