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 # register commands in Commands directory
$self->{pbot}->{logger}->log("Registering commands:\n"); $self->{pbot}->{logger}->log("Registering commands:\n");
load_packages($self, 'Commands'); load_packages($self, 'PBot::Core::Commands');
} }
sub register { sub register {

View File

@ -57,7 +57,7 @@ sub register_handlers {
my ($self, %conf) = @_; my ($self, %conf) = @_;
$self->{pbot}->{logger}->log("Registering IRC handlers:\n"); $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 # 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 PBot::Imports;
use Cwd; use File::Basename;
# export load_packages subroutine # export load_packages subroutine
require Exporter; require Exporter;
@ -17,42 +17,42 @@ our @ISA = qw/Exporter/;
our @EXPORT = qw/load_packages/; our @EXPORT = qw/load_packages/;
sub 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) { eval {
$package = "PBot/Core/$package"; require "$package";
my $class = $package; my $class = $base . '::' . $name;
$class =~ s/\//::/g; $self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
$class =~ s/\.pm$//; $self->{pbot}->{refresher}->{refresher}->update_cache($package);
};
my ($name) = $class =~ /.*::(.*)$/; # error loading a package
if (my $exception = $@) {
$self->{pbot}->{logger}->log(" $name\n"); $self->{pbot}->{logger}->log("Error loading $package: $exception");
exit;
$self->{pbot}->{refresher}->{refresher}->refresh_module($package); }
}
eval { # packages loaded successfully
require "$package"; return 1;
$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;
} }
} }
# no packages found
return 0;
} }
1; 1;