File Coverage

blib/lib/Dist/Zilla/Role/File.pm
Criterion Covered Total %
statement 51 55 92.7
branch 12 14 85.7
condition n/a
subroutine 14 16 87.5
pod 1 1 100.0
total 78 86 90.7


line stmt bran cond sub pod time code
1             package Dist::Zilla::Role::File 6.029;
2             # ABSTRACT: something that can act like a file
3              
4 52     52   36306 use Moose::Role;
  52         5175  
  52         381  
5              
6 52     52   272113 use Dist::Zilla::Pragmas;
  52         174  
  52         428  
7              
8 52     52   1842 use Dist::Zilla::Types qw(_Filename);
  52         159  
  52         615  
9 52     52   122345 use Moose::Util::TypeConstraints;
  52         230  
  52         485  
10 52     52   114772 use Try::Tiny;
  52         151  
  52         3966  
11              
12 52     52   424 use namespace::autoclean;
  52         198  
  52         382  
13              
14             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             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             default => sub { [] },
46             traits => ['Array'],
47             init_arg => undef,
48             handles => {
49             _push_added_by => 'push',
50             added_by => [ join => '; ' ],
51             },
52             );
53              
54             around name => sub {
55             my $orig = shift;
56             my $self = shift;
57             if (@_) {
58             my ($pkg, $line) = $self->_caller_of('name');
59             $self->_push_added_by(sprintf("filename set by %s (%s line %s)", $self->_caller_plugin_name, $pkg, $line));
60             }
61             return $self->$orig(@_);
62             };
63              
64             sub _caller_of {
65 800     800   2045 my ($self, $function) = @_;
66              
67 800         2479 for (my $level = 1; $level < 50; ++$level)
68             {
69 2138         16946 my @frame = caller($level);
70 2138 50       5364 last if not defined $frame[0];
71 2138 100       20621 return ( (caller($level))[0,2] ) if $frame[3] =~ m/::${function}$/;
72             }
73 0         0 return 'unknown', '0';
74             }
75              
76             sub _caller_plugin_name {
77 800     800   1489 my $self = shift;
78              
79 800         2378 for (my $level = 1; $level < 50; ++$level)
80             {
81 2178         10658 my @frame = caller($level);
82 2178 100       5194 last if not defined $frame[0];
83 2174 100       43481 return $1 if $frame[0] =~ m/^Dist::Zilla::Plugin::(.+)$/;
84             }
85 4         178 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             where { not( $_ & 0002) },
98             message { "file mode would be world-writeable" }
99             );
100              
101             has mode => (
102             is => 'rw',
103             isa => $safe_file_mode,
104             default => 0644,
105             );
106              
107             requires 'encoding';
108             requires 'content';
109             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             sub is_bytes {
119 625     625 1 1363 my ($self) = @_;
120 625         18595 return $self->encoding eq 'bytes';
121             }
122              
123             sub _encode {
124 177     177   701 my ($self, $text) = @_;
125 177         5388 my $enc = $self->encoding;
126 177 50       655 if ( $self->is_bytes ) {
127 0         0 return $text; # XXX hope you were right that it really was bytes
128             }
129             else {
130 177         1070 require Encode;
131             my $bytes =
132 177     177   10505 try { Encode::encode($enc, $text, Encode::FB_CROAK()) }
133 177     0   1785 catch { $self->_throw("encode $enc" => $_) };
  0         0  
134 177         19655 return $bytes;
135             }
136             }
137              
138             sub _decode {
139 177     177   653 my ($self, $bytes) = @_;
140 177         5282 my $enc = $self->encoding;
141 177 100       693 if ( $self->is_bytes ) {
142 3         26 $self->_throw(decode => "Can't decode text from 'bytes' encoding");
143             }
144             else {
145 174         1082 require Encode;
146             my $text =
147 174     174   10674 try { Encode::decode($enc, $bytes, Encode::FB_CROAK()) }
148 174     0   1734 catch { $self->_throw("decode $enc" => $_) };
  0         0  
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 174 100       16339 $enc =~ /^utf-?8$/i && $text =~ s/\A\x{FEFF}//;
163              
164 174         6305 return $text;
165             }
166             }
167              
168             sub _throw {
169 3     3   12 my ($self, $op, $msg) = @_;
170 3         8 my ($name, $added_by) = map {; $self->$_ } qw/name added_by/;
  6         129  
171 3         1649 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             1;
177              
178             __END__
179              
180             =pod
181              
182             =encoding UTF-8
183              
184             =head1 NAME
185              
186             Dist::Zilla::Role::File - something that can act like a file
187              
188             =head1 VERSION
189              
190             version 6.029
191              
192             =head1 DESCRIPTION
193              
194             This role describes a file that may be written into the shipped distribution.
195              
196             =head1 PERL VERSION
197              
198             This module should work on any version of perl still receiving updates from
199             the Perl 5 Porters. This means it should work on any version of perl released
200             in the last two to three years. (That is, if the most recently released
201             version is v5.40, then this module should work on both v5.40 and v5.38.)
202              
203             Although it may work on older versions of perl, no guarantee is made that the
204             minimum required version will not be increased. The version may be increased
205             for any reason, and there is no promise that patches will be accepted to lower
206             the minimum required perl.
207              
208             =head1 ATTRIBUTES
209              
210             =head2 name
211              
212             This is the name of the file to be written out.
213              
214             =head2 added_by
215              
216             This is a list of strings describing when and why the file was added
217             to the distribution and when it was updated (its content, filename, or other attributes). It will
218             generally be updated by a plugin implementing the
219             L<FileMunger|Dist::Zilla::Role::FileMunger> role. Its accessor will return
220             the list of strings, concatenated with C<'; '>.
221              
222             =head2 mode
223              
224             This is the mode with which the file should be written out. It's an integer
225             with the usual C<chmod> semantics. It defaults to 0644.
226              
227             =head1 METHODS
228              
229             =head2 is_bytes
230              
231             Returns true if the C<encoding> is bytes. When true, accessing
232             C<content> will be an error.
233              
234             =head1 AUTHOR
235              
236             Ricardo SIGNES 😏 <cpan@semiotic.systems>
237              
238             =head1 COPYRIGHT AND LICENSE
239              
240             This software is copyright (c) 2022 by Ricardo SIGNES.
241              
242             This is free software; you can redistribute it and/or modify it under
243             the same terms as the Perl 5 programming language system itself.
244              
245             =cut