Work towards supporting psgi.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
CommitLineData
d3082fac 1package Catalyst::ScriptRole;
2use Moose::Role;
44d28c7d 3use Plack::Runner;
d3082fac 4use MooseX::Types::Moose qw/Str Bool/;
5use Pod::Usage;
0e4038c6 6use MooseX::Getopt;
d3082fac 7use namespace::autoclean;
8
19529022 9with 'MooseX::Getopt' => {
10 excludes => [qw/
11 _getopt_spec_warnings
12 _getopt_spec_exception
2b8d5598 13 _getopt_full_usage
19529022 14 /],
15};
d3082fac 16
17has application_name => (
ad8b4c91 18 traits => ['NoGetopt'],
19 isa => Str,
20 is => 'ro',
d3082fac 21 required => 1,
22);
23
24has help => (
ad8b4c91 25 traits => ['Getopt'],
26 isa => Bool,
27 is => 'ro',
28 documentation => 'Display this help and exit',
29 cmd_aliases => ['?', 'h'],
d3082fac 30);
31
19529022 32sub _getopt_spec_exception {}
33
34sub _getopt_spec_warnings {
35 shift;
36 warn @_;
37}
38
2b8d5598 39sub _getopt_full_usage {
d3082fac 40 my $self = shift;
41 pod2usage();
42 exit 0;
43}
44
45before run => sub {
46 my $self = shift;
2b8d5598 47 $self->_getopt_full_usage if $self->help;
d3082fac 48};
49
50sub run {
51 my $self = shift;
52 $self->_run_application;
53}
54
55sub _application_args {
56 ()
57}
58
59sub _run_application {
60 my $self = shift;
61 my $app = $self->application_name;
62 Class::MOP::load_class($app);
44d28c7d 63 my $psgi_app = $app->run($self->_application_args);
64 Plack::Runner->run('--app' => $psgi_app);
d3082fac 65}
66
671;
1628b022 68
69=head1 NAME
70
71Catalyst::ScriptRole - Common functionality for Catalyst scripts.
72
73=head1 SYNOPSIS
74
12aa6ca4 75 package MyApp::Script::Foo;
76 use Moose;
77 use namespace::autoclean;
111d3c9a 78
2c298960 79 with 'Catalyst::ScriptRole';
111d3c9a 80
ad8b4c91 81 sub _application_args { ... }
111d3c9a 82
1628b022 83=head1 DESCRIPTION
84
12aa6ca4 85Role with the common functionality of Catalyst scripts.
86
87=head1 METHODS
88
89=head2 run
90
91The method invoked to run the application.
92
93=head1 ATTRIBUTES
94
95=head2 application_name
96
97The name of the application class, e.g. MyApp
98
99=head1 SEE ALSO
100
101L<Catalyst>
102
103L<MooseX::Getopt>
1628b022 104
105=head1 AUTHORS
106
107Catalyst Contributors, see Catalyst.pm
108
109=head1 COPYRIGHT
110
111This library is free software, you can redistribute it and/or modify
112it under the same terms as Perl itself.
113
114=cut