Newer Older
330 lines | 7.679kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojolicious::Commands;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
2
use Mojo::Base 'Mojolicious::Command';
copy gitweblite soruce code
root authored on 2012-11-23
3

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
4
use Getopt::Long 'GetOptions';
5
use List::Util 'max';
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
6
use Mojo::Server;
copy gitweblite soruce code
root authored on 2012-11-23
7

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
8
has hint => <<EOF;
copy gitweblite soruce code
root authored on 2012-11-23
9

            
10
These options are available for all commands:
11
    -h, --help          Get more information on a specific command.
12
        --home <path>   Path to your applications home directory, defaults to
13
                        the value of MOJO_HOME or auto detection.
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
14
    -m, --mode <name>   Operating mode for your application, defaults to the
15
                        value of MOJO_MODE/PLACK_ENV or "development".
copy gitweblite soruce code
root authored on 2012-11-23
16

            
17
See '$0 help COMMAND' for more information on a specific command.
18
EOF
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
19
has message => <<EOF;
copy gitweblite soruce code
root authored on 2012-11-23
20
usage: $0 COMMAND [OPTIONS]
21

            
22
Tip: CGI and PSGI environments can be automatically detected very often and
23
     work without commands.
24

            
25
These commands are currently available:
26
EOF
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
27
has namespaces => sub { ['Mojolicious::Command'] };
copy gitweblite soruce code
root authored on 2012-11-23
28

            
29
sub detect {
30
  my ($self, $guess) = @_;
31

            
32
  # PSGI (Plack only for now)
33
  return 'psgi' if defined $ENV{PLACK_ENV};
34

            
35
  # CGI
36
  return 'cgi' if defined $ENV{PATH_INFO} || defined $ENV{GATEWAY_INTERFACE};
37

            
38
  # Nothing
39
  return $guess;
40
}
41

            
42
# Command line options for MOJO_HELP, MOJO_HOME and MOJO_MODE
43
BEGIN {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
44
  Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case pass_through));
copy gitweblite soruce code
root authored on 2012-11-23
45
  GetOptions(
46
    'h|help'   => sub { $ENV{MOJO_HELP} = 1 },
47
    'home=s'   => sub { $ENV{MOJO_HOME} = $_[1] },
48
    'm|mode=s' => sub { $ENV{MOJO_MODE} = $_[1] }
49
  ) unless __PACKAGE__->detect;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
50
  Getopt::Long::Configure('default');
copy gitweblite soruce code
root authored on 2012-11-23
51
}
52

            
53
sub run {
54
  my ($self, $name, @args) = @_;
55

            
56
  # Application loader
57
  return $self->app if defined $ENV{MOJO_APP_LOADER};
58

            
59
  # Try to detect environment
60
  $name = $self->detect($name) unless $ENV{MOJO_NO_DETECT};
61

            
62
  # Run command
63
  if ($name && $name =~ /^\w+$/ && ($name ne 'help' || $args[0])) {
64

            
65
    # Help
66
    $name = shift @args if my $help = $name eq 'help';
67
    $help = $ENV{MOJO_HELP} = $ENV{MOJO_HELP} ? 1 : $help;
68

            
69
    my $module;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
70
    $module = _command("${_}::$name", 1) and last for @{$self->namespaces};
copy gitweblite soruce code
root authored on 2012-11-23
71

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
72
    # Unknown command
73
    die qq{Unknown command "$name", maybe you need to install it?\n}
copy gitweblite soruce code
root authored on 2012-11-23
74
      unless $module;
75

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
76
    # Run command
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
77
    my $command = $module->new(app => $self->app);
78
    return $help ? $command->help(@args) : $command->run(@args);
copy gitweblite soruce code
root authored on 2012-11-23
79
  }
80

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
81
  # Hide list for tests
copy gitweblite soruce code
root authored on 2012-11-23
82
  return 1 if $ENV{HARNESS_ACTIVE};
83

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
84
  # Find all available commands
copy gitweblite soruce code
root authored on 2012-11-23
85
  my (@commands, %seen);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
86
  my $loader = Mojo::Loader->new;
copy gitweblite soruce code
root authored on 2012-11-23
87
  for my $namespace (@{$self->namespaces}) {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
88
    for my $module (@{$loader->search($namespace)}) {
copy gitweblite soruce code
root authored on 2012-11-23
89
      next unless my $command = _command($module);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
90
      $command =~ s/^${namespace}:://;
copy gitweblite soruce code
root authored on 2012-11-23
91
      push @commands, [$command => $module] unless $seen{$command}++;
92
    }
93
  }
94

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
95
  # Print list of all available commands
96
  my $max = max map { length $_->[0] } @commands;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
97
  print $self->message;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
98
  for my $command (@commands) {
99
    my $name        = $command->[0];
100
    my $description = $command->[1]->new->description;
101
    print "  $name", (' ' x ($max - length $name)), "   $description";
copy gitweblite soruce code
root authored on 2012-11-23
102
  }
103
  return print $self->hint;
104
}
105

            
106
sub start_app {
107
  my $self = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
108
  return Mojo::Server->new->build_app(shift)->start(@_);
copy gitweblite soruce code
root authored on 2012-11-23
109
}
110

            
111
sub _command {
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
112
  my ($module, $fatal) = @_;
113
  return $module->isa('Mojolicious::Command') ? $module : undef
114
    unless my $e = Mojo::Loader->new->load($module);
115
  $fatal && ref $e ? die $e : return undef;
copy gitweblite soruce code
root authored on 2012-11-23
116
}
117

            
118
1;
119

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
120
=encoding utf8
121

            
copy gitweblite soruce code
root authored on 2012-11-23
122
=head1 NAME
123

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
124
Mojolicious::Commands - Command line interface
copy gitweblite soruce code
root authored on 2012-11-23
125

            
126
=head1 SYNOPSIS
127

            
128
  use Mojolicious::Commands;
