Iller/Role/HasPlugins.pm

filter: Statementstmt 89.1% 164 / 184 Branchbran 70.3% 76 / 108 Conditioncond 63.6% 14 / 22 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 13 totaltotal 77.5% 287 / 370 CC 71 SCAR 24.8
CC
71
Cov
81%
check_conditionals
36.7
remove_plugin
12.0
parse_plugins
9.0
CRAP 11.9
« Iller/DocType.pm ↑ Summary Iller/DocType/Dist.pm »
1 5 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dist::Iller::Role::HasPlugins;
6  
7 5 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8 # ABSTRACT: A role for DocTypes that contains plugins (like dist/weaver)
9 5 our $VERSION = '0.1411';
10  
11 5 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Types::Standard qw/ArrayRef InstanceOf/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use PerlX::Maybe qw/maybe provided/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Safe::Isa qw/$_can/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use List::Util qw/none/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Dist::Iller::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # packages_for_plugin should return a CodeRef that in turn returns an ArrayRef of HashRefs, see ::DocType::Dist
20 5 requires qw/
21 packages_for_plugin
22 /;
23  
24 has plugins => (
25 is => 'rw',
26 isa => ArrayRef[InstanceOf['Dist::Iller::Plugin']],
27 traits => [qw/Array/],
28 6 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 5 handles => {
30 add_plugin => 'push',
31 all_plugins => 'elements',
32 filter_plugins => 'grep',
33 find_plugin => 'first',
34 count_plugins => 'count',
35 has_plugins => 'count',
36 get_plugin => 'get',
37 map_plugins => 'map',
38 },
39 );
40  
41 101 ▶around add_plugin => sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
42 101 my $next = shift;
43 101 my $self = shift;
44 101 my $plugin_data = shift;
45 101 ▶ my $plugin = (InstanceOf['Dist::Iller::Plugin'])->check($plugin_data) ? $plugin_data : Dist::Iller::Plugin->new($plugin_data);
Branch: 1 branch
BranchTrueFalseTotal
&InstanceOf(['Dist::Iller::Plugin'])->check($plugin_data) ? : 0 101 101
46  
47 101 ▶ if($self->find_plugin(sub { $_->plugin_name eq $plugin->plugin_name })) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if ($self->find_plugin(sub { $_->plugin_name eq $plugin->plugin_name; } )) 0 101 101
48 0 say "[Iller] ! Duplicate plugin found - skips [@{[ $plugin->plugin_name ]}]";
49 0 return;
50 }
51  
52 101 $self->$next($plugin);
53 5 };
54  
55 8 ▶sub parse_plugins {
Subroutine parse_plugins: called CC=9 SCAR=22.0
Pod undocumented
56 8 my $self = shift;
57 8 my $yaml = shift;
58  
59 8 ▶ return if !defined $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yaml 0 8 8
60  
61 8 foreach my $item (@$yaml) {
62 103 ▶ $self->parse_config($item) if exists $item->{'+config'}; # is in ::DocType
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+config"} 2 101 103
63 103 ▶ $self->parse_plugin($item) if exists $item->{'+plugin'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+plugin"} 95 8 103
64 103 ▶ $self->parse_remove($item) if exists $item->{'+remove_plugin'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+remove_plugin"} 1 102 103
65 103 ▶ $self->parse_replace($item) if exists $item->{'+replace_plugin'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+replace_plugin"} 2 101 103
66 103 ▶ $self->parse_extend($item) if exists $item->{'+extend_plugin'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+extend_plugin"} 1 102 103
67 103 ▶ $self->parse_add($item) if exists $item->{'+add_plugin'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"+add_plugin"} 2 101 103
68 }
69 }
70  
71 95 ▶sub parse_plugin {
Subroutine parse_plugin: called CC=2 SCAR=6.9
Pod undocumented
72 95 my $self = shift;
73 95 my $plugin = shift;
74  
75 95 my $plugin_name = delete $plugin->{'+plugin'};
76  
77 95 ▶ return if !$self->check_conditionals($plugin);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditionals($plugin) 1 94 95
78  
79 $self->add_plugin({
80 plugin_name => $self->set_value_from_config($plugin_name),
81 maybe base => delete $plugin->{'+base'},
82 maybe in => delete $plugin->{'+in'},
83 maybe version => delete $plugin->{'+version'},
84 maybe documentation => delete $plugin->{'+documentation'},
85 94 maybe prereqs => delete $plugin->{'+prereqs'},
86 parameters => $self->set_values_from_config($plugin),
87 });
88 }
89  
90 2 ▶sub parse_replace {
Subroutine parse_replace: called CC=3 SCAR=11.1
Pod undocumented
91 2 my $self = shift;
92 2 my $replacer = shift;
93  
94 2 ▶ return if !$self->check_conditionals($replacer);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditionals($replacer) 0 2 2
95  
96 2 my $plugin_name = $self->set_value_from_config(delete $replacer->{'+replace_plugin'});
97 2 my $replace_with = $self->set_value_from_config(delete $replacer->{'+with'});
98  
99 my $plugin = Dist::Iller::Plugin->new(
100 plugin_name => $replace_with // $plugin_name,
101 maybe base => delete $replacer->{'+base'},
102 maybe in => delete $replacer->{'+in'},
103 maybe version => delete $replacer->{'+version'},
104 2 ▶ maybe documentation => delete $replacer->{'+documentation'},
Condition66%2/3
$replace_with // $plugin_name
l!l&&r!l&&!r
110
MC/DC0%0/2
$replace_with // $plugin_name
$replace_with $plugin_name
Truth table66%2/3
A // B
A= $replace_withB= $plugin_name
ABresult
1X1
011
000
105 parameters => $self->set_values_from_config($replacer),
106 );
107  
108 2 $self->insert_plugin($plugin_name, $plugin, after => 0, replace => 1);
109 }
110  
111 1 ▶sub parse_extend {
Subroutine parse_extend: called CC=2 SCAR=7.0
Pod undocumented
112 1 my $self = shift;
113 1 my $extender = shift;
114  
115 1 ▶ return if !$self->check_conditionals($extender);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditionals($extender) 0 1 1
116  
117 1 my $plugin_name = delete $extender->{'+extend_plugin'};
118  
119 1 my $plugin = Dist::Iller::Plugin->new(
120 plugin_name => $self->set_value_from_config($plugin_name),
121 parameters => $self->set_values_from_config($extender),
122 );
123  
124 1 $self->extend_plugin($plugin_name, $plugin, remove => delete $extender->{'+remove'});
125 }
126  
127 2 ▶sub parse_add {
Subroutine parse_add: called CC=4 SCAR=13.9
Pod undocumented
128 2 my $self = shift;
129 2 my $adder = shift;
130  
131 2 ▶ return if !$self->check_conditionals($adder);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditionals($adder) 0 2 2
132  
133 2 my $plugin_name = delete $adder->{'+add_plugin'};
134  
135 my $plugin = Dist::Iller::Plugin->new(
136 plugin_name => $self->set_value_from_config($plugin_name),
137 maybe base => delete $adder->{'+base'},
138 maybe in => delete $adder->{'+in'},
139 maybe version => delete $adder->{'+version'},
140 2 maybe documentation => delete $adder->{'+documentation'},
141 parameters => $self->set_values_from_config($adder),
142 );
143  
144 2 my $after = delete $adder->{'+after'};
145 2 my $before = delete $adder->{'+before'};
146  
147 2 ▶ $self->insert_plugin(($after ? $after : $before), $plugin, after => ($after ? 1 : 0), replace => 0);
Branch: 2 branches
BranchTrueFalseTotal
$after ? : 1 1 2
$after ? : 1 1 2
148 }
149  
150 1 ▶sub parse_remove {
Subroutine parse_remove: called CC=2 SCAR=7.6
Pod undocumented
151 1 my $self = shift;
152 1 my $remover = shift;
153  
154 1 ▶ return if !$self->check_conditionals($remover);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_conditionals($remover) 1 0 1
155 0 $self->remove_plugin($self->set_value_from_config($remover->{'+remove_plugin'}));
156 }
157  
158 101 ▶sub check_conditionals {
Subroutine check_conditionals: called CC=20 SCAR=36.0
Pod undocumented
159 101 my $self = shift;
160 101 my $plugin_data = shift;
161  
162 6 ▶ my $get_type_what = sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
163 6 my $from = shift;
164  
165 6 ▶ return () if !defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 0 6 6
166 6 ▶ return () if !length $from;
Branch: 1 branch
BranchTrueFalseTotal
unless length $from 0 6 6
167 6 ▶ return () if $from !~ m{[^.]\.[^.]};
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ /[^.]\.[^.]/u 0 6 6
168 6 return split /\./ => $from;
169 101 };
170  
171 101 ▶ if(exists $plugin_data->{'+if'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $plugin_data->{'+if'}) { } 1 100 101
elsif (exists $plugin_data->{'+remove_if'}) { } 3 97 100
elsif (exists $plugin_data->{'+add_if'}) { } 2 95 97
172 1 my($type, $what) = $get_type_what->($plugin_data->{'+if'});
173 1 ▶ return 0 if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 1 1
174 1 ▶ return 0 if $type eq '$env' && !exists $ENV{ uc $what };
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "\$env" and not exists $ENV{uc $what} 1 0 1
Condition33%1/3
$type eq "\$env" and not exists $ENV{uc $what}
!ll&&!rl&&r
001
MC/DC0%0/2
$type eq "\$env" and not exists $ENV{uc $what}
$type eq "\$env" not exists $ENV{uc $what}
Truth table33%1/3
A and not(B)
A= $type eq "\$env"B= not exists $ENV{uc $what}
ABresult
0X0
100
111
175 0 ▶ return $ENV{ uc $what } if $type eq '$env';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "\$env" 0 0 0
176 }
177 elsif(exists $plugin_data->{'+remove_if'}) {
178 3 my($type, $what) = $get_type_what->($plugin_data->{'+remove_if'});
179  
180 3 ▶ return if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 3 3
181  
182 3 ▶ if($type eq '$env') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '$env') { } 0 3 3
elsif ($type eq '$self' and $self->has_config_obj) { } 2 1 3
183 0 ▶ return 0 if !exists $ENV{ uc $what };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{uc $what} 0 0 0
184 0 return !$ENV{ uc $what };
185 }
186 ▶ elsif($type eq '$self' && $self->has_config_obj) {
Condition66%2/3
$type eq "\$self" and $self->has_config_obj
!ll&&!rl&&r
012
MC/DC50%1/2
$type eq "\$self" and $self->has_config_obj
$type eq "\$self" $self->has_config_obj
Truth table66%2/3
A and B
A= $type eq "\$self"B= $self->has_config_obj
ABresult
0X0
100
111
187 2 ▶ return 1 if !$self->config_obj->$_can($what);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what) 0 2 2
188 2 return !$self->config_obj->$what;
189 }
190 }
191 elsif(exists $plugin_data->{'+add_if'}) {
192 2 my($type, $what) = $get_type_what->($plugin_data->{'+add_if'});
193 2 ▶ return if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 2 2
194  
195 2 ▶ if($type eq '$env') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '$env') { } 0 2 2
elsif ($type eq '$self' and $self->has_config_obj) { } 1 1 2
196 0 ▶ return 0 if !exists $ENV{ uc $what };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{uc $what} 0 0 0
197 0 return $ENV{ uc $what };
198 }
199 ▶ elsif($type eq '$self' && $self->has_config_obj) {
Condition66%2/3
$type eq "\$self" and $self->has_config_obj
!ll&&!rl&&r
011
MC/DC50%1/2
$type eq "\$self" and $self->has_config_obj
$type eq "\$self" $self->has_config_obj
Truth table66%2/3
A and B
A= $type eq "\$self"B= $self->has_config_obj
ABresult
0X0
100
111
200 1 ▶ return 0 if !$self->config_obj->$_can($what);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what) 0 1 1
201 1 return $self->config_obj->$what;
202 }
203 }
204  
205 97 return 1;
206 }
207  
208 4 ▶sub insert_plugin {
Subroutine insert_plugin: called CC=9 SCAR=22.0
Pod undocumented
209 4 my $self = shift;
210 4 my $plugin_name = shift;
211 4 my $new_plugin = shift;
212 4 my %settings = @_;
213  
214 4 ▶ my $after = $settings{'after'} || 0;
Condition100%2/2
$settings{'after'} || 0
l!l
13
MC/DC100%1/1
$settings{'after'} || 0
$settings{'after'}
Truth table100%2/2
A
A= $settings{'after'}
Aresult
11
00
215 4 ▶ my $replace = $settings{'replace'} || 0;
Condition100%2/2
$settings{'replace'} || 0
l!l
22
MC/DC100%1/1
$settings{'replace'} || 0
$settings{'replace'}
Truth table100%2/2
A
A= $settings{'replace'}
Aresult
11
00
216  
217 4 foreach my $index (0 .. $self->count_plugins - 1) {
218 38 my $current_plugin = $self->get_plugin($index);
219  
220 38 ▶ if($current_plugin->plugin_name eq $plugin_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_plugin->plugin_name eq $plugin_name) 4 34 38
221 4 my @all_plugins = $self->all_plugins;
222 4 ▶ splice @all_plugins, ($after ? $index + 1 : $index), ($replace ? 1 : 0), $new_plugin;
Branch: 2 branches
BranchTrueFalseTotal
$after ? : 1 3 4
$replace ? : 2 2 4
223 4 $self->plugins(\@all_plugins);
224  
225 4 ▶ if($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) { } 2 2 4
226 2 say sprintf "[Iller] Replaced [%s] with [%s]", $current_plugin->plugin_name, $new_plugin->plugin_name;
227 }
228 else {
229 2 ▶ say sprintf "[Iller] Inserted [%s] %s [%s]", $new_plugin->plugin_name, ($after ? 'after' : 'before'), $current_plugin->plugin_name;
Branch: 1 branch
BranchTrueFalseTotal
$after ? : 1 1 2
230 }
231 4 last;
232 }
233 }
234 }
235  
236 1 ▶sub extend_plugin {
Subroutine extend_plugin: called CC=7 SCAR=19.6
Pod undocumented
237 1 my $self = shift;
238 1 my $plugin_name = shift;
239 1 my $new_plugin = shift;
240 1 my %settings = @_;
241  
242 1 my $remove = $settings{'remove'};
243  
244 1 ▶ $remove = $remove ? ref $remove eq 'ARRAY' ? $remove
Branch: 2 branches
BranchTrueFalseTotal
$remove ? : 1 0 1
ref $remove eq 'ARRAY' ? : 0 1 1
245 : [ $remove ]
246 : []
247 ;
248 1 ▶ say sprintf '[Iller] From %s remove %s', $plugin_name, join ', ' => @$remove if scalar @$remove;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$remove 1 0 1
249 1 say sprintf '[Iller] Extended [%s]', $plugin_name;
250  
251 1 foreach my $index (0 .. $self->count_plugins - 1) {
252 8 my $current_plugin = $self->get_plugin($index);
253  
254 8 ▶ if($current_plugin->plugin_name eq $plugin_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_plugin->plugin_name eq $plugin_name) 1 7 8
255 1 foreach my $param_to_remove (@$remove) {
256 1 $current_plugin->delete_parameter($param_to_remove);
257 }
258 1 $current_plugin->merge_with($new_plugin);
259 1 last;
260 }
261 }
262 }
263  
264 0 ▶sub remove_plugin {
Subroutine remove_plugin: not called CC=3 SCAR=24.8
Pod undocumented
265 0 my $self = shift;
266 0 my $remove_name = shift;
267  
268 0 foreach my $index (0 .. $self->count_plugins - 1) {
269 0 my $current_plugin = $self->get_plugin($index);
270  
271 0 ▶ if($current_plugin->plugin_name eq $remove_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_plugin->plugin_name eq $remove_name) 0 0 0
272 0 my @all_plugins = $self->all_plugins;
273 0 splice @all_plugins, $index, 1;
274 0 $self->plugins(\@all_plugins);
275 0 say "[Iller] Removed [$remove_name]";
276 0 last;
277 }
278 }
279 }
280  
281 99 ▶sub set_values_from_config {
Subroutine set_values_from_config: called CC=7 SCAR=19.5
Pod undocumented
282 99 my $self = shift;
283 99 my $parameters = shift;
284  
285 99 ▶ return $parameters if !$self->has_config_obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_config_obj 67 32 99
286  
287 32 foreach my $param (keys %$parameters) {
288 18 ▶ next if $param =~ m{^\+};
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /^\+/u 2 16 18
289 16 ▶ next if !defined $parameters->{ $param };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameters->{$param} 0 16 16
290  
291 16 ▶ $parameters->{ $param } = ref $parameters->{ $param } eq 'ARRAY' ? $parameters->{ $param } : [ $parameters->{ $param } ];
Branch: 1 branch
BranchTrueFalseTotal
ref $parameters->{$param} eq 'ARRAY' ? : 1 15 16
292  
293 VALUE:
294 16 foreach my $i (0 .. scalar @{ $parameters->{ $param } } - 1) {
295 16 $parameters->{ $param }[$i] = $self->set_value_from_config($parameters->{ $param }[$i]);
296 }
297 }
298 32 return $parameters;
299 }
300  
301 117 ▶sub set_value_from_config {
Subroutine set_value_from_config: called CC=8 SCAR=21.6
Pod undocumented
302 117 my $self = shift;
303 117 my $value = shift;
304  
305 117 ▶ return $value if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 116 117
306 116 ▶ return $value if $value !~ m{[^.]\.[^.]};
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /[^.]\.[^.]/u 113 3 116
307 3 my($type, $what) = split /\./ => $value;
308 3 ▶ return $value if none { $_ eq $type } qw/$env $self/;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $_ eq $type; } , "\$env", "\$self") 0 3 3
309  
310 3 ▶ if($type eq '$env' && exists $ENV{ uc $what }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '$env' and exists $ENV{uc $what}) { } 0 3 3
elsif ($type eq '$self' and $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what)) { } 2 1 3
Condition33%1/3
$type eq "\$env" and exists $ENV{uc $what}
!ll&&!rl&&r
300
MC/DC0%0/2
$type eq "\$env" and exists $ENV{uc $what}
$type eq "\$env" exists $ENV{uc $what}
Truth table33%1/3
A and B
A= $type eq "\$env"B= exists $ENV{uc $what}
ABresult
0X0
100
111
311 0 return $ENV{ uc $what };
312 }
313 ▶ elsif($type eq '$self' && $self->config_obj->$_can($what)) {
Condition66%2/3
$type eq "\$self" and $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what)
!ll&&!rl&&r
012
MC/DC50%1/2
$type eq "\$self" and $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what)
$type eq "\$self" $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what)
Truth table66%2/3
A and B
A= $type eq "\$self"B= $self->config_obj->$Dist::Iller::Role::HasPlugins::_can($what)
ABresult
0X0
100
111
314 2 return $self->config_obj->$what;
315 }
316 1 return $value;
317 }
318  
319  
320 2 ▶sub plugins_to_hash {
Subroutine plugins_to_hash: called CC=1 SCAR=0.0
Pod undocumented
321 2 my $self = shift;
322  
323 return [
324 32 ▶ $self->map_plugins(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
325 32 my $plugin = $_;
326 32 my $parameters = {};
327 32 $parameters->{ $_->[0] } = $_->[1] for $plugin->parameters_kv;
328 +{
329 '+plugin' => $plugin->plugin_name,
330 provided $_->has_base, '+base' => $plugin->base,
331 '+in' => $plugin->in,
332 '+version' => $plugin->version,
333 provided $_->has_prereqs, '+prereqs' => $plugin->prereqs_to_array,
334 maybe '+documentation' => $plugin->documentation,
335 32 %{ $parameters },
336 }
337 })
338 2 ];
339 }
340  
341 5 1;
342  
343 __END__
« Iller/DocType.pm ↑ Summary Iller/DocType/Dist.pm »