lib/Module/Provision/TraitFor/Rendering.pm

filter: Statementstmt 60.3% 70 / 116 Branchbran 13.3% 4 / 30 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 68.1% 15 / 22 Podpod 100% 5 / 5 totaltotal 48.4% 95 / 196 CC 23 SCAR 38.2
CC
23
Cov
47%
render_template
156.0
expand_tuple
12.0
render_templates
12.0
CRAP 45.5
« lib/Module/Provision/TraitFor/PrereqDifferences.pm ↑ Summary lib/Module/Provision/TraitFor/AddingFiles.pm »
1 package Module::Provision::TraitFor::Rendering;
2  
3 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Class::Usul::Constants qw( EXCEPTION_CLASS FALSE NUL OK TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Class::Usul::Functions qw( app_prefix is_arrayref distname io throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::DataClass::Types qw( ArrayRef Bool Directory Path SimpleStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::ShareDir ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Unexpected::Functions qw( Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Class::Usul::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 requires qw( add_leader appldir builder config dist_module exec_perms file
16 incdir initial_wd loc log perms stash testdir vcs yorn );
17  
18 # Object attributes (public)
19 1 option 'force' => is => 'ro', isa => Bool,
20 documentation => 'Overwrite files if they already exist',
21 default => FALSE, short => 'f';
22  
23 1 option 'templates' => is => 'ro', isa => SimpleStr, format => 's',
24 documentation => 'Non default location of the code templates',
25 default => NUL;
26  
27 1 has 'template_dir' => is => 'lazy', isa => Directory, coerce => TRUE,
28 init_arg => undef;
29  
30 1 has 'template_list' => is => 'lazy', isa => ArrayRef, init_arg => undef;
31  
32 # Object attributes (private)
33 1 has '_template_index' => is => 'lazy', isa => Path, coerce => TRUE,
34 init_arg => undef;
35  
36 # Private methods
37 0 ▶my $_deref_tmpl = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
38 0 ▶ my ($self, $car) = @_; '_' ne substr $car, 0, 1 and return $car;
Branch: 1 branch
BranchTrueFalseTotal
"_" ne substr($car, 0, 1) and return $car 0 0 0
39  
40 0 my $reader = substr $car, 1; return $self->$reader();
41 1 };
42  
43 1 ▶my $_merge_lists = sub {
Subroutine __ANON__: called CC=2 SCAR=7.3
44 1 my ($self, $args) = @_; my $list = $args->{templates};
45  
46 1 push @{ $list }, @{ $args->{builders}->{ $self->builder } };
47 0 ▶ $self->vcs ne 'none' and push @{ $list }, @{ $args->{vcs}->{ $self->vcs } };
Branch: 1 branch
BranchTrueFalseTotal
$self->vcs ne "none" and push @{$list;}, @{$args->{"vcs"}{$self->vcs};} 0 1 1
48  
49 1 return $list;
50 1 };
51  
52 0 ▶my $_template_args = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 my $self = shift; weaken( $self ); my $args = { ABSOLUTE => TRUE, };
54  
55 0 ▶ $args->{VARIABLES}->{loc} = sub { $self->loc( @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
56  
57 0 return $args;
58 1 };
59  
60 # Construction
61 1 ▶sub _build_template_dir {
Subroutine _build_template_dir: called CC=4 SCAR=14.0
62 1 my $self = shift;
63 1 my $class = blessed $self;
64 1 ▶ my $tgt = $self->templates
Branch: 1 branch
BranchTrueFalseTotal
$self->templates ? : 1 0 1
65 ? io( [ $self->templates ] )->absolute( $self->initial_wd )
66 : io( [ $self->config->my_home, '.'.(app_prefix $class) ] );
67  
68 1 ▶ $tgt->exists and return $tgt; $tgt->mkpath( $self->exec_perms );
Branch: 1 branch
BranchTrueFalseTotal
$tgt->exists and return $tgt 0 1 1
69  
70 1 my $dist = io( File::ShareDir::dist_dir( distname $class ) );
71  
72 1 $_->copy( $tgt ) for ($dist->all_files);
73  
74 1 return $tgt;
75 }
76  
77 1 ▶sub _build__template_index {
Subroutine _build__template_index: called CC=1 SCAR=0.0
78 1 return $_[ 0 ]->template_dir->catfile( $_[ 0 ]->config->template_index );
79 }
80  
81 1 ▶sub _build_template_list {
Subroutine _build_template_list: called CC=3 SCAR=11.2
82 1 my $self = shift; my $index = $self->_template_index;
83  
84 1 ▶ my $data; $index->exists and $data = $self->file->data_load
Branch: 1 branch
BranchTrueFalseTotal
$index->exists and $data = $self->file->data_load("paths", [$index], "storage_class", "Any") and return $self->$_merge_lists($data) 0 1 1
Condition33%1/3
$index->exists and $data = $self->file->data_load("paths", [$index], "storage_class", "Any")
!ll&&!rl&&r
100
MC/DC0%0/2
$index->exists and $data = $self->file->data_load("paths", [$index], "storage_class", "Any")
$index->exists $data = $self->file->data_load("paths", [$index], "storage_class", "Any")
Truth table33%1/3
A and B
A= $index->existsB= $data = $self->file->data_load("paths", [$index], "storage_class", "Any")
ABresult
0X0
100
111
85 ( paths => [ $index ], storage_class => 'Any' )
86 and return $self->$_merge_lists( $data );
87 1 my $builders = {
88 DZ => [ [ 'dist.ini', '_appldir' ],
89 [ 'DZ_Build.PL', [ '_appldir', '.build.PL' ], ], ],
90 MB => [ [ 'Build.PL', '_appldir' ], ], };
91 1 my $templates = [ [ 'Changes', '_appldir' ],
92 [ 'MANIFEST.SKIP', '_appldir' ],
93 [ 'travis.yml', [ '_appldir', '.travis.yml' ] ],
94 [ 'perl_module.pm', '_dist_module' ],
95 [ '02pod.t', '_testdir' ],
96 [ '03podcoverage.t', '_testdir' ],
97 [ '05kwalitee.t', '_testdir' ],
98 [ '06yaml.t', '_testdir' ],
99 [ '07podspelling.t', '_testdir' ],
100 [ '10test_script.t', '_testdir' ],
101 [ 'boilerplate.pm', '_testdir' ], ];
102 1 my $vcs = {
103 git => [ [ 'gitcommit-msg', [ '_appldir', '.gitcommit-msg' ] ],
104 [ 'gitignore', [ '_appldir', '.gitignore' ] ],
105 [ 'gitpre-commit', [ '_appldir', '.gitpre-commit' ] ], ],
106 svn => [], };
107  
108 1 $self->output( 'Creating index [_1]', { args => [ $index ] } );
109 1 $data = { builders => $builders, templates => $templates, vcs => $vcs };
110 1 $self->file->data_dump
111 ( data => $data, path => $index, storage_class => 'Any' );
112 1 return $self->$_merge_lists( $data );
113 }
114  
115 # Public methods
116 0 ▶sub dump_stash : method {
Subroutine dump_stash: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift; $self->dumper( $self->stash ); return OK;
118 }
119  
120 0 ▶sub expand_tuple {
Subroutine expand_tuple: not called CC=3 SCAR=24.8
Pod documented
121 0 my ($self, $tuple) = @_;
122  
123 0 ▶ for (my $i = 0, my $max = @{ $tuple }; $i < $max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 0 0 0
124 0 ▶ if (is_arrayref $tuple->[ $i ]) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_arrayref($tuple->[$i])) { } 0 0 0
125 0 $tuple->[ $i ]->[ 0 ] = $self->$_deref_tmpl( $tuple->[ $i ]->[ 0 ] );
126 0 $tuple->[ $i ] = io( $tuple->[ $i ] );
127 }
128 else {
129 0 $tuple->[ $i ] = $self->$_deref_tmpl( $tuple->[ $i ] );
130 }
131 }
132  
133 0 return $tuple;
134 }
135  
136 1 ▶sub init_templates : method {
Subroutine init_templates: called CC=1 SCAR=0.0
Pod documented
137 1 my $self = shift; $self->template_list; return OK;
138 }
139  
140 0 ▶sub render_template {
Subroutine render_template: not called CC=12 SCAR=50.5
Pod documented
141 0 my ($self, $template, $target) = @_;
142  
143 0 ▶ $template or throw Unspecified, [ 'Template' ];
Branch: 1 branch
BranchTrueFalseTotal
$template or &throw(Unspecified(), ["Template"]) 0 0 0
144 0 ▶ $target or throw Unspecified, [ 'Template target' ];
Branch: 1 branch
BranchTrueFalseTotal
$target or &throw(Unspecified(), ["Template target"]) 0 0 0
145  
146 0 ▶ $target->exists and $target->is_dir
Branch: 1 branch
BranchTrueFalseTotal
$target->exists and $target->is_dir and $target = $target->catfile($template) 0 0 0
Condition0%0/3
$target->exists and $target->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$target->exists and $target->is_dir
$target->exists $target->is_dir
Truth table0%0/3
A and B
A= $target->existsB= $target->is_dir
ABresult
0X0
100
111
147 and $target = $target->catfile( $template );
148 0 $template = $self->template_dir->catfile( $template );
149  
150 0 ▶ $template->exists or
Branch: 1 branch
BranchTrueFalseTotal
$template->exists or return $self->log->warn($self->loc("Path [_1] not found", $template)) 0 0 0
151 return $self->log->warn( $self->loc( 'Path [_1] not found', $template ) );
152  
153 0 my $file = $target->filename; my $prompt;
154  
155 0 ▶ $target->exists and not $self->force
Branch: 1 branch
BranchTrueFalseTotal
$target->exists and not $self->force and $prompt = $self->loc("File [_1] exists, overwrite?", $file) and not $self->yorn($self->add_leader($prompt), 0, 1) and return $target 0 0 0
Condition0%0/9
$target->exists and not $self->force and $prompt = $self->loc("File [_1] exists, overwrite?", $file) and not $self->yorn($self->add_leader($prompt), 0, 1)
!ll&&!rl&&r
000
$target->exists and not $self->force and $prompt = $self->loc("File [_1] exists, overwrite?", $file)
!ll&&!rl&&r
000
$target->exists and not $self->force
!ll&&!rl&&r
000
MC/DC0%0/4
$target->exists and not $self->force and $prompt = $self->loc("File [_1] exists, overwrite?", $file) and not $self->yorn($self->add_leader($prompt), 0, 1)
$target->exists not $self->force $prompt = $self->loc("File [_1] exists, overwrite?", $file) not $self->yorn($self->add_leader($prompt), 0, 1)
Truth table0%0/5
A and not(B) and C and not(D)
A= $target->existsB= not $self->forceC= $prompt = $self->loc("File [_1] exists, overwrite?", $file)D= not $self->yorn($self->add_leader($prompt), 0, 1)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
156 and $prompt = $self->loc( 'File [_1] exists, overwrite?', $file )
157 and not $self->yorn( $self->add_leader( $prompt ), FALSE, TRUE )
158 and return $target;
159  
160 0 ▶ my $tmplt = Template->new( $self->$_template_args )
Branch: 1 branch
BranchTrueFalseTotal
my $tmplt = "Template"->new($self->$_template_args) or &throw($Template::ERROR) 0 0 0
161 or throw $Template::ERROR;
162 0 my $text = NUL;
163  
164 0 ▶ $tmplt->process( $template->pathname, $self->stash, \$text )
Branch: 1 branch
BranchTrueFalseTotal
$tmplt->process($template->pathname, $self->stash, \$text) or &throw($tmplt->error) 0 0 0
165 or throw $tmplt->error();
166 0 $target->perms( $self->perms )->print( $text ); $target->close;
167 0 return $target;
168 }
169  
170 0 ▶sub render_templates {
Subroutine render_templates: not called CC=3 SCAR=24.8
Pod documented
171 0 ▶ my $self = shift; $self->quiet or $self->output( 'Rendering templates' );
Branch: 1 branch
BranchTrueFalseTotal
$self->quiet or $self->output("Rendering templates") 0 0 0
172  
173 0 for my $tuple (map { $self->expand_tuple( $_ ) } @{ $self->template_list }) {
174 0 $self->render_template( @{ $tuple } );
175 }
176  
177 0 return;
178 }
179  
180 1 1;
181  
182 __END__
« lib/Module/Provision/TraitFor/PrereqDifferences.pm ↑ Summary lib/Module/Provision/TraitFor/AddingFiles.pm »