File Coverage

blib/lib/Dist/Zilla/App/Command/listdeps.pm
Criterion Covered Total %
statement 49 73 67.1
branch 15 28 53.5
condition n/a
subroutine 6 8 75.0
pod 3 6 50.0
total 73 115 63.4


line stmt bran cond sub pod time code
1             # ABSTRACT: print your distribution's prerequisites
2              
3             use Dist::Zilla::Pragmas;
4 4     4   2806  
  4         44  
  4         31  
5             use Dist::Zilla::App -command;
6 4     4   30  
  4         13  
  4         39  
7             #pod =head1 SYNOPSIS
8             #pod
9             #pod $ dzil listdeps | cpan
10             #pod
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This is a command plugin for L<Dist::Zilla>. It provides the C<listdeps>
14             #pod command, which prints your distribution's prerequisites. You could pipe that
15             #pod list to a CPAN client like L<cpan> to install all of the dependencies in one
16             #pod quick go.
17             #pod
18             #pod =head1 OPTIONS
19             #pod
20             #pod =head2 --author (or --develop)
21             #pod
22             #pod Include author dependencies (those listed under C<develop_requires>).
23             #pod
24             #pod =head2 --missing
25             #pod
26             #pod List only dependencies which are unsatisfied.
27             #pod
28             #pod =head2 --requires / --no-requires
29             #pod
30             #pod Add required dependencies to the list (or don't).
31             #pod
32             #pod Default: on.
33             #pod
34             #pod =head2 --recommends / --no-recommends
35             #pod
36             #pod Add recommended dependencies to the list (or don't).
37             #pod
38             #pod Default: on.
39             #pod
40             #pod =head2 --suggests / --no-suggests
41             #pod
42             #pod Add suggested dependencies to the list (or don't).
43             #pod
44             #pod Default: off.
45             #pod
46             #pod =head2 --versions
47             #pod
48             #pod Also display the required versions of listed modules.
49             #pod
50             #pod =head2 --cpanm-versions
51             #pod
52             #pod Also display the required versions of listed modules, but in a format suitable
53             #pod for piping into F<cpanm>.
54             #pod
55             #pod =head2 --json
56             #pod
57             #pod Lists all prerequisites in JSON format, as they would appear in META.json
58             #pod (broken out into phases and types)
59             #pod
60             #pod =head1 ACKNOWLEDGEMENTS
61             #pod
62             #pod This code was originally more or less a direct copy of Marcel Gruenauer (hanekomu)
63             #pod Dist::Zilla::App::Command::prereqs, updated to work with the Dist::Zilla v2
64             #pod API.
65             #pod
66             #pod =cut
67              
68              
69 0     0 1 0 [ 'develop|author', 'include author/develop dependencies' ],
70             [ 'missing', 'list only the missing dependencies' ],
71             [ 'requires!', 'list the required dependencies', { default => 1 } ],
72 18     18 1 62382 [ 'recommends!', 'list the recommended dependencies', { default => 1 } ],
73             [ 'suggests!', 'list the suggested dependencies', {default => 0 } ],
74             [ 'versions', 'include required version numbers in listing' ],
75             [ 'cpanm-versions', 'format versions for consumption by cpanm' ],
76             [ 'json', 'list dependencies by phase, in JSON format' ],
77             [ 'omit-core=s', 'Omit dependencies that are shipped with the specified version of perl' ],
78             }
79              
80             my ($self, $zilla) = @_;
81              
82             $_->before_build for @{ $zilla->plugins_with(-BeforeBuild) };
83             $_->gather_files for @{ $zilla->plugins_with(-FileGatherer) };
84 18     18 0 55 $_->set_file_encodings for @{ $zilla->plugins_with(-EncodingProvider) };
85             $_->prune_files for @{ $zilla->plugins_with(-FilePruner) };
86 18         30 $_->munge_files for @{ $zilla->plugins_with(-FileMunger) };
  18         92  
87 18         47 $_->register_prereqs for @{ $zilla->plugins_with(-PrereqSource) };
  18         60  
88 18         73  
  18         87  
89 18         54 my $prereqs = $zilla->prereqs;
  18         79  
90 18         50 }
  18         98  
91 18         50  
  18         61  