129

            
130
  my $commands = Mojolicious::Commands->new;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
131
  push @{$commands->namespaces}, 'MyApp::Command';
132
  $commands->run('daemon');
copy gitweblite soruce code
root authored on 2012-11-23
133

            
134
=head1 DESCRIPTION
135

            
136
L<Mojolicious::Commands> is the interactive command line interface to the
137
L<Mojolicious> framework. It will automatically detect available commands in
138
the C<Mojolicious::Command> namespace.
139

            
140
=head1 COMMANDS
141

            
142
These commands are available by default.
143

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
144
=head2 help
copy gitweblite soruce code
root authored on 2012-11-23
145

            
146
  $ mojo
147
  $ mojo help
148
  $ ./myapp.pl help
149

            
150
List available commands with short descriptions.
151

            
152
  $ mojo help <command>
153
  $ ./myapp.pl help <command>
154

            
155
List available options for the command with short descriptions.
156

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
157
=head2 cgi
copy gitweblite soruce code
root authored on 2012-11-23
158

            
159
  $ ./myapp.pl cgi
160

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
161
Start application with CGI backend, usually auto detected.
copy gitweblite soruce code
root authored on 2012-11-23
162

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
163
=head2 cpanify
copy gitweblite soruce code
root authored on 2012-11-23
164

            
165
  $ mojo cpanify -u sri -p secr3t Mojolicious-Plugin-Fun-0.1.tar.gz
166

            
167
Upload files to CPAN.
168

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
169
=head2 daemon
copy gitweblite soruce code
root authored on 2012-11-23
170

            
171
  $ ./myapp.pl daemon
172

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
173
Start application with standalone HTTP and WebSocket server.
copy gitweblite soruce code
root authored on 2012-11-23
174

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
175
=head2 eval
copy gitweblite soruce code
root authored on 2012-11-23
176

            
177
  $ ./myapp.pl eval 'say app->home'
178

            
179
Run code against application.
180

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
181
=head2 generate
copy gitweblite soruce code
root authored on 2012-11-23
182

            
183
  $ mojo generate
184
  $ mojo generate help
185
  $ ./myapp.pl generate help
186

            
187
List available generator commands with short descriptions.
188

            
189
  $ mojo generate help <generator>
190
  $ ./myapp.pl generate help <generator>
191

            
192
List available options for generator command with short descriptions.
193

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
194
=head2 generate app
copy gitweblite soruce code
root authored on 2012-11-23
195

            
196
  $ mojo generate app <AppName>
197

            
198
Generate application directory structure for a fully functional L<Mojolicious>
199
application.
200

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
201
=head2 generate lite_app
copy gitweblite soruce code
root authored on 2012-11-23
202

            
203
  $ mojo generate lite_app
204

            
205
Generate a fully functional L<Mojolicious::Lite> application.
206

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
207
=head2 generate makefile
copy gitweblite soruce code
root authored on 2012-11-23
208

            
209
  $ mojo generate makefile
210
  $ ./myapp.pl generate makefile
