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

filter: Statementstmt 93.2% 69 / 74 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.0% 17 / 20 Podpod 100% 1 / 1 totaltotal 89.5% 103 / 115 CC 11 SCAR 8.0
CC
11
Cov
90%
_caller_of
4.2
_caller_plugin_name
4.0
_decode
3.0
CRAP 2.2
« blib/lib/Dist/Zilla/Role/TestRunner.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/GatherFile.pm »
1 package Dist::Zilla::Role::File 6.040;
2 # ABSTRACT: something that can act like a file
3  
4 54 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 54 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use Dist::Zilla::Types qw(_Filename);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 54 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 54 with 'Dist::Zilla::Role::StubBuild';
15  
16 #pod =head1 DESCRIPTION
17 #pod
18 #pod This role describes a file that may be written into the shipped distribution.
19 #pod
20 #pod =attr name
21 #pod
22 #pod This is the name of the file to be written out.
23 #pod
24 #pod =cut
25  
26 54 has name => (
27 is => 'rw',
28 isa => _Filename,
29 required => 1,
30 );
31  
32 #pod =attr added_by
33 #pod
34 #pod This is a list of strings describing when and why the file was added
35 #pod to the distribution and when it was updated (its content, filename, or other attributes). It will
36 #pod generally be updated by a plugin implementing the
37 #pod L<FileMunger|Dist::Zilla::Role::FileMunger> role. Its accessor will return
38 #pod the list of strings, concatenated with C<'; '>.
39 #pod
40 #pod =cut
41  
42 has added_by => (
43 isa => 'ArrayRef[Str]',
44 lazy => 1,
45 803 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 54 traits => ['Array'],
47 init_arg => undef,
48 handles => {
49 _push_added_by => 'push',
50 added_by => [ join => '; ' ],
51 },
52 );
53  
54 11881 ▶around name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 11881 my $orig = shift;
56 11881 my $self = shift;
57 11881 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 691 11190 11881
58 691 my ($pkg, $line) = $self->_caller_of('name');
59 691 $self->_push_added_by(sprintf("filename set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
60 }
61 11881 return $self->$orig(@_);
62 54 };
63  
64 823 ▶sub _caller_of {
Subroutine _caller_of: called CC=4 SCAR=14.5
65 823 my ($self, $function) = @_;
66  
67 823 ▶ for (my $level = 1; $level < 50; ++$level)
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 50) 2205 0 2205
68 {
69 2205 my @frame = caller($level);
70 2205 ▶ last if not defined $frame[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame[0] 0 2205 2205
71 2205 ▶ return ( (caller($level))[0,2] ) if $frame[3] =~ m/::${function}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $frame[3] =~ /::$function$/u 823 1382 2205
72 }
73 0 return 'unknown', '0';
74 }
75  
76 823 ▶sub _caller_plugin_name {
Subroutine _caller_plugin_name: called CC=4 SCAR=13.9
77 823 my $self = shift;
78  
79 823 ▶ for (my $level = 1; $level < 50; ++$level)
Branch: 1 branch
BranchTrueFalseTotal
if ($level < 50) 2245 0 2245
80 {
81 2245 my @frame = caller($level);
82 2245 ▶ last if not defined $frame[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame[0] 4 2241 2245
83 2241 ▶ return $1 if $frame[0] =~ m/^Dist::Zilla::Plugin::(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $frame[0] =~ /^Dist::Zilla::Plugin::(.+)$/u 819 1422 2241
84 }
85 4 return 'unknown';
86 }
87  
88 #pod =attr mode
89 #pod
90 #pod This is the mode with which the file should be written out. It's an integer
91 #pod with the usual C<chmod> semantics. It defaults to 0644.
92 #pod
93 #pod =cut
94  
95 my $safe_file_mode = subtype(
96 as 'Int',
97 840 ▶ where { not( $_ & 0002) },
Subroutine __ANON__: called CC=1 SCAR=0.0
98 0 ▶ message { "file mode would be world-writeable" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 54 );
100  
101 54 has mode => (
102 is => 'rw',
103 isa => $safe_file_mode,
104 default => 0644,
105 );
106  
107 54 requires 'encoding';
108 54 requires 'content';
109 54 requires 'encoded_content';
110  
111 #pod =method is_bytes
112 #pod
113 #pod Returns true if the C<encoding> is bytes. When true, accessing
114 #pod C<content> will be an error.
115 #pod
116 #pod =cut
117  
118 639 ▶sub is_bytes {
Subroutine is_bytes: called CC=1 SCAR=0.0
Pod documented
119 639 my ($self) = @_;
120 639 return $self->encoding eq 'bytes';
121 }
122  
123 180 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=7.3
124 180 my ($self, $text) = @_;
125 180 my $enc = $self->encoding;
126 180 ▶ if ( $self->is_bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bytes) { } 0 180 180
127 0 return $text; # XXX hope you were right that it really was bytes
128 }
129 else {
130 180 require Encode;
131 my $bytes =
132 180 ▶ try { Encode::encode($enc, $text, Encode::FB_CROAK()) }
Subroutine __ANON__: called CC=1 SCAR=0.0
133 0 ▶ catch { $self->_throw("encode $enc" => $_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
134 180 return $bytes;
135 }
136 }
137  
138 183 ▶sub _decode {
Subroutine _decode: called CC=3 SCAR=11.0
139 183 my ($self, $bytes) = @_;
140 183 my $enc = $self->encoding;
141 183 ▶ if ( $self->is_bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bytes) { } 3 180 183
142 3 $self->_throw(decode => "Can't decode text from 'bytes' encoding");
143 }
144 else {
145 180 require Encode;
146 my $text =
147 180 ▶ try { Encode::decode($enc, $bytes, Encode::FB_CROAK()) }
Subroutine __ANON__: called CC=1 SCAR=0.0
148 0 ▶ catch { $self->_throw("decode $enc" => $_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
149  
150 # Okay, look, buddy… If you're using a BOM on UTF-8, that's fine. You can
151 # use it. You're just not going to get it back. If we don't do this, the
152 # sequence of events will be:
153 # * read file from UTF-8-BOM file on disk
154 # * end up with FEFF as first character of file
155 # * pass file content to PPI
156 # * PPI blows up
157 #
158 # I'm not going to try to account for the BOM and add it back. It's awful!
159 #
160 # Meanwhile, if you're using UTF-16, you can get the BOM handled by picking
161 # the right encoding type, I think. -- rjbs, 2016-04-24
162 180 ▶ $enc =~ /^utf-?8$/i && $text =~ s/\A\x{FEFF}//;
Branch: 1 branch
BranchTrueFalseTotal
$enc =~ /^utf-?8$/iu and $text =~ s/\A\x{FEFF}//u 177 3 180
163  
164 180 return $text;
165 }
166 }
167  
168 3 ▶sub _throw {
Subroutine _throw: called CC=1 SCAR=0.0
169 3 my ($self, $op, $msg) = @_;
170 3 my ($name, $added_by) = map {; $self->$_ } qw/name added_by/;
171 3 confess(
172 "Could not $op $name; $added_by; error was: $msg; maybe you need the [Encoding] plugin to specify an encoding"
173 );
174 }
175  
176 54 1;
177  
178 __END__
« blib/lib/Dist/Zilla/Role/TestRunner.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/GatherFile.pm »