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.037;
2             # ABSTRACT: set the encoding of arbitrary files
3              
4 5     5   42657 use Moose;
  5         367052  
  5         43  
5             with 'Dist::Zilla::Role::EncodingProvider';
6              
7 5     5   32304 use Dist::Zilla::Pragmas;
  5         10  
  5         62  
8              
9 5     5   33 use namespace::autoclean;
  5         10  
  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 7309 sub mvp_multivalue_args { qw(filenames matches ignore) }
29 21     21 0 4353 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 77 my ($self) = @_;
85              
86             # never match (at least the filename characters)
87 21         145 my $matches_regex = qr/\000/;
88              
89 21         44 $matches_regex = qr/$matches_regex|$_/ for @{$self->matches};
  21         1115  
90              
91             # \A\Q$_\E should also handle the `eq` check
92 21         50 $matches_regex = qr/$matches_regex|\A\Q$_\E/ for @{$self->filenames};
  21         882  
93              
94 21 100       57 my( $ignore_regex ) = map { $_ && qr/$_/ } join '|', @{ $self->ignore };
  21         144  
  21         839  
95              
96 21         77 for my $file (@{$self->zilla->files}) {
  21         827  
97 101 100       331 next unless $file->name =~ $matches_regex;
98              
99 23 100 100     93 next if $ignore_regex and $file->name =~ $ignore_regex;
100              
101 22         76 $self->log_debug([
102             'setting encoding of %s to %s',
103             $file->name,
104             $self->encoding,
105             ]);
106              
107 22         1908 $file->encoding($self->encoding);
108             }
109              
110 21         116 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.037
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
149             released in the last two to three years. (That is, if the most recently
150             released version is v5.40, then this module should work on both v5.40 and
151             v5.38.)
152              
153             Although it may work on older versions of perl, no guarantee is made that the
154             minimum required version will not be increased. The version may be increased
155             for any reason, and there is no promise that patches will be accepted to
156             lower the minimum required perl.
157              
158             =head1 ATTRIBUTES
159              
160             =head2 encoding
161              
162             This is the encoding to set on the selected files. The special value "bytes"
163             can be used to indicate raw files that should not be encoded.
164              
165             =head2 filenames
166              
167             This is an arrayref of filenames to have their encoding set.
168              
169             =head2 matches
170              
171             This is an arrayref of regular expressions. Any file whose name matches one of
172             these regex will have its encoding set.
173              
174             =head2 ignore
175              
176             This is an arrayref of regular expressions. Any file whose name matches one of
177             these regex will B<not> have its encoding set. Useful to ignore a few files
178             that would otherwise be selected by C<matches>.
179              
180             =head1 AUTHOR
181              
182             Ricardo SIGNES 😏 <cpan@semiotic.systems>
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186             This software is copyright (c) 2026 by Ricardo SIGNES.
187              
188             This is free software; you can redistribute it and/or modify it under
189             the same terms as the Perl 5 programming language system itself.
190              
191             =cut