211

            
212
Generate C<Makefile.PL> file for application.
213

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
214
=head2 generate plugin
copy gitweblite soruce code
root authored on 2012-11-23
215

            
216
  $ mojo generate plugin <PluginName>
217

            
218
Generate directory structure for a fully functional L<Mojolicious> plugin.
219

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
220
=head2 get
copy gitweblite soruce code
root authored on 2012-11-23
221

            
222
  $ mojo get http://mojolicio.us
223
  $ ./myapp.pl get /foo
224

            
225
Perform requests to remote host or local application.
226

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
227
=head2 inflate
copy gitweblite soruce code
root authored on 2012-11-23
228

            
229
  $ ./myapp.pl inflate
230

            
231
Turn templates and static files embedded in the C<DATA> sections of your
232
application into real files.
233

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
234
=head2 prefork
235

            
236
  $ ./myapp.pl prefork
237

            
238
Start application with standalone preforking HTTP and WebSocket server.
239

            
240
=head2 psgi
copy gitweblite soruce code
root authored on 2012-11-23
241

            
242
  $ ./myapp.pl psgi
243

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
244
Start application with PSGI backend, usually auto detected.
copy gitweblite soruce code
root authored on 2012-11-23
245

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
246
=head2 routes
copy gitweblite soruce code
root authored on 2012-11-23
247

            
248
  $ ./myapp.pl routes
249

            
250
List application routes.
251

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
252
=head2 test
copy gitweblite soruce code
root authored on 2012-11-23
253

            
254
  $ ./myapp.pl test
255
  $ ./myapp.pl test t/fun.t
256

            
257
Runs application tests from the C<t> directory.
258

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
259
=head2 version
copy gitweblite soruce code
root authored on 2012-11-23
260

            
261
  $ mojo version
262
  $ ./myapp.pl version
263

            
264
Show version information for installed core and optional modules, very useful
265
for debugging.
266

            
267
=head1 ATTRIBUTES
268

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
269
L<Mojolicious::Commands> inherits all attributes from L<Mojolicious::Command>
270
and implements the following new ones.
copy gitweblite soruce code
root authored on 2012-11-23
271

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
272
=head2 hint
copy gitweblite soruce code
root authored on 2012-11-23
273

            
274
  my $hint  = $commands->hint;
275
  $commands = $commands->hint('Foo!');
276

            
277
Short hint shown after listing available commands.
278

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
279
=head2 message
copy gitweblite soruce code
root authored on 2012-11-23
280

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
281
  my $msg   = $commands->message;
282
  $commands = $commands->message('Hello World!');
copy gitweblite soruce code
root authored on 2012-11-23
283

            
284
Short usage message shown before listing available commands.
285

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
286
=head2 namespaces
copy gitweblite soruce code
root authored on 2012-11-23
287

            
288
  my $namespaces = $commands->namespaces;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
289
  $commands      = $commands->namespaces(['MyApp::Command']);
290

            
291
Namespaces to load commands from, defaults to C<Mojolicious::Command>.
copy gitweblite soruce code
root authored on 2012-11-23
292

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
293
  # Add another namespace to load commands from
294
  push @{$commands->namespaces}, 'MyApp::Command';
copy gitweblite soruce code
root authored on 2012-11-23
295

            
296
=head1 METHODS
297

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
298
L<Mojolicious::Commands> inherits all methods from L<Mojolicious::Command> and
copy gitweblite soruce code
root authored on 2012-11-23
299
implements the following new ones.
300

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
301
=head2 detect
copy gitweblite soruce code
root authored on 2012-11-23
302

            
303
  my $env = $commands->detect;
304
  my $env = $commands->detect($guess);
305

            
306
Try to detect environment.
307

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
308
=head2 run
copy gitweblite soruce code
root authored on 2012-11-23
309

            
310
  $commands->run;
311
  $commands->run(@ARGV);
312

            
313
Load and run commands. Automatic deployment environment detection can be
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
314
disabled with the MOJO_NO_DETECT environment variable.
copy gitweblite soruce code
root authored on 2012-11-23
315

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
316
=head2 start_app
copy gitweblite soruce code
root authored on 2012-11-23
317

            
318
  Mojolicious::Commands->start_app('MyApp');
319
  Mojolicious::Commands->start_app(MyApp => @ARGV);
320

            
321
Load application and start the command line interface for it.
322

            
323
  # Always start daemon for application and ignore @ARGV
324
  Mojolicious::Commands->start_app('MyApp', 'daemon', '-l', 'http://*:8080');
325

            
326
=head1 SEE ALSO
327

            
328
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
329

            
330
=cut