lib/Skilletor/Render.pm

filter: Statementstmt 94.8% 111 / 117 Branchbran 79.5% 35 / 44 Conditioncond 55.8% 19 / 34 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 95.0% 19 / 20 Podpod 100% 5 / 5 totaltotal 80.0% 193 / 241 CC 38 SCAR 17.8
CC
38
Cov
85%
build
10.4
renders_empty
8.2
_template_value
8.1
CRAP 5.9
« /usr/local/lib/perl5/site_perl/5.44.0/Moo.pm ↑ Summary /usr/local/lib/perl5/5.44.0/Tie/Hash.pm »
1 package Skilletor::Render;
2 # ABSTRACT: Build an item in memory – the reference's render.ts
3  
4 400 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 400 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 400 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 400 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 400 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 400 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 400 ▶use Skilletor::Error::Render;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 400 ▶use Skilletor::FSUtil qw( decode_utf8_lossy encode_text is_executable path_join path_normalize );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 400 ▶use Skilletor::JSON qw( js_sort_keys );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 400 ▶use Skilletor::JSString qw( js_ws_re );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 400 our $VERSION = '0.008000';
17  
18 400 our @EXPORT_OK = qw( build executables install_path renders_empty templates_of );
19  
20 # The item's Nunjucks templates: its files ending in `.njk`.
21 0 ▶sub templates_of {
Subroutine templates_of: not called CC=1 SCAR=6.9
Pod documented
22 0 my ( $item ) = @_;
23 0 return grep { /\.njk\z/ } @{ $item->{files} };
24 }
25  
26 # posix.relative for two normalized relative paths.
27 3640 ▶sub _relative {
Subroutine _relative: called CC=6 SCAR=19.3
28 3640 my ( $from, $to ) = @_;
29 3640 ▶ my @f = grep { length && $_ ne '.' } split m{/}, path_normalize($from);
Condition33%1/3
length $_ && $_ ne '.'
!ll&&!rl&&r
007280
MC/DC0%0/2
length $_ && $_ ne '.'
length $_ $_ ne '.'
Truth table33%1/3
A && B
A= length $_B= $_ ne '.'
ABresult
0X0
100
111
30 3640 ▶ my @t = grep { length && $_ ne '.' } split m{/}, path_normalize($to);
Condition33%1/3
length $_ && $_ ne '.'
!ll&&!rl&&r
0010979
MC/DC0%0/2
length $_ && $_ ne '.'
length $_ $_ ne '.'
Truth table33%1/3
A && B
A= length $_B= $_ ne '.'
ABresult
0X0
100
111
31 3640 ▶ 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]) 7280 3640 10920
Condition66%4/6
@f and @t and $f[0] eq $t[0]
!ll&&!rl&&r
364007280
@f and @t
!ll&&!rl&&r
364007280
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
32 0 return join '/', ( map {'..'} @f ), @t;
33 }
34  
35 # Where a source file of the item installs: a skill's files under `skills/<name>/`, an agent's or
36 # rule's under `agents/` or `rules/`, wherever the item's `dir` sits in the source. Without a
37 # `dir` the item lives where it installs.
38 5847 ▶sub install_path {
Subroutine install_path: called CC=3 SCAR=11.0
Pod documented
39 5847 my ( $item, $file ) = @_;
40 5847 ▶ return $file unless defined $item->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"dir"} 2207 3640 5847
41 3640 ▶ my $into = $item->{type} eq 'skill' ? 'skills/'.$item->{name} : $item->{type}.'s';
Branch: 1 branch
BranchTrueFalseTotal
$item->{'type'} eq 'skill' ? : 3640 0 3640
42 3640 return path_join( $into, _relative( $item->{dir}, $file ) );
43 }
44  
45 # The reference's Nunjucks environment: the loader confined to the source root, autoescape off
46 # (Markdown), throwOnUndefined on (a typo must not silently produce an empty skill). Text::Nunjucks
47 # is loaded with the first template, so an item without one never pays for it.
48 527 ▶sub _env {
Subroutine _env: called CC=1 SCAR=0.0
49 527 my ( $source_dir ) = @_;
50 527 require Text::Nunjucks::Environment;
51 527 require Skilletor::Render::Loader;
52 527 return Text::Nunjucks::Environment->new( Skilletor::Render::Loader->new( root => $source_dir ),
53 { autoescape => !!0, throwOnUndefined => !!1 } );
54 }
55  
56 # A value of the port's JSON model as the template sees it in the reference (Text::Nunjucks takes
57 # Perl 5.40's value kinds): a JSON null is Nunjucks' null, a JSON::MaybeXS boolean a core one, an
58 # object a Hash::Ordered in its property order.
59 7493 ▶sub _template_value {
Subroutine _template_value: called CC=8 SCAR=21.0
60 7493 my ( $v ) = @_;
61 7493 ▶ return Text::Nunjucks::Runtime->None unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 7493 7493
62 7493 ▶ return $v ? !!1 : !!0 if JSON::MaybeXS::is_bool($v);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($v) 82 7411 7493
$v ? : 41 41 82
63 7411 my $ref = ref $v;
64 0 ▶ return [ map { _template_value($_) } @$v ] if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 0 7411 7411
65 7411 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 2336 5075 7411
66 2336 my $h = Hash::Ordered->new;
67 2336 ▶ $h->set( $_ => _template_value( $v->{$_} ) ) for tied %$v ? keys %$v : js_sort_keys( keys %$v );
Branch: 1 branch
BranchTrueFalseTotal
tied %$v ? : 1940 396 2336
68 2336 return $h;
69 }
70 5075 return $v;
71 }
72  
73 # The render context (spec §5): every field but an undefined one (`project` in the user scope).
74 485 ▶sub _template_context {
Subroutine _template_context: called CC=2 SCAR=6.9
75 485 my ( $context ) = @_;
76 485 require Hash::Ordered;
77 485 ▶ return { map { defined $context->{$_} ? ( $_ => _template_value( $context->{$_} ) ) : () } keys %$context };
Branch: 1 branch
BranchTrueFalseTotal
defined $context->{$_} ? : 3224 369 3593
78 }
79  
80 82 ▶sub _message {
Subroutine _message: called CC=4 SCAR=15.6
81 82 my ( $err ) = @_;
82 82 ▶ my $message = blessed $err && $err->can('message')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($err) && $err->can('message') ? : 82 0 82
Condition33%1/3
builtin::blessed($err) and $err->can("message")
!ll&&!rl&&r
0082
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
83 ? $err->message
84 : ( "$err" =~ s/ at \S+ line \d+\.?\n?\z//r );
85 # Text::Nunjucks repeats the root template around this parser error in an include; Nunjucks
86 # reports that root once, then the included template. Keep the public RenderError byte-identical.
87 82 ▶ $message =~ s/\A(\([^\n]+\))\n\s*Template render error: \1\n/$1\n/
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /\n\s*unexpected token: %}\z/ 0 82 82
88 if $message =~ /\n\s*unexpected token: %}\z/;
89 82 return $message;
90 }
91  
92 # The item's output: [ [ install path, bytes ], ... ] in the item's order. `X.njk` is rendered and
93 # installed as `X`, encoded as UTF-8; every other file is copied byte for byte, so a skill that
94 # uses `{{ }}` itself stays intact.
95 5427 ▶sub build {
Subroutine build: called CC=10 SCAR=23.4
Pod documented
96 5427 my ( $item, $source_dir, $context ) = @_;
97 5427 my ( $env, $vars, $template_context, @out );
98 5427 for my $file ( @{ $item->{files} } ) {
99 5609 ▶ unless ( $file =~ /\.njk\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file =~ /\.njk\z/) 5000 609 5609
100 5000 push @out, [ install_path( $item, $file ), path( encode_text( path_join( $source_dir, $file ) ) )->slurp_raw ];
101 5000 next;
102 }
103 609 ▶ $env //= _env($source_dir);
Condition66%2/3
$env //= _env($source_dir)
l!l&&r!l&&!r
825270
MC/DC0%0/2
$env //= _env($source_dir)
$env _env($source_dir)
Truth table66%2/3
A //= B
A= $envB= _env($source_dir)
ABresult
1X1
011
000
104 609 ▶ $template_context //= ref $context eq 'CODE' ? $context->() : $context;
Branch: 1 branch
BranchTrueFalseTotal
ref $context eq 'CODE' ? : 527 0 527
Condition100%3/3
$template_context //= ref $context eq "CODE" ? &$context() : $context
l!l&&r!l&&!r
825261
MC/DC100%2/2
$template_context //= ref $context eq "CODE" ? &$context() : $context
$template_context ref $context eq "CODE" ? &$context() : $context
Truth table100%3/3
A //= B
A= $template_contextB= ref $context eq "CODE" ? &$context() : $context
ABresult
1X1
011
000
105 567 ▶ $vars //= _template_context($template_context);
Condition66%2/3
$vars //= _template_context($template_context)
l!l&&r!l&&!r
824850
MC/DC0%0/2
$vars //= _template_context($template_context)
$vars _template_context($template_context)
Truth table66%2/3
A //= B
A= $varsB= _template_context($template_context)
ABresult
1X1
011
000
106 567 my $rendered;
107 567 ▶ unless ( eval { $rendered = $env->render( $file, $vars ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $rendered = $env->render($file, $vars); 1 } }) 82 485 567
108 82 my $err = $@;
109 82 ▶ die $err if blessed $err && $err->isa('Skilletor::Error::Render');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa("Skilletor::Error::Render") 0 82 82
Condition33%1/3
builtin::blessed($err) and $err->isa("Skilletor::Error::Render")
!ll&&!rl&&r
0820
MC/DC0%0/2
builtin::blessed($err) and $err->isa("Skilletor::Error::Render")
builtin::blessed($err) $err->isa("Skilletor::Error::Render")
Truth table33%1/3
A and B
A= builtin::blessed($err)B= $err->isa("Skilletor::Error::Render")
ABresult
0X0
100
111
110 82 Skilletor::Error::Render->throw( $file.': '._message($err) );
111 }
112 485 push @out, [ install_path( $item, $file =~ s/\.njk\z//r ), encode_text($rendered) ];
113 }
114 5303 return \@out;
115 }
116  
117 # The install paths of the item's files whose source file has the owner's executable bit: a
118 # template passes its bit to what it renders.
119 3260 ▶sub executables {
Subroutine executables: called CC=4 SCAR=13.9
Pod documented
120 3260 my ( $item, $source_dir ) = @_;
121 3260 my %out;
122 3260 for my $file ( @{ $item->{files} } ) {
123 3360 my @st = stat encode_text( path_join( $source_dir, $file ) );
124 3360 ▶ die 'stat '.path_join( $source_dir, $file ).': '.$!."\n" unless @st;
Branch: 1 branch
BranchTrueFalseTotal
unless @st 0 3360 3360
125 3360 ▶ next unless is_executable( $st[2] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_executable($st[2]) 3319 41 3360
126 41 $out{ install_path( $item, $file =~ s/\.njk\z//r ) } = 1;
127 }
128 3260 return \%out;
129 }
130  
131 # A leading YAML frontmatter block (after optional whitespace), closed by `---`: the reference's
132 # FRONTMATTER, with JavaScript's \s.
133 400 my $WS = js_ws_re();
134 400 my $FRONTMATTER = qr/\A$WS*---[ \t]*\r?\n(?:.*?\r?\n)?---[ \t]*(?:\r?\n|\z)/s;
135  
136 # Does the item's main file render to nothing but whitespace once a leading frontmatter block is
137 # removed (spec §5)? Then the item does not apply here. A non-template main file is never empty.
138 5221 ▶sub renders_empty {
Subroutine renders_empty: called CC=8 SCAR=21.0
Pod documented
139 5221 my ( $item, $output ) = @_;
140 my $src = $item->{type} eq 'skill'
141 ? path_join( $item->{dir} // 'skills/'.$item->{name}, 'SKILL.md' )
142 5221 ▶ : path_join( $item->{dir} // $item->{type}.'s', $item->{name}.'.md' );
Branch: 1 branch
BranchTrueFalseTotal
$item->{'type'} eq 'skill' ? : 3260 1961 5221
Condition50%2/4
$item->{"dir"} // 'skills/' . $item->{"name"}
l!l
32600
$item->{"dir"} // $item->{"type"} . 's'
l!l
01961
MC/DC0%0/1
$item->{"dir"} // 'skills/' . $item->{"name"}
$item->{"dir"}
MC/DC0%0/1
$item->{"dir"} // $item->{"type"} . 's'
$item->{"dir"}
Truth table50%1/2
A
A= $item->{"dir"}
Aresult
11
00
Truth table50%1/2
A
A= $item->{"dir"}
Aresult
11
00
143 5221 my %files = map { ( $_ => 1 ) } @{ $item->{files} };
144 5221 ▶ return 0 if !$files{ $src.'.njk' } || $files{$src};
Branch: 1 branch
BranchTrueFalseTotal
if not $files{$src . '.njk'} or $files{$src} 4900 321 5221
Condition66%2/3
not $files{$src . '.njk'} or $files{$src}
l!l&&r!l&&!r
49000321
MC/DC50%1/2
not $files{$src . '.njk'} or $files{$src}
not $files{$src . '.njk'} $files{$src}
Truth table66%2/3
not(A) or B
A= not $files{$src . '.njk'}B= $files{$src}
ABresult
1X1
011
000
145 321 my $want = install_path( $item, $src );
146 321 my ($bytes) = map { $_->[1] } grep { $_->[0] eq $want } @$output;
147 321 ▶ return 0 unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 321 321
148 321 ( my $text = decode_utf8_lossy($bytes) ) =~ s/$FRONTMATTER//;
149 321 ▶ return $text =~ /\A$WS*\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\A$WS*\z/ ? : 41 280 321
150 }
151  
152 400 1;
153  
154 __END__
« /usr/local/lib/perl5/site_perl/5.44.0/Moo.pm ↑ Summary /usr/local/lib/perl5/5.44.0/Tie/Hash.pm »