Mojolicious/Commands.pm

filter: Statementstmt 100% 59 / 59 Branchbran 93.3% 28 / 30 Conditioncond 93.9% 31 / 33 MC/DCmcdc 77.7% 14 / 18 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 94.8% 147 / 155 CC 31 SCAR 26.3
CC
31
Cov
97%
run
23.0
_command
5.1
detect
4.0
CRAP 13.9
« Mojolicious/Plugin/Mount.pm ↑ Summary Mojo/Server/Daemon.pm »
1 package Mojolicious::Commands;
2 20 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Mojo::Loader qw(find_modules find_packages load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Mojo::Util qw(getopt tablify);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 has hint => <<EOF;
9  
10 See 'APPLICATION help COMMAND' for more information on a specific command.
11 EOF
12 2 ▶has message => sub { shift->extract_usage . "\nCommands:\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 25 ▶has namespaces => sub { ['Mojolicious::Command::Author', 'Mojolicious::Command'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 71 ▶sub detect {
Subroutine detect: called CC=4 SCAR=13.9
Pod documented
16  
17 # PSGI (Plack only for now)
18 71 ▶ return 'psgi' if defined $ENV{PLACK_ENV};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PLACK_ENV"} 5 66 71
19  
20 # CGI
21 66 ▶ return 'cgi' if defined $ENV{PATH_INFO} || defined $ENV{GATEWAY_INTERFACE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"PATH_INFO"} or defined $ENV{"GATEWAY_INTERFACE"} 2 64 66
Condition100%3/3
defined $ENV{"PATH_INFO"} or defined $ENV{"GATEWAY_INTERFACE"}
l!l&&r!l&&!r
1164
MC/DC100%2/2
defined $ENV{"PATH_INFO"} or defined $ENV{"GATEWAY_INTERFACE"}
defined $ENV{"PATH_INFO"} defined $ENV{"GATEWAY_INTERFACE"}
Truth table100%3/3
A or B
A= defined $ENV{"PATH_INFO"}B= defined $ENV{"GATEWAY_INTERFACE"}
ABresult
1X1
011
000
22  
23 # Nothing
24 64 return undef;
25 }
26  
27 34 ▶sub run {
Subroutine run: called CC=23 SCAR=31.4
Pod documented
28 34 my ($self, $name, @args) = @_;
29  
30 # Application loader
31 34 ▶ return $self->app if defined $ENV{MOJO_APP_LOADER};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"MOJO_APP_LOADER"} 6 28 34
32  
33 # Try to detect environment
34 28 ▶ if (!$ENV{MOJO_NO_DETECT} && (my $env = $self->detect)) { $name = $env }
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"MOJO_NO_DETECT"} and my $env = $self->detect) 2 26 28
Condition100%3/3
not $ENV{"MOJO_NO_DETECT"} and my $env = $self->detect
!ll&&!rl&&r
1252
MC/DC100%2/2
not $ENV{"MOJO_NO_DETECT"} and my $env = $self->detect
not $ENV{"MOJO_NO_DETECT"} my $env = $self->detect
Truth table100%3/3
not(A) and B
A= not $ENV{"MOJO_NO_DETECT"}B= my $env = $self->detect
ABresult
0X0
100
111
35  
36 # Run command
37 28 ▶ if ($name && $name =~ /^\w[\w-]+$/ && ($name ne 'help' || $args[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name and $name =~ /^\w[\w-]+$/u and $name ne 'help' || $args[0]) { } 20 8 28
elsif ($name and $name ne 'help' and $name ne '--help' and $name ne '-h') { } 1 13 14
Condition100%9/9
$name and $name =~ /^\w[\w-]+$/u and $name ne "help" || $args[0]
!ll&&!rl&&r
7120
$name and $name =~ /^\w[\w-]+$/u
!ll&&!rl&&r
4321
$name ne "help" or $args[0]
l!l&&r!l&&!r
1821
MC/DC100%4/4
$name and $name =~ /^\w[\w-]+$/u and $name ne "help" || $args[0]
$name $name =~ /^\w[\w-]+$/u $name ne "help" $args[0]
Truth table100%5/5
A and B and C or D
A= $nameB= $name =~ /^\w[\w-]+$/uC= $name ne "help"D= $args[0]
ABCDresult
0XXX0
10XX0
11000
111X1
11011
38  
39 # Help
40 20 ▶ $name = shift @args if my $help = $name eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if my $help = $name eq "help" 2 18 20
41 20 ▶ local $ENV{MOJO_HELP} = $help = $ENV{MOJO_HELP} || $help;
Condition100%3/3
$ENV{'MOJO_HELP'} || $help
l!l&&r!l&&!r
3215
MC/DC100%2/2
$ENV{'MOJO_HELP'} || $help
$ENV{'MOJO_HELP'} $help
Truth table100%3/3
A || B
A= $ENV{'MOJO_HELP'}B= $help
ABresult
1X1
011
000
42 20 $name =~ s/-/_/g;
43  
44 # Remove options shared by all commands before loading the command
45 20 _args(\@args);
46 20 my $module;
47 20 ▶ $module = _command("${_}::$name", 1) and last for @{$self->namespaces};
Branch: 1 branch
BranchTrueFalseTotal
$module = _command("${_}::$name", 1) and last 20 19 39
48  
49 # Unknown command
50 20 ▶ die qq{Unknown command "$name", maybe you need to install it?\n} unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 20 20
51  
52 # Run command
53 20 my $app = $self->app;
54 20 my $command = $module->new(app => $app);
55 20 ▶ return $command->help(@args) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 5 15 20
56 15 $app->plugins->emit_hook(before_command => $command, \@args);
57 15 return $command->run(@args);
58 }
59  
60 1 ▶ elsif ($name && $name ne 'help' && $name ne '--help' && $name ne '-h') { die qq{Invalid command "$name".\n} }
Condition100%9/9
$name and $name ne "help" and $name ne "--help" and $name ne "-h"
!ll&&!rl&&r
611
$name and $name ne "help" and $name ne "--help"
!ll&&!rl&&r
512
$name and $name ne "help"
!ll&&!rl&&r
413
MC/DC100%4/4
$name and $name ne "help" and $name ne "--help" and $name ne "-h"
$name $name ne "help" $name ne "--help" $name ne "-h"
Truth table100%5/5
A and B and C and D
A= $nameB= $name ne "help"C= $name ne "--help"D= $name ne "-h"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
61  
62 # Hide list for tests
63 7 ▶ return 1 if $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HARNESS_ACTIVE"} 1 6 7
64  
65 # Find all available commands
66 6 my %all;
67 6 for my $ns (@{$self->namespaces}) {
68 $all{substr $_, length "${ns}::"} //= $_->new->description
69 12 ▶ for grep { _command($_) } find_modules($ns), find_packages($ns);
Condition66%2/3
$all{substr $_, length "${ns}::"} //= $_->new->description
l!l&&r!l&&!r
62670
MC/DC0%0/2
$all{substr $_, length "${ns}::"} //= $_->new->description
$all{substr $_, length "${ns}::"} $_->new->description
Truth table66%2/3
A //= B
A= $all{substr $_, length "${ns}::"}B= $_->new->description
ABresult
1X1
011
000
70 }
71  
72 6 my @rows;
73 6 for my $class (sort keys %all) {
74 67 my $command = $class;
75 67 $command =~ s/(?<!^)_/-/g;
76 67 push @rows, [" $command", $all{$class}];
77 }
78  
79 6 return print $self->message, tablify(\@rows), $self->hint;
80 }
81  
82 7 ▶sub start_app { shift; Mojo::Server->new->build_app(shift)->start(@_) }
Subroutine start_app: called CC=1 SCAR=0.0
Pod documented
83  
84 # Command line options for MOJO_HELP, MOJO_HOME and MOJO_MODE
85 40 ▶sub _args {
Subroutine _args: called CC=2 SCAR=6.9
86 getopt shift, ['pass_through'],
87 'h|help' => \$ENV{MOJO_HELP},
88 'home=s' => \$ENV{MOJO_HOME},
89 'm|mode=s' => \$ENV{MOJO_MODE}
90 40 ▶ unless __PACKAGE__->detect;
Branch: 1 branch
BranchTrueFalseTotal
unless __PACKAGE__->detect 38 2 40
91 }
92  
93 # Do not remove options from @ARGV
94 20 ▶BEGIN { _args([@ARGV]) }
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 208 ▶sub _command {
Subroutine _command: called CC=5 SCAR=16.3
97 208 my ($module, $fatal) = @_;
98 208 ▶ return $module->isa('Mojolicious::Command') ? $module : undef unless my $e = load_class $module;
Branch: 2 branches
BranchTrueFalseTotal
unless my $e = load_class($module) 154 54 208
$module->isa('Mojolicious::Command') ? : 149 5 154
99 54 ▶ $fatal && ref $e ? die $e : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$fatal && ref $e ? : 0 54 54
Condition66%2/3
$fatal and ref $e
!ll&&!rl&&r
35190
MC/DC0%0/2
$fatal and ref $e
$fatal ref $e
Truth table66%2/3
A and B
A= $fatalB= ref $e
ABresult
0X0
100
111
100 }
101  
102 20 1;
103  
104 =encoding utf8
105  
106 =head1 NAME
107  
108 Mojolicious::Commands - Command line interface
109  
110 =head1 SYNOPSIS
111  
112 Usage: APPLICATION COMMAND [OPTIONS]
113  
114 mojo version
115 mojo generate lite-app
116 ./myapp.pl daemon -m production -l http://*:8080
117 ./myapp.pl get /foo
118 ./myapp.pl routes -v
119  
120 Tip: CGI and PSGI environments can be automatically detected very often and
121 work without commands.
122  
123 Options (for all commands):
124 -h, --help Get more information on a specific command
125 --home <path> Path to home directory of your application, defaults to
126 the value of MOJO_HOME or auto-detection
127 -m, --mode <name> Operating mode for your application, defaults to the
128 value of MOJO_MODE/PLACK_ENV or "development"
129  
130 =head1 DESCRIPTION
131  
132 L<Mojolicious::Commands> is the interactive command line interface for the L<Mojolicious> framework. It will
133 automatically detect available commands in the C<Mojolicious::Command> and C<Mojolicious::Command::Author> namespaces.
134  
135 =head1 COMMANDS
136  
137 These commands are available by default.
138  
139 =head2 cgi
140  
141 $ ./myapp.pl cgi
142  
143 Use L<Mojolicious::Command::cgi> to start application with CGI backend, usually auto detected.
144  
145 =head2 cpanify
146  
147 $ mojo cpanify -u sri -p secr3t Mojolicious-Plugin-Fun-0.1.tar.gz
148  
149 Use L<Mojolicious::Command::Author::cpanify> for uploading files to CPAN.
150  
151 =head2 daemon
152  
153 $ ./myapp.pl daemon
154  
155 Use L<Mojolicious::Command::daemon> to start application with standalone HTTP and WebSocket server.
156  
157 =head2 eval
158  
159 $ ./myapp.pl eval 'say app->home'
160  
161 Use L<Mojolicious::Command::eval> to run code against application.
162  
163 =head2 generate
164  
165 $ mojo generate
166 $ mojo generate help
167 $ ./myapp.pl generate help
168  
169 List available generator commands with short descriptions.
170  
171 $ mojo generate help <generator>
172 $ ./myapp.pl generate help <generator>
173  
174 List available options for generator command with short descriptions.
175  
176 =head2 generate app
177  
178 $ mojo generate app <AppName>
179  
180 Use L<Mojolicious::Command::Author::generate::app> to generate application directory structure for a fully functional
181 L<Mojolicious> application.
182  
183 =head2 generate dockerfile
184  
185 $ ./myapp.pl generate dockerfile
186 $ ./script/my_app generate dockerfile
187  
188 Use L<Mojolicious::Command::Author::generate::dockerfile> to generate C<Dockerfile> for application.
189  
190 =head2 generate lite-app
191  
192 $ mojo generate lite-app
193  
194 Use L<Mojolicious::Command::Author::generate::lite_app> to generate a fully functional L<Mojolicious::Lite>
195 application.
196  
197 =head2 generate makefile
198  
199 $ mojo generate makefile
200 $ ./myapp.pl generate makefile
201  
202 Use L<Mojolicious::Command::Author::generate::makefile> to generate C<Makefile.PL> file for application.
203  
204 =head2 generate plugin
205  
206 $ mojo generate plugin <PluginName>
207  
208 Use L<Mojolicious::Command::Author::generate::plugin> to generate directory structure for a fully functional
209 L<Mojolicious> plugin.
210  
211 =head2 get
212  
213 $ mojo get https://mojolicious.org
214 $ ./myapp.pl get /foo
215  
216 Use L<Mojolicious::Command::get> to perform requests to remote host or local application.
217  
218 =head2 help
219  
220 $ mojo
221 $ mojo help
222 $ ./myapp.pl help
223  
224 List available commands with short descriptions.
225  
226 $ mojo help <command>
227 $ ./myapp.pl help <command>
228  
229 List available options for the command with short descriptions.
230  
231 =head2 inflate
232  
233 $ ./myapp.pl inflate
234  
235 Use L<Mojolicious::Command::Author::inflate> to turn templates and static files embedded in the C<DATA> sections of
236 your application into real files.
237  
238 =head2 prefork
239  
240 $ ./myapp.pl prefork
241  
242 Use L<Mojolicious::Command::prefork> to start application with standalone pre-forking HTTP and WebSocket server.
243  
244 =head2 psgi
245  
246 $ ./myapp.pl psgi
247  
248 Use L<Mojolicious::Command::psgi> to start application with PSGI backend, usually auto detected.
249  
250 =head2 routes
251  
252 $ ./myapp.pl routes
253  
254 Use L<Mojolicious::Command::routes> to list application routes.
255  
256 =head2 version
257  
258 $ mojo version
259 $ ./myapp.pl version
260  
261 Use L<Mojolicious::Command::version> to show version information for available core and optional modules, very useful
262 for debugging.
263  
264 =head1 ATTRIBUTES
265  
266 L<Mojolicious::Commands> inherits all attributes from L<Mojolicious::Command> and implements the following new ones.
267  
268 =head2 hint
269  
270 my $hint = $commands->hint;
271 $commands = $commands->hint('Foo');
272  
273 Short hint shown after listing available commands.
274  
275 =head2 message
276  
277 my $msg = $commands->message;
278 $commands = $commands->message('Hello World!');
279  
280 Short usage message shown before listing available commands.
281  
282 =head2 namespaces
283  
284 my $namespaces = $commands->namespaces;
285 $commands = $commands->namespaces(['MyApp::Command']);
286  
287 Namespaces to load commands from, defaults to C<Mojolicious::Command::Author> and C<Mojolicious::Command>.
288  
289 # Add another namespace to load commands from
290 push @{$commands->namespaces}, 'MyApp::Command';
291  
292 =head1 METHODS
293  
294 L<Mojolicious::Commands> inherits all methods from L<Mojolicious::Command> and implements the following new ones.
295  
296 =head2 detect
297  
298 my $env = $commands->detect;
299  
300 Try to detect environment, or return C<undef> if none could be detected.
301  
302 =head2 run
303  
304 $commands->run;
305 $commands->run(@ARGV);
306  
307 Load and run commands. Automatic deployment environment detection can be disabled with the C<MOJO_NO_DETECT>
308 environment variable.
309  
310 =head2 start_app
311  
312 Mojolicious::Commands->start_app('MyApp');
313 Mojolicious::Commands->start_app(MyApp => @ARGV);
314  
315 Load application from class and start the command line interface for it. Note that the options C<-h>/C<--help>,
316 C<--home> and C<-m>/C<--mode>, which are shared by all commands, will be parsed from C<@ARGV> during compile time.
317  
318 # Always start daemon for application
319 Mojolicious::Commands->start_app('MyApp', 'daemon', '-l', 'http://*:8080');
320  
321 =head1 SEE ALSO
322  
323 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
324  
325 =cut
« Mojolicious/Plugin/Mount.pm ↑ Summary Mojo/Server/Daemon.pm »