3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-11 12:32:37 +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") {
chdir "$RealBin/../lib/PBot/Core"; my @packages = glob "$inc_path/$base_path/*.pm";
my @packages = glob "$directory/*.pm";
chdir $cwd;
foreach my $package (sort @packages) { foreach my $package (sort @packages) {
$package = "PBot/Core/$package"; $self->{pbot}->{refresher}->{refresher}->refresh_module($package);
my $class = $package; my $name = basename $package;
$class =~ s/\//::/g; $name =~ s/\.pm$//;
$class =~ s/\.pm$//;
my ($name) = $class =~ /.*::(.*)$/;
$self->{pbot}->{logger}->log(" $name\n"); $self->{pbot}->{logger}->log(" $name\n");
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
eval { eval {
require "$package"; require "$package";
my $class = $base . '::' . $name;
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot}); $self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
$self->{pbot}->{refresher}->{refresher}->update_cache($package); $self->{pbot}->{refresher}->{refresher}->update_cache($package);
}; };
# error loading a package
if (my $exception = $@) { if (my $exception = $@) {
$self->{pbot}->{logger}->log("Error loading $package: $exception"); $self->{pbot}->{logger}->log("Error loading $package: $exception");
exit; exit;
} }
} }
# packages loaded successfully
return 1;
}
}
# no packages found
return 0;
} }
1; 1;