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;
|
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
use File::Basename;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
# export load_packages subroutine
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw/Exporter/;
|
|
|
|
our @EXPORT = qw/load_packages/;
|
|
|
|
|
|
|
|
sub load_packages {
|
2021-07-22 14:09:07 +02:00
|
|
|
my ($self, $base) = @_;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
my $base_path = join '/', split '::', $base;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
foreach my $inc_path (@INC) {
|
|
|
|
if (-d "$inc_path/$base_path") {
|
|
|
|
my @packages = glob "$inc_path/$base_path/*.pm";
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
foreach my $package (sort @packages) {
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->refresh_module($package);
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
my $name = basename $package;
|
|
|
|
$name =~ s/\.pm$//;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
$self->{pbot}->{logger}->log(" $name\n");
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
eval {
|
|
|
|
require "$package";
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
my $class = $base . '::' . $name;
|
|
|
|
$self->{packages}->{$name} = $class->new(pbot => $self->{pbot});
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->update_cache($package);
|
|
|
|
};
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
# error loading a package
|
|
|
|
if (my $exception = $@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error loading $package: $exception");
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# packages loaded successfully
|
|
|
|
return 1;
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 14:09:07 +02:00
|
|
|
# no packages found
|
|
|
|
return 0;
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|