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