| 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.05'; | |||||||||||||||||||||||||||
| 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(cp); | ||||||||||||||||||||||||||
|
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 ':mode'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Log::Any; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Template::Overlay; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | my $logger = Log::Any->get_logger(); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub new { | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | return bless( {}, shift )->_init(@_); | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _init { | ||||||||||||||||||||||||||||
| 26 | 4 | ▶ | my ( $self, $base, $resolver, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | $self->{base} = File::Spec->rel2abs($base); | |||||||||||||||||||||||||||
| 29 | 4 | $self->{resolver} = $resolver; | |||||||||||||||||||||||||||
| 30 | 4 | $self->{key} = $options{key}; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 4 | $logger->debug( 'new overlay [', $self->{base}, ']' ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | return $self; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub _overlay_files { | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | my ( $self, $overlays ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_overlay_files: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | my %overlay_files = (); | |||||||||||||||||||||||||||
| 41 | 4 | ▶ | foreach my $overlay ( ref($overlays) eq 'ARRAY' ? @$overlays : ($overlays) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 6 | $overlay = File::Spec->rel2abs($overlay); | |||||||||||||||||||||||||||
| 43 | find( | ||||||||||||||||||||||||||||
| 44 | sub { | ||||||||||||||||||||||||||||
| 45 | 15 | ▶ | if ( -f $File::Find::name && $File::Find::name =~ /^$overlay\/(.*[^~])$/ ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
-f $File::Find::name and $File::Find::name =~ m[^$overlay/(.*[^~])$]
MC/DC50%1/2
-f $File::Find::name and $File::Find::name =~ m[^$overlay/(.*[^~])$]
-f $File::Find::name
$File::Find::name =~ m[^$overlay/(.*[^~])$]
Truth table66%2/3
A and B
A= -f $File::Find::nameB= $File::Find::name =~ m[^$overlay/(.*[^~])$]
| |||||||||||||||||||||||||||||
| 46 | 6 | $overlay_files{$1} = $File::Find::name; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 6 | $overlay | |||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | return %overlay_files; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub overlay { | ||||||||||||||||||||||||||||
| 57 | 4 | ▶ | my ( $self, $overlays, %options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
overlay: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | my %overlay_files = $self->_overlay_files($overlays); | |||||||||||||||||||||||||||
| 60 | 4 | my $destination = $self->{base}; | |||||||||||||||||||||||||||
| 61 | 4 | ▶ | if ( $options{to} && $options{to} ne $self->{base} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$options{"to"} and $options{"to"} ne $self->{"base"}
MC/DC0%0/2
$options{"to"} and $options{"to"} ne $self->{"base"}
$options{"to"}
$options{"to"} ne $self->{"base"}
Truth table33%1/3
A and B
A= $options{"to"}B= $options{"to"} ne $self->{"base"}
| |||||||||||||||||||||||||||||
| 62 | 4 | $destination = File::Spec->rel2abs( $options{to} ); | |||||||||||||||||||||||||||
| 63 | 4 | my $length = length( $self->{base} ); | |||||||||||||||||||||||||||
| 64 | find( | ||||||||||||||||||||||||||||
| 65 | sub { | ||||||||||||||||||||||||||||
| 66 | 20 | ▶ | my $relative = substr( $File::Find::name, $length ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 67 | 20 | ▶ | if ( -d $File::Find::name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 8 | make_path( File::Spec->catdir( $destination, $relative ) ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 20 | ▶ | if ( -f $File::Find::name ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 12 | my $template = delete( $overlay_files{$relative} ); | |||||||||||||||||||||||||||
| 72 | 12 | my $file = File::Spec->catfile( $destination, $relative ); | |||||||||||||||||||||||||||
| 73 | 12 | ▶ | if ($template) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $self->_resolve( $template, $file ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | 12 | cp( $_, $file ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||
| 81 | $self->{base} | ||||||||||||||||||||||||||||
| 82 | 4 | ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 4 | foreach my $relative ( keys(%overlay_files) ) { | |||||||||||||||||||||||||||
| 85 | 6 | my $file = File::Spec->catfile( $destination, $relative ); | |||||||||||||||||||||||||||
| 86 | 6 | $self->_resolve( $overlay_files{$relative}, $file ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub _resolve { | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | my ( $self, $template, $file ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_resolve: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | $logger->info( 'processing [', $template, '] -> [', $file, ']' ); | |||||||||||||||||||||||||||
| 94 | 6 | ▶ | open( my $handle, '>', $file ) || croak("open $file failed: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 6 | eval { | |||||||||||||||||||||||||||
| 96 | print( $handle $self->{resolver}->resolve( | ||||||||||||||||||||||||||||
| 97 | filename => $template, | ||||||||||||||||||||||||||||
| 98 | 6 | ▶ | ( $self->{key} ? ( key => $self->{key} ) : () ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | ) | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||
| 102 | 6 | my $error = $@; | |||||||||||||||||||||||||||
| 103 | 6 | close($handle); | |||||||||||||||||||||||||||
| 104 | 6 | ▶ | croak($error) if ($error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||