blib/lib/Dist/Zilla/Role/PrereqScanner.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 100% 16 / 16 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 1 totaltotal 95.5% 108 / 113 CC 21 SCAR 27.4
CC
21
Cov
98%
scan_prereqs
21.0
__ANON__
1.1
BEGIN
1.0
CRAP 15.5
« blib/lib/Dist/Zilla/Role/PluginBundle/Easy.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PruneCruft.pm »
1 package Dist::Zilla::Role::PrereqScanner 6.040;
2 # ABSTRACT: automatically extract prereqs from your modules
3  
4 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 with(
6 'Dist::Zilla::Role::FileFinderUser' => {
7 default_finders => [ ':InstallModules', ':ExecFiles' ],
8 },
9 'Dist::Zilla::Role::FileFinderUser' => {
10 method => 'found_test_files',
11 finder_arg_names => [ 'test_finder' ],
12 default_finders => [ ':TestFiles' ],
13 },
14 'Dist::Zilla::Role::FileFinderUser' => {
15 method => 'found_configure_files',
16 finder_arg_names => [ 'configure_finder' ],
17 default_finders => [],
18 },
19 'Dist::Zilla::Role::FileFinderUser' => {
20 method => 'found_develop_files',
21 finder_arg_names => [ 'develop_finder' ],
22 default_finders => [ ':ExtraTestFiles' ],
23 },
24 );
25  
26 6 ▶use Dist::Zilla::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 6 ▶use MooseX::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #pod =attr finder
33 #pod
34 #pod This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
35 #pod whose files will be scanned to determine runtime prerequisites. It
36 #pod may be specified multiple times. The default value is
37 #pod C<:InstallModules> and C<:ExecFiles>.
38 #pod
39 #pod =attr test_finder
40 #pod
41 #pod Just like C<finder>, but for test-phase prerequisites. The default
42 #pod value is C<:TestFiles>.
43 #pod
44 #pod =attr configure_finder
45 #pod
46 #pod Just like C<finder>, but for configure-phase prerequisites. There is
47 #pod no default value; AutoPrereqs will not determine configure-phase
48 #pod prerequisites unless you set configure_finder.
49 #pod
50 #pod =attr develop_finder
51 #pod
52 #pod Just like <finder>, but for develop-phase prerequisites. The default value
53 #pod is C<:ExtraTestFiles>.
54 #pod
55 #pod =attr skips
56 #pod
57 #pod This is an arrayref of regular expressions, derived from all the 'skip' lines
58 #pod in the configuration. Any module names matching any of these regexes will not
59 #pod be registered as prerequisites.
60 #pod
61 #pod =cut
62  
63 6 has skips => (
64 is => 'ro',
65 isa => 'ArrayRef[Str]',
66 );
67  
68 30 ▶around mvp_multivalue_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 30 my ($orig, $self) = @_;
70 30 ($self->$orig, 'skips')
71 6 };
72  
73 30 ▶around mvp_aliases => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 30 my ($orig, $self) = @_;
75 30 my $aliases = $self->$orig;
76 30 $aliases->{skip} = 'skips';
77 30 return $aliases
78 6 };
79  
80 6 requires 'scan_file_reqs';
81  
82 # this is a bunk heuristic and can still capture strings from pod - the
83 # proper thing to do is grab all packages from Module::Metadata
84 95 ▶sub _declared_packages {
Subroutine _declared_packages: called CC=1 SCAR=0.0
85 95 my ($self, $file) = @_;
86 95 return $file->content =~ /^[^#]*?(?:^|\s)package\s+([^\s;#]+)/mg;
87 }
88  
89 30 ▶sub scan_prereqs {
Subroutine scan_prereqs: called CC=21 SCAR=30.4
Pod undocumented
90 30 my $self = shift;
91  
92 30 require CPAN::Meta::Requirements;
93 30 require List::Util;
94 30 List::Util->VERSION(1.45); # uniq
95  
96 # not a hash, because order is important
97 30 my @sets = (
98 # phase => file finder method
99 [ configure => 'found_configure_files' ], # must come before runtime
100 [ runtime => 'found_files' ],
101 [ test => 'found_test_files' ],
102 [ develop => 'found_develop_files' ],
103 );
104  
105 30 my %reqs_by_phase;
106 my %runtime_final;
107 30 my @modules;
108  
109 # The file finders normally cover the main module, but a main_module outside
110 # of lib/ (like a top-level .pm file) is invisible to them, so its packages
111 # get left in prereqs.
112 30 ▶ if ($self->zilla->_has_main_module_override) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zilla->_has_main_module_override) 1 29 30
113 1 push @modules, $self->_declared_packages($self->zilla->main_module);
114 }
115  
116 30 for my $fileset (@sets) {
117 120 my ($phase, $method) = @$fileset;
118  
119 120 my $req = CPAN::Meta::Requirements->new;
120 120 my $files = $self->$method;
121  
122 120 foreach my $file (@$files) {
123 # skip binary files
124 120 ▶ next if $file->is_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $file->is_bytes 19 101 120
125 # parse only perl files
126 101 ▶ next unless $file->name =~ /\.(?:pm|pl|t|psgi)$/i
Branch: 1 branch
BranchTrueFalseTotal
unless $file->name =~ /\.(?:pm|pl|t|psgi)$/iu or $file->content =~ /^#!(?:.*)perl(?:$|\s)/u 5 96 101
Condition100%3/3
$file->name =~ /\.(?:pm|pl|t|psgi)$/iu or $file->content =~ /^#!(?:.*)perl(?:$|\s)/u
l!l&&r!l&&!r
72245
MC/DC100%2/2
$file->name =~ /\.(?:pm|pl|t|psgi)$/iu or $file->content =~ /^#!(?:.*)perl(?:$|\s)/u
$file->name =~ /\.(?:pm|pl|t|psgi)$/iu $file->content =~ /^#!(?:.*)perl(?:$|\s)/u
Truth table100%3/3
A or B
A= $file->name =~ /\.(?:pm|pl|t|psgi)$/iuB= $file->content =~ /^#!(?:.*)perl(?:$|\s)/u
ABresult
1X1
011
000
127 || $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
128 # RT#76305 skip extra tests produced by ExtraTests plugin
129 96 ▶ next if $file->name =~ m{^t/(?:author|release)-[^/]*\.t$};
Branch: 1 branch
BranchTrueFalseTotal
if $file->name =~ m[^t/(?:author|release)-[^/]*\.t$]u 2 94 96
130  
131 # store module name, to trim it from require list later on
132 94 my @this_thing = $file->name;
133  
134 # t/lib/Foo.pm is treated as providing t::lib::Foo, lib::Foo, and Foo
135 94 ▶ if ($this_thing[0] =~ /^t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_thing[0] =~ /^t/u) { } 7 87 94
136 7 push @this_thing, ($this_thing[0]) x 2;
137 7 $this_thing[1] =~ s{^t/}{};
138 7 $this_thing[2] =~ s{^t/lib/}{};
139 } else {
140 87 $this_thing[0] =~ s{^lib/}{};
141 }
142 94 s{\.pm$}{} for @this_thing;
143 94 s{/}{::}g for @this_thing;
144  
145 94 push @this_thing, $self->_declared_packages($file);
146 94 push @modules, @this_thing;
147  
148 # parse a file, and merge with existing prereqs
149 94 $self->log_debug([ 'scanning %s for %s prereqs', $file->name, $phase ]);
150 94 my $file_req = $self->scan_file_reqs($file);
151  
152 94 $req->add_requirements($file_req);
153  
154 }
155  
156 # remove prereqs from skiplist
157 120 ▶ for my $skip (@{ $self->skips || [] }) {
Condition100%2/2
$self->skips || []
l!l
9228
MC/DC100%1/1
$self->skips || []
$self->skips
Truth table100%2/2
A
A= $self->skips
Aresult
11
00
158 92 my $re = qr/$skip/;
159  
160 92 foreach my $k ($req->required_modules) {
161 634 ▶ $req->clear_requirement($k) if $k =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /$re/u 46 588 634
162 }
163 }
164  
165 # remove prereqs shipped with current dist
166 120 ▶ if (@modules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@modules) 94 26 120
167 $self->log_debug([
168 'excluding local packages: %s',
169 0 ▶ sub { join(', ', List::Util::uniq(@modules)) } ]
Subroutine __ANON__: not called CC=1 SCAR=1.2
170 )
171 }
172 120 $req->clear_requirement($_) for @modules;
173  
174 120 $req->clear_requirement($_) for qw(Config DB Errno NEXT Pod::Functions); # never indexed
175  
176 # we're done, return what we've found
177 120 my %got = %{ $req->as_string_hash };
178 120 ▶ if ($phase eq 'runtime') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq 'runtime') { } 30 90 120
179 30 %runtime_final = %got;
180 } else {
181 # do not test-require things required for runtime
182 90 delete $got{$_} for
183 1082 ▶ grep { exists $got{$_} and $runtime_final{$_} ge $got{$_} }
Condition66%2/3
exists $got{$_} && $runtime_final{$_} ge $got{$_}
!ll&&!rl&&r
107606
MC/DC50%1/2
exists $got{$_} && $runtime_final{$_} ge $got{$_}
exists $got{$_} $runtime_final{$_} ge $got{$_}
Truth table66%2/3
A && B
A= exists $got{$_}B= $runtime_final{$_} ge $got{$_}
ABresult
0X0
100
111
184 keys %runtime_final;
185 }
186  
187 120 $reqs_by_phase{$phase} = \%got;
188 }
189  
190 30 return \%reqs_by_phase
191 }
192  
193 6 1;
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 Dist::Zilla::Role::PrereqScanner - automatically extract prereqs from your modules
202  
203 =head1 VERSION
204  
205 version 6.040
206  
207 =head1 PERL VERSION
208  
209 This module should work on any version of perl still receiving updates from
210 the Perl 5 Porters. This means it should work on any version of perl
211 released in the last two to three years. (That is, if the most recently
212 released version is v5.40, then this module should work on both v5.40 and
213 v5.38.)
214  
215 Although it may work on older versions of perl, no guarantee is made that the
216 minimum required version will not be increased. The version may be increased
217 for any reason, and there is no promise that patches will be accepted to
218 lower the minimum required perl.
219  
220 =head1 ATTRIBUTES
221  
222 =head2 finder
223  
224 This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
225 whose files will be scanned to determine runtime prerequisites. It
226 may be specified multiple times. The default value is
227 C<:InstallModules> and C<:ExecFiles>.
228  
229 =head2 test_finder
230  
231 Just like C<finder>, but for test-phase prerequisites. The default
232 value is C<:TestFiles>.
233  
234 =head2 configure_finder
235  
236 Just like C<finder>, but for configure-phase prerequisites. There is
237 no default value; AutoPrereqs will not determine configure-phase
238 prerequisites unless you set configure_finder.
239  
240 =head2 develop_finder
241  
242 Just like <finder>, but for develop-phase prerequisites. The default value
243 is C<:ExtraTestFiles>.
244  
245 =head2 skips
246  
247 This is an arrayref of regular expressions, derived from all the 'skip' lines
248 in the configuration. Any module names matching any of these regexes will not
249 be registered as prerequisites.
250  
251 =head1 SEE ALSO
252  
253 L<Dist::Zilla::Plugin::AutoPrereqs>.
254  
255 =head1 CREDITS
256  
257 The role was provided by Olivier Mengué (DOLMEN) and Philippe Bruhat (BOOK) at Perl QA Hackathon 2016
258 (but it is just a refactor of the AutoPrereqs plugin).
259  
260 =head1 AUTHOR
261  
262 Ricardo SIGNES 😏 <cpan@semiotic.systems>
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 This software is copyright (c) 2026 by Ricardo SIGNES.
267  
268 This is free software; you can redistribute it and/or modify it under
269 the same terms as the Perl 5 programming language system itself.
270  
271 =cut
272  
273 __END__
« blib/lib/Dist/Zilla/Role/PluginBundle/Easy.pm ↑ Summary blib/lib/Dist/Zilla/Plugin/PruneCruft.pm »