Overlay.pm

filter: Statementstmt 97.6% 83 / 85 Branchbran 87.5% 21 / 24 Conditioncond 75.0% 9 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 92.5% 137 / 148 SCAR 29.5
CC
19
Cov
93%
_resolve
7.3
__ANON__
4.1
overlay
4.0
CRAP 19.1
« Resolver.pm ↑ Summary Transformer.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Template::Overlay;
5 1 $Template::Overlay::VERSION = '1.14';
6 # ABSTRACT: A powerful, and simple, library for resolving placeholders in templated files
7 # PODNAME: Template::Resolver
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my $logger = Log::Any->get_logger();
19  
20 sub new {
21 8 ▶ return bless( {}, shift )->_init(@_);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 }
23  
24 sub _init {
25 8 ▶ my ( $self, $base, $resolver, %options ) = @_;
Subroutine _init: called CC=1 SCAR=0.0
26  
27 8 $self->{base} = File::Spec->rel2abs($base);
28 8 $self->{resolver} = $resolver;
29 8 $self->{key} = $options{key};
30  
31 8 $logger->debug( 'new overlay [', $self->{base}, ']' );
32  
33 8 return $self;
34 }
35  
36 sub _overlay_files {
37 8 ▶ my ( $self, $overlays ) = @_;
Subroutine _overlay_files: called CC=3 SCAR=11.0
38  
39 8 my %overlay_files = ();
40 8 ▶ foreach my $overlay ( ref($overlays) eq 'ARRAY' ? @$overlays : ($overlays) ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $overlays eq 'ARRAY' ? : 2 6 8
41 10 $overlay = File::Spec->rel2abs($overlay);
42 10 my $base_path_length = length($overlay);
43 find(
44 sub {
45 26 ▶ if ( -f $File::Find::name && $_ !~ /~$/ && $_ !~ /^\..+\.swp$/ ) {
Subroutine __ANON__: called CC=4 SCAR=14.1
Branch: 1 branch
BranchTrueFalseTotal
if (-f $File::Find::name and not $_ =~ /~$/ and not $_ =~ /^\..+\.swp$/) 10 16 26
Condition66%4/6
-f $File::Find::name and not $_ =~ /~$/ and not $_ =~ /^\..+\.swp$/
!ll&&!rl&&r
16010
-f $File::Find::name and not $_ =~ /~$/
!ll&&!rl&&r
16010
MC/DC33%1/3
-f $File::Find::name and not $_ =~ /~$/ and not $_ =~ /^\..+\.swp$/
-f $File::Find::name not $_ =~ /~$/ not $_ =~ /^\..+\.swp$/
Truth table50%2/4
A and not(B) and not(C)
A= -f $File::Find::nameB= not $_ =~ /~$/C= not $_ =~ /^\..+\.swp$/
ABCresult
0XX0
10X0
1100
1111
46 10 my $relative = _relative_path( $File::Find::name, $base_path_length );
47 10 $overlay_files{$relative} = $File::Find::name;
48 }
49 },
50 10 $overlay
51 );
52 }
53  
54 8 return %overlay_files;
55 }
56  
57 sub overlay {
58 8 ▶ my ( $self, $overlays, %options ) = @_;
Subroutine overlay: called CC=4 SCAR=13.9
Pod documented
59  
60 8 my %overlay_files = $self->_overlay_files($overlays);
61 8 my $destination = $self->{base};
62 8 ▶ if ( $options{to} && $options{to} ne $self->{base} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"to"} and $options{"to"} ne $self->{"base"}) 5 3 8
Condition66%2/3
$options{"to"} and $options{"to"} ne $self->{"base"}
!ll&&!rl&&r
035
MC/DC50%1/2
$options{"to"} and $options{"to"} ne $self->{"base"}
$options{"to"} $options{"to"} ne $self->{"base"}
Truth table66%2/3
A and B
A= $options{"to"}B= $options{"to"} ne $self->{"base"}
ABresult
0X0
100
111
63 5 $destination = File::Spec->rel2abs( $options{to} );
64 5 my $base_path_length = length( File::Spec->rel2abs( $self->{base} ) );
65 find(
66 sub {
67 22 ▶ my $relative = _relative_path( $File::Find::name, $base_path_length );
Subroutine __ANON__: called CC=4 SCAR=13.9
68 22 ▶ if ( -d $File::Find::name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $File::Find::name) 9 13 22
69 9 make_path( File::Spec->catdir( $destination, $relative ) );
70 }
71 22 ▶ if ( -f $File::Find::name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $File::Find::name) 13 9 22
72 13 my $template = delete( $overlay_files{$relative} );
73 13 my $file = File::Spec->catfile( $destination, $relative );
74 13 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) { } 7 6 13
75 7 $self->_resolve( $template, $file, $options{resolver} );
76 }
77 else {
78 6 copy( $_, $file );
79 }
80 }
81 },
82 $self->{base}
83 5 );
84 }
85 8 foreach my $relative ( keys(%overlay_files) ) {
86 3 my $file = File::Spec->catfile( $destination, $relative );
87 3 make_path( ( File::Spec->splitpath($file) )[1] );
88 3 $self->_resolve( $overlay_files{$relative}, $file, $options{resolver} );
89 }
90 }
91  
92 sub _relative_path {
93 32 ▶ my ( $path, $base_path_length ) = @_;
Subroutine _relative_path: called CC=2 SCAR=6.9
94 return
95 32 ▶ length($path) == $base_path_length
Branch: 1 branch
BranchTrueFalseTotal
length $path == $base_path_length ? : 5 27 32
96 ? ''
97 : substr( $File::Find::name, $base_path_length + 1 );
98 }
99  
100 sub _resolve {
101 10 ▶ my ( $self, $template, $file, $resolver ) = @_;
Subroutine _resolve: called CC=7 SCAR=19.9
102  
103 10 ▶ return if ( $resolver && &$resolver( $template, $file ) );
Branch: 1 branch
BranchTrueFalseTotal
if $resolver and &$resolver($template, $file) 1 9 10
Condition100%3/3
$resolver and &$resolver($template, $file)
!ll&&!rl&&r
811
MC/DC100%2/2
$resolver and &$resolver($template, $file)
$resolver &$resolver($template, $file)
Truth table100%3/3
A and B
A= $resolverB= &$resolver($template, $file)
ABresult
0X0
100
111
104  
105 9 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 9 9
106 0 $logger->debugf(
107 '[%s] already exists, deleting to ensure creation with proper permissions', $file );
108 0 unlink($file);
109 }
110  
111 9 my $mode = stat($template)->mode() & 07777; ## no critic
112 9 $logger->infof( 'processing [%s] -> [%04o] [%s]', $template, $mode, $file );
113 9 ▶ sysopen( my $handle, $file, O_CREAT | O_TRUNC | O_WRONLY, $mode )
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $handle, $file, 577, $mode or croak("open $file failed: $!") 0 9 9
114 || croak("open $file failed: $!");
115 9 eval {
116 print( $handle $self->{resolver}->resolve(
117 filename => $template,
118 9 ▶ ( $self->{key} ? ( key => $self->{key} ) : () )
Branch: 1 branch
BranchTrueFalseTotal
$self->{'key'} ? : 8 1 9
119 )
120 );
121 };
122 9 my $error = $@;
123 9 close($handle);
124 9 ▶ croak($error) if ($error);
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 9 9
125 }
126  
127 1 1;
128  
129 __END__
« Resolver.pm ↑ Summary Transformer.pm »