Skip to content

Instantly share code, notes, and snippets.

/packages.diff Secret

Created June 16, 2015 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/31a50119319a19efb0af to your computer and use it in GitHub Desktop.
Save anonymous/31a50119319a19efb0af to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Loader.pm b/lib/Mojo/Loader.pm
index 5b49894..99ac129 100644
--- a/lib/Mojo/Loader.pm
+++ b/lib/Mojo/Loader.pm
@@ -7,7 +7,8 @@ use File::Spec::Functions qw(catdir catfile splitdir);
use Mojo::Exception;
use Mojo::Util qw(b64_decode class_to_path);
-our @EXPORT_OK = qw(data_section file_is_binary find_modules load_class);
+our @EXPORT_OK
+ = qw(data_section file_is_binary find_modules find_packages load_class);
my (%BIN, %CACHE);
@@ -33,6 +34,12 @@ sub find_modules {
return sort keys %modules;
}
+sub find_packages {
+ my $ns = shift;
+ no strict 'refs';
+ return sort map { $_ =~ /^(.+)::$/ ? ("${ns}::$1") : () } keys %{"${ns}::"};
+}
+
sub load_class {
my $class = shift;
@@ -148,6 +155,12 @@ cached once they have been accessed for the first time.
Check if embedded file from the C<DATA> section of a class was Base64 encoded.
+=head2 find_packages
+
+ my @pkgs = find_packages 'MyApp::Namespace';
+
+Search for packages in a namespace non-recursively.
+
=head2 find_modules
my @modules = find_modules 'MyApp::Namespace';
diff --git a/lib/Mojolicious/Commands.pm b/lib/Mojolicious/Commands.pm
index e6f8cff..486ba1f 100644
--- a/lib/Mojolicious/Commands.pm
+++ b/lib/Mojolicious/Commands.pm
@@ -2,7 +2,7 @@ package Mojolicious::Commands;
use Mojo::Base 'Mojolicious::Command';
use Getopt::Long 'GetOptionsFromArray';
-use Mojo::Loader qw(find_modules load_class);
+use Mojo::Loader qw(find_modules find_packages load_class);
use Mojo::Server;
use Mojo::Util 'tablify';
@@ -62,7 +62,7 @@ sub run {
my %all;
for my $ns (@{$self->namespaces}) {
$all{substr $_, length "${ns}::"} //= $_->new->description
- for grep { _command($_) } find_modules $ns;
+ for grep { _command($_) } find_modules($ns), find_packages($ns);
}
my @rows = map { [" $_", $all{$_}] } sort keys %all;
diff --git a/t/mojo/loader.t b/t/mojo/loader.t
index 310e182..8855a8e 100644
--- a/t/mojo/loader.t
+++ b/t/mojo/loader.t
@@ -7,7 +7,14 @@ use Test::More;
use FindBin;
use lib "$FindBin::Bin/lib";
-use Mojo::Loader qw(data_section file_is_binary find_modules load_class);
+use Mojo::Loader
+ qw(data_section file_is_binary find_packages find_modules load_class);
+
+package MyLoaderTest::Foo::Bar;
+
+package MyLoaderTest::Foo::Baz;
+
+package main;
# Single character core module
ok !load_class('B'), 'loaded';
@@ -49,7 +56,7 @@ is $e->lines_after->[1][0], 6, 'right number';
is $e->lines_after->[1][1], '1;', 'right line';
like "$e", qr/Exception/, 'right message';
-# Search
+# Search modules
my @modules = find_modules 'Mojo::LoaderTest';
is_deeply \@modules,
[qw(Mojo::LoaderTest::A Mojo::LoaderTest::B Mojo::LoaderTest::C)],
@@ -58,6 +65,11 @@ is_deeply [find_modules "Mojo'LoaderTest"],
[qw(Mojo'LoaderTest::A Mojo'LoaderTest::B Mojo'LoaderTest::C)],
'found the right modules';
+# Search packages
+my @pkgs = find_packages 'MyLoaderTest::Foo';
+is_deeply \@pkgs, ['MyLoaderTest::Foo::Bar', 'MyLoaderTest::Foo::Baz'],
+ 'found the rigth packages';
+
# Load
ok !load_class("Mojo'LoaderTest::A"), 'loaded successfully';
ok !!Mojo::LoaderTest::A->can('new'), 'loaded successfully';
diff --git a/t/mojolicious/commands.t b/t/mojolicious/commands.t
index 02e2004..af189e0 100644
--- a/t/mojolicious/commands.t
+++ b/t/mojolicious/commands.t
@@ -13,6 +13,11 @@ use lib "$FindBin::Bin/lib";
use Cwd 'cwd';
use File::Temp 'tempdir';
+package Mojolicious::Command::my_test_command;
+use Mojo::Base 'Mojolicious::Command';
+
+package main;
+
# Make sure @ARGV is not changed
{
local $ENV{MOJO_MODE};
@@ -97,7 +102,8 @@ my $buffer = '';
local $ENV{HARNESS_ACTIVE} = 0;
$commands->run;
}
-like $buffer, qr/Usage: APPLICATION COMMAND \[OPTIONS\].*daemon.*version/s,
+like $buffer,
+ qr/Usage: APPLICATION COMMAND \[OPTIONS\].*daemon.*my_test_command.*version/s,
'right output';
# help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment