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