blib/lib/Langertha/Raider/Packs/Collection.pm

filter: Statementstmt 97.7% 129 / 132 Branchbran 78.0% 39 / 50 Conditioncond 68.9% 20 / 29 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 90.9% 20 / 22 Podpod 90.9% 10 / 11 totaltotal 87.1% 224 / 257 CC 44 SCAR 18.7
CC
44
Cov
89%
enable
10.1
BUILD
10.1
activation_report
10.0
CRAP 6.5
« blib/lib/Langertha/Raider/Provider/Fetch.pm ↑ Summary blib/lib/Langertha/Raid/Parallel.pm »
1 package Langertha::Raider::Packs::Collection;
2 76 our $VERSION = '0.503';
3 # ABSTRACT: Set of loaded raider packs with per-session enable state
4  
5  
6 76 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 76 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 76 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 76 has packs_by_name => (is => 'ro', isa => 'HashRef', required => 1);
11 76 has exclusive_groups => (is => 'ro', isa => 'HashRef', required => 1);
12  
13 # Enabled pack names for this session. Mutated in-place by enable/disable/toggle.
14 has enabled_pack_names => (
15 is => 'rw',
16 isa => 'ArrayRef',
17 376 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 76 );
19  
20 # All pack names known
21 76 has all_pack_names => (
22 is => 'ro',
23 isa => 'ArrayRef',
24 lazy => 1,
25 builder => '_build_all_pack_names',
26 );
27  
28 376 ▶sub BUILD {
Subroutine BUILD: called CC=8 SCAR=23.1
Pod undocumented
29 376 my ($self) = @_;
30 376 ▶ my $init = $self->_init_defaults // [];
Condition50%1/2
$self->_init_defaults // []
l!l
3760
MC/DC0%0/1
$self->_init_defaults // []
$self->_init_defaults
Truth table50%1/2
A
A= $self->_init_defaults
Aresult
11
00
31 376 my %active;
32 my %seen_exclusive;
33  
34 376 for my $name (@$init) {
35 375 ▶ my $pack = $self->packs_by_name->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or next 375 0 375
36 375 my $grp = $pack->exclusive_group;
37  
38 375 ▶ if ($grp eq 'power') {
Branch: 1 branch
BranchTrueFalseTotal
if ($grp eq 'power') { } 0 375 375
39 0 ▶ $active{$name} = 1 unless $active{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $active{$name} 0 0 0
40 }
41 else {
42 375 ▶ unless ($seen_exclusive{$grp}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_exclusive{$grp}++) 375 0 375
43 375 $active{$name} = 1;
44 }
45 }
46 }
47  
48 376 $self->enabled_pack_names([sort keys %active]);
49 376 $self->sources->{$_} = { source => 'default', reason => 'enabled_by_default' } for keys %active;
50 }
51  
52  
53 376 ▶has sources => (is => 'ro', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
54 376 ▶has switched_off => (is => 'ro', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
55 376 ▶has detections => (is => 'rw', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
56 0 ▶has skipped_packs => (is => 'ro', isa => 'ArrayRef[HashRef]', default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
57  
58 has _init_defaults => (
59 is => 'ro',
60 isa => 'ArrayRef',
61 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 76 );
63  
64 339 ▶sub _build_all_pack_names {
Subroutine _build_all_pack_names: called CC=1 SCAR=0.0
65 339 my ($self) = @_;
66 339 return [sort keys %{$self->packs_by_name}];
67 }
68  
69  
70 94 ▶sub enable {
Subroutine enable: called CC=10 SCAR=23.1
Pod documented
71 94 my ($self, $name, $source, $reason) = @_;
72 94 ▶ my $pack = $self->packs_by_name->{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or return 93 1 94
73 93 ▶ $self->sources->{$name} = { source => $source // 'manual', reason => $reason // '/pack' };
Condition100%4/4
$source // "manual"
l!l
7617
$reason // "/pack"
l!l
7617
MC/DC100%1/1
$source // "manual"
$source
MC/DC100%1/1
$reason // "/pack"
$reason
Truth table100%2/2
A
A= $source
Aresult
11
00
Truth table100%2/2
A
A= $reason
Aresult
11
00
74 93 delete $self->switched_off->{$name};
75 93 my $grp = $pack->exclusive_group;
76  
77 93 ▶ if ($grp eq 'power') {
Branch: 1 branch
BranchTrueFalseTotal
if ($grp eq 'power') { } 62 31 93
78 62 ▶ push @{$self->enabled_pack_names}, $name unless $self->_is_enabled($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_enabled($name) 62 0 62
79 }
80 else {
81 # Exclusive group: remove others in same group first
82 my @others = grep {
83 6 my $p = $self->packs_by_name->{$_};
84 6 ▶ $p && $p->exclusive_group eq $grp && $_ ne $name;
Condition66%4/6
$p && $p->exclusive_group eq $grp && $_ ne $name
!ll&&!rl&&r
105
$p and $p->exclusive_group eq $grp
!ll&&!rl&&r
015
MC/DC33%1/3
$p && $p->exclusive_group eq $grp && $_ ne $name
$p $p->exclusive_group eq $grp $_ ne $name
Truth table50%2/4
A and B && C
A= $pB= $p->exclusive_group eq $grpC= $_ ne $name
ABCresult
0XX0
10X0
1100
1111
85 31 } @{$self->enabled_pack_names};
86  
87 31 ▶ if (@others) {
Branch: 1 branch
BranchTrueFalseTotal
if (@others) 5 26 31
88 5 my %remove = map { $_ => 1 } @others;
89 5 delete @{$self->sources}{@others};
90 5 @{$self->enabled_pack_names} = grep { !$remove{$_} } @{$self->enabled_pack_names};
91 }
92  
93 31 ▶ push @{$self->enabled_pack_names}, $name unless $self->_is_enabled($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_enabled($name) 31 0 31
94 }
95  
96 93 return;
97 }
98  
99  
100 371 ▶sub disable {
Subroutine disable: called CC=2 SCAR=6.9
Pod documented
101 371 my ($self, $name, $source, $reason) = @_;
102 371 delete $self->sources->{$name};
103 371 ▶ $self->switched_off->{$name} = { source => $source, reason => $reason } if defined $source;
Branch: 1 branch
BranchTrueFalseTotal
if defined $source 8 363 371
104 371 my %remove = map { $_ => 1 } ($name);
105 371 @{$self->enabled_pack_names} = grep { !$remove{$_} } @{$self->enabled_pack_names};
106 371 return;
107 }
108  
109  
110 6 ▶sub toggle {
Subroutine toggle: called CC=2 SCAR=6.9
Pod documented
111 6 my ($self, $name) = @_;
112 6 ▶ if ($self->_is_enabled($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_enabled($name)) { } 2 4 6
113 2 $self->disable($name);
114 }
115 else {
116 4 $self->enable($name);
117 }
118 }
119  
120  
121 40 ▶sub enable_detected {
Subroutine enable_detected: called CC=8 SCAR=22.0
Pod documented
122 40 my ($self, $name, $reason) = @_;
123 40 ▶ my $pack = $self->packs_by_name->{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or return 40 0 40
124 40 my $grp = $pack->exclusive_group;
125 40 ▶ if ($grp ne 'power') {
Branch: 1 branch
BranchTrueFalseTotal
if ($grp ne "power") 4 36 40
126 my ($holder) = grep {
127 4 my $p = $self->packs_by_name->{$_};
128 $_ ne $name && $p && $p->exclusive_group eq $grp
129 4 ▶ && ($self->sources->{$_}{source} // '') ne 'default';
Condition45%5/11
$_ ne $name && $p && $p->exclusive_group eq $grp && ($self->sources->{$_}{'source'} // '') ne 'default'
!ll&&!rl&&r
013
$_ ne $name and $p and $p->exclusive_group eq $grp
!ll&&!rl&&r
004
$_ ne $name and $p
!ll&&!rl&&r
004
$self->sources->{$_}{"source"} // ""
l!l
40
MC/DC0%0/4
$_ ne $name && $p && $p->exclusive_group eq $grp && ($self->sources->{$_}{'source'} // '') ne 'default'
$_ ne $name $p $p->exclusive_group eq $grp $self->sources->{$_}{"source"}
Truth table20%1/5
A and B and C && D
A= $_ ne $nameB= $pC= $p->exclusive_group eq $grpD= $self->sources->{$_}{"source"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
130 4 } @{$self->enabled_pack_names};
131 4 ▶ return $holder if $holder;
Branch: 1 branch
BranchTrueFalseTotal
if $holder 3 1 4
132 }
133 37 $self->enable($name, detected => $reason);
134 37 return;
135 }
136  
137  
138 103 ▶sub activation_report {
Subroutine activation_report: called CC=10 SCAR=23.0
Pod documented
139 103 my ($self) = @_;
140 103 my @report;
141 103 for my $name (@{$self->all_pack_names}) {
142 770 ▶ my $active = $self->_is_enabled($name) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_enabled($name) ? : 131 639 770
143 770 ▶ my $why = $active ? $self->sources->{$name} : $self->switched_off->{$name};
Branch: 1 branch
BranchTrueFalseTotal
$active ? : 131 639 770
144 770 my $detection = $self->detections->{$name};
145 770 ▶ next unless $active || $why || $detection;
Branch: 1 branch
BranchTrueFalseTotal
unless $active or $why or $detection 538 232 770
Condition100%6/6
$active or $why or $detection
l!l&&r!l&&!r
13894538
$active or $why
l!l&&r!l&&!r
1317632
MC/DC100%3/3
$active or $why or $detection
$active $why $detection
Truth table100%4/4
A or B or C
A= $activeB= $whyC= $detection
ABCresult
1XX1
01X1
0011
0000
146 232 my $pack = $self->packs_by_name->{$name};
147 push @report, {
148 name => $name,
149 exclusive_group => $pack->exclusive_group,
150 origin => $pack->origin,
151 active => $active,
152 232 ▶ ( $why ? ( source => $why->{source}, reason => $why->{reason} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$why ? : 138 94 232
$detection ? : 120 112 232
153 ( $detection ? ( detection => $detection ) : () ),
154 };
155 }
156 103 for my $name (sort grep { !$self->packs_by_name->{$_} } keys %{$self->detections}) {
157 3 push @report, { name => $name, active => 0, detection => $self->detections->{$name} };
158 }
159 103 return \@report;
160 }
161  
162 1317 ▶sub _is_enabled {
Subroutine _is_enabled: called CC=1 SCAR=0.0
163 1317 my ($self, $name) = @_;
164 1317 my %enabled = map { $_ => 1 } @{$self->enabled_pack_names};
165 1317 return $enabled{$name};
166 }
167  
168  
169 204 ▶sub skill_texts {
Subroutine skill_texts: called CC=4 SCAR=13.9
Pod documented
170 204 my ($self) = @_;
171 204 my @texts;
172 204 for my $name (@{$self->enabled_pack_names}) {
173 208 ▶ my $pack = $self->packs_by_name->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or next 208 0 208
174 208 ▶ if ($pack->has_skill_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack->has_skill_text) 207 1 208
175 207 push @texts, "### Pack: $name\n\n" . $pack->skill_text;
176 }
177 }
178 204 return @texts;
179 }
180  
181  
182 2 ▶sub requested_tools {
Subroutine requested_tools: called CC=4 SCAR=13.9
Pod documented
183 2 my ($self) = @_;
184 2 my %seen;
185 2 for my $name (@{$self->enabled_pack_names}) {
186 2 ▶ my $pack = $self->packs_by_name->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or next 2 0 2
187 2 $seen{$_}++ for @{$pack->tools};
188 }
189 2 return [ sort keys %seen ];
190 }
191  
192  
193 106 ▶sub active_pack_names { $_[0]->enabled_pack_names }
Subroutine active_pack_names: called CC=1 SCAR=0.0
Pod documented
194  
195  
196 421 ▶sub is_active { $_[0]->_is_enabled($_[1]) }
Subroutine is_active: called CC=1 SCAR=0.0
Pod documented
197  
198  
199 28 ▶sub pack_info {
Subroutine pack_info: called CC=4 SCAR=13.9
Pod documented
200 28 my ($self, $name) = @_;
201 28 ▶ my $pack = $self->packs_by_name->{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pack = $self->packs_by_name->{$name} or return 27 1 28
202 return {
203 27 ▶ name => $pack->name,
Branch: 2 branches
BranchTrueFalseTotal
$self->_is_enabled($name) ? : 8 19 27
$pack->has_skill_text ? : 27 0 27
204 exclusive_group => $pack->exclusive_group,
205 is_active => $self->_is_enabled($name) ? JSON::MaybeXS::true : JSON::MaybeXS::false,
206 has_skill_text => $pack->has_skill_text ? JSON::MaybeXS::true : JSON::MaybeXS::false,
207 path => $pack->path,
208 origin => $pack->origin,
209 };
210 }
211  
212 76 __PACKAGE__->meta->make_immutable;
213  
214 76 1;
215  
216 __END__
217  
218 =pod
219  
220 =encoding UTF-8
221  
222 =head1 NAME
223  
224 Langertha::Raider::Packs::Collection - Set of loaded raider packs with per-session enable state
225  
226 =head1 VERSION
227  
228 version 0.503
229  
230 =head1 DESCRIPTION
231  
232 Returned by L<Langertha::Raider::Packs/build_packs>. Tracks which packs are
233 enabled, honouring exclusive groups, and why: every enabled pack has a
234 source (C<default>, C<flag>, C<config>, C<detected>, C<manual>) and a
235 reason, and the outcome of each detection rule (ADR 0012) is kept for
236 L</activation_report>.
237  
238 =head2 sources
239  
240 Why each enabled pack is on: C<< { NAME => { source, reason } } >>.
241  
242 =head2 switched_off
243  
244 Packs switched off explicitly (C<--no-pack>): C<< { NAME => { source, reason } } >>.
245  
246 =head2 detections
247  
248 Outcome of each detection rule: C<< { NAME => { rule_from, result, reason, notes } } >>,
249 C<result> being C<matched>, C<not matched> or C<skipped>.
250  
251 =head2 skipped_packs
252  
253 Pack directories that could not be loaded, in search order:
254 C<< [ { name, origin, path, reason } ] >> (see
255 L<Langertha::Raider::Packs/build_packs>).
256  
257 =head2 enable
258  
259 $collection->enable('polite'); # enable polite (toggles off caveman)
260 $collection->enable('git-guru'); # stack git-guru on top
261 $collection->enable('teacher', config => '.raider.yml packs:');
262  
263 The optional source and reason say why (L</sources>); they default to
264 C<manual> and C</pack>.
265  
266 =head2 disable
267  
268 $collection->disable('caveman');
269 $collection->disable('git-guru', flag => '--no-pack');
270  
271 With a source and reason the pack is recorded in L</switched_off>.
272  
273 =head2 toggle
274  
275 $collection->toggle('polite'); # on if off, off if on
276  
277 =head2 enable_detected
278  
279 my $holder = $collection->enable_detected('perl', $clause);
280  
281 Enables a detected pack with source C<detected>, unless its exclusive group
282 already holds a pack that is not merely a C<default> one: explicit beats
283 detected, and the first detected pack of a group keeps it. Returns the
284 name of that holder when the pack stays off, nothing when it was enabled.
285  
286 =head2 activation_report
287  
288 my @packs = @{ $collection->activation_report };
289  
290 One entry per pack that is enabled, was switched off explicitly or has a
291 detection outcome, sorted by name; rules for packs that are not installed
292 come last. C<origin> is L<Langertha::Raider::Packs::Pack/origin>:
293  
294 { name => 'perl', exclusive_group => 'power', origin => 'shipped',
295 active => 1,
296 source => 'detected', reason => 'must file=cpanfile (cpanfile)',
297 detection => { rule_from => 'pack default', result => 'matched',
298 reason => '...', notes => [] } }
299  
300 =head2 skill_texts
301  
302 my @texts = $collection->skill_texts;
303  
304 The F<SKILL.md> texts of the enabled packs, in the order they were enabled,
305 each headed C<### Pack: NAME>; packs without one are left out.
306  
307 =head2 requested_tools
308  
309 my @servers = @{ $collection->requested_tools }; # ['perl']
310  
311 The built-in tool servers the enabled packs request (C<tools:> in
312 F<pack.yml>), sorted and deduplicated. A request, not a grant.
313  
314 =head2 active_pack_names
315  
316 ArrayRef of the names of the packs currently enabled, in the order they
317 were enabled.
318  
319 =head2 is_active
320  
321 if ($collection->is_active('caveman')) { ... }
322  
323 =head2 pack_info
324  
325 my $info = $collection->pack_info('caveman');
326 # { name, exclusive_group, is_active, has_skill_text, path, origin }
327  
328 =head1 SUPPORT
329  
330 =head2 Issues
331  
332 Please report bugs and feature requests on GitHub at
333 L<https://github.com/Getty/langertha-raider/issues>.
334  
335 =head2 IRC
336  
337 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
338  
339 =head1 CONTRIBUTING
340  
341 Contributions are welcome! Please fork the repository and submit a pull request.
342  
343 =head1 AUTHOR
344  
345 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
346  
347 =head1 COPYRIGHT AND LICENSE
348  
349 This software is copyright (c) 2026 by Torsten Raudssus.
350  
351 This is free software; you can redistribute it and/or modify it under
352 the same terms as the Perl 5 programming language system itself.
353  
354 =cut
« blib/lib/Langertha/Raider/Provider/Fetch.pm ↑ Summary blib/lib/Langertha/Raid/Parallel.pm »