blib/lib/Langertha/Raider/Packs.pm

filter: Statementstmt 100% 114 / 114 Branchbran 79.5% 35 / 44 Conditioncond 69.5% 16 / 23 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 89.0% 187 / 210 CC 39 SCAR 23.6
CC
39
Cov
91%
_pack_search_paths
19.9
_load_pack
10.0
build_packs
8.0
CRAP 10.6
« blib/lib/Langertha/Raider/Application.pm ↑ Summary blib/lib/Langertha/Raider/Hall/Telegram.pm »
1 package Langertha::Raider::Packs;
2 76 our $VERSION = '0.503';
3 # ABSTRACT: Pack discovery, loading, and management for raider personas and power bundles
4  
5 76 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 76 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 76 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 76 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 76 ▶use YAML::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 76 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 76 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 76 ▶use Langertha::Raider::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 76 ▶use Langertha::Raider::Packs::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 76 ▶use Langertha::Raider::Packs::Pack;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 76 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 76 our @EXPORT_OK = qw( build_packs );
19  
20  
21 376 ▶sub _pack_defaults {
Subroutine _pack_defaults: called CC=6 SCAR=18.0
22 376 my ($packs) = @_;
23 376 my @defaults;
24  
25 376 for my $pack (values %$packs) {
26 2718 ▶ push @defaults, $pack->name if $pack->enabled_by_default;
Branch: 1 branch
BranchTrueFalseTotal
if $pack->enabled_by_default 375 2343 2718
27 }
28  
29 376 my %seen_group;
30 my @filtered;
31 376 for my $name (@defaults) {
32 375 my $pack = $packs->{$name};
33 375 my $grp = $pack->exclusive_group;
34 375 ▶ next if $grp eq 'power';
Branch: 1 branch
BranchTrueFalseTotal
if $grp eq "power" 0 375 375
35 375 ▶ unless ($seen_group{$grp}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_group{$grp}++) 375 0 375
36 375 push @filtered, $name;
37 }
38 }
39  
40 376 push @filtered, grep { $packs->{$_}->exclusive_group eq 'power' } @defaults;
41  
42 376 return \@filtered;
43 }
44  
45 # [ origin, directory ] in search order.
46 376 ▶sub _pack_search_paths {
Subroutine _pack_search_paths: called CC=18 SCAR=29.9
47 376 my (%args) = @_;
48 376 ▶ my $root = path($args{root} // '.')->absolute;
Condition50%1/2
$args{"root"} // "."
l!l
3760
MC/DC0%0/1
$args{"root"} // "."
$args{"root"}
Truth table50%1/2
A
A= $args{"root"}
Aresult
11
00
49 376 ▶ my $home_dir = $args{home} // Langertha::Raider::Home->home_dir;
Condition66%2/3
$args{"home"} // "Langertha::Raider::Home"->home_dir
l!l&&r!l&&!r
13750
MC/DC0%0/2
$args{"home"} // "Langertha::Raider::Home"->home_dir
$args{"home"} "Langertha::Raider::Home"->home_dir
Truth table66%2/3
A // B
A= $args{"home"}B= "Langertha::Raider::Home"->home_dir
ABresult
1X1
011
000
50 376 ▶ my $home = defined $home_dir ? path($home_dir)->absolute : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $home_dir ? : 376 0 376
51 376 my @search_paths;
52  
53 376 my $project = Langertha::Raider::Home->project_base($root)->child('packs');
54 376 ▶ if (-d $project) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $project) 7 369 376
55 7 ▶ my $is_home = $home && -d $home && $root->realpath eq $home->realpath;
Condition50%3/6
$home && -d $home && $root->realpath eq $home->realpath
!ll&&!rl&&r
061
$home and -d $home
!ll&&!rl&&r
007
MC/DC33%1/3
$home && -d $home && $root->realpath eq $home->realpath
$home -d $home $root->realpath eq $home->realpath
Truth table50%2/4
A and B && C
A= $homeB= -d $homeC= $root->realpath eq $home->realpath
ABCresult
0XX0
10X0
1100
1111
56 7 ▶ push @search_paths, [ project => $project ] unless $is_home;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_home 6 1 7
57 }
58 376 ▶ if ($home) {
Branch: 1 branch
BranchTrueFalseTotal
if ($home) 376 0 376
59 376 my $p = Langertha::Raider::Home->home_base($home)->child('packs');
60 376 ▶ push @search_paths, [ home => $p ] if -d $p;
Branch: 1 branch
BranchTrueFalseTotal
if -d $p 8 368 376
61 }
62  
63 # Bundled share/packs/ discovery, in order of likelihood:
64 #
65 # 1. File::ShareDir when installed from CPAN (ShareDir plugin
66 # copies share/ into auto/share/dist/Langertha-Raider/).
67 # 2. Source-tree layout: $INC{Langertha/Raider/Application.pm} = lib/Langertha/Raider/Application.pm,
68 # sibling share/packs/ is four parents up after ->absolute.
69 # 3. blib layout used by `dzil test` / `make test`:
70 # blib/lib/Langertha/Raider/Application.pm with share/packs/ one parent less.
71 {
72 376 my $sd = eval {
73 376 File::ShareDir::dist_dir('Langertha-Raider');
74 };
75 376 ▶ if ($sd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sd) 376 0 376
76 376 my $p = path($sd)->child('packs');
77 376 ▶ push @search_paths, [ shipped => $p ] if -d $p;
Branch: 1 branch
BranchTrueFalseTotal
if -d $p 376 0 376
78 }
79 }
80  
81 376 my $mod_path = path($INC{'Langertha/Raider/Application.pm'})->absolute;
82 376 for my $up (qw( parent_x4 parent_x3 )) {
83 752 ▶ my $base = $up eq 'parent_x4'
Branch: 1 branch
BranchTrueFalseTotal
$up eq 'parent_x4' ? : 376 376 752
84 ? $mod_path->parent->parent->parent->parent
85 : $mod_path->parent->parent->parent;
86 752 my $cand = $base->child('share', 'packs');
87 752 ▶ push @search_paths, [ shipped => $cand ] if -d $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -d $cand 0 752 752
88 }
89  
90 # $RAIDER_PACK_DIRS env
91 376 ▶ if (my $env_dirs = $ENV{RAIDER_PACK_DIRS}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env_dirs = $ENV{"RAIDER_PACK_DIRS"}) 24 352 376
92 24 for my $d (split /:/, $env_dirs) {
93 26 my $p = path($d)->absolute;
94 26 ▶ push @search_paths, [ env => $p ] if -d $p;
Branch: 1 branch
BranchTrueFalseTotal
if -d $p 26 0 26
95 }
96 }
97  
98 376 return @search_paths;
99 }
100  
101 # First line of an error, without the " at FILE line N." Perl and Moose append.
102 9 ▶sub _error_line {
Subroutine _error_line: called CC=1 SCAR=0.0
103 9 my ($error) = @_;
104 9 my ($line) = split /\n/, "$error";
105 9 $line =~ s/ at (?:\w+ \S+ \(defined at .+ line \d+\)|(?:(?! at ).)+) line \d+\.?\z//;
106 9 return $line;
107 }
108  
109 # One pack from its directory; croaks with the reason when it cannot be loaded.
110 2725 ▶sub _load_pack {
Subroutine _load_pack: called CC=10 SCAR=23.0
111 2725 my ($pack_dir, $origin) = @_;
112 2725 my $yml_file = $pack_dir->child('pack.yml');
113 2725 my $skill_file = $pack_dir->child('SKILL.md');
114  
115 2725 my $config = {};
116 2725 ▶ if (-f $yml_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $yml_file) 1584 1141 2725
117 1584 $config = eval { YAML::PP->new->load_string($yml_file->slurp_utf8) };
118 1584 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 1582 1584
119 2 croak 'pack.yml is not valid YAML: '._error_line($err);
120 }
121 1582 ▶ $config //= {};
Condition50%1/2
$config //= {}
l!l
15820
MC/DC0%0/1
$config //= {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
122 1582 ▶ croak 'pack.yml is not a mapping' unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 3 1579 1582
123 }
124  
125 return Langertha::Raider::Packs::Pack->new({
126 name => $pack_dir->basename,
127 path => $pack_dir->stringify,
128 origin => $origin,
129 ( -f $skill_file ? ( skill_text => $skill_file->slurp_utf8 ) : () ),
130 exclusive_group => $config->{exclusive_group} // 'power',
131 enabled_by_default => $config->{enabled_by_default} // 0,
132 tools => $config->{tools} // [],
133 2720 ▶ ( exists $config->{detect} ? ( detect => $config->{detect} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
-f $skill_file ? : 2715 5 2720
exists $config->{'detect'} ? : 423 2297 2720
Condition100%6/6
$config->{"exclusive_group"} // "power"
l!l
15531167
$config->{"enabled_by_default"} // 0
l!l
3752345
$config->{"tools"} // []
l!l
3762344
MC/DC100%1/1
$config->{"exclusive_group"} // "power"
$config->{"exclusive_group"}
MC/DC100%1/1
$config->{"enabled_by_default"} // 0
$config->{"enabled_by_default"}
MC/DC100%1/1
$config->{"tools"} // []
$config->{"tools"}
Truth table100%2/2
A
A= $config->{"exclusive_group"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"enabled_by_default"}
Aresult
11
00
Truth table100%2/2
A
A= $config->{"tools"}
Aresult
11
00
134 });
135 }
136  
137 376 ▶sub build_packs {
Subroutine build_packs: called CC=8 SCAR=20.8
Pod documented
138 376 my %args = @_;
139  
140 376 my %packs_by_name;
141 my %exclusive_groups;
142 376 my @skipped;
143  
144 376 for my $search (_pack_search_paths(%args)) {
145 416 my ( $origin, $sp ) = @$search;
146 416 for my $pack_dir (sort { $a->basename cmp $b->basename } $sp->children) {
147 2729 ▶ next unless -d $pack_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $pack_dir 0 2729 2729
148 2729 my $name = $pack_dir->basename;
149  
150 # Skip if already loaded (first-wins from search order)
151 2729 ▶ next if $packs_by_name{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $packs_by_name{$name} 4 2725 2729
152  
153 2725 my $pack = eval { _load_pack($pack_dir, $origin) };
154 2725 ▶ unless ($pack) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pack) 7 2718 2725
155 7 ▶ my $reason = _error_line($@ || 'unknown error');
Condition50%1/2
$@ || 'unknown error'
l!l
70
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
156 7 push @skipped, { name => $name, origin => $origin, path => $pack_dir->stringify, reason => $reason };
157 7 next;
158 }
159  
160 2718 $packs_by_name{$name} = $pack;
161 2718 ▶ push @{$exclusive_groups{$pack->exclusive_group}//=[]}, $name;
Condition100%2/2
$exclusive_groups{$pack->exclusive_group} //= []
l!l
1966752
MC/DC100%1/1
$exclusive_groups{$pack->exclusive_group} //= []
$exclusive_groups{$pack->exclusive_group}
Truth table100%2/2
A
A= $exclusive_groups{$pack->exclusive_group}
Aresult
11
00
162 }
163 }
164  
165 # Build the collection with initial defaults
166 376 return Langertha::Raider::Packs::Collection->new({
167 packs_by_name => \%packs_by_name,
168 exclusive_groups => \%exclusive_groups,
169 skipped_packs => \@skipped,
170 _init_defaults => _pack_defaults(\%packs_by_name),
171 });
172 }
173  
174 76 1;
175  
176 __END__
177  
178 =pod
179  
180 =encoding UTF-8
181  
182 =head1 NAME
183  
184 Langertha::Raider::Packs - Pack discovery, loading, and management for raider personas and power bundles
185  
186 =head1 VERSION
187  
188 version 0.503
189  
190 =head1 DESCRIPTION
191  
192 Finds and loads the packs of a workspace (ADR 0012): named bundles of skill
193 text, persona defaults and requested tools that L<raider> stacks onto the
194 mission.
195  
196 =head2 build_packs
197  
198 my $packs = Langertha::Raider::Packs::build_packs(
199 root => '/path/to/project', # workspace root
200 home => '/home/me', # optional
201 );
202  
203 Returns a L<Langertha::Raider::Packs::Collection> of every pack found.
204 A pack is a directory with an optional F<pack.yml> and an optional
205 F<SKILL.md> (without one it contributes no skill text). They are searched in these places; a pack name found in an earlier place hides
206 the same name in a later one, so a bundled pack can be replaced by a
207 project or home pack of its name, not by one from C<$RAIDER_PACK_DIRS>:
208  
209 =over
210  
211 =item 1. C<project>: F<< <root>/.raider/packs/<name>/ >>
212  
213 =item 2. C<home>: F<< ~/.raider/packs/<name>/ >>, F<~> being C<home>, else
214 C<$ENV{HOME}>, else the user's home from the password database
215  
216 =item 3. C<shipped>: the bundled F<share/packs/>
217  
218 =item 4. C<env>: the directories in C<$RAIDER_PACK_DIRS> (colon separated)
219  
220 =back
221  
222 Each pack keeps the kind of place it came from as
223 L<Langertha::Raider::Packs::Pack/origin>. When C<root> is the home
224 directory, its F<.raider/packs/> counts as C<home>.
225  
226 A pack directory that cannot be loaded (a F<pack.yml> that is not valid
227 YAML or not a mapping, a value of the wrong type) is skipped
228 and listed in L<Langertha::Raider::Packs::Collection/skipped_packs>; a pack
229 of the same name further down the search order takes its place. It never
230 stops the start.
231  
232 Project packs are loaded right away, like the C<detect:> rules of the
233 project's own config: the workspace trust decision of ADR 0004, which is
234 meant to gate both, does not exist yet.
235  
236 F<pack.yml> knows these keys; others are ignored:
237  
238 =over
239  
240 =item C<exclusive_group> -- the group the pack belongs to, default
241 C<power>. Packs of C<power> stack; of any other group (the bundled personas
242 use C<persona>) only one is active at a time.
243  
244 =item C<enabled_by_default> -- true: the pack is on unless something names
245 the packs explicitly (the bundled C<caveman>).
246  
247 =item C<tools> -- built-in tool servers the pack requests, today only
248 C<perl> (L<Langertha::Raider::Packs::Collection/requested_tools>). A request, not
249 a grant.
250  
251 =item C<detect> -- the pack's default detection rule
252 (L<Langertha::Raider::Detect>); C<detect:> in F<.raider.yml> replaces it
253 per pack.
254  
255 =back
256  
257 =head1 ENVIRONMENT
258  
259 =head2 RAIDER_PACK_DIRS
260  
261 Colon-separated directories searched for packs after the bundled ones.
262  
263 =head2 HOME
264  
265 The home whose F<.raider/packs/> is searched, when no C<home> is passed.
266  
267 =head1 SUPPORT
268  
269 =head2 Issues
270  
271 Please report bugs and feature requests on GitHub at
272 L<https://github.com/Getty/langertha-raider/issues>.
273  
274 =head2 IRC
275  
276 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
277  
278 =head1 CONTRIBUTING
279  
280 Contributions are welcome! Please fork the repository and submit a pull request.
281  
282 =head1 AUTHOR
283  
284 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
285  
286 =head1 COPYRIGHT AND LICENSE
287  
288 This software is copyright (c) 2026 by Torsten Raudssus.
289  
290 This is free software; you can redistribute it and/or modify it under
291 the same terms as the Perl 5 programming language system itself.
292  
293 =cut
« blib/lib/Langertha/Raider/Application.pm ↑ Summary blib/lib/Langertha/Raider/Hall/Telegram.pm »