File Coverage

blib/lib/Dist/Zilla/Plugin/Encoding.pm
Criterion Covered Total %
statement 27 27 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 6 6 100.0
pod 0 3 0.0
total 42 45 93.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::Encoding 6.029;
2             # ABSTRACT: set the encoding of arbitrary files
3              
4 5     5   39046 use Moose;
  5         510352  
  5         48  
5             with 'Dist::Zilla::Role::EncodingProvider';
6              
7 5     5   36799 use Dist::Zilla::Pragmas;
  5         15  
  5         66  
8              
9 5     5   56 use namespace::autoclean;
  5         14  
  5         50  
10              
11             #pod =head1 SYNOPSIS
12             #pod
13             #pod This plugin allows you to explicitly set the encoding on some files in your
14             #pod distribution. You can either specify the exact set of files (with the
15             #pod "filenames" parameter) or provide the regular expressions to check (using
16             #pod "match").
17             #pod
18             #pod In your F<dist.ini>:
19             #pod
20             #pod [Encoding]
21             #pod encoding = Latin-3
22             #pod
23             #pod filename = t/esperanto.t ; this file is Esperanto
24             #pod match = ^t/urkish/ ; these are all Turkish
25             #pod
26             #pod =cut
27              
28 21     21 0 5697 sub mvp_multivalue_args { qw(filenames matches ignore) }
29 21     21 0 3310 sub mvp_aliases { return { filename => 'filenames', match => 'matches' } }
30              
31             #pod =attr encoding
32             #pod
33             #pod This is the encoding to set on the selected files. The special value "bytes"
34             #pod can be used to indicate raw files that should not be encoded.
35             #pod
36             #pod =cut
37              
38             has encoding => (
39             is => 'ro',
40             isa => 'Str',
41             required => 1,
42             );
43              
44             #pod =attr filenames
45             #pod
46             #pod This is an arrayref of filenames to have their encoding set.
47             #pod
48             #pod =cut
49              
50             has filenames => (
51             is => 'ro',
52             isa => 'ArrayRef',
53             default => sub { [] },
54             );
55              
56             #pod =attr matches
57             #pod
58             #pod This is an arrayref of regular expressions. Any file whose name matches one of
59             #pod these regex will have its encoding set.
60             #pod
61             #pod =cut
62              
63             has matches => (
64             is => 'ro',
65             isa => 'ArrayRef',
66             default => sub { [] },
67             );
68              
69             #pod =attr ignore
70             #pod
71             #pod This is an arrayref of regular expressions. Any file whose name matches one of
72             #pod these regex will B<not> have its encoding set. Useful to ignore a few files
73             #pod that would otherwise be selected by C<matches>.
74             #pod
75             #pod =cut
76              
77             has ignore => (
78             is => 'ro',
79             isa => 'ArrayRef',
80             default => sub { [] },
81             );
82              
83             sub set_file_encodings {
84 21     21 0 82 my ($self) = @_;
85              
86             # never match (at least the filename characters)
87 21         104 my $matches_regex = qr/\000/;
88              
89 21         51 $matches_regex = qr/$matches_regex|$_/ for @{$self->matches};
  21         749  
90              
91             # \A\Q$_\E should also handle the `eq` check
92 21         60 $matches_regex = qr/$matches_regex|\A\Q$_\E/ for @{$self->filenames};
  21         670  
93              
94 21 100       88 my( $ignore_regex ) = map { $_ && qr/$_/ } join '|', @{ $self->ignore };
  21         126  
  21         627  
95              
96 21         59 for my $file (@{$self->zilla->files}) {
  21         601  
97 101 100       292 next unless $file->name =~ $matches_regex;
98              
99 23 100 100     124 next if $ignore_regex and $file->name =~ $ignore_regex;
100              
101 22         84 $self->log_debug([
102             'setting encoding of %s to %s',
103             $file->name,
104             $self->encoding,
105             ]);
106              
107 22         1212 $file->encoding($self->encoding);
108             }
109              
110 21         95 return;
111             }
112              
113             __PACKAGE__->meta->make_immutable;
114             1;
115              
116             __END__
117              
118             =pod
119              
120             =encoding UTF-8
121              
122             =head1 NAME
123              
124             Dist::Zilla::Plugin::Encoding - set the encoding of arbitrary files
125              
126             =head1 VERSION
127              
128             version 6.029
129              
130             =head1 SYNOPSIS
131              
132             This plugin allows you to explicitly set the encoding on some files in your
133             distribution. You can either specify the exact set of files (with the
134             "filenames" parameter) or provide the regular expressions to check (using
135             "match").
136              
137             In your F<dist.ini>:
138              
139             [Encoding]
140             encoding = Latin-3
141              
142             filename = t/esperanto.t ; this file is Esperanto
143             match = ^t/urkish/ ; these are all Turkish
144              
145             =head1 PERL VERSION
146              
147             This module should work on any version of perl still receiving updates from
148             the Perl 5 Porters. This means it should work on any version of perl released
149             in the last two to three years. (That is, if the most recently released
150             version is v5.40, then this module should work on both v5.40 and v5.38.)
151              
152             Although it may work on older versions of perl, no guarantee is made that the
153             minimum required version will not be increased. The version may be increased
154             for any reason, and there is no promise that patches will be accepted to lower
155             the minimum required perl.
156              
157             =head1 ATTRIBUTES
158              
159             =head2 encoding
160              
161             This is the encoding to set on the selected files. The special value "bytes"
162             can be used to indicate raw files that should not be encoded.
163              
164             =head2 filenames
165              
166             This is an arrayref of filenames to have their encoding set.
167              
168             =head2 matches
169              
170             This is an arrayref of regular expressions. Any file whose name matches one of
171             these regex will have its encoding set.
172              
173             =head2 ignore
174              
175             This is an arrayref of regular expressions. Any file whose name matches one of
176             these regex will B<not> have its encoding set. Useful to ignore a few files
177             that would otherwise be selected by C<matches>.
178              
179             =head1 AUTHOR
180              
181             Ricardo SIGNES 😏 <cpan@semiotic.systems>
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is copyright (c) 2022 by Ricardo SIGNES.
186              
187             This is free software; you can redistribute it and/or modify it under
188             the same terms as the Perl 5 programming language system itself.
189              
190             =cut