File Coverage

blib/lib/Dist/Zilla/Plugin/CPANFile.pm
Criterion Covered Total %
statement 40 40 100.0
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 53 58 91.3


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::CPANFile 6.030;
2             # ABSTRACT: produce a cpanfile prereqs file
3              
4 2     2   2589 use Moose;
  2         453422  
  2         13  
5             with 'Dist::Zilla::Role::FileGatherer';
6              
7 2     2   14951 use Dist::Zilla::Pragmas;
  2         5  
  2         19  
8              
9 2     2   15 use namespace::autoclean;
  2         7  
  2         17  
10              
11 2     2   1339 use Dist::Zilla::File::FromCode;
  2         8  
  2         1573  
12              
13             #pod =head1 SYNOPSIS
14             #pod
15             #pod # dist.ini
16             #pod [CPANFile]
17             #pod
18             #pod =head1 DESCRIPTION
19             #pod
20             #pod This plugin will add a F<cpanfile> file to the distribution.
21             #pod
22             #pod =attr filename
23             #pod
24             #pod If given, parameter allows you to specify an alternate name for the generated
25             #pod file. It defaults, of course, to F<cpanfile>.
26             #pod
27             #pod # dist.ini
28             #pod [CPANFile]
29             #pod filename = dzil-generated-cpanfile
30             #pod
31             #pod =attr comment
32             #pod
33             #pod If given, override the default C<cpanfile> header comment with your own. The default comment
34             #pod explains that this file was generated by Dist::Zilla and tells users to edit the dist.ini
35             #pod file to change prereqs
36             #pod
37             #pod # dist.ini
38             #pod [CPANFile]
39             #pod comment = This file is generated by Dist::Zilla
40             #pod comment = Prereqs are detected automatically. You do not need to edit this file
41             #pod
42             #pod =head1 SEE ALSO
43             #pod
44             #pod =for :list
45             #pod * L<Module::CPANfile>
46             #pod * L<Carton>
47             #pod * L<cpanm>
48             #pod
49             #pod =cut
50              
51             has filename => (
52             is => 'ro',
53             isa => 'Str',
54             default => 'cpanfile',
55             );
56              
57 4     4 0 1165 sub mvp_multivalue_args { qw( comment ) }
58              
59             has comment => (
60             is => 'ro',
61             isa => 'ArrayRef[Str]',
62             default => sub {
63             [
64             qq{This file is generated by Dist::Zilla::Plugin::CPANFile v}
65             . ( $Dist::Zilla::Plugin::CPANFile::VERSION // '<internal>' ),
66             qq{Do not edit this file directly. To change prereqs, edit the `dist.ini` file.},
67             ]
68             }
69             );
70              
71             sub _hunkify_hunky_hunk_hunks {
72 4     4   12 my ($self, $indent, $type, $req) = @_;
73              
74 4         14 my $str = '';
75 4         14 for my $module (sort $req->required_modules) {
76 8         51 my $vstr = $req->requirements_for_module($module);
77 8         420 $str .= qq{$type "$module" => "$vstr";\n};
78             }
79 4         28 $str =~ s/^/' ' x $indent/egm;
  8         36  
80 4         17 return $str;
81             }
82              
83             sub gather_files {
84 4     4 0 18 my ($self, $arg) = @_;
85              
86 4         124 my $zilla = $self->zilla;
87              
88             my $file = Dist::Zilla::File::FromCode->new({
89             name => $self->filename,
90             code => sub {
91 4     4   131 my $prereqs = $zilla->prereqs;
92              
93 4         20 my @types = qw(requires recommends suggests conflicts);
94 4         15 my @phases = qw(runtime build test configure develop);
95              
96 4         27 my $str = join "\n", ( map { "# $_" } @{ $self->comment } ), '', '';
  7         43  
  4         135  
97 4         15 for my $phase (@phases) {
98 20         116 for my $type (@types) {
99 80         442 my $req = $prereqs->requirements_for($phase, $type);
100 80 100       4017 next unless $req->required_modules;
101 4 50       37 $str .= qq[\non '$phase' => sub {\n] unless $phase eq 'runtime';
102 4 50       27 $str .= $self->_hunkify_hunky_hunk_hunks(
103             ($phase eq 'runtime' ? 0 : 1),
104             $type,
105             $req,
106             );
107 4 50       17 $str .= qq[};\n] unless $phase eq 'runtime';
108             }
109             }
110              
111 4         40 return $str;
112             },
113 4         129 });
114              
115 4         25 $self->add_file($file);
116 4         22 return;
117             }
118              
119             __PACKAGE__->meta->make_immutable;
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Dist::Zilla::Plugin::CPANFile - produce a cpanfile prereqs file
131              
132             =head1 VERSION
133              
134             version 6.030
135              
136             =head1 SYNOPSIS
137              
138             # dist.ini
139             [CPANFile]
140              
141             =head1 DESCRIPTION
142              
143             This plugin will add a F<cpanfile> file to the distribution.
144              
145             =head1 PERL VERSION
146              
147             This module should work on any version of perl still receiving updates from
148             the Perl 5 Porters. This means it should work on any version of perl released
149             in the last two to three years. (That is, if the most recently released
150             version is v5.40, then this module should work on both v5.40 and v5.38.)
151              
152             Although it may work on older versions of perl, no guarantee is made that the
153             minimum required version will not be increased. The version may be increased
154             for any reason, and there is no promise that patches will be accepted to lower
155             the minimum required perl.
156              
157             =head1 ATTRIBUTES
158              
159             =head2 filename
160              
161             If given, parameter allows you to specify an alternate name for the generated
162             file. It defaults, of course, to F<cpanfile>.
163              
164             # dist.ini
165             [CPANFile]
166             filename = dzil-generated-cpanfile
167              
168             =head2 comment
169              
170             If given, override the default C<cpanfile> header comment with your own. The default comment
171             explains that this file was generated by Dist::Zilla and tells users to edit the dist.ini
172             file to change prereqs
173              
174             # dist.ini
175             [CPANFile]
176             comment = This file is generated by Dist::Zilla
177             comment = Prereqs are detected automatically. You do not need to edit this file
178              
179             =head1 SEE ALSO
180              
181             =over 4
182              
183             =item *
184              
185             L<Module::CPANfile>
186              
187             =item *
188              
189             L<Carton>
190              
191             =item *
192              
193             L<cpanm>
194              
195             =back
196              
197             =head1 AUTHOR
198              
199             Ricardo SIGNES 😏 <cpan@semiotic.systems>
200              
201             =head1 COPYRIGHT AND LICENSE
202              
203             This software is copyright (c) 2023 by Ricardo SIGNES.
204              
205             This is free software; you can redistribute it and/or modify it under
206             the same terms as the Perl 5 programming language system itself.
207              
208             =cut