0177d733b46fa2f351576d345e6ae45ffb2648d2
[srvgit] / misc / devel / install_plugins.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2019 Koha Development Team
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use Getopt::Long;
22 use Pod::Usage;
23
24 use Koha::Script;
25
26 use C4::Context;
27 use Koha::Plugins;
28
29 my ($help);
30 GetOptions( 'help|?' => \$help );
31
32 pod2usage(1) if $help;
33
34 unless ( C4::Context->config("enable_plugins") ) {
35     print
36 "The plugin system must be enabled for one to be able to install plugins\n";
37     exit 1;
38 }
39
40 my @existing_plugins = Koha::Plugins->new()->GetPlugins({
41     all    => 1,
42     errors => 1,
43 });
44 my $existing_plugins;
45 for my $existing_plugin (@existing_plugins) {
46     if( defined $existing_plugin->{error} ){
47         print "Could not load: ".$existing_plugin->{name}." any associated method will be removed\n";
48     } else {
49         $existing_plugins->{ $existing_plugin->{metadata}->{name} } =
50           $existing_plugin->{metadata}->{version};
51     }
52 }
53
54 my @installed_plugins = Koha::Plugins->new()->InstallPlugins();
55 unless (@installed_plugins) {
56     my $plugins_dir = C4::Context->config("pluginsdir");
57     if ( ref($plugins_dir) eq 'ARRAY' ) {
58         print "No plugins found\n";
59         print "pluginsdir contains: \n" . join( '\n', @{$plugins_dir} );
60     }
61     else {
62         print "No plugins found at $plugins_dir\n";
63     }
64     exit 0;
65 }
66
67 for my $installed_plugin (@installed_plugins) {
68     if ( !exists( $existing_plugins->{ $installed_plugin->{metadata}->{name} } )
69       )
70     {
71         print "Installed "
72           . $installed_plugin->{metadata}->{name}
73           . " version "
74           . $installed_plugin->{metadata}->{version} . "\n";
75     }
76     elsif ( $existing_plugins->{ $installed_plugin->{metadata}->{name} } ne
77         $installed_plugin->{metadata}->{version} )
78     {
79         print "Upgraded "
80           . $installed_plugin->{metadata}->{name}
81           . " from version "
82           . $existing_plugins->{ $installed_plugin->{metadata}->{name} }
83           . " to version "
84           . $installed_plugin->{metadata}->{version} . "\n";
85     }
86 }
87 print "All plugins successfully re-initialised\n";
88
89 =head1 NAME
90
91 install_plugins.pl - install all plugins found in plugins_dir
92
93 =head1 SYNOPSIS
94
95  install_plugins.pl
96
97 Options:
98   -?|--help        brief help message
99
100 =head1 OPTIONS
101
102 =over 8
103
104 =item B<--help|-?>
105
106 Print a brief help message and exits
107
108 =back
109
110 =head1 DESCRIPTION
111
112 A simple script to install plugins from the command line
113
114 =cut