work with MooseX::Getopt 0.48 by using the new public method for printing usage
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
CommitLineData
d3082fac 1package Catalyst::ScriptRole;
2use Moose::Role;
d3082fac 3use Pod::Usage;
0e4038c6 4use MooseX::Getopt;
b1ededd4 5use Catalyst::EngineLoader;
8c848c33 6use Moose::Util::TypeConstraints;
7use Catalyst::Utils qw/ ensure_class_loaded /;
d3082fac 8use namespace::autoclean;
9
8c848c33 10subtype 'Catalyst::ScriptRole::LoadableClass',
11 as 'ClassName';
12coerce 'Catalyst::ScriptRole::LoadableClass',
13 from 'Str',
14 via { ensure_class_loaded($_); 1 };
15
19529022 16with 'MooseX::Getopt' => {
8ba29999 17 -version => 0.48,
9c74923d 18 -excludes => [qw/
19529022 19 _getopt_spec_warnings
20 _getopt_spec_exception
8ba29999 21 print_usage_text
19529022 22 /],
23};
d3082fac 24
25has application_name => (
ad8b4c91 26 traits => ['NoGetopt'],
8c848c33 27 isa => 'Str',
ad8b4c91 28 is => 'ro',
d3082fac 29 required => 1,
30);
31
532f0516 32has loader_class => (
8c848c33 33 isa => 'Catalyst::ScriptRole::LoadableClass',
532f0516 34 is => 'ro',
35 coerce => 1,
b1ededd4 36 default => 'Catalyst::EngineLoader',
532f0516 37 documentation => 'The class to use to detect and load the PSGI engine',
38);
39
40has _loader => (
41 isa => 'Plack::Loader',
42 default => sub {
acbecf08 43 my $self = shift;
44 $self->loader_class->new(application_name => $self->application_name);
532f0516 45 },
46 handles => {
47 load_engine => 'load',
48 autoload_engine => 'auto',
49 },
50 lazy => 1,
51);
52
19529022 53sub _getopt_spec_exception {}
54
55sub _getopt_spec_warnings {
56 shift;
57 warn @_;
58}
59
8ba29999 60=for Pod::Coverage
61 print_usage_text
62=cut
63sub print_usage_text {
d3082fac 64 my $self = shift;
65 pod2usage();
66 exit 0;
67}
68
d3082fac 69sub run {
70 my $self = shift;
71 $self->_run_application;
72}
73
74sub _application_args {
aee7cdcc 75 my $self = shift;
76 return {
77 argv => $self->ARGV,
78 extra_argv => $self->extra_argv,
79 }
d3082fac 80}
81
c821df21 82sub _plack_loader_args {
acbecf08 83 my $self = shift;
84 my @app_args = $self->_application_args;
c821df21 85 return (port => $app_args[0]);
86}
87
aee7cdcc 88sub _plack_engine_name {}
89
d3082fac 90sub _run_application {
91 my $self = shift;
92 my $app = $self->application_name;
93 Class::MOP::load_class($app);
c821df21 94 my $server;
aee7cdcc 95 if (my $e = $self->_plack_engine_name ) {
96 $server = $self->load_engine($e, $self->_plack_loader_args);
c821df21 97 }
98 else {
532f0516 99 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 100 }
4b0f97fc 101 $app->run($self->_application_args, $server);
d3082fac 102}
103
1041;
1628b022 105
106=head1 NAME
107
108Catalyst::ScriptRole - Common functionality for Catalyst scripts.
109
110=head1 SYNOPSIS
111
12aa6ca4 112 package MyApp::Script::Foo;
113 use Moose;
114 use namespace::autoclean;
111d3c9a 115
2c298960 116 with 'Catalyst::ScriptRole';
111d3c9a 117
ad8b4c91 118 sub _application_args { ... }
111d3c9a 119
1628b022 120=head1 DESCRIPTION
121
12aa6ca4 122Role with the common functionality of Catalyst scripts.
123
124=head1 METHODS
125
126=head2 run
127
128The method invoked to run the application.
129
130=head1 ATTRIBUTES
131
132=head2 application_name
133
134The name of the application class, e.g. MyApp
135
136=head1 SEE ALSO
137
138L<Catalyst>
139
140L<MooseX::Getopt>
1628b022 141
142=head1 AUTHORS
143
144Catalyst Contributors, see Catalyst.pm
145
146=head1 COPYRIGHT
147
148This library is free software, you can redistribute it and/or modify
149it under the same terms as Perl itself.
150
151=cut