mirror of
https://github.com/pragma-/pbot.git
synced 2024-12-23 11:12:42 +01:00
Refactor Utils::LoadPackages to use @INC
This commit is contained in:
parent
c6f8490491
commit
dfda37227d
@ -29,7 +29,7 @@ sub register_commands {
|
||||
|
||||
# register commands in Commands directory
|
||||
$self->{pbot}->{logger}->log("Registering commands:\n");
|
||||
load_packages($self, 'Commands');
|
||||
load_packages($self, 'PBot::Core::Commands');
|
||||
}
|
||||
|
||||
sub register {
|
||||
|
@ -57,7 +57,7 @@ sub register_handlers {
|
||||
my ($self, %conf) = @_;
|
||||
|
||||
$self->{pbot}->{logger}->log("Registering IRC handlers:\n");
|
||||
load_packages($self, 'IRCHandlers');
|
||||
load_packages($self, 'PBot::Core::IRCHandlers');
|
||||
}
|
||||
|
||||
# this default handler prepends 'irc.' to the event-type and then dispatches
|
||||
|
@ -9,7 +9,7 @@ package PBot::Utils::LoadPackages;
|
||||
|
||||
use PBot::Imports;
|
||||
|
||||
use Cwd;
|
||||
use File::Basename;
|
||||
|
||||
# export load_packages subroutine
|
||||
require Exporter;
|
||||
@ -17,42 +17,42 @@ our @ISA = qw/Exporter/;
|
||||
our @EXPORT = qw/load_packages/;
|
||||
|
||||
sub load_packages {
|
||||
my ($self, $directory) = @_;
|
||||
my ($self, $base) = @_;
|
||||
|
||||
use FindBin qw/$RealBin/;
|
||||
my $base_path = join '/', split '::', $base;
|
||||
|
||||
my $cwd = getcwd;
|
||||
foreach my $inc_path (@INC) {
|
||||
if (-d "$inc_path/$base_path") {
|
||||
my @packages = glob "$inc_path/$base_path/*.pm";
|
||||
|
||||
chdir "$RealBin/../lib/PBot/Core";
|
||||
foreach my $package (sort @packages) {
|
||||
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
|
||||
|
||||
my @packages = glob "$directory/*.pm";
|
||||
my $name = basename $package;
|
||||
$name =~ s/\.pm$//;
|
||||
|
||||
chdir $cwd;
|
||||
$self->{pbot}->{logger}->log(" $name\n");
|
||||
|
||||
foreach my $package (sort @packages) {
|
||||
$package = "PBot/Core/$package";
|
||||
eval {
|
||||
require "$package";
|
||||
|
||||
my $class = $package;
|
||||
$class =~ s/\//::/g;
|
||||
$class =~ s/\.pm$//;
|
||||
my $class = $base . '::' . $name;
|
||||
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache($package);
|
||||
};
|
||||
|
||||
my ($name) = $class =~ /.*::(.*)$/;
|
||||
|
||||
$self->{pbot}->{logger}->log(" $name\n");
|
||||
|
||||
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
|
||||
|
||||
eval {
|
||||
require "$package";
|
||||
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
|
||||
$self->{pbot}->{refresher}->{refresher}->update_cache($package);
|
||||
};
|
||||
|
||||
if (my $exception = $@) {
|
||||
$self->{pbot}->{logger}->log("Error loading $package: $exception");
|
||||
exit;
|
||||
# error loading a package
|
||||
if (my $exception = $@) {
|
||||
$self->{pbot}->{logger}->log("Error loading $package: $exception");
|
||||
exit;
|
||||
}
|
||||
}
|
||||
# packages loaded successfully
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
# no packages found
|
||||
return 0;
|
||||
}
|
||||
|
||||
1;
|
||||
|
Loading…
Reference in New Issue
Block a user