File Coverage

blib/lib/Dist/Zilla/Plugin/AutoPrereqs.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 25 27 92.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::AutoPrereqs 6.037;
2             # ABSTRACT: automatically extract prereqs from your modules
3              
4 6     6   5353 use Moose;
  6         14  
  6         314  
5             with(
6             'Dist::Zilla::Role::PrereqScanner',
7             'Dist::Zilla::Role::PrereqSource',
8             'Dist::Zilla::Role::PPI',
9             );
10              
11 6     6   48339 use Dist::Zilla::Pragmas;
  6         170  
  6         68  
12              
13 6     6   226 use Moose::Util::TypeConstraints 'enum';
  6         20  
  6         107  
14 6     6   3393 use namespace::autoclean;
  6         15  
  6         68  
15              
16             #pod =head1 SYNOPSIS
17             #pod
18             #pod In your F<dist.ini>:
19             #pod
20             #pod [AutoPrereqs]
21             #pod skip = ^Foo|Bar$
22             #pod skip = ^Other::Dist
23             #pod
24             #pod =head1 DESCRIPTION
25             #pod
26             #pod This plugin will extract loosely your distribution prerequisites from
27             #pod your files using L<Perl::PrereqScanner>.
28             #pod
29             #pod If some prereqs are not found, you can still add them manually with the
30             #pod L<Prereqs|Dist::Zilla::Plugin::Prereqs> plugin.
31             #pod
32             #pod This plugin will skip the modules shipped within your dist.
33             #pod
34             #pod B<Note>, if you have any non-Perl files in your C<t/> directory or other
35             #pod directories being scanned, be sure to mark those files' encoding as C<bytes>
36             #pod with the L<Encoding|Dist::Zilla::Plugin::Encoding> plugin so they won't be
37             #pod scanned:
38             #pod
39             #pod [Encoding]
40             #pod encoding = bytes
41             #pod match = ^t/data/
42             #pod
43             #pod =attr finder
44             #pod
45             #pod This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
46             #pod whose files will be scanned to determine runtime prerequisites. It
47             #pod may be specified multiple times. The default value is
48             #pod C<:InstallModules> and C<:ExecFiles>.
49             #pod
50             #pod =attr test_finder
51             #pod
52             #pod Just like C<finder>, but for test-phase prerequisites. The default
53             #pod value is C<:TestFiles>.
54             #pod
55             #pod =attr configure_finder
56             #pod
57             #pod Just like C<finder>, but for configure-phase prerequisites. There is
58             #pod no default value; AutoPrereqs will not determine configure-phase
59             #pod prerequisites unless you set configure_finder.
60             #pod
61             #pod =attr develop_finder
62             #pod
63             #pod Just like C<finder>, but for develop-phase prerequisites. The default value
64             #pod is C<:ExtraTestFiles>.
65             #pod
66             #pod =attr skips
67             #pod
68             #pod This is an arrayref of regular expressions, derived from all the 'skip' lines
69             #pod in the configuration. Any module names matching any of these regexes will not
70             #pod be registered as prerequisites.
71             #pod
72             #pod =attr relationship
73             #pod
74             #pod The relationship used for the registered prerequisites. The default value is
75             #pod 'requires'; other options are 'recommends' and 'suggests'.
76             #pod
77             #pod =attr extra_scanners
78             #pod
79             #pod This is an arrayref of scanner names (as expected by L<Perl::PrereqScanner>).
80             #pod It will be passed as the C<extra_scanners> parameter to L<Perl::PrereqScanner>.
81             #pod
82             #pod =attr scanners
83             #pod
84             #pod This is an arrayref of scanner names (as expected by L<Perl::PrereqScanner>).
85             #pod If present, it will be passed as the C<scanners> parameter to
86             #pod L<Perl::PrereqScanner>, which means that it will replace the default list
87             #pod of scanners.
88             #pod
89             #pod =head1 SEE ALSO
90             #pod
91             #pod L<Prereqs|Dist::Zilla::Plugin::Prereqs>, L<Perl::PrereqScanner>.
92             #pod
93             #pod =head1 CREDITS
94             #pod
95             #pod This plugin was originally contributed by Jerome Quelin.
96             #pod
97             #pod =cut
98              
99             sub mvp_multivalue_args { qw(extra_scanners scanners) }
100             sub mvp_aliases { return { extra_scanner => 'extra_scanners',
101             scanner => 'scanners',
102             relationship => 'type' } }
103              
104             has extra_scanners => (
105             is => 'ro',
106             isa => 'ArrayRef[Str]',
107             default => sub { [] },
108             );
109              
110             has scanners => (
111             is => 'ro',
112             isa => 'ArrayRef[Str]',
113             predicate => 'has_scanners',
114             );
115              
116              
117             has _scanner => (
118             is => 'ro',
119             lazy => 1,
120             default => sub {
121             my $self = shift;
122              
123             require Perl::PrereqScanner;
124             Perl::PrereqScanner->VERSION('1.016'); # don't skip "lib"
125              
126             return Perl::PrereqScanner->new(
127             ($self->has_scanners ? (scanners => $self->scanners) : ()),
128             extra_scanners => $self->extra_scanners,
129             )
130             },
131             init_arg => undef,
132             );
133              
134             has type => (
135             is => 'ro',
136             isa => enum([qw(requires recommends suggests)]),
137             default => 'requires',
138             );
139              
140             sub scan_file_reqs {
141 91     91 0 282 my ($self, $file) = @_;
142 91         3979 return $self->_scanner->scan_ppi_document($self->ppi_document_for_file($file))
143             }
144              
145             sub register_prereqs {
146 29     29 0 75 my $self = shift;
147              
148 29         1555 my $type = $self->type;
149              
150 29         218 my $reqs_by_phase = $self->scan_prereqs;
151 29         200 while (my ($phase, $reqs) = each %$reqs_by_phase) {
152 116         5351 $self->zilla->register_prereqs({ phase => $phase, type => $type }, %$reqs);
153             }
154             }
155              
156             __PACKAGE__->meta->make_immutable;
157             1;
158              
159             __END__
160              
161             =pod
162              
163             =encoding UTF-8
164              
165             =head1 NAME
166              
167             Dist::Zilla::Plugin::AutoPrereqs - automatically extract prereqs from your modules
168              
169             =head1 VERSION
170              
171             version 6.037
172              
173             =head1 SYNOPSIS
174              
175             In your F<dist.ini>:
176              
177             [AutoPrereqs]
178             skip = ^Foo|Bar$
179             skip = ^Other::Dist
180              
181             =head1 DESCRIPTION
182              
183             This plugin will extract loosely your distribution prerequisites from
184             your files using L<Perl::PrereqScanner>.
185              
186             If some prereqs are not found, you can still add them manually with the
187             L<Prereqs|Dist::Zilla::Plugin::Prereqs> plugin.
188              
189             This plugin will skip the modules shipped within your dist.
190              
191             B<Note>, if you have any non-Perl files in your C<t/> directory or other
192             directories being scanned, be sure to mark those files' encoding as C<bytes>
193             with the L<Encoding|Dist::Zilla::Plugin::Encoding> plugin so they won't be
194             scanned:
195              
196             [Encoding]
197             encoding = bytes
198             match = ^t/data/
199              
200             =head1 PERL VERSION
201              
202             This module should work on any version of perl still receiving updates from
203             the Perl 5 Porters. This means it should work on any version of perl
204             released in the last two to three years. (That is, if the most recently
205             released version is v5.40, then this module should work on both v5.40 and
206             v5.38.)
207              
208             Although it may work on older versions of perl, no guarantee is made that the
209             minimum required version will not be increased. The version may be increased
210             for any reason, and there is no promise that patches will be accepted to
211             lower the minimum required perl.
212              
213             =head1 ATTRIBUTES
214              
215             =head2 finder
216              
217             This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
218             whose files will be scanned to determine runtime prerequisites. It
219             may be specified multiple times. The default value is
220             C<:InstallModules> and C<:ExecFiles>.
221              
222             =head2 test_finder
223              
224             Just like C<finder>, but for test-phase prerequisites. The default
225             value is C<:TestFiles>.
226              
227             =head2 configure_finder
228              
229             Just like C<finder>, but for configure-phase prerequisites. There is
230             no default value; AutoPrereqs will not determine configure-phase
231             prerequisites unless you set configure_finder.
232              
233             =head2 develop_finder
234              
235             Just like C<finder>, but for develop-phase prerequisites. The default value
236             is C<:ExtraTestFiles>.
237              
238             =head2 skips
239              
240             This is an arrayref of regular expressions, derived from all the 'skip' lines
241             in the configuration. Any module names matching any of these regexes will not
242             be registered as prerequisites.
243              
244             =head2 relationship
245              
246             The relationship used for the registered prerequisites. The default value is
247             'requires'; other options are 'recommends' and 'suggests'.
248              
249             =head2 extra_scanners
250              
251             This is an arrayref of scanner names (as expected by L<Perl::PrereqScanner>).
252             It will be passed as the C<extra_scanners> parameter to L<Perl::PrereqScanner>.
253              
254             =head2 scanners
255              
256             This is an arrayref of scanner names (as expected by L<Perl::PrereqScanner>).
257             If present, it will be passed as the C<scanners> parameter to
258             L<Perl::PrereqScanner>, which means that it will replace the default list
259             of scanners.
260              
261             =head1 SEE ALSO
262              
263             L<Prereqs|Dist::Zilla::Plugin::Prereqs>, L<Perl::PrereqScanner>.
264              
265             =head1 CREDITS
266              
267             This plugin was originally contributed by Jerome Quelin.
268              
269             =head1 AUTHOR
270              
271             Ricardo SIGNES 😏 <cpan@semiotic.systems>
272              
273             =head1 COPYRIGHT AND LICENSE
274              
275             This software is copyright (c) 2026 by Ricardo SIGNES.
276              
277             This is free software; you can redistribute it and/or modify it under
278             the same terms as the Perl 5 programming language system itself.
279              
280             =cut