forked from rjbs/Dist-Zilla
/
AutoPrereqs.pm
196 lines (147 loc) · 5.09 KB
/
AutoPrereqs.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package Dist::Zilla::Plugin::AutoPrereqs;
use Moose;
with(
'Dist::Zilla::Role::PrereqSource',
'Dist::Zilla::Role::FileFinderUser' => {
default_finders => [ ':InstallModules', ':ExecFiles' ],
},
'Dist::Zilla::Role::FileFinderUser' => {
method => 'found_test_files',
finder_arg_names => [ 'test_finder' ],
default_finders => [ ':TestFiles' ],
},
'Dist::Zilla::Role::FileFinderUser' => {
method => 'found_configure_files',
finder_arg_names => [ 'configure_finder' ],
default_finders => [],
},
);
=attr finder
This is the name of a L<FileFinder|Dist::Zilla::Role::FileFinder>
whose files will be scanned to determine runtime prerequisites. It
may be specified multiple times. The default value is
C<:InstallModules> and C<:ExecFiles>.
=attr test_finder
Just like C<finder>, but for test-phase prerequisites. The default
value is C<:TestFiles>.
=attr configure_finder
Just like C<finder>, but for configure-phase prerequisites. There is
no default value; AutoPrereqs will not determine configure-phase
prerequisites unless you set configure_finder.
=cut
use namespace::autoclean;
# ABSTRACT: automatically extract prereqs from your modules
use List::AllUtils 'uniq';
use Moose::Autobox;
use Perl::PrereqScanner 1.005; # do not prune common libs
use PPI;
use CPAN::Meta::Requirements;
use version;
=head1 SYNOPSIS
In your F<dist.ini>:
[AutoPrereqs]
skip = ^Foo|Bar$
skip = ^Other::Dist
=head1 DESCRIPTION
This plugin will extract loosely your distribution prerequisites from
your files using L<Perl::PrereqScanner>.
If some prereqs are not found, you can still add them manually with the
L<Prereqs|Dist::Zilla::Plugin::Prereqs> plugin.
This plugin will skip the modules shipped within your dist.
=attr extra_scanners
This is an arrayref of scanner names (as expected by Perl::PrereqScanner).
It will be passed as the C<extra_scanners> parameter to Perl::PrereqScanner.
=attr scanners
This is an arrayref of scanner names (as expected by Perl::PrereqScanner).
If present, it will be passed as the C<scanners> parameter to
Perl::PrereqScanner, which means that it will replace the default list
of scanners.
=attr skips
This is an arrayref of regular expressions, derived from all the 'skip' lines
in the configuration. Any module names matching any of these regexes will not
be registered as prerequisites.
=head1 SEE ALSO
L<Prereqs|Dist::Zilla::Plugin::Prereqs>, L<Perl::PrereqScanner>.
=head1 CREDITS
This plugin was originally contributed by Jerome Quelin.
=cut
sub mvp_multivalue_args { qw(extra_scanners scanners skips) }
sub mvp_aliases { return { extra_scanner => 'extra_scanners',
scanner => 'scanners', skip => 'skips' } }
has extra_scanners => (
is => 'ro',
isa => 'ArrayRef[Str]',
default => sub { [] },
);
has scanners => (
is => 'ro',
isa => 'ArrayRef[Str]',
predicate => 'has_scanners',
);
has skips => (
is => 'ro',
isa => 'ArrayRef[Str]',
);
sub register_prereqs {
my $self = shift;
my @modules;
my $scanner = Perl::PrereqScanner->new(
($self->has_scanners ? (scanners => $self->scanners) : ()),
extra_scanners => $self->extra_scanners,
);
my @sets = (
[ configure => 'found_configure_files' ], # must come before runtime
[ runtime => 'found_files' ],
[ test => 'found_test_files' ],
);
my %runtime_final;
for my $fileset (@sets) {
my ($phase, $method) = @$fileset;
my $req = CPAN::Meta::Requirements->new;
my $files = $self->$method;
foreach my $file (@$files) {
# parse only perl files
next unless $file->name =~ /\.(?:pm|pl|t|psgi)$/i
|| $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
# RT#76305 skip extra tests produced by ExtraTests plugin
next if $file->name =~ m{^t/(?:author|release)-[^/]*\.t$};
# store module name, to trim it from require list later on
my @this_thing = $file->name;
if ($this_thing[0] =~ /^t/) {
push @this_thing, ($this_thing[0]) x 2;
$this_thing[1] =~ s{^t/}{};
$this_thing[2] =~ s{^t/lib/}{};
} else {
$this_thing[0] =~ s{^lib/}{};
}
@this_thing = uniq @this_thing;
s{\.pm$}{} for @this_thing;
s{/}{::}g for @this_thing;
push @modules, @this_thing;
# parse a file, and merge with existing prereqs
my $file_req = $scanner->scan_string($file->content);
$req->add_requirements($file_req);
}
# remove prereqs shipped with current dist
$req->clear_requirement($_) for @modules;
# remove prereqs from skiplist
for my $skip (($self->skips || [])->flatten) {
my $re = qr/$skip/;
foreach my $k ($req->required_modules) {
$req->clear_requirement($k) if $k =~ $re;
}
}
# we're done, return what we've found
my %got = %{ $req->as_string_hash };
if ($phase eq 'runtime') {
%runtime_final = %got;
} else {
delete $got{$_} for
grep { exists $got{$_} and $runtime_final{$_} ge $got{$_} }
keys %runtime_final;
}
$self->zilla->register_prereqs({ phase => $phase }, %got);
}
}
__PACKAGE__->meta->make_immutable;
1;