2021-07-21 06:38:07 +02:00
|
|
|
# File: LoadPackages.pm
|
|
|
|
#
|
|
|
|
# Purpose: Loads all Perl package files in a given directory.
|
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package PBot::Utils::LoadPackages;
|
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
|
|
|
|
use Cwd;
|
|
|
|
|
|
|
|
# export load_packages subroutine
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw/Exporter/;
|
|
|
|
our @EXPORT = qw/load_packages/;
|
|
|
|
|
|
|
|
sub load_packages {
|
|
|
|
my ($self, $directory) = @_;
|
|
|
|
|
|
|
|
use FindBin qw/$RealBin/;
|
|
|
|
|
|
|
|
my $cwd = getcwd;
|
|
|
|
|
2021-07-21 08:06:03 +02:00
|
|
|
chdir "$RealBin/../lib/PBot/Core";
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
my @packages = glob "$directory/*.pm";
|
|
|
|
|
|
|
|
chdir $cwd;
|
|
|
|
|
|
|
|
foreach my $package (sort @packages) {
|
2021-07-21 08:06:03 +02:00
|
|
|
$package = "PBot/Core/$package";
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
my $class = $package;
|
|
|
|
$class =~ s/\//::/g;
|
|
|
|
$class =~ s/\.pm$//;
|
|
|
|
|
|
|
|
my ($name) = $class =~ /.*::(.*)$/;
|
|
|
|
|
|
|
|
$self->{pbot}->{logger}->log(" $name\n");
|
|
|
|
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
|
|
|
|
|
|
|
|
eval {
|
|
|
|
require "$package";
|
2021-07-21 19:49:38 +02:00
|
|
|
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
|
2021-07-21 06:38:07 +02:00
|
|
|
$self->{pbot}->{refresher}->{refresher}->update_cache($package);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (my $exception = $@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error loading $package: $exception");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|