2015-06-16 04:55:46 +02:00
|
|
|
# File: Refresher.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Refreshes/reloads module subroutines. Does not refresh/reload
|
2020-02-03 18:48:19 +01:00
|
|
|
# module member data, only subroutines. TODO: reinitialize modules in order
|
|
|
|
# to refresh member data too.
|
2015-06-16 04:55:46 +02:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-06-16 04:55:46 +02:00
|
|
|
package PBot::Refresher;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::Class';
|
2015-06-16 04:55:46 +02:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2015-06-16 04:55:46 +02:00
|
|
|
use Module::Refresh;
|
2020-04-20 19:53:35 +02:00
|
|
|
use File::Basename;
|
2015-06-16 04:55:46 +02:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
$self->{refresher} = Module::Refresh->new;
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_refresh(@_) }, "refresh", 1);
|
2015-06-16 04:55:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_refresh {
|
|
|
|
my ($self, $context) = @_;
|
2020-04-20 19:53:35 +02:00
|
|
|
|
2020-04-21 02:53:32 +02:00
|
|
|
my $last_update = $self->{pbot}->{updater}->get_last_update_version;
|
|
|
|
my @updates = $self->{pbot}->{updater}->get_available_updates($last_update);
|
2020-04-20 19:53:35 +02:00
|
|
|
|
2020-04-21 02:53:32 +02:00
|
|
|
if (@updates) {
|
|
|
|
return "Update available; cannot refresh. Please restart PBot to begin update of " . join(', ', map { basename $_ } @updates);
|
2020-04-20 19:53:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:51:59 +01:00
|
|
|
my $refresh_error;
|
|
|
|
local $SIG{__WARN__} = sub {
|
2020-02-16 21:36:30 +01:00
|
|
|
my $warning = shift;
|
|
|
|
warn $warning and return if $warning =~ /Can't undef active/;
|
2020-02-17 01:12:31 +01:00
|
|
|
warn $warning and return if $warning =~ /subroutine .* redefined/i;
|
2020-02-16 21:36:30 +01:00
|
|
|
$refresh_error = $warning;
|
2020-02-16 18:51:59 +01:00
|
|
|
$refresh_error =~ s/\s+Compilation failed in require at \/usr.*//;
|
2020-02-16 21:36:30 +01:00
|
|
|
$refresh_error =~ s/in \@INC.*/in \@INC/;
|
2020-02-16 18:51:59 +01:00
|
|
|
$self->{pbot}->{logger}->log("Error refreshing: $refresh_error\n");
|
|
|
|
};
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $result = eval {
|
2020-05-04 22:21:35 +02:00
|
|
|
if (not $context->{arguments}) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Refreshing all modified modules\n");
|
|
|
|
$self->{refresher}->refresh;
|
2020-02-16 18:51:59 +01:00
|
|
|
return "Error refreshing: $refresh_error" if defined $refresh_error;
|
2020-02-15 23:38:32 +01:00
|
|
|
return "Refreshed all modified modules.\n";
|
|
|
|
} else {
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{logger}->log("Refreshing module $context->{arguments}\n");
|
|
|
|
$self->{refresher}->refresh_module($context->{arguments});
|
2020-02-16 18:51:59 +01:00
|
|
|
return "Error refreshing: $refresh_error" if defined $refresh_error;
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Refreshed module.\n");
|
|
|
|
return "Refreshed module.\n";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error refreshing: $@\n");
|
|
|
|
return $@;
|
2015-06-16 04:55:46 +02:00
|
|
|
}
|
2020-04-20 19:53:35 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
2015-06-16 04:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|