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