Skilletor.pm

filter: Statementstmt 99.3% 141 / 142 Branchbran 86.3% 38 / 44 Conditioncond 84.2% 16 / 19 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 96.8% 31 / 32 Podpod 100% 12 / 12 totaltotal 94.6% 247 / 261 CC 34 SCAR 13.5
CC
34
Cov
95%
_items_of
9.0
_record
8.2
_scopes_for
4.0
CRAP 3.9
« Skilletor/Targets.pm ↑ Summary Skilletor/Backend.pm »
1 package Skilletor;
2 # ABSTRACT: Install skills, agents and rules from remote sources – the Perl port of skilletor
3  
4 28 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use Skilletor::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Skilletor::Entry qw( name_key );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Skilletor::Error::Ambiguous;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Skilletor::Error::Unsupported;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Skilletor::FSUtil qw( path_join );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 28 our $VERSION = '0.001';
14  
15 28 has home => (
16 is => 'ro',
17 required => 1,
18 );
19  
20 28 has project_dir => (
21 is => 'ro',
22 );
23  
24 has fetch => (
25 is => 'ro',
26 76 ▶ default => sub { 'always' },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 28 );
28  
29 has require_harness => (
30 is => 'ro',
31 192 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 28 );
33  
34 28 has [qw( state_root cache_root codex_home timeout_ms lock_timeout_ms is_git_work_tree git_tracked git_remote plugin_root )] => (
35 is => 'ro',
36 );
37  
38 has resolver => (
39 is => 'lazy',
40 64 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
Subroutine _build_resolver: called CC=2 SCAR=7.0
41 64 my ( $self ) = @_;
42 return Skilletor::Engine->new(
43 home => $self->home,
44 project_dir => $self->project_dir,
45 fetch => $self->fetch,
46 require_harness => $self->require_harness,
47 64 ▶ map { defined $self->$_ ? ( $_ => $self->$_ ) : () }
Branch: 2 branches
BranchTrueFalseTotal
defined $self->$_ ? : 0 576 576
defined $self->$_ ? : 6 1362 1368
48 qw( state_root cache_root codex_home timeout_ms lock_timeout_ms is_git_work_tree git_tracked git_remote plugin_root ),
49 );
50 },
51 28 );
52  
53 0 ▶sub engine { $_[0]->resolver }
Subroutine engine: not called CC=1 SCAR=6.9
Pod documented
54  
55 # A sync changes what the lookups would answer: the resolution they answer from goes with it.
56 6 ▶sub sync {
Subroutine sync: called CC=1 SCAR=0.0
Pod documented
57 6 my ( $self, %opts ) = @_;
58 6 my $report = $self->resolver->sync(%opts);
59 4 $self->refresh;
60 4 return $report;
61 }
62  
63 4 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
64 4 my ( $self, %opts ) = @_;
65 4 return $self->resolver->check(%opts);
66 }
67  
68 2 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
69 2 my ( $self, %opts ) = @_;
70 2 return $self->resolver->status(%opts);
71 }
72  
73 # The resolution of every scope that resolve_* answer from, made on first use.
74 has _resolution => (
75 is => 'lazy',
76 init_arg => undef,
77 clearer => 'refresh',
78 143 ▶ builder => sub { $_[0]->resolver->resolve( scope => 'all', throw => 1 ) },
Subroutine _build__resolution: called CC=1 SCAR=0.0
79 28 );
80  
81 107 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=0.0
Pod documented
82 107 my ( $self, %opts ) = @_;
83 107 return $self->resolver->resolve(%opts);
84 }
85  
86 # One resolved item as the record resolve_item returns.
87 1241 ▶sub _record {
Subroutine _record: called CC=8 SCAR=21.0
88 1241 my ( $self, $scope, $ri ) = @_;
89 1241 my $ci = $ri->{item};
90 1241 my $root = $ri->{sourceDir};
91 1241 my @files = @{ $ci->{files} };
92 1241 my ( $path, $dir, $main );
93 1241 ▶ if ( $ri->{type} eq 'skill' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ri->{'type'} eq 'skill') { } 568 673 1241
94 568 $path = $ci->{dir};
95 568 $dir = path_join( $root, $path );
96 568 ($main) = grep { $_ eq $path.'/SKILL.md' } @files;
97 568 ▶ $main //= $path.'/SKILL.md.njk';
Condition50%1/2
$main //= $path . '/SKILL.md.njk'
l!l
5680
MC/DC0%0/1
$main //= $path . '/SKILL.md.njk'
$main
Truth table50%1/2
A
A= $main
Aresult
11
00
98 }
99 else {
100 673 $path = $main = $files[0];
101 673 ( my $parent = $path ) =~ s{/[^/]*\z}{};
102 673 ▶ $dir = path_join( $root, $parent eq $path ? '' : $parent );
Branch: 1 branch
BranchTrueFalseTotal
$parent eq $path ? : 0 673 673
103 }
104 1241 my @templates = grep { /\.njk\z/ } @files;
105 1241 my %source = map { ( $_->{name} => $_ ) } @{ $self->_scope_report($scope)->{sources} };
106 return {
107 type => $ri->{type},
108 name => $ri->{name},
109 scope => $scope,
110 source => $ri->{source},
111 version => $source{ $ri->{source} } ? $source{ $ri->{source} }{version} : undef,
112 source_dir => $root,
113 path => $path,
114 dir => $dir,
115 file => path_join( $root, $main ),
116 1529 ▶ files => [ map { path_join( $root, $_ ) } @files ],
Branch: 3 branches
BranchTrueFalseTotal
defined $ci->{'description'} ? : 1241 0 1241
$ri->{'via'} ? : 0 1241 1241
@templates ? : 216 1025 1241
117 ( defined $ci->{description} ? ( description => $ci->{description} ) : () ),
118 vars => $ri->{vars},
119 declaredBy => $ri->{declaredBy},
120 1241 ▶ ( $ri->{via} ? ( via => $ri->{via} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$source{$ri->{'source'}} ? : 1241 0 1241
121 templated => @templates ? 1 : 0,
122 templates => \@templates,
123 };
124 }
125  
126 2906 ▶sub _scope_report {
Subroutine _scope_report: called CC=1 SCAR=0.0
127 2906 my ( $self, $scope ) = @_;
128 2906 my ($rep) = grep { $_->{scope} eq $scope } @{ $self->_resolution->{scopes} };
129 2865 return $rep;
130 }
131  
132 # Claude Code's order per type when both scopes hold an item of one name: a personal skill wins
133 # over a project one, a project agent over a personal one; rules do not override each other –
134 # both load, the user's first.
135 28 my %ORDER = ( skill => [qw( user project )], agent => [qw( project user )], rule => [qw( user project )] );
136 28 my %SHADOWS = ( skill => 1, agent => 1, rule => 0 );
137 28 my @TYPES = qw( skill agent rule );
138  
139 925 ▶sub _check_type {
Subroutine _check_type: called CC=3 SCAR=11.1
140 925 my ( $type ) = @_;
141 925 ▶ croak 'type must be skill, agent or rule' unless defined $type && $ORDER{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $ORDER{$type} 24 901 925
Condition66%2/3
defined $type and $ORDER{$type}
!ll&&!rl&&r
024901
MC/DC50%1/2
defined $type and $ORDER{$type}
defined $type $ORDER{$type}
Truth table66%2/3
A and B
A= defined $typeB= $ORDER{$type}
ABresult
0X0
100
111
142 }
143  
144 2326 ▶sub _scopes_for {
Subroutine _scopes_for: called CC=4 SCAR=13.9
145 2326 my ( $scope, $type ) = @_;
146 2326 ▶ $scope //= 'all';
Condition100%2/2
$scope //= "all"
l!l
1641685
MC/DC100%1/1
$scope //= "all"
$scope
Truth table100%2/2
A
A= $scope
Aresult
11
00
147 2326 ▶ croak 'scope must be user, project or all' unless $scope =~ /\A(?:user|project|all)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $scope =~ /\A(?:user|project|all)\z/ 0 2326 2326
148 2326 ▶ return $scope eq 'all' ? @{ $ORDER{$type} } : ($scope);
Branch: 1 branch
BranchTrueFalseTotal
$scope eq 'all' ? : 685 1641 2326
149 }
150  
151 # Every record of a type, in the type's order: one per name where one scope's item wins.
152 1521 ▶sub _items_of {
Subroutine _items_of: called CC=9 SCAR=22.0
153 1521 my ( $self, $type, $scope, $key ) = @_;
154 1521 my ( %seen, @out );
155 1521 for my $s ( _scopes_for( $scope, $type ) ) {
156 1665 ▶ my $rep = $self->_scope_report($s) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $rep = $self->_scope_report($s) or next 1532 92 1624
157 1532 for my $ri ( @{ $rep->{items} } ) {
158 6477 ▶ next unless $ri->{type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $ri->{"type"} eq $type 4204 2273 6477
159 2273 my $k = name_key( $ri->{name} );
160 2273 ▶ next if defined $key && $k ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and $k ne $key 936 1337 2273
Condition100%3/3
defined $key and $k ne $key
!ll&&!rl&&r
552785936
MC/DC100%2/2
defined $key and $k ne $key
defined $key $k ne $key
Truth table100%3/3
A and B
A= defined $keyB= $k ne $key
ABresult
0X0
100
111
161 1337 ▶ next if $SHADOWS{$type} && $seen{$k}++;
Branch: 1 branch
BranchTrueFalseTotal
if $SHADOWS{$type} and $seen{$k}++ 96 1241 1337
Condition100%3/3
$SHADOWS{$type} and $seen{$k}++
!ll&&!rl&&r
40883396
MC/DC100%2/2
$SHADOWS{$type} and $seen{$k}++
$SHADOWS{$type} $seen{$k}++
Truth table100%3/3
A and B
A= $SHADOWS{$type}B= $seen{$k}++
ABresult
0X0
100
111
162 1241 push @out, $self->_record( $s, $ri );
163 }
164 }
165 1480 return @out;
166 }
167  
168 120 ▶sub items {
Subroutine items: called CC=3 SCAR=11.0
Pod documented
169 120 my ( $self, %opts ) = @_;
170 120 ▶ _check_type( $opts{type} ) if defined $opts{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"type"} 72 48 120
171 120 ▶ return [ map { $self->_items_of( $_, $opts{scope} ) } defined $opts{type} ? ( $opts{type} ) : @TYPES ];
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'type'} ? : 72 48 120
172 }
173  
174 853 ▶sub resolve_items {
Subroutine resolve_items: called CC=4 SCAR=13.9
Pod documented
175 853 my ( $self, $type, $name, %opts ) = @_;
176 853 _check_type($type);
177 829 ▶ return [] unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 24 805 829
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
024805
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
178 805 my $key = name_key($name);
179 805 my @all;
180 805 for my $s ( _scopes_for( $opts{scope}, $type ) ) {
181 1305 push @all, grep { $_->{scope} eq $s } $self->_items_of( $type, $s, $key );
182 }
183 764 return \@all;
184 }
185  
186 757 ▶sub resolve_item {
Subroutine resolve_item: called CC=4 SCAR=13.9
Pod documented
187 757 my ( $self, $type, $name, %opts ) = @_;
188 757 my @found = @{ $self->resolve_items( $type, $name, %opts ) };
189 692 ▶ return undef unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 147 545 692
190 545 ▶ if ( @found > 1 && !$SHADOWS{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found > 1 and not $SHADOWS{$type}) 48 497 545
Condition100%3/3
@found > 1 and not $SHADOWS{$type}
!ll&&!rl&&r
4257248
MC/DC100%2/2
@found > 1 and not $SHADOWS{$type}
@found > 1 not $SHADOWS{$type}
Truth table100%3/3
A and not(B)
A= @found > 1B= not $SHADOWS{$type}
ABresult
0X0
100
111
191 48 Skilletor::Error::Ambiguous->throw( $type.' '.$found[0]{name}.' is declared in the user and the project scope; '
192 .'Claude Code loads both (neither overrides the other) – ask resolve_'.$type.'s, or pass a scope' );
193 }
194 497 return $found[0];
195 }
196  
197 385 ▶sub _refuse_template {
Subroutine _refuse_template: called CC=2 SCAR=6.9
198 385 my ( $rec ) = @_;
199 385 ▶ return unless $rec->{templated};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"templated"} 337 48 385
200 Skilletor::Error::Unsupported->throw( $rec->{type}.' '.$rec->{name}.'@'.$rec->{source}.' is a Nunjucks template ('
201 48 .join( ', ', @{ $rec->{templates} } ).'); the Perl port of skilletor renders no templates' );
202 }
203  
204 508 ▶sub _main_file {
Subroutine _main_file: called CC=2 SCAR=6.9
205 508 my ( $self, $type, $name, %opts ) = @_;
206 508 ▶ my $rec = $self->resolve_item( $type, $name, %opts ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->resolve_item($type, $name, %opts) or return undef 313 147 460
207 313 _refuse_template($rec);
208 265 return $rec->{file};
209 }
210  
211 360 ▶sub resolve_skill { my $self = shift; return $self->_main_file( skill => @_ ) }
Subroutine resolve_skill: called CC=1 SCAR=0.0
Pod documented
212 76 ▶sub resolve_agent { my $self = shift; return $self->_main_file( agent => @_ ) }
Subroutine resolve_agent: called CC=1 SCAR=0.0
Pod documented
213 72 ▶sub resolve_rule { my $self = shift; return $self->_main_file( rule => @_ ) }
Subroutine resolve_rule: called CC=1 SCAR=0.0
Pod documented
214  
215 72 ▶sub resolve_rules {
Subroutine resolve_rules: called CC=2 SCAR=6.9
Pod documented
216 72 my ( $self, $name, %opts ) = @_;
217 72 my @recs = @{ $self->resolve_items( rule => $name, %opts ) };
218 72 _refuse_template($_) for @recs;
219 72 return map { $_->{file} } @recs;
220 }
221  
222 28 1;
223  
224 __END__
« Skilletor/Targets.pm ↑ Summary Skilletor/Backend.pm »