2021-07-22 14:36:46 +02:00
|
|
|
# File: LoadModules.pm
|
2021-07-21 06:38:07 +02:00
|
|
|
#
|
2021-07-22 14:36:46 +02:00
|
|
|
# Purpose: Loads all Perl modules in a given directory, nonrecursively
|
|
|
|
# (i.e. at one depth level).
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-07-24 04:22:25 +02:00
|
|
|
package PBot::Core::Utils::LoadModules;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
|
2021-07-22 14:09:07 +02:00
|
|
|
use File::Basename;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw/Exporter/;
|
2021-07-22 14:36:46 +02:00
|
|
|
our @EXPORT = qw/load_modules/;
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub load_modules($self, $base) {
|
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") {
|
2021-07-22 14:36:46 +02:00
|
|
|
my @modules = glob "$inc_path/$base_path/*.pm";
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:36:46 +02:00
|
|
|
foreach my $module (sort @modules) {
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->refresh_module($module);
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:36:46 +02:00
|
|
|
my $name = basename $module;
|
2021-07-22 14:09:07 +02:00
|
|
|
$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 {
|
|
|
|
my $class = $base . '::' . $name;
|
2021-07-23 16:24:30 +02:00
|
|
|
require "$module";
|
|
|
|
$class->import(quiet => 1);
|
2021-07-22 14:36:46 +02:00
|
|
|
$self->{modules}->{$name} = $class->new(pbot => $self->{pbot});
|
|
|
|
$self->{pbot}->{refresher}->{refresher}->update_cache($module);
|
2021-07-22 14:09:07 +02:00
|
|
|
};
|
2021-07-21 06:38:07 +02:00
|
|
|
|
2021-07-22 14:36:46 +02:00
|
|
|
# error loading a module
|
2021-07-22 14:09:07 +02:00
|
|
|
if (my $exception = $@) {
|
2021-07-22 14:36:46 +02:00
|
|
|
$self->{pbot}->{logger}->log("Error loading $module: $exception");
|
2021-07-22 14:09:07 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 14:36:46 +02:00
|
|
|
# modules loaded successfully
|
2021-07-22 14:09:07 +02:00
|
|
|
return 1;
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-22 14:36:46 +02:00
|
|
|
# no module found
|
2021-07-22 14:09:07 +02:00
|
|
|
return 0;
|
2021-07-21 06:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|