Skip to content

Commit

Permalink
add list method to Mojolicious::Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 19, 2015
1 parent 111c38b commit 029a7b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

5.81 2015-02-19
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
39 changes: 25 additions & 14 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -24,6 +24,22 @@ 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}::";
push @rows, [$command, $module->new->description] if !$seen{$command}++;
}
}

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

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

Expand Down Expand Up @@ -57,20 +73,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 { [" $_->[0]", $_->[1]] } @{$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 +307,12 @@ 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">.
=head2 run
$commands->run;
Expand Down
18 changes: 18 additions & 0 deletions t/mojolicious/commands.t
Expand Up @@ -85,6 +85,24 @@ 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 = [
['cgi', 'Start application with CGI'],
['cpanify', 'Upload distribution to CPAN'],
['daemon', 'Start application with HTTP and WebSocket server'],
['eval', 'Run code against application'],
['generate', 'Generate files and directories from templates'],
['get', 'Perform HTTP request'],
['inflate', 'Inflate embedded files to real files'],
['prefork', 'Start application with preforking HTTP and WebSocket server'],
['psgi', 'Start application with PSGI'],
['routes', 'Show available routes'],
['test', 'Run tests'],
['version', 'Show versions of available modules']
];
is_deeply $commands->list, $list, 'right command list';

# mojo
ok $commands->description, 'has a description';
like $commands->message, qr/COMMAND/, 'has a message';
Expand Down

0 comments on commit 029a7b6

Please sign in to comment.