blib/lib/Skilletor.pm

filter: Statementstmt 99.3% 144 / 145 Branchbran 90.9% 40 / 44 Conditioncond 84.2% 16 / 19 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 96.9% 32 / 33 Podpod 100% 12 / 12 totaltotal 95.4% 253 / 265 CC 34 SCAR 13.4
CC
34
Cov
96%
_items_of
9.0
_record
8.1
_scopes_for
4.0
CRAP 3.8
« /usr/local/lib/perl5/5.44.0/PerlIO.pm ↑ Summary lib/Skilletor/Backend.pm »
1 package Skilletor;
2 # ABSTRACT: Install and sync skills, agents and rules for Claude Code, Codex and Hermes Agent
3  
4 71 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 71 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 71 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 71 ▶use Skilletor::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 71 ▶use Skilletor::Entry qw( name_key );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 71 ▶use Skilletor::Error::Ambiguous;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 71 ▶use Skilletor::Error::Unsupported;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 71 ▶use Skilletor::FSUtil qw( path_join );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 71 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 71 our $VERSION = '0.008000';
15  
16 71 has home => (
17 is => 'ro',
18 required => 1,
19 );
20  
21 71 has project_dir => (
22 is => 'ro',
23 );
24  
25 has fetch => (
26 is => 'ro',
27 153 ▶ default => sub { 'always' },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 71 );
29  
30 has require_harness => (
31 is => 'ro',
32 269 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 71 );
34  
35 71 has [qw( state_root cache_root codex_home timeout_ms lock_timeout_ms is_git_work_tree git_tracked git_remote hermes_home plugin_root )] => (
36 is => 'ro',
37 );
38  
39 has resolver => (
40 is => 'lazy',
41 104 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
Subroutine _build_resolver: called CC=2 SCAR=6.9
42 104 my ( $self ) = @_;
43 return Skilletor::Engine->new(
44 home => $self->home,
45 project_dir => $self->project_dir,
46 fetch => $self->fetch,
47 require_harness => $self->require_harness,
48 104 ▶ map { defined $self->$_ ? ( $_ => $self->$_ ) : () }
Branch: 2 branches
BranchTrueFalseTotal
defined $self->$_ ? : 34 1006 1040
defined $self->$_ ? : 48 1842 1890
49 qw( state_root cache_root codex_home timeout_ms lock_timeout_ms is_git_work_tree git_tracked git_remote hermes_home plugin_root ),
50 );
51 },
52 71 );
53  
54 0 ▶sub engine { $_[0]->resolver }
Subroutine engine: not called CC=1 SCAR=6.9
Pod documented
55  
56 # A sync changes what the lookups would answer: the resolution they answer from goes with it.
57 120 ▶sub sync {
Subroutine sync: called CC=1 SCAR=0.0
Pod documented
58 120 my ( $self, %opts ) = @_;
59 120 my $report = $self->resolver->sync(%opts);
60 118 $self->refresh;
61 118 return $report;
62 }
63  
64 80 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
Pod documented
65 80 my ( $self, %opts ) = @_;
66 80 return $self->resolver->check(%opts);
67 }
68  
69 78 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
70 78 my ( $self, %opts ) = @_;
71 78 return $self->resolver->status(%opts);
72 }
73  
74 # The resolution of every scope that resolve_* answer from, made on first use.
75 has _resolution => (
76 is => 'lazy',
77 init_arg => undef,
78 clearer => 'refresh',
79 175 ▶ builder => sub { $_[0]->resolver->resolve( scope => 'all', throw => 1 ) },
Subroutine _build__resolution: called CC=1 SCAR=0.0
80 71 );
81  
82 107 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=0.0
Pod documented
83 107 my ( $self, %opts ) = @_;
84 107 return $self->resolver->resolve(%opts);
85 }
86  
87 # One resolved item as the record resolve_item returns.
88 1281 ▶sub _record {
Subroutine _record: called CC=8 SCAR=20.9
89 1281 my ( $self, $scope, $ri ) = @_;
90 1281 my $ci = $ri->{item};
91 1281 my $root = $ri->{sourceDir};
92 1281 my @files = @{ $ci->{files} };
93 1281 my ( $path, $dir, $main );
94 1281 ▶ if ( $ri->{type} eq 'skill' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ri->{'type'} eq 'skill') { } 568 713 1281
95 568 $path = $ci->{dir};
96 568 $dir = path_join( $root, $path );
97 568 ($main) = grep { $_ eq $path.'/SKILL.md' } @files;
98 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
99 }
100 else {
101 713 $path = $main = $files[0];
102 713 ( my $parent = $path ) =~ s{/[^/]*\z}{};
103 713 ▶ $dir = path_join( $root, $parent eq $path ? '' : $parent );
Branch: 1 branch
BranchTrueFalseTotal
$parent eq $path ? : 0 713 713
104 }
105 1281 my @templates = grep { /\.njk\z/ } @files;
106 1281 my %source = map { ( $_->{name} => $_ ) } @{ $self->_scope_report($scope)->{sources} };
107 return {
108 type => $ri->{type},
109 name => $ri->{name},
110 scope => $scope,
111 source => $ri->{source},
112 version => $source{ $ri->{source} } ? $source{ $ri->{source} }{version} : undef,
113 source_dir => $root,
114 path => $path,
115 dir => $dir,
116 file => path_join( $root, $main ),
117 1569 ▶ files => [ map { path_join( $root, $_ ) } @files ],
Branch: 3 branches
BranchTrueFalseTotal
defined $ci->{'description'} ? : 1279 2 1281
$ri->{'via'} ? : 0 1281 1281
@templates ? : 216 1065 1281
118 ( defined $ci->{description} ? ( description => $ci->{description} ) : () ),
119 vars => $ri->{vars},
120 declaredBy => $ri->{declaredBy},
121 1281 ▶ ( $ri->{via} ? ( via => $ri->{via} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$source{$ri->{'source'}} ? : 1281 0 1281
122 templated => @templates ? 1 : 0,
123 templates => \@templates,
124 };
125 }
126  
127 3100 ▶sub _scope_report {
Subroutine _scope_report: called CC=1 SCAR=0.0
128 3100 my ( $self, $scope ) = @_;
129 3100 my ($rep) = grep { $_->{scope} eq $scope } @{ $self->_resolution->{scopes} };
130 3059 return $rep;
131 }
132  
133 # Claude Code's order per type when both scopes hold an item of one name: a personal skill wins
134 # over a project one, a project agent over a personal one; rules do not override each other –
135 # both load, the user's first.
136 71 my %ORDER = ( skill => [qw( user project )], agent => [qw( project user )], rule => [qw( user project )] );
137 71 my %SHADOWS = ( skill => 1, agent => 1, rule => 0 );
138 71 my @TYPES = qw( skill agent rule );
139  
140 1002 ▶sub _check_type {
Subroutine _check_type: called CC=3 SCAR=11.1
141 1002 my ( $type ) = @_;
142 1002 ▶ croak 'type must be skill, agent or rule' unless defined $type && $ORDER{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and $ORDER{$type} 24 978 1002
Condition66%2/3
defined $type and $ORDER{$type}
!ll&&!rl&&r
024978
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
143 }
144  
145 2557 ▶sub _scopes_for {
Subroutine _scopes_for: called CC=4 SCAR=13.9
146 2557 my ( $scope, $type ) = @_;
147 2557 ▶ $scope //= 'all';
Condition100%2/2
$scope //= "all"
l!l
1795762
MC/DC100%1/1
$scope //= "all"
$scope
Truth table100%2/2
A
A= $scope
Aresult
11
00
148 2557 ▶ 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 2557 2557
149 2557 ▶ return $scope eq 'all' ? @{ $ORDER{$type} } : ($scope);
Branch: 1 branch
BranchTrueFalseTotal
$scope eq 'all' ? : 762 1795 2557
150 }
151  
152 # Every record of a type, in the type's order: one per name where one scope's item wins.
153 1675 ▶sub _items_of {
Subroutine _items_of: called CC=9 SCAR=22.0
154 1675 my ( $self, $type, $scope, $key ) = @_;
155 1675 my ( %seen, @out );
156 1675 for my $s ( _scopes_for( $scope, $type ) ) {
157 1819 ▶ my $rep = $self->_scope_report($s) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $rep = $self->_scope_report($s) or next 1610 168 1778
158 1610 for my $ri ( @{ $rep->{items} } ) {
159 6593 ▶ next unless $ri->{type} eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $ri->{"type"} eq $type 4280 2313 6593
160 2313 my $k = name_key( $ri->{name} );
161 2313 ▶ next if defined $key && $k ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and $k ne $key 936 1377 2313
Condition100%3/3
defined $key and $k ne $key
!ll&&!rl&&r
552825936
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
162 1377 ▶ next if $SHADOWS{$type} && $seen{$k}++;
Branch: 1 branch
BranchTrueFalseTotal
if $SHADOWS{$type} and $seen{$k}++ 96 1281 1377
Condition100%3/3
$SHADOWS{$type} and $seen{$k}++
!ll&&!rl&&r
41087196
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
163 1281 push @out, $self->_record( $s, $ri );
164 }
165 }
166 1634 return @out;
167 }
168  
169 120 ▶sub items {
Subroutine items: called CC=3 SCAR=11.0
Pod documented
170 120 my ( $self, %opts ) = @_;
171 120 ▶ _check_type( $opts{type} ) if defined $opts{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"type"} 72 48 120
172 120 ▶ return [ map { $self->_items_of( $_, $opts{scope} ) } defined $opts{type} ? ( $opts{type} ) : @TYPES ];
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'type'} ? : 72 48 120
173 }
174  
175 930 ▶sub resolve_items {
Subroutine resolve_items: called CC=4 SCAR=13.9
Pod documented
176 930 my ( $self, $type, $name, %opts ) = @_;
177 930 _check_type($type);
178 906 ▶ return [] unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 24 882 906
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
024882
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
179 882 my $key = name_key($name);
180 882 my @all;
181 882 for my $s ( _scopes_for( $opts{scope}, $type ) ) {
182 1459 push @all, grep { $_->{scope} eq $s } $self->_items_of( $type, $s, $key );
183 }
184 841 return \@all;
185 }
186  
187 834 ▶sub resolve_item {
Subroutine resolve_item: called CC=4 SCAR=13.9
Pod documented
188 834 my ( $self, $type, $name, %opts ) = @_;
189 834 my @found = @{ $self->resolve_items( $type, $name, %opts ) };
190 769 ▶ return undef unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 185 584 769
191 584 ▶ if ( @found > 1 && !$SHADOWS{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@found > 1 and not $SHADOWS{$type}) 49 535 584
Condition100%3/3
@found > 1 and not $SHADOWS{$type}
!ll&&!rl&&r
4637249
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
192 49 Skilletor::Error::Ambiguous->throw( $type.' '.$found[0]{name}.' is declared in the user and the project scope; '
193 .'Claude Code loads both (neither overrides the other) – ask resolve_'.$type.'s, or pass a scope' );
194 }
195 535 return $found[0];
196 }
197  
198 423 ▶sub _refuse_template {
Subroutine _refuse_template: called CC=2 SCAR=6.9
199 423 my ( $rec ) = @_;
200 423 ▶ return unless $rec->{templated};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"templated"} 375 48 423
201 Skilletor::Error::Unsupported->throw( $rec->{type}.' '.$rec->{name}.'@'.$rec->{source}.' is a Nunjucks template ('
202 48 .join( ', ', @{ $rec->{templates} } ).'); a lookup serves source files and no unrendered template (a sync renders it)' );
203 }
204  
205 585 ▶sub _main_file {
Subroutine _main_file: called CC=2 SCAR=6.9
206 585 my ( $self, $type, $name, %opts ) = @_;
207 585 ▶ 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 351 185 536
208 351 _refuse_template($rec);
209 303 return $rec->{file};
210 }
211  
212 360 ▶sub resolve_skill { my $self = shift; return $self->_main_file( skill => @_ ) }
Subroutine resolve_skill: called CC=1 SCAR=0.0
Pod documented
213 152 ▶sub resolve_agent { my $self = shift; return $self->_main_file( agent => @_ ) }
Subroutine resolve_agent: called CC=1 SCAR=0.0
Pod documented
214 73 ▶sub resolve_rule { my $self = shift; return $self->_main_file( rule => @_ ) }
Subroutine resolve_rule: called CC=1 SCAR=0.0
Pod documented
215  
216 72 ▶sub resolve_rules {
Subroutine resolve_rules: called CC=2 SCAR=6.9
Pod documented
217 72 my ( $self, $name, %opts ) = @_;
218 72 my @recs = @{ $self->resolve_items( rule => $name, %opts ) };
219 72 _refuse_template($_) for @recs;
220 72 return map { $_->{file} } @recs;
221 }
222  
223 71 1;
224  
225 __END__
« /usr/local/lib/perl5/5.44.0/PerlIO.pm ↑ Summary lib/Skilletor/Backend.pm »