92             my @phases = qw/configure build test runtime develop/;
93 18         595 my @relationships = qw/requires recommends suggests/;
94              
95             my ($prereqs, $core_version) = @_;
96             $core_version = sprintf '%7.6f', $core_version if $core_version >= 5.010;
97             $prereqs = $prereqs->clone if $prereqs->is_finalized;
98             require Module::CoreList;
99             for my $phase (@phases) {
100 0     0 0 0 for my $relation (@relationships) {
101 0 0       0 my $req = $prereqs->requirements_for($phase, $relation);
102 0 0       0 for my $module ($req->required_modules) {
103 0         0 next if not exists $Module::CoreList::version{$core_version}{$module};
104 0         0 $req->clear_requirement($module) if $req->accepts_module($module, $Module::CoreList::version{$core_version}{$module});
105 0         0 }
106 0         0 }
107 0         0 }
108 0 0       0 return $prereqs;
109 0 0       0 }
110              
111             my ($self, $zilla, $phases, $opt) = @_;
112              
113 0         0 my $prereqs = $self->prereqs($zilla);
114             $prereqs = filter_core($prereqs, $opt->omit_core) if $opt->omit_core;
115              
116             require CPAN::Meta::Requirements;
117 18     18 0 80 my $req = CPAN::Meta::Requirements->new;
118              
119 18         89 for my $phase (@$phases) {
120 18 50       117 $req->add_requirements( $prereqs->requirements_for($phase, 'requires') ) if $opt->requires;
121             $req->add_requirements( $prereqs->requirements_for($phase, 'recommends') ) if $opt->recommends;
122 18         194 $req->add_requirements( $prereqs->requirements_for($phase, 'suggests') ) if $opt->suggests;
123 18         76 }
124              
125 18         316 my @required = grep { $_ ne 'perl' } $req->required_modules;
126 78 100       878 if ($opt->missing) {
127 78 100       20021 require Module::Runtime;
128 78 100       5818 @required =
129             grep {
130             # Keep modules that can't be loaded or that don't have a $VERSION
131 18         520 # matching our requirements
  369         859  
132 18 50       100 ! eval {
133 0         0 my $m = $_;
134             # Will die if module is not installed
135             Module::Runtime::require_module($m);
136             # Returns true if $VERSION matches, so we will exclude the module
137             $req->accepts_module($m => $m->VERSION)
138 0         0 }
  0         0  
139 0         0 } @required;
140             }
141 0         0  
142             my $versions = $req->as_string_hash;
143 0         0 return map { $_ => $versions->{$_} } @required;
144             }
145              
146             my ($self, $opt, $arg) = @_;
147              
148 18         137 $self->app->chrome->logger->mute;
149 18         11565  
  354         974  
150             my @phases = qw(build test configure runtime);
151             push @phases, 'develop' if $opt->develop;
152              
153 18     18 1 40285 my $omit_core = $opt->omit_core;
154             if($opt->json) {
155 18         102 my $prereqs = $self->prereqs($self->zilla);
156             $prereqs = filter_core($prereqs, $omit_core) if $omit_core;
157 18         137 my $output = $prereqs->as_string_hash;
158 18 100       85  
159             require JSON::MaybeXS;
160 18         223 print JSON::MaybeXS->new(ascii => 1, canonical => 1, pretty => 1)->encode($output), "\n";
161 18 50       130 return 1;
162 0         0 }
163 0 0       0  
164 0         0 my %modules = $self->extract_dependencies($self->zilla, \@phases, $opt);
165              
166 0         0 my @names = sort { lc $a cmp lc $b } keys %modules;
167 0         0 if ($opt->versions) {
168 0         0 print "$_ = $modules{$_}\n" for @names;
169             } elsif ($opt->cpanm_versions) {
170             print qq{$_~"$modules{$_}"\n} for @names;
171 18         176 } else {
172             print "$_\n" for @names;
173 18         219 }
  1177         2113  
174 18 100       112 }
    100          
175 6         2539  
176             1;
177 6         2914  
178              
179 6         2387 =pod
180              
181             =encoding UTF-8
182              
183             =head1 NAME
184              
185             Dist::Zilla::App::Command::listdeps - print your distribution's prerequisites
186              
187             =head1 VERSION
188              
189             version 6.028
190              
191             =head1 SYNOPSIS
192              
193             $ dzil listdeps | cpan
194              
195             =head1 DESCRIPTION
196              
197             This is a command plugin for L<Dist::Zilla>. It provides the C<listdeps>
198             command, which prints your distribution's prerequisites. You could pipe that
199             list to a CPAN client like L<cpan> to install all of the dependencies in one
200             quick go.
201              
202             =head1 PERL VERSION
203              
204             This module should work on any version of perl still receiving updates from
205             the Perl 5 Porters. This means it should work on any version of perl released
206             in the last two to three years. (That is, if the most recently released
207             version is v5.40, then this module should work on both v5.40 and v5.38.)
208              
209             Although it may work on older versions of perl, no guarantee is made that the
210             minimum required version will not be increased. The version may be increased
211             for any reason, and there is no promise that patches will be accepted to lower
212             the minimum required perl.
213              
214             =head1 OPTIONS
215              
216             =head2 --author (or --develop)
217              
218             Include author dependencies (those listed under C<develop_requires>).
219              
220             =head2 --missing
221              
222             List only dependencies which are unsatisfied.
223              
224             =head2 --requires / --no-requires
225              
226             Add required dependencies to the list (or don't).
227              
228             Default: on.
229              
230             =head2 --recommends / --no-recommends
231              
232             Add recommended dependencies to the list (or don't).
233              
234             Default: on.
235              
236             =head2 --suggests / --no-suggests
237              
238             Add suggested dependencies to the list (or don't).
239              
240             Default: off.
241              
242             =head2 --versions
243              
244             Also display the required versions of listed modules.
245              
246             =head2 --cpanm-versions
247              
248             Also display the required versions of listed modules, but in a format suitable
249             for piping into F<cpanm>.
250              
251             =head2 --json
252              
253             Lists all prerequisites in JSON format, as they would appear in META.json
254             (broken out into phases and types)
255              
256             =head1 ACKNOWLEDGEMENTS
257              
258             This code was originally more or less a direct copy of Marcel Gruenauer (hanekomu)
259             Dist::Zilla::App::Command::prereqs, updated to work with the Dist::Zilla v2
260             API.
261              
262             =head1 AUTHOR
263              
264             Ricardo SIGNES 😏 <cpan@semiotic.systems>
265              
266             =head1 COPYRIGHT AND LICENSE
267              
268             This software is copyright (c) 2022 by Ricardo SIGNES.
269              
270             This is free software; you can redistribute it and/or modify it under
271             the same terms as the Perl 5 programming language system itself.
272              
273             =cut