1 package MooseX::Runnable::Invocation;
3 use MooseX::Types -declare => ['RunnableClass'];
4 use MooseX::Types::Moose qw(Str HashRef ArrayRef);
5 use namespace::autoclean;
9 # we can't load the class until plugins are loaded,
10 # so we have to handle this outside of coerce
12 subtype RunnableClass,
14 where { $_ =~ /^[:A-Za-z_]+$/ };
17 with 'MooseX::Runnable'; # this class technically follows
18 # MX::Runnable's protocol
28 isa => HashRef[ArrayRef[Str]],
29 default => sub { +{} },
37 # it would be nice to use MX::Object::Pluggable, but our plugins
38 # are too configurable
40 my $plugin_ns = 'MooseX::Runnable::Invocation::Plugin::';
41 for my $plugin (keys %{$self->plugins}){
43 $plugin = "$plugin_ns$plugin" unless $plugin =~ /^[+]/;
46 Class::MOP::load_class( $plugin );
49 if($plugin->meta->does_role('MooseX::Runnable::Invocation::Plugin::Role::CmdlineArgs')){
51 $plugin->_build_initargs_from_cmdline(
52 @{$self->plugins->{$orig}},
57 confess "Error building initargs for $plugin: $@";
63 defined $args ? (rebless_params => $args) : (),
70 my $class = $self->class;
72 Class::MOP::load_class( $class );
74 confess 'We can only work with Moose classes with "meta" methods'
75 if !$class->can('meta');
77 my $meta = $class->meta;
79 confess "The metaclass of $class is not a Moose::Meta::Class, it's $meta"
80 unless $meta->isa('Moose::Meta::Class');
82 confess 'MooseX::Runnable can only run classes tagged with '.
83 'the MooseX::Runnable role'
84 unless $meta->does_role('MooseX::Runnable');
90 my ($self, $class) = @_;
92 my @schemes = grep { defined } map {
93 $self->_convert_role_to_scheme($_)
94 } $class->calculate_all_roles;
97 foreach my $scheme (@schemes) {
98 $scheme->apply($self);
104 sub _convert_role_to_scheme {
105 my ($self, $role) = @_;
107 my $name = $role->name;
108 return if $name =~ /\|/;
109 $name = "MooseX::Runnable::Invocation::Scheme::$name";
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');
120 my ($self, $class) = @_;
122 my @bad_attributes = map { $_->name } grep {
123 $_->is_required && !($_->has_default || $_->has_builder)
124 } $class->get_all_attributes;
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.'.
132 "The class is @{[$class->name]}, and the required attributes are ".
133 join ', ', map { "'$_'" } @bad_attributes
136 return; # return value is meaningless
139 sub create_instance {
140 my ($self, $class, @args) = @_;
141 return ($class->name->new, @args);
144 sub start_application {
146 my $instance = shift;
149 return $instance->run(@args);
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);