From 3d6521f7d2028fb51047963a477c97933ea56ac8 Mon Sep 17 00:00:00 2001 From: Emily-Rose Francoeur Date: Wed, 4 Oct 2023 16:19:15 -0400 Subject: [PATCH] Bug 34978: Add --include and --exclude options to install_plugins.pl I have added the options --include and --exclude to the install_plugins.pl script. When running the script, you can now specify which plugins you want to install (--include) and which ones you do not want to install (--exclude). --include installs only the plugins from the specified classes. --exclude installs all plugins except those from the specified classes. It's possible to specify the package to install a specific plugin. --include and --exclude cannot be used simultaneously. Usage example: ./misc/devel/install_plugins --include --include " TEST PLAN 1) Apply the patch 2) Add at least three plugins to the plugin directory ([Koha]/plugins/Koha/Plugin/) 3) Empty the "plugin_data" and "plugin_methods" tables (SQL command: "delete from plugin_data; delete from plugin_methods;") 4) In a terminal, execute this command "./misc/devel/install_plugins.pl --include --include " (Replace and with the class and package of your plugins) 5) In your Koha intranet, go to "Koha administration > Manage plugins" 6) Verify that only the specified plugins are installed 7) Repeat step 3 8) In a terminal, execute this command from your Koha project"./misc/devel/install_plugins.pl --exclude --exclude " 9) Verify that all plugins except the specified ones are installed 10) Run the tests from the Plugins.t file ([Koha]/t/db_dependent/Koha/Plugins/Plugins.t) 11) Ensure that all tests are successful Signed-off-by: Pedro Amorim Signed-off-by: Tomas Cohen Arazi --- Koha/Plugins.pm | 30 +++++++++++++- misc/devel/install_plugins.pl | 28 ++++++++++++-- t/db_dependent/Koha/Plugins/Plugins.t | 56 ++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 5 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 2b4c07956e6..6669da3702f 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -277,12 +277,40 @@ sub InstallPlugins { my $verbose = $params->{verbose} // $self->_verbose; my @plugin_classes = $self->plugins(); - my @plugins; + my (@plugins, @classes_filters); + + my $has_filters = defined($params->{include}) || defined($params->{exclude}); + + # Warn user if the specified classes doesn't exist and return nothing + if ($has_filters) { + @classes_filters = defined($params->{include}) ? @{$params->{include}} : @{$params->{exclude}}; + + foreach my $classes_filter (@classes_filters) { + my $is_found = 0; + + foreach my $plugin_class (@plugin_classes) { + $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$"); + } + unless ($is_found) { + warn "$classes_filter have not been found, try a different name"; + return; + } + } + } foreach my $plugin_class (@plugin_classes) { if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) { next unless $plugin_class->isa('Koha::Plugins::Base'); + # Apply the filters + if ($has_filters) { + my $is_found = 0; + foreach my $classes_filter (@classes_filters) { + $is_found = 1 if $plugin_class =~ ":$classes_filter\$|^$classes_filter\$" || ($classes_filter =~ "^::" && $plugin_class =~ "$classes_filter\$"); + } + next if (defined($params->{include}) && !$is_found) || (defined($params->{exclude}) && $is_found); + } + my $plugin; my $failed_instantiation; diff --git a/misc/devel/install_plugins.pl b/misc/devel/install_plugins.pl index 2ccc68a15bd..02c96474cfe 100755 --- a/misc/devel/install_plugins.pl +++ b/misc/devel/install_plugins.pl @@ -26,11 +26,18 @@ use Koha::Script; use C4::Context; use Koha::Plugins; -my ($help); -GetOptions( 'help|?' => \$help ); +my ($help, @include, @exclude); + +GetOptions( + 'help|?' => \$help, + 'include=s' => \@include, + 'exclude=s' => \@exclude, +) or die "Installation aborted\n"; pod2usage(1) if $help; +die "Installation aborted : --include and --exclude can't be used simultaneously\n" if @include && @exclude; + unless ( C4::Context->config("enable_plugins") ) { print "The plugin system must be enabled for one to be able to install plugins\n"; @@ -46,7 +53,12 @@ for my $existing_plugin (@existing_plugins) { $existing_plugins->{ $existing_plugin->{metadata}->{name} } = $existing_plugin->{metadata}->{version}; } -my @installed_plugins = Koha::Plugins->new()->InstallPlugins( { verbose => 0 } ); +my $params = {}; +$params->{'include'} = \@include if @include; +$params->{'exclude'} = \@exclude if @exclude; + +my @installed_plugins = Koha::Plugins->new()->InstallPlugins($params); + unless (@installed_plugins) { my $plugins_dir = C4::Context->config("pluginsdir"); if ( ref($plugins_dir) eq 'ARRAY' ) { @@ -91,6 +103,8 @@ install_plugins.pl - install all plugins found in plugins_dir Options: -?|--help brief help message + --include install only the plugins of the specified classes + --exclude install all the plugins except the specified ones =head1 OPTIONS @@ -100,6 +114,14 @@ Options: Print a brief help message and exits +=item B<--include> + +Retrieve the list of plugin classes and install only those. It's possible to specify the package of a class to install a specific plugin (e.g. --include --include ). + +=item B<--exclude> + +Retrieve the list of plugin classes and install all plugins except those. It's possible to specify the package of a class to install a specific plugin (e.g. --exclude --exclude ). + =back =head1 DESCRIPTION diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 1c8f96ab156..7fdebf55a1e 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -24,7 +24,7 @@ use File::Temp qw( tempdir tempfile ); use FindBin qw($Bin); use Module::Load::Conditional qw(can_load); use Test::MockModule; -use Test::More tests => 18; +use Test::More tests => 30; use Test::Warn; use C4::Context; @@ -185,6 +185,60 @@ subtest 'GetPlugins() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'InstallPlugins() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + # Temporarily remove any installed plugins data + Koha::Plugins::Methods->delete; + $schema->resultset('PluginData')->delete; + + # Tests for the exclude paramater + # Test the returned plugins of the InstallPlugins subroutine + my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); + my @installed_plugins = $plugins->InstallPlugins({ exclude => ["Test", "Koha::Plugin::MarcFieldValues"] }); + my $plugin_classes = join(" ", map{$_->{class}} @installed_plugins); + + my $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"] + && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$"; + ok($result, "Excluded plugins are not returned"); + + # Test the plugins in the database + my @plugins = $plugins->GetPlugins({ all => 1, error => 1 }); + $plugin_classes = join(" ", map{$_->{class}} @plugins); + + $result = grep { $plugin_classes !~ $_ } [":Test |:Test\$", ":MarcFieldValues |:MarcFieldValues\$"] + && $plugin_classes =~ ":TestItemBarcodeTransform |:TestItemBarcodeTransform\$"; + ok($result, "Excluded plugins are not installed"); + + # Remove installed plugins data + Koha::Plugins::Methods->delete; + $schema->resultset('PluginData')->delete; + + # Tests for the include parameter + # Test the returned plugins of the InstallPlugins subroutine + @installed_plugins = $plugins->InstallPlugins({ include => ["Test", "Koha::Plugin::MarcFieldValues"]}); + + $result = 1; + foreach my $plugin_class ( map{ $_->{class} } @installed_plugins ) { + $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$"); + } + ok($result, "Only included plugins are returned"); + + # Test the plugins in the database + @plugins = $plugins->GetPlugins({ all => 1, error => 1}); + + $result = 1; + foreach my $plugin_class ( map{ $_->{class} } @plugins ) { + $result = 0 unless("$plugin_class" =~ ":Test\$" || "$plugin_class" =~ ":MarcFieldValues\$"); + } + ok($result, "Only included plugins are installed"); + + $schema->storage->txn_rollback; +}; + subtest 'Version upgrade tests' => sub { plan tests => 1; -- 2.45.0