Footprintless/Overlay.pm

filter: Statementstmt 100% 113 / 113 Branchbran 68.7% 22 / 32 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 28 / 28 Podpod 100% 3 / 3 totaltotal 93.3% 167 / 179 CC 19 SCAR 11.7
CC
19
Cov
93%
_resolve_footprintless
8.7
_dirs_template
5.0
_resolver
4.0
CRAP 3.2
« Footprintless/App.pm ↑ Summary Footprintless/App/Command/log/follow.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Overlay;
5 2 $Footprintless::Overlay::VERSION = '1.29';
6 # ABSTRACT: An overlay manager
7 # PODNAME: Footprintless::Overlay
8  
9 2 ▶use parent qw(Footprintless::MixableBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Footprintless::Mixins qw (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 _clean
14 _command_options
15 _entity
16 _extract_resource
17 _local_template
18 _push_to_destination
19 _sub_coordinate
20 _sub_entity
21 2 );
22 2 ▶use Footprintless::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 dynamic_module_new
24 invalid_entity
25 temp_dir
26 2 );
27 2 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Template::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Template::Overlay;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 my $logger = Log::Any->get_logger();
34  
35 9 ▶sub clean {
Subroutine clean: called CC=1 SCAR=0.0
Pod documented
36 9 my ($self) = @_;
37 9 $self->_clean();
38 }
39  
40 13 ▶sub _dirs_template {
Subroutine _dirs_template: called CC=5 SCAR=16.1
41 13 my ( $self, $to_dir, $with_dirs_work ) = @_;
42  
43 13 my $base_dir = $self->_sub_entity('base_dir');
44 13 my $template_dir = $self->_sub_entity('template_dir');
45  
46 13 my $unpack_dir;
47 13 my $resource = $self->_sub_entity('resource');
48 13 ▶ if ($resource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resource) 4 9 13
49 4 $unpack_dir = temp_dir();
50 4 $self->_extract_resource( $resource, $unpack_dir );
51  
52 4 ▶ if ($base_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_dir) 4 0 4
53 4 $base_dir = File::Spec->catdir( $unpack_dir, $base_dir );
54 }
55 4 ▶ if ($template_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template_dir) 4 0 4
56 $template_dir =
57 ref($template_dir) eq 'ARRAY'
58 4 ▶ ? [ map { File::Spec->catdir( $unpack_dir, $_ ) } @$template_dir ]
Branch: 1 branch
BranchTrueFalseTotal
ref $template_dir eq 'ARRAY' ? : 2 2 4
59 : File::Spec->catdir( $unpack_dir, $template_dir );
60 }
61 }
62  
63 13 &$with_dirs_work( $base_dir, $template_dir, $to_dir );
64 }
65  
66 13 ▶sub _dot_footprintless_resolver {
Subroutine _dot_footprintless_resolver: called CC=1 SCAR=0.0
67 13 my ($self) = @_;
68 23 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
69 23 my ( $template, $destination ) = @_;
70 23 ▶ if ( $template =~ /\/\.footprintless$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ m[/\.footprintless$]) 2 21 23
71 2 $self->_resolve_footprintless( $template, $destination );
72 2 return 1;
73 }
74 21 return 0;
75 13 };
76 }
77  
78 6 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=6.9
Pod documented
79 6 my ( $self, %options ) = @_;
80  
81 6 $self->clean();
82  
83 6 ▶ if ( $options{to_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'to_dir'}) { } 1 5 6
84 $self->_dirs_template(
85 $options{to_dir},
86 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 $self->_initialize(@_);
88 }
89 1 );
90 }
91 else {
92 $self->_local_with_dirs_template(
93 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 5 $self->_initialize(@_);
95 }
96 5 );
97 }
98 }
99  
100 6 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=0.0
101 6 my ( $self, $base_dir, $template_dir, $to_dir ) = @_;
102 6 $self->_overlay($base_dir)->overlay(
103 $template_dir,
104 resolver => $self->_dot_footprintless_resolver(),
105 to => $to_dir
106 );
107 }
108  
109 11 ▶sub _local_with_dirs_template {
Subroutine _local_with_dirs_template: called CC=1 SCAR=0.0
110 11 my ( $self, $local_work ) = @_;
111 $self->_local_template(
112 11 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 11 $self->_dirs_template( $_[0], $local_work );
114 }
115 11 );
116 }
117  
118 13 ▶sub _overlay {
Subroutine _overlay: called CC=2 SCAR=7.0
119 13 my ( $self, $base_dir ) = @_;
120  
121 13 my @overlay_opts = ();
122 13 my $key = $self->_sub_entity('key');
123 13 ▶ push( @overlay_opts, key => $key ) if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 13 0 13
124  
125 13 return Template::Overlay->new( $base_dir, $self->_resolver(), @overlay_opts );
126 }
127  
128 13 ▶sub _resolver {
Subroutine _resolver: called CC=4 SCAR=13.9
129 13 my ($self) = @_;
130  
131 13 my @resolver_opts = ();
132 13 my $os = $self->_sub_entity('os');
133 13 ▶ push( @resolver_opts, os => $os ) if ($os);
Branch: 1 branch
BranchTrueFalseTotal
if $os 13 0 13
134  
135 13 my $resolver_coordinate = $self->_sub_entity('resolver_coordinate');
136 13 ▶ my $resolver_spec =
Branch: 1 branch
BranchTrueFalseTotal
$resolver_coordinate ? : 13 0 13
137 $resolver_coordinate
138 ? $self->_entity($resolver_coordinate)
139 : $self->_entity();
140  
141 13 my $resolver;
142 13 my $resolver_factory = $self->_entity('footprintless.overlay.resolver_factory');
143 13 ▶ if ($resolver_factory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolver_factory) { } 1 12 13
144 1 $logger->tracef( "using resolver_factory: %s", $resolver_factory );
145 1 $resolver =
146 dynamic_module_new($resolver_factory)->new_resolver( $resolver_spec, @resolver_opts );
147 }
148 else {
149 12 $resolver = Template::Resolver->new( $resolver_spec, @resolver_opts );
150 }
151 13 return $resolver;
152 }
153  
154 2 ▶sub _resolve_footprintless {
Subroutine _resolve_footprintless: called CC=8 SCAR=21.6
155 2 my ( $self, $template, $footprintless_path ) = @_;
156 2 my $destination = ( File::Spec->splitpath($footprintless_path) )[1];
157 2 $logger->debugf( "resolving [%s]->[%s]", $template, $destination );
158  
159 2 ▶ my $spec = do($template) || return;
Condition50%1/2
do $template || (return)
l!l
20
MC/DC0%0/1
do $template || (return)
do $template
Truth table50%1/2
A
A= do $template
Aresult
11
00
160 2 ▶ croak("invalid $template") unless ( ref($spec) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" 0 2 2
161  
162 2 ▶ if ( $spec->{clean} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"clean"}) 2 0 2
163 my @to_be_cleaned =
164 2 ▶ map { File::Spec->catdir( $destination, $_ ) . ( /\/$/ ? '/' : '' ); }
Branch: 2 branches
BranchTrueFalseTotal
m[/$] ? : 2 0 2
ref $spec->{'clean'} ? : 2 0 2
165 2 ref( $spec->{clean} ) ? @{ $spec->{clean} } : ( $spec->{clean} );
166  
167 Footprintless::Util::clean(
168 \@to_be_cleaned,
169 2 command_runner => $self->{factory}->command_runner(),
170 command_options => $self->_command_options()
171 );
172 }
173  
174 2 ▶ if ( $spec->{resources} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"resources"}) 2 0 2
175 2 my $resource_manager = $self->{factory}->resource_manager();
176 2 foreach my $resource ( keys( %{ $spec->{resources} } ) ) {
177 2 $resource_manager->download( $spec->{resources}{$resource}, to => $destination );
178 }
179 }
180 }
181  
182 7 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod documented
183 7 my ( $self, %options ) = @_;
184  
185 7 ▶ if ( $options{to_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'to_dir'}) { } 1 6 7
186 $self->_dirs_template(
187 $options{to_dir},
188 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 1 $self->_update(@_);
190 }
191 1 );
192 }
193 else {
194 $self->_local_with_dirs_template(
195 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 6 $self->_update(@_);
197 }
198 6 );
199 }
200 }
201  
202 7 ▶sub _update {
Subroutine _update: called CC=1 SCAR=0.0
203 7 my ( $self, $base_dir, $template_dir, $to_dir ) = @_;
204 7 $logger->tracef( "update to=[%s], template=[%s]", $to_dir, $template_dir );
205 7 $self->_overlay($to_dir)
206 ->overlay( $template_dir, resolver => $self->_dot_footprintless_resolver() );
207 }
208  
209 2 1;
210  
211 __END__
« Footprintless/App.pm ↑ Summary Footprintless/App/Command/log/follow.pm »