Qgoda/PluginUtils.pm

filter: Statementstmt 46.7% 43 / 92 Branchbran 10.7% 3 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 3 totaltotal 37.5% 56 / 149 CC 20 SCAR 44.0
CC
20
Cov
36%
init_plugin
182.0
search_local_plugins
12.2
load_plugins
6.6
CRAP 81.9
« Qgoda/Migrator/Jekyll/LiquidConverter.pm ↑ Summary Qgoda/HTMLFilter/TOC.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::PluginUtils;
20 6 $Qgoda::PluginUtils::VERSION = 'v0.9.2';
21 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 6 ▶use Locale::TextDomain qw(qgoda);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use Qgoda::Util qw(read_file empty);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 @EXPORT_OK = qw(load_plugins);
33  
34 sub search_local_plugins($$$);
35 sub init_plugin($$$);
36  
37 6 my %languages = (
38 Perl => 'Qgoda::Plugger::Perl',
39 Python => 'Qgoda::Plugger::Inline::Python',
40 );
41  
42 6 my %types = (
43 'TT2::Filter' => 'Qgoda::PluginLoader::TT2::Filter',
44 );
45  
46 sub load_plugins {
47 2 ▶ my ($q) = @_;
Subroutine load_plugins: called CC=5 SCAR=18.9
Pod undocumented
48  
49 2 my $logger = $q->logger('plugin-loader');
50  
51 2 $logger->info(__("initializing plug-ins."));
52  
53 2 my $config = $q->config;
54  
55 # Allow loading local Perl plug-ins.
56 2 push @INC, $config->{srcdir};
57  
58 2 my $modules_dir = File::Spec->catfile($config->{srcdir}, 'node_modules');
59 my %plugins = map {
60 0 $_ => {
61 package_dir => File::Spec->catfile($modules_dir, $_),
62 package_json => File::Spec->catfile($modules_dir, $_,
63 'package.json'),
64 }
65 2 ▶ } @{$config->{plugins} || []};
Branch: 1 branch
BranchTrueFalseTotal
$config->{"plugins"} or [] 2 0 2
66 2 foreach my $name (keys %plugins) {
67 0 $logger->info(__x("plugin {name} found in configuration.",
68 name => $name));
69 }
70  
71 2 my $plugin_dir = $config->{paths}->{plugins};
72 2 search_local_plugins(\%plugins, $plugin_dir, $logger);
73  
74 2 ▶ while (my ($name, $plugin) = each %plugins) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %plugins) 0 2 2
75 0 eval {
76 0 init_plugin $name, $plugin, $logger;
77 };
78 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
79 0 $logger->fatal(__x("plugin '{name}': {error}",
80 name => $name, error => $@));
81 }
82 }
83  
84 2 return 1;
85 }
86  
87 sub search_local_plugins($$$) {
88 2 ▶ my ($plugins, $plugin_dir, $logger) = @_;
Subroutine search_local_plugins: called CC=4 SCAR=25.0
Pod undocumented
89  
90 2 ▶ return 1 if !-e $plugin_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $plugin_dir 2 0 2
91  
92 0 local *DIR;
93 0 ▶ opendir DIR, $plugin_dir
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $plugin_dir or $logger->fatal(&__x("cannot open directory '{dir}': {error}!")) 0 0 0
94 or $logger->fatal(__x("cannot open directory '{dir}': {error}!"));
95  
96 0 my @subdirs = grep {!/^[._]/} readdir DIR;
97 0 foreach my $name (@subdirs) {
98 0 $logger->info(__x("plugin {name} found in plugin directory.",
99 name => $name));
100 0 my $package_dir = File::Spec->catfile($plugin_dir, $name);
101 0 $plugins->{$name} = {
102 package_dir => $package_dir,
103 package_json => File::Spec->catfile($package_dir, 'package.json'),
104 };
105 }
106  
107 0 return 1;
108 }
109  
110 sub init_plugin($$$) {
111 0 ▶ my ($name, $plugin, $logger) = @_;
Subroutine init_plugin: not called CC=13 SCAR=52.0
Pod undocumented
112  
113 0 $logger->debug(__x('initializing plugin {name}.', name => $name));
114  
115 0 my $package_json = $plugin->{package_json};
116 0 my $json = read_file $package_json;
117 0 ▶ $logger->fatal(__x('error reading plugin package file {file}: {error}!',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 0 0 0
118 file => $package_json, error => $!))
119 if !defined $json;
120  
121 0 my $data = decode_json $json;
122 0 my $plugin_data = $data->{qgoda};
123 0 ▶ $logger->fatal(__x("{file}: plugin definition (key 'qgoda') missing!",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plugin_data 0 0 0
124 file => $package_json))
125 if !defined $plugin_data;
126 0 ▶ $logger->fatal(__x("{file}: value for key 'qgoda' must be a dictionary!",
Branch: 1 branch
BranchTrueFalseTotal
unless ref $plugin_data and "HASH" eq builtin::reftype($plugin_data) 0 0 0
Condition0%0/3
ref $plugin_data and "HASH" eq builtin::reftype($plugin_data)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $plugin_data and "HASH" eq builtin::reftype($plugin_data)
ref $plugin_data "HASH" eq builtin::reftype($plugin_data)
Truth table0%0/3
A and B
A= ref $plugin_dataB= "HASH" eq builtin::reftype($plugin_data)
ABresult
0X0
100
111
127 file => $package_json))
128 if !(ref $plugin_data && 'HASH' eq reftype $plugin_data);
129 0 ▶ $plugin_data->{main} ||= $data->{main};
Condition0%0/3
$plugin_data->{"main"} ||= $data->{"main"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$plugin_data->{"main"} ||= $data->{"main"}
$plugin_data->{"main"} $data->{"main"}
Truth table0%0/3
A ||= B
A= $plugin_data->{"main"}B= $data->{"main"}
ABresult
1X1
011
000
130 $logger->fatal(__x("{file}: 'qgoda.main' or 'main' must point to main source file!",
131 file => $package_json))
132 if (empty $plugin_data->{main}
133 0 ▶ || ref $plugin_data->{main});
Branch: 1 branch
BranchTrueFalseTotal
if &empty($plugin_data->{"main"}) or ref $plugin_data->{"main"} 0 0 0
Condition0%0/3
&empty($plugin_data->{"main"}) or ref $plugin_data->{"main"}
l!l&&r!l&&!r
000
MC/DC0%0/2
&empty($plugin_data->{"main"}) or ref $plugin_data->{"main"}
&empty($plugin_data->{"main"}) ref $plugin_data->{"main"}
Truth table0%0/3
A or B
A= &empty($plugin_data->{"main"})B= ref $plugin_data->{"main"}
ABresult
1X1
011
000
134 $plugin_data->{main} = File::Spec->catfile($plugin->{package_dir},
135 0 $plugin_data->{main});
136  
137 0 my $language = $plugin_data->{language};
138 0 ▶ $logger->fatal(__x("{file}: language (qgoda.language) missing!",
Branch: 1 branch
BranchTrueFalseTotal
if &empty($language) 0 0 0
139 file => $package_json))
140 if empty $language;
141  
142 0 my $plugger_class = $languages{$language};
143 # TRANSLATORS: Language is a programming language.
144 0 ▶ $logger->fatal(__x("{file}: unsupported language '{language}'!",
Branch: 1 branch
BranchTrueFalseTotal
if &empty($language) 0 0 0
145 file => $package_json, language => $language))
146 if empty $language;
147  
148 0 my $plugger_module = $plugger_class;
149 0 $plugger_module =~ s{::}{/}g;
150 0 $plugger_module .= '.pm';
151  
152 0 require $plugger_module;
153 0 $plugin_data->{plugger} = $plugger_class->new($plugin_data);
154  
155 0 ▶ return 1 if $plugger_class->native;
Branch: 1 branch
BranchTrueFalseTotal
if $plugger_class->native 0 0 0
156  
157 0 my $type = $plugin_data->{type};
158 0 ▶ $logger->fatal(__x("{file}: type (qgoda.type) missing!",
Branch: 1 branch
BranchTrueFalseTotal
if &empty($type) 0 0 0
159 file => $package_json))
160 if empty $type;
161  
162 0 my $factory_class = $types{$type};
163 # TRANSLATORS: Language is a programming language.
164 0 ▶ $logger->fatal(__x("{file}: unsupported plugin type '{type}'!",
Branch: 1 branch
BranchTrueFalseTotal
if &empty($type) 0 0 0
165 file => $package_json, type => $type))
166 if empty $type;
167  
168 0 my $factory_module = $factory_class;
169 0 $factory_module =~ s{::}{/}g;
170 0 $factory_module .= '.pm';
171  
172 0 require $factory_module;
173 0 my $plugin_loader = $factory_class->new;
174 0 $plugin_data->{plugin_loader} = $plugin_loader;
175  
176 0 $plugin_loader->addPlugin($plugin_data);
177  
178 0 return 1;
179 }
« Qgoda/Migrator/Jekyll/LiquidConverter.pm ↑ Summary Qgoda/HTMLFilter/TOC.pm »