Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add a different variant of the list method to Mojolicious::Commands
  • Loading branch information
kraih committed Feb 19, 2015
1 parent c42a9c9 commit b01f0a4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.81 2015-02-20
- Added list method to Mojolicious::Commands.

5.80 2015-02-18
- Deprecated Mojo::IOLoop::accept_interval, Mojo::IOLoop::lock and
Expand Down
42 changes: 28 additions & 14 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -24,6 +24,23 @@ sub detect {
return undef;
}

sub list {
my $self = shift;

my (@rows, %seen);
my $loader = Mojo::Loader->new;
for my $ns (@{$self->namespaces}) {
for my $module (@{$loader->search($ns)}) {
next unless my $command = _command($module);
$command = substr $command, length "${ns}::";
next if $seen{$command}++;
push @rows, {name => $command, description => $module->new->description};
}
}

return [sort { $a->{name} cmp $b->{name} } @rows];
}

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

Expand Down Expand Up @@ -57,20 +74,9 @@ sub run {
# Hide list for tests
return 1 if $ENV{HARNESS_ACTIVE};

# Find all available commands
my (@rows, %seen);
my $loader = Mojo::Loader->new;
for my $ns (@{$self->namespaces}) {
for my $module (@{$loader->search($ns)}) {
next unless my $command = _command($module);
$command = substr $command, length "${ns}::";
next if $seen{$command}++;
push @rows, [" $command", $module->new->description];
}
}
@rows = sort { $a->[0] cmp $b->[0] } @rows;

return print $self->message, tablify(\@rows), $self->hint;
# List all available commands
my @list = map { [" $_->{name}", $_->{description}] } @{$self->list};
return print $self->message, tablify(\@list), $self->hint;
}

sub start_app { shift; Mojo::Server->new->build_app(shift)->start(@_) }
Expand Down Expand Up @@ -302,6 +308,14 @@ implements the following new ones.
Try to detect environment or return C<undef> if none could be detected.
=head2 list
my $list = $commands->list;
List available commands in L</"namespaces">.
say $_->{name} for @{$commands->list};
=head2 run
$commands->run;
Expand Down
52 changes: 50 additions & 2 deletions t/mojolicious/commands.t
Expand Up @@ -85,6 +85,56 @@ is $app->start('test_command'), 'works!', 'right result';
is $ENV{MOJO_MODE}, undef, 'no mode';
}

# List commands
is_deeply $commands->namespaces, ['Mojolicious::Command'], 'right namespaces';
my $list = [
{name => 'cgi', description => 'Start application with CGI'},
{name => 'cpanify', description => 'Upload distribution to CPAN'},
{
name => 'daemon',
description => 'Start application with HTTP and WebSocket server'
},
{name => 'eval', description => 'Run code against application'},
{
name => 'generate',
description => 'Generate files and directories from templates'
},
{name => 'get', description => 'Perform HTTP request'},
{name => 'inflate', description => 'Inflate embedded files to real files'},
{
name => 'prefork',
description =>
'Start application with preforking HTTP and WebSocket server'
},
{name => 'psgi', description => 'Start application with PSGI'},
{name => 'routes', description => 'Show available routes'},
{name => 'test', description => 'Run tests'},
{name => 'version', description => 'Show versions of available modules'}
];
is_deeply $commands->list, $list, 'right command list';

# List generator commands
require Mojolicious::Command::generate;
my $generator = Mojolicious::Command::generate->new;
is_deeply $generator->namespaces, ['Mojolicious::Command::generate'],
'right namespaces';
$list = [
{
name => 'app',
description => 'Generate Mojolicious application directory structure'
},
{
name => 'lite_app',
description => 'Generate Mojolicious::Lite application'
},
{name => 'makefile', description => 'Generate "Makefile.PL"'},
{
name => 'plugin',
description => 'Generate Mojolicious plugin directory structure'
}
];
is_deeply $generator->list, $list, 'right command list';

# mojo
ok $commands->description, 'has a description';
like $commands->message, qr/COMMAND/, 'has a message';
Expand Down Expand Up @@ -156,8 +206,6 @@ $buffer = '';
like $buffer, qr/Mojolicious::Controller/, 'right output';

# generate
require Mojolicious::Command::generate;
my $generator = Mojolicious::Command::generate->new;
ok $generator->description, 'has a description';
like $generator->message, qr/generate/, 'has a message';
like $generator->hint, qr/help/, 'has a hint';
Expand Down

0 comments on commit b01f0a4

Please sign in to comment.