3
0
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:
Pragmatic Software 2021-07-22 05:09:07 -07:00
parent c6f8490491
commit dfda37227d
3 changed files with 29 additions and 29 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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;
chdir "$RealBin/../lib/PBot/Core";
my @packages = glob "$directory/*.pm";
chdir $cwd;
foreach my $inc_path (@INC) {
if (-d "$inc_path/$base_path") {
my @packages = glob "$inc_path/$base_path/*.pm";
foreach my $package (sort @packages) {
$package = "PBot/Core/$package";
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
my $class = $package;
$class =~ s/\//::/g;
$class =~ s/\.pm$//;
my ($name) = $class =~ /.*::(.*)$/;
my $name = basename $package;
$name =~ s/\.pm$//;
$self->{pbot}->{logger}->log(" $name\n");
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
eval {
require "$package";
my $class = $base . '::' . $name;
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
$self->{pbot}->{refresher}->{refresher}->update_cache($package);
};
# 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;