Remove that idiocy
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ScriptRole.pm
CommitLineData
d3082fac 1package Catalyst::ScriptRole;
2use Moose::Role;
3use MooseX::Types::Moose qw/Str Bool/;
4use Pod::Usage;
0e4038c6 5use MooseX::Getopt;
532f0516 6use Catalyst::Engine::Loader;
7use MooseX::Types::LoadableClass qw/LoadableClass/;
d3082fac 8use namespace::autoclean;
9
19529022 10with 'MooseX::Getopt' => {
11 excludes => [qw/
12 _getopt_spec_warnings
13 _getopt_spec_exception
2b8d5598 14 _getopt_full_usage
19529022 15 /],
16};
d3082fac 17
18has application_name => (
ad8b4c91 19 traits => ['NoGetopt'],
20 isa => Str,
21 is => 'ro',
d3082fac 22 required => 1,
23);
24
25has help => (
ad8b4c91 26 traits => ['Getopt'],
27 isa => Bool,
28 is => 'ro',
29 documentation => 'Display this help and exit',
30 cmd_aliases => ['?', 'h'],
d3082fac 31);
32
532f0516 33has loader_class => (
34 isa => LoadableClass,
35 is => 'ro',
36 coerce => 1,
37 default => 'Catalyst::Engine::Loader',
38 documentation => 'The class to use to detect and load the PSGI engine',
39);
40
41has _loader => (
42 isa => 'Plack::Loader',
43 default => sub {
44 shift->loader_class->new
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
2b8d5598 60sub _getopt_full_usage {
d3082fac 61 my $self = shift;
62 pod2usage();
63 exit 0;
64}
65
66before run => sub {
67 my $self = shift;
2b8d5598 68 $self->_getopt_full_usage if $self->help;
d3082fac 69};
70
71sub run {
72 my $self = shift;
73 $self->_run_application;
74}
75
76sub _application_args {
77 ()
78}
79
c821df21 80sub _plack_loader_args {
81 my @app_args = shift->_application_args;
82 return (port => $app_args[0]);
83}
84
d3082fac 85sub _run_application {
86 my $self = shift;
87 my $app = $self->application_name;
88 Class::MOP::load_class($app);
c821df21 89 my $server;
90 if (my $e = $self->can('_plack_engine_name') ) {
532f0516 91 $server = $self->load_engine($self->$e, $self->_plack_loader_args);
c821df21 92 }
93 else {
532f0516 94 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 95 }
4b0f97fc 96 $app->run($self->_application_args, $server);
d3082fac 97}
98
991;
1628b022 100
101=head1 NAME
102
103Catalyst::ScriptRole - Common functionality for Catalyst scripts.
104
105=head1 SYNOPSIS
106
12aa6ca4 107 package MyApp::Script::Foo;
108 use Moose;
109 use namespace::autoclean;
111d3c9a 110
2c298960 111 with 'Catalyst::ScriptRole';
111d3c9a 112
ad8b4c91 113 sub _application_args { ... }
111d3c9a 114
1628b022 115=head1 DESCRIPTION
116
12aa6ca4 117Role with the common functionality of Catalyst scripts.
118
119=head1 METHODS
120
121=head2 run
122
123The method invoked to run the application.
124
125=head1 ATTRIBUTES
126
127=head2 application_name
128
129The name of the application class, e.g. MyApp
130
131=head1 SEE ALSO
132
133L<Catalyst>
134
135L<MooseX::Getopt>
1628b022 136
137=head1 AUTHORS
138
139Catalyst Contributors, see Catalyst.pm
140
141=head1 COPYRIGHT
142
143This library is free software, you can redistribute it and/or modify
144it under the same terms as Perl itself.
145
146=cut