File Coverage

blib/lib/Dist/Zilla/Plugin/Prereqs/Plugins.pm
Criterion Covered Total %
statement 70 73 95.8
branch 12 16 75.0
condition 1 2 50.0
subroutine 15 15 100.0
pod 2 2 100.0
total 100 108 92.5


line stmt bran cond sub pod time code
1 4     4   6927864 use 5.006;
  4         14  
2 4     4   23 use strict;
  4         7  
  4         136  
3 4     4   36 use warnings;
  4         19  
  4         384  
4              
5             package Dist::Zilla::Plugin::Prereqs::Plugins;
6              
7             our $VERSION = '1.003003';
8              
9             # ABSTRACT: Add all Dist::Zilla plugins presently in use as prerequisites.
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 4     4   614 use Moose qw( with has around );
  4         364700  
  4         42  
14 4     4   21279 use Dist::Zilla::Util;
  4         9609  
  4         140  
15 4     4   442 use MooseX::Types::Moose qw( HashRef ArrayRef Str );
  4         46379  
  4         50  
16 4     4   20287 use Dist::Zilla::Util::BundleInfo;
  4         87394  
  4         138  
17 4     4   2302 use Dist::Zilla::Util::ExpandINI::Reader;
  4         48783  
  4         142  
18 4     4   27 use Module::Runtime qw( require_module );
  4         6  
  4         34  
19 4     4   1139 use Path::Tiny qw( path );
  4         11819  
  4         1479  
20             with 'Dist::Zilla::Role::PrereqSource';
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42             has phase => ( is => ro =>, isa => Str, lazy => 1, default => sub { 'develop' }, );
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54              
55              
56              
57              
58              
59              
60              
61              
62              
63              
64             has relation => ( is => ro =>, isa => Str, lazy => 1, default => sub { 'requires' }, );
65              
66              
67              
68              
69              
70              
71              
72              
73              
74              
75              
76              
77              
78             has exclude => ( is => ro =>, isa => ArrayRef [Str], lazy => 1, default => sub { [] } );
79              
80              
81              
82              
83              
84             has _exclude_hash => ( is => ro =>, isa => HashRef [Str], lazy => 1, builder => '_build__exclude_hash' );
85              
86              
87              
88              
89              
90              
91              
92              
93              
94 3     3 1 1062 sub mvp_multivalue_args { return qw(exclude) }
95              
96              
97              
98              
99              
100             sub _build__exclude_hash {
101 3     3   6 my ( $self, ) = @_;
102 3         5 return { map { ( $_ => 1 ) } @{ $self->exclude } };
  0         0  
  3         95  
103             }
104             around dump_config => sub {
105             my ( $orig, $self, @args ) = @_;
106             my $config = $self->$orig(@args);
107             my $localconf = $config->{ +__PACKAGE__ } = {};
108              
109             $localconf->{phase} = $self->phase;
110             $localconf->{relation} = $self->relation;
111             $localconf->{exclude} = $self->exclude;
112              
113             $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION
114             unless __PACKAGE__ eq ref $self;
115              
116             return $config;
117             };
118              
119             __PACKAGE__->meta->make_immutable;
120 4     4   27 no Moose;
  4         13  
  4         35  
