File Coverage

blib/lib/Dist/Zilla/Prereqs.pm
Criterion Covered Total %
statement 40 40 100.0
branch 1 2 50.0
condition 5 6 83.3
subroutine 9 9 100.0
pod 1 2 50.0
total 56 59 94.9


line stmt bran cond sub pod time code
1             # ABSTRACT: the prerequisites of a Dist::Zilla distribution
2              
3             use Moose;
4 52     52   405  
  52         123  
  52         519  
5             use Dist::Zilla::Pragmas;
6 52     52   383362  
  52         149  
  52         470  
7             use MooseX::Types::Moose qw(Bool HashRef);
8 52     52   437  
  52         152  
  52         915  
9             use CPAN::Meta::Prereqs 2.120630; # add_string_requirement
10 52     52   294097 use String::RewritePrefix;
  52         336923  
  52         1613  
11 52     52   404 use CPAN::Meta::Requirements 2.121; # requirements_for_module
  52         136  
  52         426  
12 52     52   11096  
  52         717  
  52         1244  
13             use namespace::autoclean;
14 52     52   307  
  52         123  
  52         520  
15             #pod =head1 DESCRIPTION
16             #pod
17             #pod Dist::Zilla::Prereqs is a subcomponent of Dist::Zilla. The C<prereqs>
18             #pod attribute on your Dist::Zilla object is a Dist::Zilla::Prereqs object, and is
19             #pod responsible for keeping track of the distribution's prerequisites.
20             #pod
21             #pod In fact, a Dist::Zilla::Prereqs object is just a thin layer over a
22             #pod L<CPAN::Meta::Prereqs> object, stored in the C<cpan_meta_prereqs> attribute.
23             #pod
24             #pod Almost everything this object does is proxied to the CPAN::Meta::Prereqs
25             #pod object, so you should really read how I<that> works.
26             #pod
27             #pod Dist::Zilla::Prereqs proxies the following methods to the CPAN::Meta::Prereqs
28             #pod object:
29             #pod
30             #pod =for :list
31             #pod * finalize
32             #pod * is_finalized
33             #pod * requirements_for
34             #pod * as_string_hash
35             #pod
36             #pod =cut
37              
38             has cpan_meta_prereqs => (
39             is => 'ro',
40             isa => 'CPAN::Meta::Prereqs',
41             init_arg => undef,
42             default => sub { CPAN::Meta::Prereqs->new },
43             handles => [ qw(
44             finalize
45             is_finalized
46             requirements_for
47             as_string_hash
48             ) ],
49             );
50              
51             # storing this is sort of gross, but MakeMaker winds up needing the same data
52             # anyway. -- xdg, 2013-10-22
53             # This does *not* contain configure requires, as MakeMaker explicitly should
54             # not have it in its fallback prereqs.
55             has merged_requires => (
56             is => 'ro',
57             isa => 'CPAN::Meta::Requirements',
58             init_arg => undef,
59             default => sub { CPAN::Meta::Requirements->new },
60             );
61              
62             #pod =method register_prereqs
63             #pod
64             #pod $prereqs->register_prereqs(%prereqs);
65             #pod
66             #pod $prereqs->register_prereqs(\%arg, %prereqs);
67             #pod
68             #pod This method adds new minimums to the prereqs object. If a hashref is the first
69             #pod arg, it may have entries for C<phase> and C<type> to indicate what kind of
70             #pod prereqs are being registered. (For more information on phase and type, see
71             #pod L<CPAN::Meta::Spec>.) For example, you might say:
72             #pod
73             #pod $prereqs->register_prereqs(
74             #pod { phase => 'test', type => 'recommends' },
75             #pod 'Test::Foo' => '1.23',
76             #pod 'XML::YZZY' => '2.01',
77             #pod );
78             #pod
79             #pod If not given, phase and type default to runtime and requires, respectively.
80             #pod
81             #pod =cut
82              
83             my $self = shift;
84             my $arg = ref($_[0]) ? shift(@_) : {};
85 335     335 1 3452 my %prereq = @_;
86 335 50       920  
87 335         1337 my $phase = $arg->{phase} || 'runtime';
88             my $type = $arg->{type} || 'requires';
89 335   50     951  
90 335   100     920 my $req = $self->requirements_for($phase, $type);
91              
92 335         1289 while (my ($package, $version) = each %prereq) {
93             $req->add_string_requirement($package, $version || 0);
94 335         20295 }
95 819   100     43655  
96             return;
97             }
98 335         31153  
99             before 'finalize' => sub {
100             my ($self) = @_;
101             $self->sync_runtime_build_test_requires;
102             };
103              
104              
105             # this avoids a long-standing CPAN.pm bug that incorrectly merges runtime and
106             # "build" (build+test) requirements by ensuring requirements stay unified
107             # across all three phases
108             my $self = shift;
109              
110             # first pass: generated merged requirements
111 146     146 0 340 for my $phase ( qw/runtime build test/ ) {
112             my $req = $self->requirements_for($phase, 'requires');
113             $self->merged_requires->add_requirements( $req );
114 146         526 };
115 438         17401  
116 438         35655 # second pass: update from merged requirements
117             for my $phase ( qw/runtime build test/ ) {
118             my $req = $self->requirements_for($phase, 'requires');
119             for my $mod ( $req->required_modules ) {
120 146         4094 $req->clear_requirement( $mod );
121 438         12557 $req->add_string_requirement(
122 438         17100 $mod => $self->merged_requires->requirements_for_module($mod)
123 222         15995 );
124 222         9196 }
125             }
126              
127             return;
128             }
129              
130 146         4486 __PACKAGE__->meta->make_immutable;
131             1;
132              
133              
134             =pod
135              
136             =encoding UTF-8
137              
138             =head1 NAME
139              
140             Dist::Zilla::Prereqs - the prerequisites of a Dist::Zilla distribution
141              
142             =head1 VERSION
143              
144             version 6.028
145              
146             =head1 DESCRIPTION
147              
148             Dist::Zilla::Prereqs is a subcomponent of Dist::Zilla. The C<prereqs>
149             attribute on your Dist::Zilla object is a Dist::Zilla::Prereqs object, and is
150             responsible for keeping track of the distribution's prerequisites.
151              
152             In fact, a Dist::Zilla::Prereqs object is just a thin layer over a
153             L<CPAN::Meta::Prereqs> object, stored in the C<cpan_meta_prereqs> attribute.
154              
155             Almost everything this object does is proxied to the CPAN::Meta::Prereqs
156             object, so you should really read how I<that> works.
157              
158             Dist::Zilla::Prereqs proxies the following methods to the CPAN::Meta::Prereqs
159             object:
160              
161             =over 4
162              
163             =item *
164              
165             finalize
166              
167             =item *
168              
169             is_finalized
170              
171             =item *
172              
173             requirements_for
174              
175             =item *
176              
177             as_string_hash
178              
179             =back
180              
181             =head1 PERL VERSION
182              
183             This module should work on any version of perl still receiving updates from
184             the Perl 5 Porters. This means it should work on any version of perl released
185             in the last two to three years. (That is, if the most recently released
186             version is v5.40, then this module should work on both v5.40 and v5.38.)
187              
188             Although it may work on older versions of perl, no guarantee is made that the
189             minimum required version will not be increased. The version may be increased
190             for any reason, and there is no promise that patches will be accepted to lower
191             the minimum required perl.
192              
193             =head1 METHODS
194              
195             =head2 register_prereqs
196              
197             $prereqs->register_prereqs(%prereqs);
198              
199             $prereqs->register_prereqs(\%arg, %prereqs);
200              
201             This method adds new minimums to the prereqs object. If a hashref is the first
202             arg, it may have entries for C<phase> and C<type> to indicate what kind of
203             prereqs are being registered. (For more information on phase and type, see
204             L<CPAN::Meta::Spec>.) For example, you might say:
205              
206             $prereqs->register_prereqs(
207             { phase => 'test', type => 'recommends' },
208             'Test::Foo' => '1.23',
209             'XML::YZZY' => '2.01',
210             );
211              
212             If not given, phase and type default to runtime and requires, respectively.
213              
214             =head1 AUTHOR
215              
216             Ricardo SIGNES 😏 <cpan@semiotic.systems>
217              
218             =head1 COPYRIGHT AND LICENSE
219              
220             This software is copyright (c) 2022 by Ricardo SIGNES.
221              
222             This is free software; you can redistribute it and/or modify it under
223             the same terms as the Perl 5 programming language system itself.
224              
225             =cut