blib/lib/Dist/Zilla/Role/RepoFileInjector.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 70.0% 14 / 20 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 92.4% 86 / 93 CC 13 SCAR 16.6
CC
13
Cov
91%
write_repo_files
10.4
__ANON__
3.0
add_repo_file
2.0
CRAP 5.3
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Dist::Zilla::Role::RepoFileInjector; # git description: v0.008-2-g97bfee5
4 # vim: set ts=8 sts=4 sw=4 tw=115 et :
5 # ABSTRACT: Create files outside the build directory
6 # KEYWORDS: plugin distribution generate create file repository
7  
8 3 our $VERSION = '0.009';
9  
10 3 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use MooseX::Types qw(enum role_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use MooseX::Types::Moose qw(ArrayRef Str Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Path::Tiny 0.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has repo_root => (
18 is => 'ro', isa => Str,
19 predicate => '_has_repo_root',
20 lazy => 1,
21 3 ▶ default => sub { shift->zilla->root->stringify },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 3 );
23  
24 3 has allow_overwrite => (
25 is => 'ro', isa => Bool,
26 default => 1,
27 );
28  
29 has _repo_files => (
30 isa => ArrayRef[role_type('Dist::Zilla::Role::File')],
31 lazy => 1,
32 4 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 3 traits => ['Array'],
34 handles => {
35 __push_repo_file => 'push',
36 _repo_files => 'elements',
37 },
38 );
39  
40 around dump_config => sub
41 4 ▶{
Subroutine __ANON__: called CC=3 SCAR=11.0
42 4 my ($orig, $self) = @_;
43 4 my $config = $self->$orig;
44  
45 4 ▶ $config->{+__PACKAGE__} = {
Branch: 2 branches
BranchTrueFalseTotal
$self->allow_overwrite ? : 3 1 4
$self->_has_repo_root ? : 1 3 4
46 version => $VERSION,
47 allow_overwrite => ( $self->allow_overwrite ? 1 : 0 ),
48 repo_root => ( $self->_has_repo_root ? path($self->repo_root)->stringify : '.' ),
49 };
50 4 return $config;
51 3 };
52  
53 sub add_repo_file
54 4 ▶{
Subroutine add_repo_file: called CC=2 SCAR=7.1
Pod documented
55 4 my ($self, $file) = @_;
56  
57 4 my ($pkg, undef, $line) = caller;
58 4 ▶ if ($file->can('_set_added_by'))
Branch: 1 branch
BranchTrueFalseTotal
if ($file->can('_set_added_by')) { } 4 0 4
59 {
60 4 $file->_set_added_by(sprintf("%s (%s line %s)", $self->plugin_name, $pkg, $line));
61 }
62 else
63 {
64 # as done in Dist::Zilla::Role::FileInjector 4.300039
65 0 $file->meta->get_attribute('added_by')->set_value(
66 $file,
67 sprintf("%s (%s line %s)", $self->plugin_name, $pkg, $line),
68 );
69 }
70  
71 4 $self->log_debug([ 'adding file %s', $file->name ]);
72  
73 4 $self->__push_repo_file($file);
74 }
75  
76 sub write_repo_files
77 4 ▶{
Subroutine write_repo_files: called CC=10 SCAR=23.4
Pod documented
78 4 my $self = shift;
79  
80 4 foreach my $file ($self->_repo_files)
81 {
82 4 my $filename = path($file->name);
83 4 ▶ my $abs_filename = $filename->is_relative
Branch: 1 branch
BranchTrueFalseTotal
$filename->is_relative ? : 4 0 4
84 ? path($self->repo_root)->child($file->name)
85 : $file->name;
86  
87 4 ▶ if ($abs_filename->exists and $self->allow_overwrite)
Branch: 1 branch
BranchTrueFalseTotal
if ($abs_filename->exists and $self->allow_overwrite) 1 3 4
Condition100%3/3
$abs_filename->exists and $self->allow_overwrite
!ll&&!rl&&r
211
MC/DC100%2/2
$abs_filename->exists and $self->allow_overwrite
$abs_filename->exists $self->allow_overwrite
Truth table100%3/3
A and B
A= $abs_filename->existsB= $self->allow_overwrite
ABresult
0X0
100
111
88 {
89 1 $self->log_debug([ 'removing pre-existing %s', $abs_filename->stringify ]);
90 1 $abs_filename->remove;
91 }
92 4 ▶ $self->log_fatal([ '%s already exists (allow_overwrite = 0)', $abs_filename->stringify ])
Branch: 1 branch
BranchTrueFalseTotal
if $abs_filename->exists 1 3 4
93 if $abs_filename->exists;
94  
95 3 ▶ $self->log_debug([ 'writing out %s%s', $file->name,
Branch: 1 branch
BranchTrueFalseTotal
$filename->is_relative ? : 3 0 3
96 $filename->is_relative ? ' to ' . path($self->repo_root)->stringify : '' ]);
97  
98 3 ▶ Carp::croak("attempted to write $filename multiple times") if $abs_filename->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $abs_filename->exists 0 3 3
99 3 $abs_filename->touchpath;
100  
101 # handle dzil v4 files by assuming no (or latin1) encoding
102 3 ▶ my $encoded_content = $file->can('encoded_content') ? $file->encoded_content : $file->content;
Branch: 1 branch
BranchTrueFalseTotal
$file->can('encoded_content') ? : 3 0 3
103  
104 3 $abs_filename->spew_raw($encoded_content);
105 3 ▶ chmod $file->mode, "$abs_filename" or die "couldn't chmod $abs_filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod $file->mode, "$abs_filename" or die "couldn't chmod ${abs_filename}: $!" 3 0 3
106 }
107 }
108  
109 3 1;
110  
111 __END__
↑ Summary