File Coverage

blib/lib/Dist/Zilla/Plugin/AutoPrereqsFast.pm
Criterion Covered Total %
statement 47 50 94.0
branch 11 16 68.7
condition 2 3 66.6
subroutine 4 4 100.0
pod 0 2 0.0
total 64 75 85.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::AutoPrereqsFast;
2             $Dist::Zilla::Plugin::AutoPrereqsFast::VERSION = '0.002';
3 1     1   1885063 use Moose;
  1         2  
  1         8  
4             with(
5             'Dist::Zilla::Role::PrereqSource',
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             );
20              
21             sub mvp_multivalue_args { qw(extra_scanners skips) }
22             sub mvp_aliases {
23             return {
24 1     1 0 147 extra_scanner => 'extra_scanners',
25             skip => 'skips'
26             }
27             }
28              
29             has extra_scanners => (
30             is => 'ro',
31             isa => 'ArrayRef[Str]',
32             default => sub { [ 'Moose' ] },
33             );
34              
35             has skips => (
36             is => 'ro',
37             isa => 'ArrayRef[Str]',
38             );
39              
40             sub register_prereqs {
41 1     1 0 172692 my $self = shift;
42              
43 1         527 require Perl::PrereqScanner::Lite;
44 1         7182 require CPAN::Meta::Requirements;
45 1         3 require List::Util; # uniq
46              
47 1         2 my @modules;
48              
49 1         5 my @sets = (
50             [ configure => 'found_configure_files' ], # must come before runtime
51             [ runtime => 'found_files' ],
52             [ test => 'found_test_files' ],
53             );
54              
55 1         2 my %runtime_final;
56              
57 1         3 for my $fileset (@sets) {
58 3         1222 my ($phase, $method) = @$fileset;
59              
60 3         113 my $scanner = Perl::PrereqScanner::Lite->new({ extra_scanners => $self->extra_scanners });
61 3         1138 my $req = CPAN::Meta::Requirements->new;
62              
63 3         31 my $files = $self->$method;
64              
65 3         2644 foreach my $file (@$files) {
66             # skip binary files
67 4 100       551 next if $file->is_bytes;
68             # parse only perl files
69 3 50 66     126 next unless $file->name =~ /\.(?:pm|pl|t|psgi)$/i
70             || $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
71             # RT#76305 skip extra tests produced by ExtraTests plugin
72 3 50       868 next if $file->name =~ m{^t/(?:author|release)-[^/]*\.t$};
73              
74             # store module name, to trim it from require list later on
75 3         99 my @this_thing = $file->name;
76              
77             # t/lib/Foo.pm is treated as providing t::lib::Foo, lib::Foo, and Foo
78 3 50       91 if ($this_thing[0] =~ /^t/) {
79 0         0 push @this_thing, ($this_thing[0]) x 2;
80 0         0 $this_thing[1] =~ s{^t/}{};
81 0         0 $this_thing[2] =~ s{^t/lib/}{};
82             } else {
83 3         8 $this_thing[0] =~ s{^lib/}{};
84             }
85 3         10 s{\.pm$}{} for @this_thing;
86 3         8 s{/}{::}g for @this_thing;
87              
88 3         7 push @this_thing, $file->content =~ /package\s+([^\s;]+)/g;
89 3         1014 push @modules, List::Util::uniq(@this_thing);
90              
91             # parse a file, and merge with existing prereqs
92 3         10 $self->log_debug([ 'scanning %s for %s prereqs', $file->name, $phase ]);
93 3         246 my $file_req = $scanner->scan_string($file->content);
94              
95 3         2287 $req->add_requirements($file_req);
96             }
97              
98             # remove prereqs shipped with current dist
99 3         822 $req->clear_requirement($_) for @modules;
100              
101 3         35 $req->clear_requirement($_) for qw(Config Errno); # never indexed
102              
103             # remove prereqs from skiplist
104 3 50       30 for my $skip (@{ $self->skips || [] }) {
  3         97  
105 3         18 my $re = qr/$skip/;
106              
107 3         9 foreach my $k ($req->required_modules) {
108 23 100       74 $req->clear_requirement($k) if $k =~ $re;
109             }
110             }
111              
112             # we're done, return what we've found
113 3         13 my %got = %{ $req->as_string_hash };
  3         7  
114 3 100       120 if ($phase eq 'runtime') {
115 1         6 %runtime_final = %got;
116             } else {
117 2         7 delete $got{$_} for
118 21 50       28 grep { exists $got{$_} and $runtime_final{$_} ge $got{$_} }
119             keys %runtime_final;
120             }
121              
122 3         76 $self->zilla->register_prereqs({ phase => $phase }, %got);
123             }
124             }
125              
126             __PACKAGE__->meta->make_immutable;
127 1     1   4935 no Moose;
  1         1  
  1         5  
128              
129             1;
130              
131              
132             # ABSTRACT: Automatically extract prereqs from your modules, but faster
133              
134             __END__
135              
136             =pod
137              
138             =encoding UTF-8
139              
140             =head1 NAME
141              
142             Dist::Zilla::Plugin::AutoPrereqsFast - Automatically extract prereqs from your modules, but faster
143              
144             =head1 VERSION
145              
146             version 0.002
147              
148             =head1 SYNOPSIS
149              
150             In your F<dist.ini>:
151              
152             [AutoPrereqsFast]
153             skip = ^Foo|Bar$
154             skip = ^Other::Dist
155              
156             =head1 DESCRIPTION
157              
158             This plugin will extract loosely your distribution prerequisites from
159             your files using L<Perl::PrereqScanner::Lite>.
160              
161             If some prereqs are not found, you can still add them manually with the
162             L<Prereqs|Dist::Zilla::Plugin::Prereqs> plugin.
163              
164             This plugin will skip the modules shipped within your dist.
165              
166             =head1 ATTRIBUTES
167              
168             =head2 finder
169              
170             This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
171             whose files will be scanned to determine runtime prerequisites. It
172             may be specified multiple times. The default value is
173             C<:InstallModules> and C<:ExecFiles>.
174              
175             =head2 test_finder
176              
177             Just like C<finder>, but for test-phase prerequisites. The default
178             value is C<:TestFiles>.
179              
180             =head2 configure_finder
181              
182             Just like C<finder>, but for configure-phase prerequisites. There is
183             no default value; AutoPrereqs will not determine configure-phase
184             prerequisites unless you set configure_finder.
185              
186             =head2 extra_scanners
187              
188             This is an arrayref of scanner names (as expected by Perl::PrereqScanner::Lite).
189             It will be passed as the C<extra_scanners> parameter to Perl::PrereqScanner::Lite.
190              
191             =head2 skips
192              
193             This is an arrayref of regular expressions, derived from all the 'skip' lines
194             in the configuration. Any module names matching any of these regexes will not
195             be registered as prerequisites.
196              
197             =head1 SEE ALSO
198              
199             L<Prereqs|Dist::Zilla::Plugin::Prereqs>, L<Perl::PrereqScanner::Lite>.
200              
201             =head1 CREDITS
202              
203             This plugin includes code by Jerome Quelin and Ricardo Signes
204              
205             =head1 AUTHOR
206              
207             Leon Timmermans <leont@cpan.org>
208              
209             =head1 COPYRIGHT AND LICENSE
210              
211             This software is copyright (c) 2015 by Leon Timmermans.
212              
213             This is free software; you can redistribute it and/or modify it under
214             the same terms as the Perl 5 programming language system itself.
215              
216             =cut