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