Stubb/Render.pm

filter: Statementstmt 86.0% 246 / 286 Branchbran 71.3% 97 / 136 Conditioncond 82.5% 33 / 40 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 82.9% 34 / 41 Podpod 100% 17 / 17 totaltotal 81.2% 443 / 545 CC 90 SCAR 26.2
CC
90
Cov
81%
_render_file
31.5
_render_dir
28.6
_file_targets
13.0
CRAP 13.8
« Stubb.pm ↑ Summary Stubb/Home.pm »
1 package File::Stubb::Render;
2 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 our $VERSION = '0.05';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # TODO: '?' sigil for '#' and '$' restricted targets
12 # TODO: Set '$_' in Perl and shell substs to rendered file
13  
14 3 ▶my $ILLEGAL_PATH_RX = $^O eq 'MSWin32'
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
15 ? qr/[<>:"\/\\|?*]/
16 : qr/\//;
17  
18 3 ▶my $PATH_SEP = $^O eq 'MSWin32' ? '\\' : '/';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
19  
20 3 our $SUBST_TARGET_NAME_RX = qr/[a-zA-Z0-9_]+/;
21  
22 3 my $STDOUT_PATH = '-';
23  
24 3 my $SUBST_TARGET_RX = qr{
25 (?<NONTARGET> (?<!\\)\\\^\^) |
26 (?<SIGIL> (?: \\? [\?\$\#]) | \\\\)?
27 \^\^
28 \s* (?<TARGET> .*?) \s*
29 \^\^
30 }x;
31  
32 # Path substitution targets only contain basic targets
33 3 my $SUBST_TARGET_PATH_RX = qr{
34 \^\^
35 \s* (?<TARGET> $SUBST_TARGET_NAME_RX) \s*
36 \^\^
37 }x;
38  
39 18 ▶sub _dir {
Subroutine _dir: called CC=4 SCAR=13.9
40  
41 18 my ($dir, $hidden) = @_;
42 18 ▶ $hidden //= 0;
Condition50%1/2
$hidden //= 0
l!l
180
MC/DC0%0/1
$hidden //= 0
$hidden
Truth table50%1/2
A
A= $hidden
Aresult
11
00
43  
44 18 ▶ opendir my $dh, $dir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Failed to open $dir as a directory: $!\n" 18 0 18
45 or die "Failed to open $dir as a directory: $!\n";
46 my @files =
47 sort
48 18 grep { ! /^\.\.?$/ }
49 readdir $dh;
50 18 closedir $dh;
51  
52 18 ▶ unless ($hidden) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hidden) 12 6 18
53 12 @files = grep { ! /^\./ } @files;
54 }
55  
56 18 return map { File::Spec->catfile($dir, $_) } @files;
57  
58 }
59  
60 4 ▶sub _read_json {
Subroutine _read_json: called CC=9 SCAR=22.7
61  
62 4 my ($self, $file) = @_;
63  
64 4 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Failed to open $file for reading: $!\n" 4 0 4
65 or die "Failed to open $file for reading: $!\n";
66 4 my $json = do { local $/ = undef; <$fh> };
67 4 close $fh;
68  
69 4 my $ref = decode_json($json);
70  
71 4 ▶ unless (ref $ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref eq "HASH") 0 4 4
72 0 die "Invalid .stubb.json\n";
73 }
74  
75 4 ▶ if ($self->{ Defaults } and ref $ref->{ defaults } eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Defaults"} and ref $ref->{"defaults"} eq "HASH") 2 2 4
Condition66%2/3
$self->{"Defaults"} and ref $ref->{"defaults"} eq "HASH"
!ll&&!rl&&r
202
MC/DC50%1/2
$self->{"Defaults"} and ref $ref->{"defaults"} eq "HASH"
$self->{"Defaults"} ref $ref->{"defaults"} eq "HASH"
Truth table66%2/3
A and B
A= $self->{"Defaults"}B= ref $ref->{"defaults"} eq "HASH"
ABresult
0X0
100
111
76 2 for my $k (keys %{ $ref->{ defaults } }) {
77 6 $self->{ Subst }{ $k } = $ref->{ defaults }{ $k };
78 }
79 }
80  
81 4 ▶ if (exists $ref->{ render_hidden }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{"render_hidden"}) 4 0 4
82 4 $self->{ Hidden } = !! $ref->{ render_hidden };
83 }
84  
85 4 ▶ if (exists $ref->{ follow_symlinks }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{"follow_symlinks"}) 4 0 4
86 4 $self->{ FollowLink } = !! $ref->{ follow_symlinks };
87 }
88  
89 4 ▶ if (exists $ref->{ copy_perms }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{"copy_perms"}) 4 0 4
90 4 $self->{ CopyPerms } = !! $ref->{ copy_perms };
91 }
92  
93 4 return 1;
94  
95 }
96  
97 4 ▶sub _pl_subst {
Subroutine _pl_subst: called CC=2 SCAR=6.9
98  
99 4 my ($self, $pl) = @_;
100  
101 4 local %_ = %{ $self->{ Subst } };
102 4 ▶ return eval("{ no strict; no warnings; $pl }") // '';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition100%2/2
eval "{ no strict; no warnings; $pl }" // ""
l!l
31
MC/DC100%1/1
eval "{ no strict; no warnings; $pl }" // ""
eval "{ no strict; no warnings; $pl }"
Truth table100%2/2
A
A= eval "{ no strict; no warnings; $pl }"
Aresult
11
00
103  
104 }
105  
106 4 ▶sub _qx_subst {
Subroutine _qx_subst: called CC=2 SCAR=6.9
107  
108 4 my ($self, $qx) = @_;
109  
110 4 local %ENV = %ENV;
111 4 for my $k (%{ $self->{ Subst } }) {
112 24 $ENV{ $k } = $self->{ Subst }{ $k };
113 }
114  
115 4 my $rt = qx/$qx/;
116 4 chomp $rt;
117 4 return $rt;
118  
119 }
120  
121 0 ▶sub _render_link {
Subroutine _render_link: not called CC=2 SCAR=17.9
122  
123 0 my ($self, $link, $out) = @_;
124  
125 0 my $to = $self->render_path(readlink $link);
126  
127 0 ▶ symlink $to, $out
Branch: 1 branch
BranchTrueFalseTotal
symlink $to, $out or die "Failed to symlink $to to ${out}: $!\n" 0 0 0
128 or die "Failed to symlink $to to $out: $!\n";
129  
130 0 return $out;
131  
132 }
133  
134 14 ▶sub _render_dir {
Subroutine _render_dir: called CC=15 SCAR=33.5
135  
136 14 my ($self, $template, $out) = @_;
137  
138 14 ▶ if ($out eq $STDOUT_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out eq $STDOUT_PATH) 0 14 14
139 0 die "Cannot render directory to stdout\n";
140 }
141  
142 14 my @created;
143  
144 14 ▶ mkdir $out or die "Failed to mkdir $out: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $out or die "Failed to mkdir ${out}: $!\n" 14 0 14
145 14 ▶ if ($self->{ CopyPerms }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CopyPerms"}) 6 8 14
146 6 chmod((stat($template))[2] & 0777, $out);
147 }
148 14 push @created, $out;
149  
150 14 for my $f (_dir($template, $self->{ Hidden })) {
151  
152 17 my $bn = basename($f);
153  
154 17 ▶ next if $bn eq '.stubb.json';
Branch: 1 branch
BranchTrueFalseTotal
if $bn eq ".stubb.json" 2 15 17
155  
156 15 my $o = File::Spec->catfile(
157 $out,
158 $self->render_path($bn)
159 );
160  
161 15 my @c;
162  
163 15 eval {
164 15 ▶ if (-d $f) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $f) { } 7 8 15
elsif (not $self->{'FollowLink'} and -l $f) { } 0 8 8
165 7 @c = $self->_render_dir($f, $o);
166 ▶ } elsif (!$self->{ FollowLink } and -l $f) {
Condition66%2/3
not $self->{"FollowLink"} and -l $f
!ll&&!rl&&r
440
MC/DC0%0/2
not $self->{"FollowLink"} and -l $f
not $self->{"FollowLink"} -l $f
Truth table66%2/3
not(A) and B
A= not $self->{"FollowLink"}B= -l $f
ABresult
0X0
100
111
167 0 @c = $self->_render_link($f, $o);
168 } else {
169 8 @c = $self->_render_file($f, $o);
170 }
171 };
172  
173 15 ▶ if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 15 15
174 0 for my $del (reverse @created) {
175 0 ▶ if (-d $del) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $del) { } 0 0 0
elsif (-f $del) { } 0 0 0
176 0 ▶ rmdir $del or next;
Branch: 1 branch
BranchTrueFalseTotal
rmdir $del or next 0 0 0
177 } elsif (-f $del) {
178 0 ▶ unlink $del or next;
Branch: 1 branch
BranchTrueFalseTotal
unlink $del or next 0 0 0
179 }
180 }
181 0 die $@;
182 }
183  
184 15 push @created, @c;
185  
186 }
187  
188 14 return @created;
189  
190 }
191  
192 23 ▶sub _render_file {
Subroutine _render_file: called CC=25 SCAR=34.5
193  
194 23 my ($self, $template, $out) = @_;
195  
196 23 ▶ open my $rh, '<', $template
Branch: 1 branch
BranchTrueFalseTotal
open my $rh, "<", $template or die "Failed to open $template for reading: $!\n" 23 0 23
197 or die "Failed to open $template for reading: $!\n";
198 23 binmode $rh;
199  
200 23 my $wh;
201 23 ▶ if ($out eq $STDOUT_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out eq $STDOUT_PATH) { } 0 23 23
202 0 $wh = *STDOUT;
203 } else {
204 23 ▶ open $wh, '>', $out or do {
Branch: 1 branch
BranchTrueFalseTotal
open $wh, ">", $out or do { close $rh; die "Failed to open $out for writing: $!\n" } 23 0 23
205 0 close $rh;
206 0 die "Failed to open $out for writing: $!\n";
207 };
208 }
209 23 binmode $wh;
210  
211 23 my $ln = 0;
212 23 eval {
213 23 ▶ while (my $l = readline $rh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $rh)) 60 23 83
214 60 $ln++;
215 60 $l =~ s{($SUBST_TARGET_RX)}{
216 90 my $m = $1;
217 90 my %c = %+;
218 90 my $repl = '';
219 {
220 90 ▶ if (defined $c{ NONTARGET }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c{"NONTARGET"}) 10 80 90
221 10 $repl = '^^';
222 10 last;
223 }
224 80 ▶ if (defined $c{ SIGIL } and $c{ SIGIL } =~ /^\\/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c{"SIGIL"} and $c{"SIGIL"} =~ /^\\/u) 12 68 80
Condition100%3/3
defined $c{"SIGIL"} and $c{"SIGIL"} =~ /^\\/u
!ll&&!rl&&r
422612
MC/DC100%2/2
defined $c{"SIGIL"} and $c{"SIGIL"} =~ /^\\/u
defined $c{"SIGIL"} $c{"SIGIL"} =~ /^\\/u
Truth table100%3/3
A and B
A= defined $c{"SIGIL"}B= $c{"SIGIL"} =~ /^\\/u
ABresult
0X0
100
111
225 # Copy escaped sigil
226 12 $repl .= substr $c{ SIGIL }, 1, 1;
227 12 $m = substr $m, length $c{ SIGIL };
228 }
229 80 ▶ if (not defined $c{ SIGIL } or $c{ SIGIL } =~ /^\\/) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $c{'SIGIL'} or $c{'SIGIL'} =~ /^\\/u) { } 54 26 80
elsif ($c{'SIGIL'} eq '?') { } 6 20 26
elsif ($c{'SIGIL'} eq '$') { } 12 8 20
elsif ($c{'SIGIL'} eq '#') { } 8 0 8
Condition100%3/3
not defined $c{"SIGIL"} or $c{"SIGIL"} =~ /^\\/u
l!l&&r!l&&!r
421226
MC/DC100%2/2
not defined $c{"SIGIL"} or $c{"SIGIL"} =~ /^\\/u
not defined $c{"SIGIL"} $c{"SIGIL"} =~ /^\\/u
Truth table100%3/3
not(A) or B
A= not defined $c{"SIGIL"}B= $c{"SIGIL"} =~ /^\\/u
ABresult
1X1
011
000
230 54 my ($targ, $def) = split /\s*\/\/\s*/, $c{ TARGET };
231 54 ▶ if ($targ !~ /^$SUBST_TARGET_NAME_RX$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($targ =~ /^$File::Stubb::Render::SUBST_TARGET_NAME_RX$/u) 0 54 54
232 0 die "$template line $ln: invalid target\n";
233 }
234 $repl .= exists $self->{ Subst }{ $targ }
235 54 ▶ ? $self->{ Subst }{ $targ }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'Subst'}{$targ} ? : 27 27 54
Condition66%2/3
$def // $m
l!l&&r!l&&!r
6210
MC/DC0%0/2
$def // $m
$def $m
Truth table66%2/3
A // B
A= $defB= $m
ABresult
1X1
011
000
236 : $def // $m;
237 } elsif ($c{ SIGIL } eq '?') {
238 6 ▶ if ($c{ TARGET } !~ /^$SUBST_TARGET_NAME_RX$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c{"TARGET"} =~ /^$File::Stubb::Render::SUBST_TARGET_NAME_RX$/u) 0 6 6
239 0 die "$template line $ln: invalid target\n";
240 }
241 $repl = exists $self->{ Subst }{ $c{ TARGET } }
242 ? $self->{ Subst }{ $c{ TARGET } }
243 6 ▶ : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'Subst'}{$c{'TARGET'}} ? : 5 1 6
244 } elsif ($c{ SIGIL } eq '$') {
245 $repl = $self->{ Restricted }
246 ? $m
247 12 ▶ : $self->_pl_subst($c{ TARGET });
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Restricted'} ? : 8 4 12
248 } elsif ($c{ SIGIL } eq '#') {
249 $repl = $self->{ Restricted }
250 ? $m
251 8 ▶ : $self->_qx_subst($c{ TARGET });
Branch: 1 branch
BranchTrueFalseTotal
$self->{'Restricted'} ? : 4 4 8
252 }
253 }
254 90 $repl;
255 }ge;
256 60 print { $wh } $l;
257 }
258 };
259  
260 23 ▶ if ($@ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ ne "") 0 23 23
261 0 close $rh;
262 0 ▶ if ($out ne $STDOUT_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out ne $STDOUT_PATH) 0 0 0
263 0 close $wh;
264 0 unlink $out;
265 }
266 0 die $@;
267 }
268  
269 23 close $rh;
270 23 ▶ close $wh unless $out eq $STDOUT_PATH;
Branch: 1 branch
BranchTrueFalseTotal
unless $out eq $STDOUT_PATH 23 0 23
271  
272 23 ▶ if ($self->{ CopyPerms } and $out ne $STDOUT_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CopyPerms"} and $out ne $STDOUT_PATH) 4 19 23
Condition66%2/3
$self->{"CopyPerms"} and $out ne $STDOUT_PATH
!ll&&!rl&&r
1904
MC/DC50%1/2
$self->{"CopyPerms"} and $out ne $STDOUT_PATH
$self->{"CopyPerms"} $out ne $STDOUT_PATH
Truth table66%2/3
A and B
A= $self->{"CopyPerms"}B= $out ne $STDOUT_PATH
ABresult
0X0
100
111
273 4 chmod((stat($template))[2] & 0777, $out);
274 }
275  
276 23 return $out;
277  
278 }
279  
280 6 ▶sub _path_targets {
Subroutine _path_targets: called CC=2 SCAR=6.9
281  
282 6 my ($self, $path) = @_;
283  
284 6 my %targ;
285  
286 6 ▶ while (my $m = $path =~ m/($SUBST_TARGET_PATH_RX)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $path =~ /($SUBST_TARGET_PATH_RX)/gu) 6 6 12
287 6 $targ{ $+{ TARGET } } = 1;
288 }
289  
290 6 return sort keys %targ;
291  
292 }
293  
294 13 ▶sub _file_targets {
Subroutine _file_targets: called CC=13 SCAR=25.7
295  
296 13 my ($self, $template, $targets) = @_;
297  
298 13 ▶ open my $fh, '<', $template
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $template or die "Failed to open $template for reading: $!\n" 13 0 13
299 or die "Failed to open $template for reading: $!\n";
300 13 binmode $fh;
301  
302 13 ▶ while (my $l = readline $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fh)) 36 13 49
303 36 ▶ while (my $m = $l =~ m/($SUBST_TARGET_RX)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $l =~ /($SUBST_TARGET_RX)/gu) 50 36 86
304 50 my %c = %+;
305 50 ▶ if (defined $c{ NONTARGET }) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $c{'NONTARGET'}) { } 10 40 50
elsif (not defined $c{'SIGIL'} or $c{'SIGIL'} eq '?' or $c{'SIGIL'} =~ /^\\/u) { } 24 16 40
elsif ($c{'SIGIL'} eq '$' and not $self->{'Restricted'}) { } 4 12 16
elsif ($c{'SIGIL'} eq '#' and not $self->{'Restricted'}) { } 4 12 16
306 10 next;
307 ▶ } elsif (not defined $c{ SIGIL } or $c{ SIGIL } eq '?' or $c{ SIGIL } =~ /^\\/) {
Condition100%6/6
not defined $c{"SIGIL"} or $c{"SIGIL"} eq "?" or $c{"SIGIL"} =~ /^\\/u
l!l&&r!l&&!r
18616
not defined $c{"SIGIL"} or $c{"SIGIL"} eq "?"
l!l&&r!l&&!r
12622
MC/DC100%3/3
not defined $c{"SIGIL"} or $c{"SIGIL"} eq "?" or $c{"SIGIL"} =~ /^\\/u
not defined $c{"SIGIL"} $c{"SIGIL"} eq "?" $c{"SIGIL"} =~ /^\\/u
Truth table100%4/4
not(A) or B or C
A= not defined $c{"SIGIL"}B= $c{"SIGIL"} eq "?"C= $c{"SIGIL"} =~ /^\\/u
ABCresult
1XX1
01X1
0011
0000
308 24 my $t = $c{ TARGET } =~ s/\s*\/\/.*$//r;
309 24 ▶ next unless $t =~ /^$SUBST_TARGET_NAME_RX$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /^$File::Stubb::Render::SUBST_TARGET_NAME_RX$/u 0 24 24
310 24 $targets->{ basic }{ $t } = 1;
311 ▶ } elsif ($c{ SIGIL } eq '$' and not $self->{ Restricted }) {
Condition100%3/3
$c{"SIGIL"} eq "\$" and not $self->{"Restricted"}
!ll&&!rl&&r
844
MC/DC100%2/2
$c{"SIGIL"} eq "\$" and not $self->{"Restricted"}
$c{"SIGIL"} eq "\$" not $self->{"Restricted"}
Truth table100%3/3
A and not(B)
A= $c{"SIGIL"} eq "\$"B= not $self->{"Restricted"}
ABresult
0X0
100
111
312 4 push @{ $targets->{ perl } }, $c{ TARGET };
313 ▶ } elsif ($c{ SIGIL } eq '#' and not $self->{ Restricted }) {
Condition100%3/3
$c{"SIGIL"} eq "#" and not $self->{"Restricted"}
!ll&&!rl&&r
444
MC/DC100%2/2
$c{"SIGIL"} eq "#" and not $self->{"Restricted"}
$c{"SIGIL"} eq "#" not $self->{"Restricted"}
Truth table100%3/3
A and not(B)
A= $c{"SIGIL"} eq "#"B= not $self->{"Restricted"}
ABresult
0X0
100
111
314 4 push @{ $targets->{ shell } }, $c{ TARGET };
315 }
316 }
317 }
318  
319 13 close $fh;
320  
321 13 return 1;
322  
323 }
324  
325 4 ▶sub _dir_targets {
Subroutine _dir_targets: called CC=8 SCAR=21.8
326  
327 4 my ($self, $template, $targets) = @_;
328  
329 4 for my $f (_dir($template, $self->{ Hidden })) {
330  
331 7 my $bn = basename($f);
332  
333 7 ▶ next if $bn eq '.stubb.json';
Branch: 1 branch
BranchTrueFalseTotal
if $bn eq ".stubb.json" 1 6 7
334  
335 6 for my $t ($self->_path_targets($bn)) {
336 6 $targets->{ basic }{ $t } = 1;
337 }
338  
339 6 ▶ if (-l $f and !$self->{ FollowLink }) {
Branch: 2 branches
BranchTrueFalseTotal
if (-l $f and not $self->{'FollowLink'}) { } 0 6 6
elsif (-d $f) { } 2 4 6
Condition33%1/3
-l $f and not $self->{"FollowLink"}
!ll&&!rl&&r
600
MC/DC0%0/2
-l $f and not $self->{"FollowLink"}
-l $f not $self->{"FollowLink"}
Truth table33%1/3
A and not(B)
A= -l $fB= not $self->{"FollowLink"}
ABresult
0X0
100
111
340 0 for my $t ($self->_path_targets(readlink $f)) {
341 0 $targets->{ basic }{ $t } = 1;
342 }
343 } elsif (-d $f) {
344 2 $self->_dir_targets($f, $targets);
345 } else {
346 4 $self->_file_targets($f, $targets);
347 }
348  
349 }
350  
351 4 return 1;
352  
353 }
354  
355 23 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
356  
357 23 my ($class, %param) = @_;
358  
359 my $self = {
360 Template => $param{ template },
361 23 Subst => {},
362 Hidden => 0,
363 FollowLink => 1,
364 CopyPerms => 0,
365 Defaults => 1,
366 IgnoreConf => 0,
367 Restricted => 0,
368 };
369  
370 23 bless $self, $class;
371  
372 23 my $json = File::Spec->catfile($self->{ Template }, '.stubb.json');
373  
374 23 ▶ if (defined $param{ ignore_config }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"ignore_config"}) 2 21 23
375 2 $self->{ IgnoreConf } = !! $param{ ignore_config };
376 }
377  
378 23 ▶ if (defined $param{ defaults }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"defaults"}) 2 21 23
379 2 $self->{ Defaults } = !! $param{ defaults };
380 }
381  
382 23 ▶ if (-f $json and !$self->{ IgnoreConf }) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $json and not $self->{"IgnoreConf"}) 4 19 23
Condition100%3/3
-f $json and not $self->{"IgnoreConf"}
!ll&&!rl&&r
1724
MC/DC100%2/2
-f $json and not $self->{"IgnoreConf"}
-f $json not $self->{"IgnoreConf"}
Truth table100%3/3
A and not(B)
A= -f $jsonB= not $self->{"IgnoreConf"}
ABresult
0X0
100
111
383 4 $self->_read_json($json);
384 }
385  
386 23 ▶ if (defined $param{ subst }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"subst"}) 11 12 23
387 11 $self->{ Subst } = $param{ subst };
388 }
389  
390 23 ▶ if (defined $param{ render_hidden }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"render_hidden"}) 2 21 23
391 2 $self->{ Hidden } = !! $param{ render_hidden };
392 }
393  
394 23 ▶ if (defined $param{ follow_symlinks }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"follow_symlinks"}) 1 22 23
395 1 $self->{ FollowLink } = !! $param{ follow_symlinks };
396 }
397  
398 23 ▶ if (defined $param{ copy_perms }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"copy_perms"}) 1 22 23
399 1 $self->{ CopyPerms } = !! $param{ copy_perms };
400 }
401  
402 23 ▶ if (defined $param{ restricted }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{"restricted"}) 3 20 23
403 3 $self->{ Restricted } = !! $param{ restricted };
404 }
405  
406 23 for my $k (keys %{ $self->{ Subst } }) {
407 36 ▶ unless ($k =~ /^$SUBST_TARGET_NAME_RX$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /^$File::Stubb::Render::SUBST_TARGET_NAME_RX$/u) 0 36 36
408 0 die "'$k' is not a valid substitution target\n";
409 }
410 }
411  
412 23 return $self;
413  
414 }
415  
416 22 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
417  
418 22 my ($self, $out) = @_;
419  
420 22 $out = $self->render_path($out);
421  
422 my @created = -d $self->{ Template }
423 ? $self->_render_dir($self->{ Template }, $out)
424 22 ▶ : $self->_render_file($self->{ Template }, $out);
Branch: 1 branch
BranchTrueFalseTotal
-d $self->{'Template'} ? : 7 15 22
425  
426 22 return @created;
427  
428 }
429  
430 11 ▶sub targets {
Subroutine targets: called CC=2 SCAR=6.9
Pod documented
431  
432 11 my ($self) = @_;
433  
434 11 my $targets = {
435 basic => {},
436 perl => [],
437 shell => [],
438 };
439  
440 11 ▶ if (-d $self->{ Template }) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'Template'}) { } 2 9 11
441 2 $self->_dir_targets($self->{ Template }, $targets);
442 } else {
443 9 $self->_file_targets($self->{ Template }, $targets);
444 }
445  
446 11 $targets->{ basic } = [ sort keys %{ $targets->{ basic } } ];
447  
448 11 return $targets;
449  
450 }
451  
452 38 ▶sub render_path {
Subroutine render_path: called CC=4 SCAR=13.9
Pod documented
453  
454 38 my ($self, $path) = @_;
455  
456 38 my @parts = split /\Q$PATH_SEP\E/, $path;
457  
458 38 for (@parts) {
459 91 s{($SUBST_TARGET_PATH_RX)}{
460 18 my $m = $1;
461 18 my %c = %+;
462 18 my $repl = '';
463 {
464 18 ▶ if (not exists $self->{ Subst }{ $c{ TARGET } }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"Subst"}{$c{"TARGET"}}) 4 14 18
465 4 $repl = $m;
466 4 last;
467 }
468 14 ▶ if ($self->{ Subst }{ $c{ TARGET } } =~ $ILLEGAL_PATH_RX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Subst"}{$c{"TARGET"}} =~ /$ILLEGAL_PATH_RX/u) 0 14 14
469 0 die "'$c{ TARGET }' path substitution would contain illegal path characters\n";
470 }
471 14 $repl = $self->{ Subst }{ $c{ TARGET } };
472 }
473 18 $repl;
474 }ge;
475 }
476  
477 38 return File::Spec->catfile(@parts);
478  
479 }
480  
481 1 ▶sub template {
Subroutine template: called CC=1 SCAR=0.0
Pod documented
482  
483 1 my ($self) = @_;
484  
485 1 return $self->{ Template };
486  
487 }
488  
489 4 ▶sub subst {
Subroutine subst: called CC=1 SCAR=0.0
Pod documented
490  
491 4 my ($self) = @_;
492  
493 4 return $self->{ Subst };
494  
495 }
496  
497 0 ▶sub set_subst {
Subroutine set_subst: not called CC=2 SCAR=17.9
Pod documented
498  
499 0 my ($self, $subst) = @_;
500  
501 0 ▶ unless (ref $subst eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $subst eq "HASH") 0 0 0
502 0 die "\$subst must be a hash ref";
503 }
504  
505 0 $self->{ Subst } = $subst;
506  
507 }
508  
509 4 ▶sub hidden {
Subroutine hidden: called CC=1 SCAR=0.0
Pod documented
510  
511 4 my ($self) = @_;
512  
513 4 return $self->{ Hidden };
514  
515 }
516  
517 0 ▶sub set_hidden {
Subroutine set_hidden: not called CC=1 SCAR=6.9
Pod documented
518  
519 0 my ($self, $hidden) = @_;
520  
521 0 $self->{ Hidden } = !! $hidden;
522  
523 }
524  
525 4 ▶sub follow_symlinks {
Subroutine follow_symlinks: called CC=1 SCAR=0.0
Pod documented
526  
527 4 my ($self) = @_;
528  
529 4 return $self->{ FollowLink };
530  
531 }
532  
533 0 ▶sub set_follow_symlinks {
Subroutine set_follow_symlinks: not called CC=1 SCAR=6.9
Pod documented
534  
535 0 my ($self, $follow) = @_;
536  
537 0 $self->{ FollowLink } = !! $follow;
538  
539 }
540  
541 4 ▶sub copy_perms {
Subroutine copy_perms: called CC=1 SCAR=0.0
Pod documented
542  
543 4 my ($self) = @_;
544  
545 4 return $self->{ CopyPerms };
546  
547 }
548  
549 0 ▶sub set_copy_perms {
Subroutine set_copy_perms: not called CC=1 SCAR=6.9
Pod documented
550  
551 0 my ($self, $copy) = @_;
552  
553 0 $self->{ CopyPerms } = !! $copy;
554  
555 }
556  
557 3 ▶sub restricted {
Subroutine restricted: called CC=1 SCAR=0.0
Pod documented
558  
559 3 my ($self) = @_;
560  
561 3 return $self->{ Restricted };
562  
563 }
564  
565 0 ▶sub set_restricted {
Subroutine set_restricted: not called CC=1 SCAR=6.9
Pod documented
566  
567 0 my ($self, $rest) = @_;
568  
569 0 $self->{ Restricted } = !! $rest;
570  
571 }
572  
573 1 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod documented
574  
575 1 my ($self) = @_;
576  
577 1 return $self->{ Defaults };
578  
579 }
580  
581 0 ▶sub ignore_config {
Subroutine ignore_config: not called CC=1 SCAR=6.9
Pod documented
582  
583 0 my ($self) = @_;
584  
585 0 return $self->{ IgnoreConf };
586  
587 }
588  
589 3 1;
590  
591 =head1 NAME
592  
593 File::Stubb::Render - Stubb template rendering class
594  
595 =head1 USAGE
596  
597 use File::Stubb::Render;
598  
599 my $render = File::Stubb::Render->new(
600 template => '/path/to/template',
601 subst => { one => 1, two => 2 },
602 );
603  
604 my @created = $render->render('/path/to/output');
605  
606 =head1 DESCRIPTION
607  
608 B<File::Stubb::Render> is a module that provides an object-oriented interface
609 to L<stubb>'s template rendering routines. This is a private module for L<stubb>.
610 For user documentation, consult the L<stubb> manual.
611  
612 =head1 METHODS
613  
614 =over 4
615  
616 =item $render = File::Stubb::Render->new(%params)
617  
618 Creates a new B<File::Stubb::Render> object from C<%params>. The following are
619 valid parameters:
620  
621 =over 4
622  
623 =item template
624  
625 Path to template file. Required
626  
627 =item subst
628  
629 Hash ref of substitution parameters.
630  
631 =item render_hidden
632  
633 Boolean determining whether to render hidden files from a template directory.
634 Defaults to false.
635  
636 =item follow_symlinks
637  
638 Boolean determining whether to render the file symlinks point to, or to copy
639 the symlinks themselves in template directories. Defaults to true.
640  
641 =item copy_perms
642  
643 Boolean determining whether to copy the permissions of a template file when
644 rendering. Defaults to false.
645  
646 =item restricted
647  
648 Boolean determining whether to render files in a "restricted" mode. When
649 restricted, Perl targets and shell targets will not be rendered and left as-is.
650  
651 =item defaults
652  
653 Boolean determining whether to use any default substitution parameters
654 provided by a template's F<.stubb.json>.
655  
656 =item ignore_config
657  
658 Boolean determining whether a template's config file should be ignored during
659 initialization or not. Defaults to false.
660  
661 =back
662  
663 =item @created = $render->render($output)
664  
665 Render template to C<$output>. Returns a list of created files.
666  
667 For documentation on how L<stubb> renders files, consult the L<stubb> manual.
668  
669 =item $targets = $render->targets()
670  
671 Returns a hash ref list of targets in the object's template. The hash ref will
672 look something like this:
673  
674 {
675 basic => [ ... ],
676 perl => [ ... ],
677 shell => [ ... ],
678 }
679  
680 =item $new = $render->render_path($path)
681  
682 Renders C<$path> as a path name.
683  
684 =item $template = $render->template()
685  
686 Get the path to C<$render>'s template file.
687  
688 =item $subst = $render->subst()
689  
690 =item $render->set_subst($subst)
691  
692 Getter/setter for C<$render>'s substitution parameter hash ref.
693  
694 =item $hidden = $render->hidden()
695  
696 =item $render->set_hidden($hidden)
697  
698 Getter/setter for C<$render>'s render hidden files flag.
699  
700 =item $follow = $render->follow_symlinks()
701  
702 =item $render->set_follow_symlinks($follow)
703  
704 Getter/setter for C<$render>'s follow symlinks flag.
705  
706 =item $copy = $render->copy_perms
707  
708 =item $render->set_copy_perms($copy)
709  
710 Getter/setter for C<$render>'s copy perms flag.
711  
712 =item $rest = $render->restricted
713  
714 =item $render->set_restricted($rest)
715  
716 Getter/setter for C<$render>'s restricted flag.
717  
718 =item $def = $render->defaults()
719  
720 Getter for C<$render>'s defaults flag.
721  
722 =item $ign = $render->ignore_config()
723  
724 Getter for C<$render>'s ignore_config flag.
725  
726 =back
727  
728 =head1 AUTHOR
729  
730 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
731  
732 This project's source can be found on its
733 L<Codeberg page|https://codeberg.org/1-1sam/stubb.git>. Comments and pull
734 requests are welcome!
735  
736 =head1 COPYRIGHT
737  
738 Copyright (C) 2025-2026 Samuel Young
739  
740 This program is free software: you can redistribute it and/or modify
741 it under the terms of the GNU General Public License as published by
742 the Free Software Foundation, either version 3 of the License, or
743 (at your option) any later version.
744  
745 =head1 SEE ALSO
746  
747 L<stubb>
748  
749 =cut
750  
751 # vim: expandtab shiftwidth=4
« Stubb.pm ↑ Summary Stubb/Home.pm »