Skip to content

Commit

Permalink
added experimental app method to Mojo::Command and improved cookbook …
Browse files Browse the repository at this point in the history
…recipe
  • Loading branch information
kraih committed Aug 21, 2011
1 parent 139f89e commit e880f8d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

1.87 2011-08-22 00:00:00
- Added EXPERIMENTAL app method to Mojo::Command.
- Improved documentation.

1.86 2011-08-21 00:00:00
Expand Down
11 changes: 10 additions & 1 deletion lib/Mojo/Command.pm
Expand Up @@ -33,6 +33,8 @@ has usage => "usage: $0\n";
# Cache
my $CACHE = {};

sub app { Mojo::Server->new->app }

sub chmod_file {
my ($self, $path, $mod) = @_;
chmod $mod, $path or croak qq/Can't chmod path "$path": $!/;
Expand Down Expand Up @@ -190,7 +192,7 @@ sub run {
my ($self, $name, @args) = @_;

# Application loader
return Mojo::Server->new->app if defined $ENV{MOJO_APP_LOADER};
return $self->app if defined $ENV{MOJO_APP_LOADER};

# Try to detect environment
$name = $self->detect($name) unless $ENV{MOJO_NO_DETECT};
Expand Down Expand Up @@ -409,6 +411,13 @@ Usage information for command, used for the help screen.
L<Mojo::Command> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<app>
my $app = $command->app;
Currently active application, defaults to a L<Mojo::HelloWorld> object.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<chmod_file>
$command = $command->chmod_file('/foo/bar.txt', 0644);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/routes.pm
Expand Up @@ -15,7 +15,7 @@ sub run {
my $self = shift;

# Check if application has routes
my $app = Mojo::Server->new->app;
my $app = $self->app;
die "Application has no routes.\n" unless $app->can('routes');

# Walk and draw
Expand Down
17 changes: 11 additions & 6 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -508,24 +508,29 @@ By now you've propably used many of the built-in commands described in
L<Mojolicious::Commands>, but did you know that you can just add new ones and
that they will be picked up automatically by the command line interface?

package Mojolicious::Command::echo;
package Mojolicious::Command::spy;
use Mojo::Base 'Mojo::Command';

sub run {
my ($self, $whatever) = @_;
print "Echo: $whatever";

# Leak secret passphrase
if ($whatever eq 'secret') {
my $secret = $self->app->secret;
print qq/The secret of this application is "$secret".\n/;
}
}

1;

There are many more useful methods and attributes in L<Mojo::Command> that
you can use or overload.

$ mojo echo test123
Echo: test123
$ mojo spy secret
The secret of this application is "Mojolicious::Lite".

$ ./myapp.pl echo test123
Echo: test123
$ ./myapp.pl spy secret
The secret of this application is "secr3t".

=head2 Running Code Against Your Application

Expand Down
5 changes: 4 additions & 1 deletion t/mojo/command.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 23;
use Test::More tests => 24;

use Cwd 'cwd';
use File::Spec;
Expand All @@ -12,6 +12,9 @@ use_ok 'Mojo::Command';

my $command = Mojo::Command->new;

# Application
isa_ok $command->app, 'Mojo', 'right application';

# UNIX DATA templates
my $unix = "@@ template1\nFirst Template\n@@ template2\r\nSecond Template\n";
open my $data, '<', \$unix;
Expand Down

0 comments on commit e880f8d

Please sign in to comment.