Readme/Plugin/requires.pm

filter: Statementstmt 96.4% 82 / 85 Branchbran 68.1% 15 / 22 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 90.0% 117 / 130 CC 19 SCAR 18.5
CC
19
Cov
90%
cmd_requires
11.0
_get_prereqs
8.0
_write_modules
3.0
CRAP 6.4
« Readme/Plugin/changes.pm ↑ Summary Readme/Plugin.pm »
1 package Pod::Readme::Plugin::requires;
2  
3 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = 'v1.2.3';
6  
7 1 ▶use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Module::CoreList;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Types::Standard qw/ Bool Str /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Pod::Readme::Types qw/ File HeadingLevel /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Pod::Readme::Plugin::requires - Include requirements in README
17  
18 =head1 SYNOPSIS
19  
20 =for readme plugin requires
21  
22 =head1 DESCRIPTION
23  
24 This is a plugin for L<Pod::Readme> that includes module requirements
25 from the F<META.yml> file.
26  
27 Because this depends on the F<META.yml> file, the F<README> should be
28 generated after that file has been updated.
29  
30 =head1 ARGUMENTS
31  
32 =head2 C<from-file>
33  
34 =for readme plugin version from-file='MYMETA.yml'
35  
36 By default, it will extract the version from the F<META.yml> file. If,
37 for some reason, this file is in a non-standard location, then you
38 should specify it here.
39  
40 The file will be ignored if the C<zilla> attribute is set, and instead
41 obtain metadata from the L<Dist::Zilla> object (since the F<META.yml>
42 file may not exist.)
43  
44 =head2 C<no-omit-core>
45  
46 By default, core modules for the version of Perl specified in the
47 F<META.yml> file are omitted from this list. If you prefer to lise
48 all requirements, then specify this option.
49  
50 =head2 C<title>
51  
52 =for readme plugin version title='REQUIREMENTS'
53  
54 This argument allows you to change the title of the heading.
55  
56 =head1 KNOWN ISSUES
57  
58 =over
59  
60 =item *
61  
62 Trailing zeros in module versions may be dropped.
63  
64 If you specify a minimum version of a module with a trailing zero,
65 e.g. "0.30", then it may be shown as "0.3". A workaround is to
66 specify the module version in your F<Makefile.PL> as a string instead
67 of number:
68  
69 requires(
70 'CPAN::Changes' => '0.30',
71 ...
72 );
73  
74 =back
75  
76 =cut
77  
78 1 requires 'parse_cmd_args';
79  
80 has 'requires_from_file' => (
81 is => 'rw',
82 isa => File,
83 2 ▶ coerce => sub { File->coerce(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 1 default => 'META.yml',
85 lazy => 1,
86 );
87  
88 1 has 'requires_title' => (
89 is => 'rw',
90 isa => Str,
91 default => 'REQUIREMENTS',
92 lazy => 1,
93 );
94  
95 1 has 'requires_omit_core' => (
96 is => 'rw',
97 isa => Bool,
98 default => 1,
99 lazy => 1,
100 );
101  
102 1 has 'requires_heading_level' => (
103 is => 'rw',
104 isa => HeadingLevel,
105 default => 1,
106 lazy => 1,
107 );
108  
109 1 has 'requires_run' => (
110 is => 'rw',
111 isa => Bool,
112 default => 0,
113 lazy => 1,
114 );
115  
116 2 ▶around 'depends_on' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 2 my ( $orig, $self ) = @_;
118 2 return ( $self->requires_from_file, $self->$orig );
119 1 };
120  
121 3 ▶sub cmd_requires {
Subroutine cmd_requires: called CC=10 SCAR=23.9
Pod undocumented
122 3 my ( $self, @args ) = @_;
123  
124 3 ▶ die "The requires plugin can only be used once" if $self->requires_run;
Branch: 1 branch
BranchTrueFalseTotal
if $self->requires_run 0 3 3
125  
126 3 my $res = $self->parse_cmd_args(
127 [qw/ from-file title omit-core no-omit-core heading-level /], @args );
128 2 foreach my $key ( keys %{$res} ) {
129 2 ( my $name = "requires_${key}" ) =~ s/-/_/g;
130 2 ▶ if ( my $method = $self->can($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can($name)) { } 2 0 2
131 2 $self->$method( $res->{$key} );
132 }
133 else {
134 0 die "Invalid key: '${key}'";
135 }
136 }
137  
138 2 my $meta;
139  
140 2 ▶ if ( $self->zilla ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->zilla) { } 0 2 2
141  
142 0 $meta = CPAN::Meta->create( $self->zilla->distmeta,
143 { lazy_validation => 1 } );
144  
145 }
146 else {
147  
148 2 my $file =
149 path( $self->base_dir, $self->requires_from_file )->stringify;
150 2 ▶ unless ( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 0 2 2
151 0 die "Cannot find META.yml file at '${file}";
152 }
153  
154 2 $meta = CPAN::Meta->load_file($file);
155 }
156  
157 2 my ( $prereqs, $perl ) = $self->_get_prereqs( $meta, 'requires' );
158 2 ▶ if ( %{$prereqs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$prereqs;}) 2 0 2
159  
160 2 ▶ my $heading = $self->can( "write_head" . $self->requires_heading_level )
Branch: 1 branch
BranchTrueFalseTotal
my $heading = $self->can('write_head' . $self->requires_heading_level) or die 'Invalid heading level: ' . $self->requires_heading_level 2 0 2
161 or die "Invalid heading level: " . $self->requires_heading_level;
162  
163 2 $self->$heading( $self->requires_title );
164  
165 2 ▶ if ($perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perl) 2 0 2
166 2 $self->write_para(
167 sprintf( 'This distribution requires Perl %s.',
168 version->parse($perl)->normal )
169 );
170 }
171  
172 2 $self->write_para('This distribution requires the following modules:');
173  
174 2 $self->_write_modules($prereqs);
175  
176 2 my ($recommends) = $self->_get_prereqs( $meta, 'recommends' );
177 2 ▶ if ( %{$recommends} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$recommends;}) 1 1 2
178  
179 1 $self->write_para(
180 'This distribution recommends the following modules:');
181  
182 1 $self->_write_modules($recommends);
183  
184 }
185  
186 }
187  
188 2 $self->requires_run(1);
189 }
190  
191 4 ▶sub _get_prereqs {
Subroutine _get_prereqs: called CC=8 SCAR=20.8
192 4 my ( $self, $meta, $key ) = @_;
193  
194 4 my %prereqs;
195 4 foreach my $type ( values %{ $meta->prereqs } ) {
196  
197 # TODO: max version
198 10 $prereqs{$_} = $type->{$key}->{$_} for ( keys %{ $type->{$key} } );
199 }
200 4 my $perl = delete $prereqs{perl};
201 4 ▶ if ( $self->requires_omit_core && $perl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->requires_omit_core and $perl) 2 2 4
Condition66%2/3
$self->requires_omit_core and $perl
!ll&&!rl&&r
022
MC/DC50%1/2
$self->requires_omit_core and $perl
$self->requires_omit_core $perl
Truth table66%2/3
A and B
A= $self->requires_omit_coreB= $perl
ABresult
0X0
100
111
202 2 foreach ( keys %prereqs ) {
203 46 my $ver = $prereqs{$_};
204 delete $prereqs{$_}
205 46 ▶ if Module::CoreList->first_release( $_, $prereqs{$ver} )
Branch: 1 branch
BranchTrueFalseTotal
if "Module::CoreList"->first_release($_, $prereqs{$ver}) and "version"->parse("Module::CoreList"->first_release($_)) <= "version"->parse($perl) 15 31 46
Condition100%3/3
"Module::CoreList"->first_release($_, $prereqs{$ver}) and "version"->parse("Module::CoreList"->first_release($_)) <= "version"->parse($perl)
!ll&&!rl&&r
28315
MC/DC100%2/2
"Module::CoreList"->first_release($_, $prereqs{$ver}) and "version"->parse("Module::CoreList"->first_release($_)) <= "version"->parse($perl)
"Module::CoreList"->first_release($_, $prereqs{$ver}) "version"->parse("Module::CoreList"->first_release($_)) <= "version"->parse($perl)
Truth table100%3/3
A and B
A= "Module::CoreList"->first_release($_, $prereqs{$ver})B= "version"->parse("Module::CoreList"->first_release($_)) <= "version"->parse($perl)
ABresult
0X0
100
111
206 && version->parse( Module::CoreList->first_release($_) ) <=
207 version->parse($perl);
208 }
209 }
210 4 return ( \%prereqs, $perl );
211 }
212  
213 3 ▶sub _write_modules {
Subroutine _write_modules: called CC=3 SCAR=11.0
214 3 my ( $self, $prereqs ) = @_;
215 3 $self->write_over(4);
216 3 foreach my $module ( sort { lc($a) cmp lc($b) } keys %{$prereqs} ) {
217 40 my $version = $prereqs->{$module};
218 40 ▶ my $text = $version ? " (version ${version})" : '';
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 2 38 40
219 40 $self->write_item( sprintf( '* L<%s>', $module ) . $text );
220 }
221 3 $self->write_back;
222 }
223  
224 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 1 1;
« Readme/Plugin/changes.pm ↑ Summary Readme/Plugin.pm »