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