Skilletor/Catalog.pm

filter: Statementstmt 100% 367 / 367 Branchbran 87.2% 164 / 188 Conditioncond 80.5% 62 / 77 MC/DCmcdc 67.3% 31 / 46 Subroutinesub 100% 47 / 47 Podpod 100% 1 / 1 totaltotal 92.5% 672 / 726 CC 142 SCAR 20.6
CC
142
Cov
94%
_plugin_skills
16.0
scan
16.0
_scan_bundles
11.1
CRAP 7.8
« Skilletor/Config.pm ↑ Summary Skilletor/Gitignore.pm »
1 package Skilletor::Catalog;
2 # ABSTRACT: Scan a source tree into its installable items, bundles and source metadata
3  
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Skilletor::Bundles qw( parse_bundle );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Skilletor::Entry qw( name_key );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Skilletor::Error::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text inner_segments node_fs_error path_join );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Skilletor::JSON qw( js_parse js_sort_keys js_stringify js_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use Skilletor::JSString qw( js_trim js_ws_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 34 our $VERSION = '0.001';
17  
18 # The `.gitignore` skilletor writes into every skill it installs (spec §6.4): a skill directory
19 # holding exactly this is one of its installed copies.
20 34 ▶use constant SKILL_GITIGNORE => "# installed by skilletor, not committed (\"gitignore\": false in skilletor.json to commit)\n*\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # The name prefix of installed agents and rules (spec §6.4).
23 34 ▶use constant LOCAL_PREFIX => '.local.';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 34 my @TYPE_DIRS = ( [ skills => 'skill' ], [ agents => 'agent' ], [ rules => 'rule' ] );
26  
27 34 my $DOT = qr/[^\n\r\x{2028}\x{2029}]/; # JavaScript's `.`
28 34 my $WS = js_ws_re();
29  
30 34 ▶sub _fail { Skilletor::Error::Catalog->throw( $_[0] ) }
Subroutine _fail: called CC=1 SCAR=0.0
31  
32 2 ▶sub _message { my ( $err ) = @_; return blessed $err && $err->can('message') ? $err->message : "$err" =~ s/ at \S+ line \d+\.?\n?\z//r }
Subroutine _message: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->can('message') ? : 2 0 2
Condition33%1/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($err) and $err->can("message")
builtin::blessed($err) $err->can("message")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->can("message")
ABresult
0X0
100
111
33  
34 #### File system, as node:fs sees it ###################################
35  
36 # existsSync: follows symlinks, false on any error.
37 2290 ▶sub _exists { -e encode_text( $_[0] ) ? 1 : 0 }
Subroutine _exists: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
-e encode_text($_[0]) ? : 1218 1072 2290
38  
39 # lstatSync: { dir, file, link }; throws Node's message.
40 2800 ▶sub _lstat {
Subroutine _lstat: called CC=5 SCAR=16.1
41 2800 my ( $path ) = @_;
42 2800 ▶ lstat( encode_text($path) ) or _fail( node_fs_error( 0 + $!, 'lstat', $path ) );
Branch: 1 branch
BranchTrueFalseTotal
lstat encode_text($path) or _fail(node_fs_error(0 + $!, "lstat", $path)) 2800 0 2800
43 2800 ▶ return { dir => ( -d _ ? 1 : 0 ), file => ( -f _ ? 1 : 0 ), link => ( -l _ ? 1 : 0 ) };
Branch: 3 branches
BranchTrueFalseTotal
-d _ ? : 1132 1668 2800
-f _ ? : 1661 1139 2800
-l _ ? : 7 2793 2800
44 }
45  
46 # readdirSync: the names, in libuv's order (bytes, strcmp); throws Node's message.
47 1107 ▶sub _readdir {
Subroutine _readdir: called CC=3 SCAR=11.0
48 1107 my ( $path ) = @_;
49 1107 ▶ opendir( my $dh, encode_text($path) ) or _fail( node_fs_error( 0 + $!, 'scandir', $path ) );
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, encode_text($path) or _fail(node_fs_error(0 + $!, "scandir", $path)) 1106 1 1107
50 1106 ▶ my @names = sort grep { $_ ne '.' && $_ ne '..' } readdir $dh;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
110611061572
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
51 1106 closedir $dh;
52 1106 return map { decode_utf8_lossy($_) } @names;
53 }
54  
55 # readFileSync(path, "utf8").
56 1034 ▶sub _read_text {
Subroutine _read_text: called CC=4 SCAR=14.2
57 1034 my ( $path ) = @_;
58 1034 my $bytes = encode_text($path);
59 1034 ▶ _fail( node_fs_error( Errno::EISDIR(), 'read' ) ) if -d $bytes;
Branch: 1 branch
BranchTrueFalseTotal
if -d $bytes 0 1034 1034
60 1034 ▶ open( my $fh, '<:raw', $bytes ) or _fail( node_fs_error( 0 + $!, 'open', $path ) );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $bytes or _fail(node_fs_error(0 + $!, "open", $path)) 1034 0 1034
61 1034 local $/;
62 1034 my $data = <$fh>;
63 1034 close $fh;
64 1034 ▶ return decode_utf8_lossy( $data // '' );
Condition50%1/2
$data // ""
l!l
10340
MC/DC0%0/1
$data // ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
65 }
66  
67 # Is `path` a directory, not a symlink? False when it cannot be read.
68 303 ▶sub _is_plain_dir {
Subroutine _is_plain_dir: called CC=3 SCAR=11.0
69 303 my ( $path ) = @_;
70 303 ▶ return ( lstat( encode_text($path) ) && -d _ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lstat encode_text($path) && -d _ ? : 18 285 303
Condition100%3/3
lstat encode_text($path) and -d _
!ll&&!rl&&r
284118
MC/DC100%2/2
lstat encode_text($path) and -d _
lstat encode_text($path) -d _
Truth table100%3/3
A and B
A= lstat encode_text($path)B= -d _
ABresult
0X0
100
111
71 }
72  
73 3 ▶sub _is_dangling_link {
Subroutine _is_dangling_link: called CC=3 SCAR=13.1
74 3 my ( $path ) = @_;
75 3 ▶ return ( lstat( encode_text($path) ) && -l _ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
lstat encode_text($path) && -l _ ? : 0 3 3
Condition33%1/3
lstat encode_text($path) and -l _
!ll&&!rl&&r
300
MC/DC0%0/2
lstat encode_text($path) and -l _
lstat encode_text($path) -l _
Truth table33%1/3
A and B
A= lstat encode_text($path)B= -l _
ABresult
0X0
100
111
76 }
77  
78 # Throw if `path` is a symlink; its lstat otherwise.
79 2785 ▶sub _no_symlink {
Subroutine _no_symlink: called CC=2 SCAR=6.9
80 2785 my ( $path ) = @_;
81 2785 my $st = _lstat($path);
82 2785 ▶ _fail( 'symlink not allowed in source: '.$path ) if $st->{link};
Branch: 1 branch
BranchTrueFalseTotal
if $st->{"link"} 7 2778 2785
83 2778 return $st;
84 }
85  
86 916 ▶sub _basename { my ( $rel ) = @_; return ( split m{/}, $rel )[-1] }
Subroutine _basename: called CC=1 SCAR=0.0
87  
88 # String comparison as JavaScript's `<` sees it (UTF-16 code units).
89 481 ▶sub _js_sorted { js_sort_keys(@_) }
Subroutine _js_sorted: called CC=1 SCAR=0.0
90  
91 5 ▶sub _js_cmp {
Subroutine _js_cmp: called CC=3 SCAR=11.0
92 5 my ( $a, $b ) = @_;
93 5 ▶ return 0 if $a eq $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a eq $b 0 5 5
94 5 my ($first) = js_sort_keys( $a, $b );
95 5 ▶ return $first eq $a ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$first eq $a ? : 4 1 5
96 }
97  
98 #### The scan ##########################################################
99  
100 313 ▶sub scan {
Subroutine scan: called CC=16 SCAR=27.7
Pod documented
101 313 my ( $class, $dir, $paths ) = @_;
102 313 ▶ if ( defined $paths ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $paths) 12 301 313
103 12 ▶ my $items = _path_skills( $dir, ref $paths ? $paths : [$paths] );
Branch: 1 branch
BranchTrueFalseTotal
ref $paths ? : 12 0 12
104 4 return { items => $items, bundles => [], meta => _read_source_meta($dir) };
105 }
106 301 my @items;
107 301 for my $td (@TYPE_DIRS) {
108 894 my ( $sub, $type ) = @$td;
109 894 my $type_dir = path_join( $dir, $sub );
110 894 ▶ next unless _exists($type_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($type_dir) 260 634 894
111 634 _no_symlink($type_dir);
112 633 my @found; # a skill per directory, an agent or rule per file
113 633 for my $entry ( _readdir($type_dir) ) {
114 905 my $p = path_join( $type_dir, $entry );
115 905 my $st = _no_symlink($p);
116 905 ▶ if ( $type eq 'skill' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'skill') { } 440 465 905
117 440 ▶ next unless $st->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"dir"} 1 439 440
118 439 ▶ my $file = _skill_file($p) // next;
Condition100%2/2
_skill_file($p) // (next)
l!l
4371
MC/DC100%1/1
_skill_file($p) // (next)
_skill_file($p)
Truth table100%2/2
A
A= _skill_file($p)
Aresult
11
00
119 437 push @found, _skill_item( $dir, $p, $sub.'/'.$entry, $file );
120 }
121 else {
122 465 ▶ next unless $st->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"file"} 1 464 465
123 464 ▶ my $name = _item_name($entry) // next;
Condition100%2/2
_item_name($entry) // (next)
l!l
4631
MC/DC100%1/1
_item_name($entry) // (next)
_item_name($entry)
Truth table100%2/2
A
A= _item_name($entry)
Aresult
11
00
124 463 push @found, { type => $type, name => $name, files => [ $sub.'/'.$entry ] };
125 }
126 }
127 # Both `<name>.md` and `<name>.md.njk` is an error of that item, like a bundle's `.yaml`
128 # beside its `.yml` (spec §4.1, §15.4); so are two spellings of one name (k135).
129 629 ▶ for my $i ( _one_name( \@found, $type eq 'skill' ? \&_in_dirs : \&_both_exist ) ) {
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'skill' ? : 275 354 629
130 897 ▶ if ( !defined $i->{error} && $type ne 'skill' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i->{"error"} and $type ne "skill") 459 438 897
Condition100%3/3
not defined $i->{"error"} and $type ne "skill"
!ll&&!rl&&r
7431459
MC/DC100%2/2
not defined $i->{"error"} and $type ne "skill"
not defined $i->{"error"} $type ne "skill"
Truth table100%3/3
not(A) and B
A= not defined $i->{"error"}B= $type ne "skill"
ABresult
0X0
100
111
131 459 my $d = _description_of( path_join( $dir, $i->{files}[0] ) );
132 459 ▶ $i = { %$i, ( defined $d ? ( description => $d ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 454 5 459
133 }
134 897 push @items, $i;
135 }
136 }
137 296 push @items, _plugin_skills( $dir, [@items] );
138 286 push @items, _claude_items( $dir, [@items] );
139 286 my $bundles = _scan_bundles($dir);
140 286 return { items => \@items, bundles => $bundles, meta => _read_source_meta($dir) };
141 }
142  
143 # A top-level frontmatter scalar per key (name, description): the catalog's own reader, read raw.
144 914 ▶sub _frontmatter {
Subroutine _frontmatter: called CC=6 SCAR=18.0
145 914 my ( $text ) = @_;
146 # A BOM and CRLF line ends are no part of a value.
147 914 $text =~ s/\A\x{FEFF}//;
148 914 $text =~ s/\r\n/\n/g;
149 914 ▶ return {} unless substr( $text, 0, 3 ) eq '---';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($text, 0, 3) eq "---" 10 904 914
150 904 my $first_nl = index $text, "\n";
151 904 ▶ return {} if $first_nl == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $first_nl == -1 0 904 904
152 904 my $close = index $text, "\n---", $first_nl;
153 904 ▶ return {} if $close == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $close == -1 1 903 904
154 903 my $block = substr $text, $first_nl + 1, $close - $first_nl - 1;
155 903 my %out;
156 903 for my $line ( split /\n/, $block, -1 ) {
157 912 ▶ $out{$1} = _unquote( js_trim($2) ) if $line =~ /\A([A-Za-z0-9_-]+):$WS*($DOT*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A([A-Za-z0-9_-]+):$WS*($DOT*)\z/ 912 0 912
158 }
159 903 return \%out;
160 }
161  
162 912 ▶sub _unquote {
Subroutine _unquote: called CC=6 SCAR=17.9
163 912 my ( $v ) = @_;
164 912 ▶ if ( length($v) >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v >= 2) 855 57 912
165 855 my ( $f, $l ) = ( substr( $v, 0, 1 ), substr( $v, -1 ) );
166 855 ▶ return substr( $v, 1, -1 ) if ( $f eq '"' && $l eq '"' ) || ( $f eq "'" && $l eq "'" );
Branch: 1 branch
BranchTrueFalseTotal
if $f eq "\"" and $l eq "\"" or $f eq "'" and $l eq "'" 3 852 855
Condition88%8/9
$f eq "\"" and $l eq "\"" or $f eq "'" and $l eq "'"
l!l&&r!l&&!r
12852
$f eq "\"" and $l eq "\""
!ll&&!rl&&r
85311
$f eq "'" and $l eq "'"
!ll&&!rl&&r
85202
MC/DC75%3/4
$f eq "\"" and $l eq "\"" or $f eq "'" and $l eq "'"
$f eq "\"" $l eq "\"" $f eq "'" $l eq "'"
Truth table57%4/7
A and B or C and D
A= $f eq "\""B= $l eq "\""C= $f eq "'"D= $l eq "'"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
167 }
168 909 return $v;
169 }
170  
171 914 ▶sub _description_of { _frontmatter( _read_text( $_[0] ) )->{description} }
Subroutine _description_of: called CC=1 SCAR=0.0
172  
173 # SKILL.md or SKILL.md.njk in a skill directory.
174 478 ▶sub _skill_file {
Subroutine _skill_file: called CC=3 SCAR=11.0
175 478 my ( $dir ) = @_;
176 478 for my $candidate ( 'SKILL.md', 'SKILL.md.njk' ) {
177 501 my $p = path_join( $dir, $candidate );
178 501 ▶ next unless _exists($p);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($p) 41 460 501
179 460 _no_symlink($p);
180 459 return $p;
181 }
182 18 return undef;
183 }
184  
185 # All files under $abs (whose path relative to the source is $rel), relative, sorted; no symlinks.
186 457 ▶sub _walk_files {
Subroutine _walk_files: called CC=4 SCAR=13.9
187 457 my ( $abs, $rel ) = @_;
188 457 my @out;
189 457 for my $entry ( _readdir($abs) ) {
190 613 my $p = path_join( $abs, $entry );
191 613 my $st = _no_symlink($p);
192 610 ▶ if ( $st->{dir} ) { push @out, _walk_files( $p, $rel.'/'.$entry ) }
Branch: 2 branches
BranchTrueFalseTotal
if ($st->{'dir'}) { } 3 607 610
elsif ($st->{'file'}) { } 607 0 607
193 607 elsif ( $st->{file} ) { push @out, $rel.'/'.$entry }
194 }
195 454 return _js_sorted(@out);
196 }
197  
198 # The skill in $skill_dir (relative: $rel), whose main file is $file. `f` beside `f.njk`, which
199 # both install as `f`, is an error of the skill naming every such pair (k113).
200 454 ▶sub _skill_item {
Subroutine _skill_item: called CC=5 SCAR=16.1
201 454 my ( $dir, $skill_dir, $rel, $file ) = @_;
202 454 my @files = _walk_files( $skill_dir, $rel );
203 451 my %have = map { $_ => 1 } @files;
204 451 ▶ my @twice = grep { !/\.njk\z/ && $have{ $_.'.njk' } } @files;
Condition100%3/3
!/\.njk\z/ && $have{$_ . '.njk'}
!ll&&!rl&&r
795233
MC/DC100%2/2
!/\.njk\z/ && $have{$_ . '.njk'}
!/\.njk\z/ $have{$_ . '.njk'}
Truth table100%3/3
not(A) && B
A= !/\.njk\z/B= $have{$_ . '.njk'}
ABresult
0X0
100
111
205 451 my $item = { type => 'skill', name => _basename($rel), files => \@files, dir => $rel };
206 451 my $main = $rel.'/'._basename($file);
207 451 ▶ unless ( grep { $_ eq $main } @twice ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $main;} @twice) 450 1 451
208 450 my $d = _description_of($file);
209 450 ▶ $item->{description} = $d if defined $d;
Branch: 1 branch
BranchTrueFalseTotal
if defined $d 446 4 450
210 }
211 451 ▶ $item->{error} = join '; ', map { 'both '.$_.' and '.$_.'.njk exist' } @twice if @twice;
Branch: 1 branch
BranchTrueFalseTotal
if @twice 2 449 451
212 451 return $item;
213 }
214  
215 # `<name>.md` or `<name>.md.njk`: the name.
216 470 ▶sub _item_name {
Subroutine _item_name: called CC=2 SCAR=6.9
217 470 my ( $file_name ) = @_;
218 470 ▶ return $file_name =~ /\A($DOT+?)\.md(?:\.njk)?\z/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$file_name =~ /\A($DOT+?)\.md(?:\.njk)?\z/ ? : 469 1 470
219 }
220  
221 # `a`, `a and b`, `a, b and c`.
222 8 ▶sub _listed {
Subroutine _listed: called CC=2 SCAR=7.1
223 8 my ( @xs ) = @_;
224 8 ▶ return join '', @xs if @xs < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @xs < 2 0 8 8
225 8 return join( ', ', @xs[ 0 .. $#xs - 1 ] ).' and '.$xs[-1];
226 }
227  
228 3 ▶sub _in_dirs {
Subroutine _in_dirs: called CC=1 SCAR=0.0
229 3 my ( $where, $why ) = @_;
230 3 return 'found in '.scalar(@$where).' directories'.$why.': '._listed(@$where);
231 }
232  
233 5 ▶sub _both_exist {
Subroutine _both_exist: called CC=2 SCAR=7.2
234 5 my ( $where, $why ) = @_;
235 5 ▶ return ( @$where == 2 ? 'both ' : '' )._listed(@$where).' exist'.$why;
Branch: 1 branch
BranchTrueFalseTotal
@$where == 2 ? : 5 0 5
236 }
237  
238 # One item per name ignoring case (k127, k135), in first-seen order; a name found more than once
239 # is an error of each of its spellings.
240 636 ▶sub _one_name {
Subroutine _one_name: called CC=9 SCAR=22.0
241 636 my ( $items, $error ) = @_;
242 636 my ( %groups, @order );
243 636 for my $item (@$items) {
244 912 my $key = name_key( $item->{name} );
245 912 ▶ push @order, $key unless $groups{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $groups{$key} 907 5 912
246 912 push @{ $groups{$key} }, $item;
247 }
248 636 my @out;
249 636 for my $key (@order) {
250 907 my $group = $groups{$key};
251 907 ▶ if ( @$group == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$group == 1) 902 5 907
252 902 push @out, $group->[0];
253 902 next;
254 }
255 5 my ( %seen, @names );
256 5 ▶ for my $i (@$group) { push @names, $i->{name} unless $seen{ $i->{name} }++ }
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$i->{"name"}}++ 8 2 10
257 5 ▶ my @where = _js_sorted( map { $_->{type} eq 'skill' ? $_->{dir} : $_->{files}[0] } @$group );
Branch: 1 branch
BranchTrueFalseTotal
$_->{'type'} eq 'skill' ? : 4 6 10
258 5 ▶ my $why = @names > 1 ? ', names equal ignoring case' : '';
Branch: 1 branch
BranchTrueFalseTotal
@names > 1 ? : 3 2 5
259 5 my @files = _js_sorted( map { @{ $_->{files} } } @$group );
260 5 push @out, map { +{ type => $group->[0]{type}, name => $_, files => [@files], error => $error->( \@where, $why ) } } @names;
261 }
262 636 return @out;
263 }
264  
265 # Items of the Claude project layout, `.claude/{skills,agents,rules}` (k96), best effort.
266 286 ▶sub _claude_items {
Subroutine _claude_items: called CC=11 SCAR=24.0
267 286 my ( $dir, $found ) = @_;
268 286 my @out;
269 286 my $base = path_join( $dir, '.claude' );
270 286 ▶ return () unless _is_plain_dir($base);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_dir($base) 285 1 286
271 1 my %known = map { ( $_->{type}.':'.name_key( $_->{name} ) => 1 ) } @$found;
272 1 for my $td (@TYPE_DIRS) {
273 3 my ( $sub, $type ) = @$td;
274 3 my $type_dir = path_join( $base, $sub );
275 3 ▶ next unless _is_plain_dir($type_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_plain_dir($type_dir) 0 3 3
276 3 ▶ my @entries = eval { _js_sorted( _readdir($type_dir) ) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@entries) = eval { do { _js_sorted(_readdir($type_dir)) } } or next 3 0 3
277 3 my @here;
278 3 for my $entry (@entries) {
279 12 my $item = eval { _claude_item( $dir, $type_dir, '.claude/'.$sub, $entry, $type ) };
280 12 ▶ next if $@; # a symlink or an unreadable file: this item only
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 10 12
281 10 ▶ next if !$item || $known{ $type.':'.name_key( $item->{name} ) } || grep { $_->{name} eq $item->{name} } @here;
Branch: 1 branch
BranchTrueFalseTotal
if not $item or $known{$type . ':' . name_key($item->{"name"})} or grep {$_->{"name"} eq $item->{"name"};} @here 4 6 10
Condition100%6/6
not $item or $known{$type . ':' . name_key($item->{"name"})} or grep {$_->{"name"} eq $item->{"name"};} @here
l!l&&r!l&&!r
316
not $item or $known{$type . ':' . name_key($item->{"name"})}
l!l&&r!l&&!r
217
MC/DC100%3/3
not $item or $known{$type . ':' . name_key($item->{"name"})} or grep {$_->{"name"} eq $item->{"name"};} @here
not $item $known{$type . ':' . name_key($item->{"name"})} grep {$_->{"name"} eq $item->{"name"};} @here
Truth table100%4/4
not(A) or B or C
A= not $itemB= $known{$type . ':' . name_key($item->{"name"})}C= grep {$_->{"name"} eq $item->{"name"};} @here
ABCresult
1XX1
01X1
0011
0000
282 6 push @here, $item;
283 }
284 3 ▶ push @out, _one_name( \@here, $type eq 'skill' ? \&_in_dirs : \&_both_exist );
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'skill' ? : 1 2 3
285 }
286 1 return @out;
287 }
288  
289 12 ▶sub _claude_item {
Subroutine _claude_item: called CC=9 SCAR=22.2
290 12 my ( $dir, $type_dir, $rel_dir, $entry, $type ) = @_;
291 12 my $p = path_join( $type_dir, $entry );
292 12 my $st = _no_symlink($p);
293 11 ▶ if ( $type eq 'skill' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "skill") 5 6 11
294 5 ▶ return undef unless $st->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"dir"} 0 5 5
295 5 ▶ my $file = _skill_file($p) // return undef;
Condition50%1/2
_skill_file($p) // (return undef)
l!l
50
MC/DC0%0/1
_skill_file($p) // (return undef)
_skill_file($p)
Truth table50%1/2
A
A= _skill_file($p)
Aresult
11
00
296 5 ▶ return undef if _is_installed_copy($p);
Branch: 1 branch
BranchTrueFalseTotal
if _is_installed_copy($p) 1 4 5
297 4 return _skill_item( $dir, $p, $rel_dir.'/'.$entry, $file );
298 }
299 6 ▶ return undef unless $st->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless $st->{"file"} 0 6 6
300 6 my $name = _item_name($entry);
301 6 ▶ return undef if !defined $name || index( $name, LOCAL_PREFIX ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or index($name, ".local.") == 0 1 5 6
Condition66%2/3
not defined $name or index($name, ".local.") == 0
l!l&&r!l&&!r
015
MC/DC50%1/2
not defined $name or index($name, ".local.") == 0
not defined $name index($name, ".local.") == 0
Truth table66%2/3
not(A) or B
A= not defined $nameB= index($name, ".local.") == 0
ABresult
1X1
011
000
302 5 my $d = _description_of($p);
303 5 ▶ return { type => $type, name => $name, ( defined $d ? ( description => $d ) : () ), files => [ $rel_dir.'/'.$entry ], dir => $rel_dir };
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 3 2 5
304 }
305  
306 # Is the skill directory one of skilletor's own installed copies (spec §6.4)?
307 15 ▶sub _is_installed_copy {
Subroutine _is_installed_copy: called CC=5 SCAR=16.2
308 15 my ( $skill_dir ) = @_;
309 15 my $gitignore = path_join( $skill_dir, '.gitignore' );
310 15 ▶ return 0 unless lstat( encode_text($gitignore) ) && -f _;
Branch: 1 branch
BranchTrueFalseTotal
unless lstat encode_text($gitignore) and -f _ 12 3 15
Condition66%2/3
lstat encode_text($gitignore) and -f _
!ll&&!rl&&r
1203
MC/DC50%1/2
lstat encode_text($gitignore) and -f _
lstat encode_text($gitignore) -f _
Truth table66%2/3
A and B
A= lstat encode_text($gitignore)B= -f _
ABresult
0X0
100
111
311 3 my $text = eval { _read_text($gitignore) };
312 3 ▶ return ( defined $text && $text eq SKILL_GITIGNORE ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $text && $text eq qq[# installed by skilletor, not committed ("gitignore": false in skilletor.json to commit)\n*\n] ? : 2 1 3
Condition66%2/3
defined $text and $text eq "# installed by skilletor, not committed (\"gitignore\": false in skilletor.json to commit)\n*\n"
!ll&&!rl&&r
012
MC/DC50%1/2
defined $text and $text eq "# installed by skilletor, not committed (\"gitignore\": false in skilletor.json to commit)\n*\n"
defined $text $text eq "# installed by skilletor, not committed (\"gitignore\": false in skilletor.json to commit)\n*\n"
Truth table66%2/3
A and B
A= defined $textB= $text eq "# installed by skilletor, not committed (\"gitignore\": false in skilletor.json to commit)\n*\n"
ABresult
0X0
100
111
313 }
314  
315 # Skills `.claude-plugin/plugin.json` lists that $found does not hold (by directory).
316 296 ▶sub _plugin_skills {
Subroutine _plugin_skills: called CC=16 SCAR=27.7
317 296 my ( $dir, $found ) = @_;
318 296 my $pdir = path_join( $dir, '.claude-plugin' );
319 296 my $p = path_join( $pdir, 'plugin.json' );
320 296 ▶ return () unless _exists($p);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($p) 283 13 296
321 13 _no_symlink($pdir);
322 13 _no_symlink($p);
323 13 my $parsed;
324 13 ▶ unless ( eval { $parsed = js_parse( _read_text($p) ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $parsed = js_parse(_read_text($p)); 1 } }) 1 12 13
325 1 _fail( $p.': invalid JSON ('._message($@).')' );
326 }
327 12 ▶ return () unless js_type($parsed) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($parsed) eq "object" 1 11 12
328 11 ▶ return () unless exists $parsed->{skills};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $parsed->{"skills"} 0 11 11
329 11 my $raw = $parsed->{skills};
330 11 ▶ my $paths = js_type($raw) eq 'string' ? [$raw] : $raw;
Branch: 1 branch
BranchTrueFalseTotal
js_type($raw) eq 'string' ? : 1 10 11
331 11 ▶ unless ( js_type($paths) eq 'array' && !grep { js_type($_) ne 'string' } @$paths ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (js_type($paths) eq "array" and not grep({js_type($_) ne "string";} @$paths)) 2 9 11
Condition100%3/3
js_type($paths) eq "array" and not grep({js_type($_) ne "string";} @$paths)
!ll&&!rl&&r
119
MC/DC100%2/2
js_type($paths) eq "array" and not grep({js_type($_) ne "string";} @$paths)
js_type($paths) eq "array" not grep({js_type($_) ne "string";} @$paths)
Truth table100%3/3
A and not(B)
A= js_type($paths) eq "array"B= not grep({js_type($_) ne "string";} @$paths)
ABresult
0X0
100
111
332 2 _fail( $p.': "skills" must be a string or an array of strings' );
333 }
334  
335 9 my ( %dirs_of, @out );
336 9 ▶ my $known = sub { my ( $name, $rel ) = @_; push @{ $dirs_of{ name_key($name) } }, $rel };
Subroutine __ANON__: called CC=1 SCAR=0.0
337 9 for my $it (@$found) {
338 3 ▶ $known->( $it->{name}, $it->{dir} // 'skills/'.$it->{name} ) if $it->{type} eq 'skill';
Branch: 1 branch
BranchTrueFalseTotal
if $it->{"type"} eq "skill" 3 0 3
Condition50%1/2
$it->{"dir"} // 'skills/' . $it->{"name"}
l!l
30
MC/DC0%0/1
$it->{"dir"} // 'skills/' . $it->{"name"}
$it->{"dir"}
Truth table50%1/2
A
A= $it->{"dir"}
Aresult
11
00
339 }
340 7 ▶ my $add = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
341 7 my ( $skill_dir, $rel, $file ) = @_;
342 7 my $name = _basename($rel);
343 7 ▶ my @dirs = @{ $dirs_of{ name_key($name) } // [] };
Condition100%2/2
$dirs_of{name_key($name)} // []
l!l
34
MC/DC100%1/1
$dirs_of{name_key($name)} // []
$dirs_of{name_key($name)}
Truth table100%2/2
A
A= $dirs_of{name_key($name)}
Aresult
11
00
344 7 ▶ return if grep { $_ eq $rel } @dirs;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $rel;} @dirs 1 6 7
345 6 ▶ if (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 2 4 6
346 2 ▶ my $why = ( grep { _basename($_) ne $name } @dirs ) ? ', names equal ignoring case' : '';
Branch: 1 branch
BranchTrueFalseTotal
grep({_basename($_) ne $name;} @dirs) ? : 1 1 2
347 2 _fail( $p.': skill "'.$name.'" found twice'.$why.': '.$dirs[0].' and '.$rel );
348 }
349 4 $known->( $name, $rel );
350 4 push @out, _skill_item( $dir, $skill_dir, $rel, $file );
351 9 };
352 9 for my $entry (@$paths) {
353 11 my ( $target, $rel ) = _inner_dir( $dir, $p.': skills path', $entry );
354 6 ▶ if ( my $file = _skill_file($target) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = _skill_file($target)) 1 5 6
355 1 $add->( $target, $rel, $file );
356 1 next;
357 }
358 5 for my $child ( _js_sorted( _readdir($target) ) ) {
359 7 my $c = path_join( $target, $child );
360 7 ▶ next unless _no_symlink($c)->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless _no_symlink($c)->{"dir"} 0 7 7
361 7 ▶ my $f = _skill_file($c) // next;
Condition100%2/2
_skill_file($c) // (next)
l!l
61
MC/DC100%1/1
_skill_file($c) // (next)
_skill_file($c)
Truth table100%2/2
A
A= _skill_file($c)
Aresult
11
00
362 6 $add->( $c, $rel.'/'.$child, $f );
363 }
364 }
365 2 return @out;
366 }
367  
368 # The skills beneath a source entry's `path` values (spec §4.1, k126), sorted by name.
369 12 ▶sub _path_skills {
Subroutine _path_skills: called CC=3 SCAR=11.0
370 12 my ( $dir, $paths ) = @_;
371 12 my ( %found, @order ); # a skill per directory, relative to the source
372 12 my $visit;
373 21 ▶ $visit = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
374 21 my ( $cur, $rel, $named ) = @_;
375 21 ▶ if ( my $file = _skill_file($cur) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = _skill_file($cur)) 10 11 21
376 10 ▶ return if _is_installed_copy($cur);
Branch: 1 branch
BranchTrueFalseTotal
if _is_installed_copy($cur) 1 9 10
377 9 my $item = _skill_item( $dir, $cur, $rel, $file );
378 9 ▶ unless ( $found{ $item->{dir} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found{$item->{"dir"}}) 8 1 9
379 8 $found{ $item->{dir} } = $item;
380 8 push @order, $item->{dir};
381 }
382 9 return;
383 }
384 11 ▶ return if !$named && substr( _basename($rel), 0, 1 ) eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if not $named and substr(_basename($rel), 0, 1) eq "." 4 7 11
Condition100%3/3
not $named and substr(_basename($rel), 0, 1) eq "."
!ll&&!rl&&r
614
MC/DC100%2/2
not $named and substr(_basename($rel), 0, 1) eq "."
not $named substr(_basename($rel), 0, 1) eq "."
Truth table100%3/3
not(A) and B
A= not $namedB= substr(_basename($rel), 0, 1) eq "."
ABresult
0X0
100
111
385 7 for my $entry ( _js_sorted( _readdir($cur) ) ) {
386 14 my $p = path_join( $cur, $entry );
387 14 ▶ $visit->( $p, $rel.'/'.$entry, 0 ) if _is_plain_dir($p);
Branch: 1 branch
BranchTrueFalseTotal
if _is_plain_dir($p) 14 0 14
388 }
389 12 };
390 12 my $ok = eval {
391 12 for my $entry (@$paths) {
392 15 my ( $abs, $rel ) = _inner_dir( $dir, '"path"', $entry );
393 7 $visit->( $abs, $rel, 1 );
394 }
395 4 1;
396 };
397 12 my $err = $@;
398 12 undef $visit;
399 12 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 8 4 12
400 4 return [ sort { _js_cmp( $a->{name}, $b->{name} ) } _one_name( [ map { $found{$_} } @order ], \&_in_dirs ) ];
401 }
402  
403 # One path of a source – a plugin.json `skills` path, a source entry's `path` – as a directory
404 # inside the source: (absolute, relative). Every segment is checked; neither `..` nor a symlink
405 # can leave the source root.
406 26 ▶sub _inner_dir {
Subroutine _inner_dir: called CC=10 SCAR=23.0
407 26 my ( $dir, $what, $entry ) = @_;
408 26 my $segments = inner_segments($entry);
409 26 ▶ _fail( $what.' must not be absolute: '.$entry ) if !ref $segments && $segments eq 'absolute';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $segments and $segments eq "absolute" 2 24 26
Condition100%3/3
not ref $segments and $segments eq "absolute"
!ll&&!rl&&r
1952
MC/DC100%2/2
not ref $segments and $segments eq "absolute"
not ref $segments $segments eq "absolute"
Truth table100%3/3
not(A) and B
A= not ref $segmentsB= $segments eq "absolute"
ABresult
0X0
100
111
410 24 ▶ _fail( $what.' must not contain "..": '.$entry ) if !ref $segments && $segments eq 'dotdot';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $segments and $segments eq "dotdot" 2 22 24
Condition100%3/3
not ref $segments and $segments eq "dotdot"
!ll&&!rl&&r
1932
MC/DC100%2/2
not ref $segments and $segments eq "dotdot"
not ref $segments $segments eq "dotdot"
Truth table100%3/3
not(A) and B
A= not ref $segmentsB= $segments eq "dotdot"
ABresult
0X0
100
111
411 # Its name would be the source root's basename, i.e. the cache directory's.
412 22 ▶ _fail( $what.' must not be the source root: '.js_stringify($entry) ) unless ref $segments;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $segments 3 19 22
413 19 my $cur = $dir;
414 19 for my $seg (@$segments) {
415 23 $cur = path_join( $cur, $seg );
416 23 ▶ _fail( $what.' does not exist: '.$entry ) if !_exists($cur) && !_is_dangling_link($cur);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($cur) or _is_dangling_link($cur) 3 20 23
Condition66%2/3
_exists($cur) or _is_dangling_link($cur)
l!l&&r!l&&!r
2003
MC/DC50%1/2
_exists($cur) or _is_dangling_link($cur)
_exists($cur) _is_dangling_link($cur)
Truth table66%2/3
A or B
A= _exists($cur)B= _is_dangling_link($cur)
ABresult
1X1
011
000
417 20 _no_symlink($cur);
418 }
419 15 ▶ _fail( $what.' is not a directory: '.$entry ) unless _lstat($cur)->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless _lstat($cur)->{"dir"} 2 13 15
420 13 return ( $cur, join '/', @$segments );
421 }
422  
423 # `bundles/<name>.yaml|.yml`; both for one name is an error of that bundle.
424 286 ▶sub _scan_bundles {
Subroutine _scan_bundles: called CC=11 SCAR=24.1
425 286 my ( $dir ) = @_;
426 286 my $bdir = path_join( $dir, 'bundles' );
427 286 ▶ return [] unless _exists($bdir);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($bdir) 284 2 286
428 2 _no_symlink($bdir);
429 2 my ( %by_name, @order );
430 2 for my $entry ( _js_sorted( _readdir($bdir) ) ) {
431 18 ▶ my ($name) = $entry =~ /\A($DOT+)\.ya?ml\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $entry =~ /\A($DOT+)\.ya?ml\z/ or next 17 1 18
432 17 my $p = path_join( $bdir, $entry );
433 17 ▶ next unless _no_symlink($p)->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless _no_symlink($p)->{"file"} 0 17 17
434 17 ▶ push @order, $name unless $by_name{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $by_name{$name} 16 1 17
435 17 push @{ $by_name{$name} }, 'bundles/'.$entry;
436 }
437 2 my @out;
438 2 for my $name (@order) {
439 16 my $files = $by_name{$name};
440 16 ▶ if ( @$files > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$files > 1) 1 15 16
441 1 push @out, { name => $name, files => $files, error => 'both '.join( ' and ', @$files ).' exist' };
442 1 next;
443 }
444 15 my $def;
445 15 ▶ if ( eval { $def = parse_bundle( _read_text( path_join( $dir, $files->[0] ) ) ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $def = parse_bundle(_read_text(path_join($dir, $files->[0]))); 1 } }) 13 2 15
446 13 push @out, { name => $name, files => $files, def => $def };
447 13 next;
448 }
449 2 my $err = $@;
450 2 ▶ die $err unless blessed $err && $err->isa('Skilletor::Error::Bundle');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($err) and $err->isa("Skilletor::Error::Bundle") 0 2 2
Condition33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Bundle")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Bundle")
builtin::blessed($err) $err->isa("Skilletor::Error::Bundle")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Bundle")
ABresult
0X0
100
111
451 2 push @out, { name => $name, files => $files, error => $files->[0].': '.$err->message };
452 }
453 2 return \@out;
454 }
455  
456 290 ▶sub _read_source_meta {
Subroutine _read_source_meta: called CC=9 SCAR=22.0
457 290 my ( $dir ) = @_;
458 290 my $p = path_join( $dir, 'skilletor.json' );
459 290 ▶ return {} unless _exists($p);
Branch: 1 branch
BranchTrueFalseTotal
unless _exists($p) 201 89 290
460 89 _no_symlink($p);
461 89 my $parsed;
462 89 ▶ unless ( eval { $parsed = js_parse( _read_text($p) ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $parsed = js_parse(_read_text($p)); 1 } }) 1 88 89
463 1 _fail( $p.': invalid JSON ('._message($@).')' );
464 }
465 88 my $type = js_type($parsed);
466 88 ▶ return {} unless $type eq 'object' || $type eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "object" or $type eq "array" 1 87 88
Condition66%2/3
$type eq "object" or $type eq "array"
l!l&&r!l&&!r
8701
MC/DC50%1/2
$type eq "object" or $type eq "array"
$type eq "object" $type eq "array"
Truth table66%2/3
A or B
A= $type eq "object"B= $type eq "array"
ABresult
1X1
011
000
467 87 ▶ return {} if $type eq 'array'; # no own `description` or `vars`
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 0 87 87
468 87 my %meta;
469 87 ▶ $meta{description} = $parsed->{description} if js_type( $parsed->{description} ) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if js_type($parsed->{"description"}) eq "string" 6 81 87
470 # k145: defaults are keyed by name; an array (or anything else) would spread by index.
471 87 ▶ if ( exists $parsed->{vars} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parsed->{"vars"}) 87 0 87
472 87 ▶ _fail( $p.': "vars" must be an object' ) unless js_type( $parsed->{vars} ) eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless js_type($parsed->{"vars"}) eq "object" 8 79 87
473 79 $meta{vars} = $parsed->{vars};
474 }
475 79 return \%meta;
476 }
477  
478 34 1;
479  
480 __END__
« Skilletor/Config.pm ↑ Summary Skilletor/Gitignore.pm »