clean up travis config for distar
[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;
c8e0714d 7use Catalyst::Utils;
d3082fac 8use namespace::autoclean;
9
8c848c33 10subtype 'Catalyst::ScriptRole::LoadableClass',
11 as 'ClassName';
12coerce 'Catalyst::ScriptRole::LoadableClass',
13 from 'Str',
c8e0714d 14 via { Catalyst::Utils::ensure_class_loaded($_); $_ };
8c848c33 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 60sub print_usage_text {
d3082fac 61 my $self = shift;
62 pod2usage();
63 exit 0;
64}
65
d3082fac 66sub run {
67 my $self = shift;
68 $self->_run_application;
69}
70
71sub _application_args {
aee7cdcc 72 my $self = shift;
73 return {
74 argv => $self->ARGV,
75 extra_argv => $self->extra_argv,
76 }
d3082fac 77}
78
c821df21 79sub _plack_loader_args {
acbecf08 80 my $self = shift;
81 my @app_args = $self->_application_args;
c821df21 82 return (port => $app_args[0]);
83}
84
aee7cdcc 85sub _plack_engine_name {}
86
d3082fac 87sub _run_application {
88 my $self = shift;
89 my $app = $self->application_name;
c8e0714d 90 Catalyst::Utils::ensure_class_loaded($app);
c821df21 91 my $server;
aee7cdcc 92 if (my $e = $self->_plack_engine_name ) {
93 $server = $self->load_engine($e, $self->_plack_loader_args);
c821df21 94 }
95 else {
532f0516 96 $server = $self->autoload_engine($self->_plack_loader_args);
c821df21 97 }
4b0f97fc 98 $app->run($self->_application_args, $server);
d3082fac 99}
100
1011;
1628b022 102
103=head1 NAME
104
105Catalyst::ScriptRole - Common functionality for Catalyst scripts.
106
107=head1 SYNOPSIS
108
12aa6ca4 109 package MyApp::Script::Foo;
110 use Moose;
111 use namespace::autoclean;
111d3c9a 112
2c298960 113 with 'Catalyst::ScriptRole';
111d3c9a 114
ad8b4c91 115 sub _application_args { ... }
111d3c9a 116
1628b022 117=head1 DESCRIPTION
118
12aa6ca4 119Role with the common functionality of Catalyst scripts.
120
121=head1 METHODS
122
123=head2 run
124
125The method invoked to run the application.
126
d0a1ec36 127=head2 print_usage_text
128
d072f850 129Prints out the usage text for the script you tried to invoke.
d0a1ec36 130
12aa6ca4 131=head1 ATTRIBUTES
132
133=head2 application_name
134
135The name of the application class, e.g. MyApp
136
137=head1 SEE ALSO
138
139L<Catalyst>
140
141L<MooseX::Getopt>
1628b022 142
143=head1 AUTHORS
144
145Catalyst Contributors, see Catalyst.pm
146
147=head1 COPYRIGHT
148
149This library is free software, you can redistribute it and/or modify
150it under the same terms as Perl itself.
151
152=cut