121              
122             sub _register_plugin_prereq {
123 9     9   41 my ( $self, $package, $lines ) = @_;
124 9 50       356 return if exists $self->_exclude_hash->{$package};
125 9         257 $self->zilla->register_prereqs( { phase => $self->phase, type => $self->relation }, $package, 0 );
126 9 50       2380 return unless @{ $lines || [] };
  9 100       42  
127 7         8 while ( @{$lines} ) {
  29         794  
128 22         17 my $key = shift @{$lines};
  22         30  
129 22         19 my $value = shift @{$lines};
  22         20  
130 22 100       43 next unless q[:version] eq $key;
131 4         100 $self->zilla->register_prereqs( { phase => $self->phase, type => $self->relation }, $package, $value );
132             }
133 7         26 return;
134             }
135              
136              
137              
138              
139              
140              
141              
142             sub register_prereqs {
143 3     3 1 560347 my ($self) = @_;
144 3         38 my $reader = Dist::Zilla::Util::ExpandINI::Reader->new();
145 3         166 my $ini = path( $self->zilla->root )->child('dist.ini');
146 3 50       250 if ( not $ini->exists ) {
147 0         0 $self->log_fatal(q[Prereqs::Plugins only works on dist.ini due to :version hidden since 5.032]);
148 0         0 return;
149             }
150 3         93 my (@sections) = @{ $reader->read_file("$ini") };
  3         9  
151 3         2518 while (@sections) {
152 8         14 my ($section) = shift @sections;
153              
154             # Special case for Dzil
155 8 100 50     33 if ( '_' eq ( $section->{name} || q[] ) ) {
156 3         13 $self->_register_plugin_prereq( q[Dist::Zilla], $section->{lines} );
157 3         7 next;
158             }
159 5         31 my $package_expanded = Dist::Zilla::Util->expand_config_package_name( $section->{package} );
160              
161             # Standard plugin.
162 5 100       167 if ( $section->{package} !~ /\A\@/msx ) {
163 4         12 $self->_register_plugin_prereq( $package_expanded, $section->{lines} );
164 4         13 next;
165             }
166              
167             # Bundle
168             # TODO: Maybe register the bundle itself?
169 1 50       34 next if exists $self->_exclude_hash->{$package_expanded};
170              
171             # Handle bundle
172             my $bundle = Dist::Zilla::Util::BundleInfo->new(
173             bundle_name => $section->{package},
174             bundle_payload => $section->{lines},
175 1         10 );
176              
177 1         1740 for my $plugin ( $bundle->plugins ) {
178 2         7636 $self->_register_plugin_prereq( $plugin->module, [ $plugin->payload_list ] );
179             }
180             }
181 3         96 return $self->zilla->prereqs;
182             }
183              
184              
185             1;
186              
187             __END__
188              
189             =pod
190              
191             =encoding UTF-8
192              
193             =head1 NAME
194              
195             Dist::Zilla::Plugin::Prereqs::Plugins - Add all Dist::Zilla plugins presently in use as prerequisites.
196              
197             =head1 VERSION
198              
199             version 1.003003
200              
201             =head1 SYNOPSIS
202              
203             [Prereqs::Plugins]
204             ; all plugins are now develop.requires deps
205              
206             [Prereqs::Plugins]
207             phase = runtime ; all plugins are now runtime.requires deps
208              
209             =head1 DESCRIPTION
210              
211             This is mostly because I am lazy, and the lengthy list of hand-updated dependencies
212             on my C<@Author::> bundle started to get overwhelming, and I'd periodically miss something.
213              
214             This module is kinda C<AutoPrereqs>y, but in ways that I can't imagine being plausible with
215             a generic C<AutoPrereqs> tool, at least, not without requiring some nasty re-implementation
216             of how C<dist.ini> is parsed.
217              
218             =head1 METHODS
219              
220             =head2 C<mvp_multivalue_args>
221              
222             The list of attributes that can be specified multiple times
223              
224             exclude
225              
226             =head2 C<register_prereqs>
227              
228             See L<<< C<< Dist::Zilla::Role::B<PrereqSource> >>|Dist::Zilla::Role::PrereqSource >>>
229              
230             =head1 ATTRIBUTES
231              
232             =head2 C<phase>
233              
234             The target installation phase to inject into:
235              
236             =over 4
237              
238             =item * C<runtime>
239              
240             =item * C<configure>
241              
242             =item * C<build>
243              
244             =item * C<test>
245              
246             =item * C<develop>
247              
248             =back
249              
250             =head2 C<relation>
251              
252             The type of dependency relation to create:
253              
254             =over 4
255              
256             =item * C<requires>
257              
258             =item * C<recommends>
259              
260             =item * C<suggests>
261              
262             =item * C<conflicts>
263              
264             Though think incredibly hard before using this last one ;)
265              
266             =back
267              
268             =head2 C<exclude>
269              
270             Specify anything you want excluded here.
271              
272             May Be specified multiple times.
273              
274             [Prereqs::Plugins]
275             exclude = Some::Module::Thingy
276             exclude = Some::Other::Module::Thingy
277              
278             =head1 PRIVATE ATTRIBUTES
279              
280             =head2 C<_exclude_hash>
281              
282             =head1 PRIVATE METHODS
283              
284             =head2 C<_build__exclude_hash>
285              
286             =head1 LIMITATIONS
287              
288             =over 4
289              
290             =item * This module will B<NOT> report C<@Bundles> as dependencies at present.
291              
292             =item * This module will B<NOT> I<necessarily> include B<ALL> dependencies, but is only intended to include the majority of them.
293              
294             =item * This module will not report I<injected> dependencies, only dependencies that can be discovered from the parse tree directly, or from the return values of any indicated bundles.
295              
296             =back
297              
298             =head1 AUTHOR
299              
300             Kent Fredric <kentnl@cpan.org>
301              
302             =head1 COPYRIGHT AND LICENSE
303              
304             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
305              
306             This is free software; you can redistribute it and/or modify it under
307             the same terms as the Perl 5 programming language system itself.
308              
309             =cut