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.037;
2             # ABSTRACT: something that can act like a file
3              
4 52     52   42026 use Moose::Role;
  52         6880  
  52         539  
5              
6 52     52   301930 use Dist::Zilla::Pragmas;
  52         160  
  52         512  
7              
8 52     52   1933 use Dist::Zilla::Types qw(_Filename);
  52         166  
  52         778  
9 52     52   132142 use Moose::Util::TypeConstraints;
  52         115  
  52         550  
10 52     52   117770 use Try::Tiny;
  52         114  
  52         4719  
11              
12 52     52   352 use namespace::autoclean;
  52         133  
  52         513  
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   1959 my ($self, $function) = @_;
66              
67 800         2541 for (my $level = 1; $level < 50; ++$level)
68             {
69 2138         17638 my @frame = caller($level);
70 2138 50       5065 last if not defined $frame[0];
71 2138 100       23780 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   1420 my $self = shift;
78              
79 800         2194 for (my $level = 1; $level < 50; ++$level)
80             {
81 2178         9608 my @frame = caller($level);
82 2178 100       4445 last if not defined $frame[0];
83 2174 100       50670 return $1 if $frame[0] =~ m/^Dist::Zilla::Plugin::(.+)$/;
84             }
85 4         197 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 1471 my ($self) = @_;
120 625         22423 return $self->encoding eq 'bytes';
121             }
122              
123             sub _encode {
124 177     177   608 my ($self, $text) = @_;
125 177         5939 my $enc = $self->encoding;
126 177 50       641 if ( $self->is_bytes ) {
127 0         0 return $text; # XXX hope you were right that it really was bytes
128             }
129             else {
130 177         1249 require Encode;
131             my $bytes =
132 177     177   10983 try { Encode::encode($enc, $text, Encode::FB_CROAK()) }
133 177     0   1927 catch { $self->_throw("encode $enc" => $_) };
  0         0  
134 177         19674 return $bytes;
135             }
136             }
137              
138             sub _decode {
139 177     177   600 my ($self, $bytes) = @_;
140 177         6627 my $enc = $self->encoding;
141 177 100       638 if ( $self->is_bytes ) {
142 3         13 $self->_throw(decode => "Can't decode text from 'bytes' encoding");
143             }
144             else {
145 174         1225 require Encode;
146             my $text =
147 174     174   12135 try { Encode::decode($enc, $bytes, Encode::FB_CROAK()) }
148 174     0   2111 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       15117 $enc =~ /^utf-?8$/i && $text =~ s/\A\x{FEFF}//;
163              
164 174         8051 return $text;
165             }
166             }
167              
168             sub _throw {
169 3     3   8 my ($self, $op, $msg) = @_;
170 3         7 my ($name, $added_by) = map {; $self->$_ } qw/name added_by/;
  6         84  
171 3         1169 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.037
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
200             released in the last two to three years. (That is, if the most recently
201             released version is v5.40, then this module should work on both v5.40 and
202             v5.38.)
203              
204             Although it may work on older versions of perl, no guarantee is made that the
205             minimum required version will not be increased. The version may be increased
206             for any reason, and there is no promise that patches will be accepted to
207             lower the minimum required perl.
208              
209             =head1 ATTRIBUTES
210              
211             =head2 name
212              
213             This is the name of the file to be written out.
214              
215             =head2 added_by
216              
217             This is a list of strings describing when and why the file was added
218             to the distribution and when it was updated (its content, filename, or other attributes). It will
219             generally be updated by a plugin implementing the
220             L<FileMunger|Dist::Zilla::Role::FileMunger> role. Its accessor will return
221             the list of strings, concatenated with C<'; '>.
222              
223             =head2 mode
224              
225             This is the mode with which the file should be written out. It's an integer
226             with the usual C<chmod> semantics. It defaults to 0644.
227              
228             =head1 METHODS
229              
230             =head2 is_bytes
231              
232             Returns true if the C<encoding> is bytes. When true, accessing
233             C<content> will be an error.
234              
235             =head1 AUTHOR
236              
237             Ricardo SIGNES 😏 <cpan@semiotic.systems>
238              
239             =head1 COPYRIGHT AND LICENSE
240              
241             This software is copyright (c) 2026 by Ricardo SIGNES.
242              
243             This is free software; you can redistribute it and/or modify it under
244             the same terms as the Perl 5 programming language system itself.
245              
246             =cut