Skilletor/Render.pm

filter: Statementstmt 96.3% 52 / 54 Branchbran 66.6% 8 / 12 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 82.0% 82 / 100 CC 12 SCAR 12.7
CC
12
Cov
85%
_relative
6.9
executables
4.2
install_path
3.0
CRAP 3.6
« Skilletor/Backend.pm ↑ Summary Skilletor/Entry.pm »
1 package Skilletor::Render;
2 # ABSTRACT: Build an item in memory – the copy half of the reference's render.ts
3  
4 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Skilletor::FSUtil qw( encode_text is_executable path_join path_normalize );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 31 our $VERSION = '0.001';
13  
14 31 our @EXPORT_OK = qw( build executables install_path templates_of );
15  
16 # The item's Nunjucks templates: its files ending in `.njk`.
17 68 ▶sub templates_of {
Subroutine templates_of: called CC=1 SCAR=0.0
Pod documented
18 68 my ( $item ) = @_;
19 68 return grep { /\.njk\z/ } @{ $item->{files} };
20 }
21  
22 # posix.relative for two normalized relative paths.
23 28 ▶sub _relative {
Subroutine _relative: called CC=6 SCAR=19.3
24 28 my ( $from, $to ) = @_;
25 28 ▶ my @f = grep { length && $_ ne '.' } split m{/}, path_normalize($from);
Condition33%1/3
length $_ && $_ ne '.'
!ll&&!rl&&r
0056
MC/DC0%0/2
length $_ && $_ ne '.'
length $_ $_ ne '.'
Truth table33%1/3
A && B
A= length $_B= $_ ne '.'
ABresult
0X0
100
111
26 28 ▶ my @t = grep { length && $_ ne '.' } split m{/}, path_normalize($to);
Condition33%1/3
length $_ && $_ ne '.'
!ll&&!rl&&r
0084
MC/DC0%0/2
length $_ && $_ ne '.'
length $_ $_ ne '.'
Truth table33%1/3
A && B
A= length $_B= $_ ne '.'
ABresult
0X0
100
111
27 28 ▶ while ( @f && @t && $f[0] eq $t[0] ) { shift @f; shift @t }
Branch: 1 branch
BranchTrueFalseTotal
if (@f and @t and $f[0] eq $t[0]) 56 28 84
Condition66%4/6
@f and @t and $f[0] eq $t[0]
!ll&&!rl&&r
28056
@f and @t
!ll&&!rl&&r
28056
MC/DC33%1/3
@f and @t and $f[0] eq $t[0]
@f @t $f[0] eq $t[0]
Truth table50%2/4
A and B and C
A= @fB= @tC= $f[0] eq $t[0]
ABCresult
0XX0
10X0
1100
1111
28 0 return join '/', ( map {'..'} @f ), @t;
29 }
30  
31 # Where a source file of the item installs: a skill's files under `skills/<name>/`, an agent's or
32 # rule's under `agents/` or `rules/`, wherever the item's `dir` sits in the source. Without a
33 # `dir` the item lives where it installs.
34 31 ▶sub install_path {
Subroutine install_path: called CC=3 SCAR=11.0
Pod documented
35 31 my ( $item, $file ) = @_;
36 31 ▶ return $file unless defined $item->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"dir"} 3 28 31
37 28 ▶ my $into = $item->{type} eq 'skill' ? 'skills/'.$item->{name} : $item->{type}.'s';
Branch: 1 branch
BranchTrueFalseTotal
$item->{'type'} eq 'skill' ? : 28 0 28
38 28 return path_join( $into, _relative( $item->{dir}, $file ) );
39 }
40  
41 # The item's files, copied byte for byte: [ [ install path, bytes ], ... ] in the item's order.
42 # The Perl port renders nothing: an item with a `.njk` file is the caller's to refuse.
43 31 ▶sub build {
Subroutine build: called CC=2 SCAR=7.0
Pod documented
44 31 my ( $item, $source_dir ) = @_;
45 31 my @tpl = templates_of($item);
46 31 ▶ croak 'build: '.$item->{type}.' '.$item->{name}.' has templates ('.join( ', ', @tpl ).'); the Perl port renders none' if @tpl;
Branch: 1 branch
BranchTrueFalseTotal
if @tpl 0 31 31
47 31 return [ map { [ install_path( $item, $_ ), path( encode_text( path_join( $source_dir, $_ ) ) )->slurp_raw ] } @{ $item->{files} } ];
48 }
49  
50 # The install paths of the item's files whose source file has the owner's executable bit.
51 28 ▶sub executables {
Subroutine executables: called CC=4 SCAR=14.3
Pod documented
52 28 my ( $item, $source_dir ) = @_;
53 28 my %out;
54 28 for my $file ( @{ $item->{files} } ) {
55 28 my @st = stat encode_text( path_join( $source_dir, $file ) );
56 28 ▶ die 'stat '.path_join( $source_dir, $file ).': '.$!."\n" unless @st;
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 28 28
57 28 ▶ next unless is_executable( $st[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_executable($st[2]) 28 0 28
58 0 $out{ install_path( $item, $file =~ s/\.njk\z//r ) } = 1;
59 }
60 28 return \%out;
61 }
62  
63 31 1;
64  
65 __END__
« Skilletor/Backend.pm ↑ Summary Skilletor/Entry.pm »