blib/lib/Dist/Zilla/Plugin/Readme/Brief.pm

filter: Statementstmt 89.2% 158 / 177 Branchbran 63.1% 24 / 38 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 91.6% 33 / 36 Podpod 0.0% 0 / 1 totaltotal 83.9% 220 / 262 CC 26 SCAR 12.2
CC
26
Cov
84%
_configured_installer
12.0
__ANON__
6.1
_copyright_from_pod
5.2
CRAP 3.4
↑ Summary
1 7 ▶use 5.010; # m regexp propagation
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dist::Zilla::Plugin::Readme::Brief;
6  
7 7 our $VERSION = '0.003003';
8  
9 # ABSTRACT: Provide a short simple README with just the essentials
10  
11 7 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13 7 ▶use Moose qw( with has around );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use MooseX::Types::Moose qw( ArrayRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Moose::Util::TypeConstraints qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use PPIx::DocumentName;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 7 with 'Dist::Zilla::Role::PPI';
20 7 with 'Dist::Zilla::Role::FileGatherer';
21  
22 7 my %installers = (
23 'eumm' => '_install_eumm',
24 'mb' => '_install_mb',
25 );
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38 7 has _source_file_override => (
39 isa => Str,
40 is => 'ro',
41 init_arg => 'source_file',
42 predicate => '_has_source_file_override',
43 );
44  
45 has source_file => (
46 is => 'ro',
47 isa => 'Dist::Zilla::Role::File',
48 lazy => 1,
49 init_arg => undef,
50 6 ▶ default => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
51 6 my ($self) = @_;
52 my $file =
53 $self->_has_source_file_override
54 1 ▶ ? first { $_->name eq $self->_source_file_override } @{ $self->zilla->files }
Subroutine __ANON__: called CC=1 SCAR=0.0
55 6 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_source_file_override ? : 1 5 6
56 5 my $main_module = $self->zilla->main_module;
57 5 my $alt = $main_module->name;
58 5 ▶ my $pod = ( $alt =~ s/\.pm\z/.pod/ ) && first { $_->name eq $alt } @{ $self->zilla->files };
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition66%2/3
$alt =~ s/\.pm\z/.pod/ && &first(sub { $_->name eq $alt; } , @{$self->zilla->files;})
!ll&&!rl&&r
041
MC/DC50%1/2
$alt =~ s/\.pm\z/.pod/ && &first(sub { $_->name eq $alt; } , @{$self->zilla->files;})
$alt =~ s/\.pm\z/.pod/ &first(sub { $_->name eq $alt; } , @{$self->zilla->files;})
Truth table66%2/3
A && B
A= $alt =~ s/\.pm\z/.pod/B= &first(sub { $_->name eq $alt; } , @{$self->zilla->files;})
ABresult
0X0
100
111
59 5 ▶ $pod or $main_module;
Condition66%2/3
$pod || $main_module
l!l&&r!l&&!r
140
MC/DC0%0/2
$pod || $main_module
$pod $main_module
Truth table66%2/3
A || B
A= $podB= $main_module
ABresult
1X1
011
000
60 };
61 6 ▶ $self->log_fatal('Unable to find source_file in the distribution') if not $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 6 6
62 6 ▶ $self->log_debug( 'Using POD from ' . $file->name ) unless $self->_has_source_file_override;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_source_file_override 5 1 6
63 6 return $file;
64 },
65 7 );
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91 7 has 'installer' => (
92 isa => ArrayRef [ enum( [ keys %installers ] ) ],
93 is => 'ro',
94 traits => ['Array'],
95 predicate => 'has_installer',
96 handles => {
97 '_installers' => 'elements',
98 },
99 );
100  
101 7 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113 has 'description_label' => (
114 isa => Str,
115 is => 'ro',
116 lazy => 1,
117 5 ▶ default => sub { 'DESCRIPTION' },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 7 );
119  
120 6 ▶around 'mvp_multivalue_args' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 6 my ( $orig, $self, @rest ) = @_;
122 6 return ( $self->$orig(@rest), 'installer' );
123 7 };
124  
125 6 ▶around 'mvp_aliases' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 6 my ( $orig, $self, @rest ) = @_;
127 6 return { %{ $self->$orig(@rest) }, installers => 'installer' };
128 7 };
129  
130 1 ▶around dump_config => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
131 1 my ( $orig, $self, @args ) = @_;
132 1 my $config = $self->$orig(@args);
133 1 my $localconf = {};
134  
135 1 for my $attrname (qw( installer source_file _source_file_override description_label )) {
136 4 ▶ if ( $self->meta->find_attribute_by_name($attrname)->has_value($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->find_attribute_by_name($attrname)->has_value($self)) 3 1 4
137 3 $localconf->{ $attrname } = $self->can($attrname)->($self);
138 }
139 }
140  
141 1 ▶ $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION unless __PACKAGE__ eq ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless __PACKAGE__ eq ref $self 0 1 1
142 1 ▶ $config->{ +__PACKAGE__ } = $localconf if keys %{$localconf};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$localconf;} 1 0 1
143 1 return $config;
144 7 };
145  
146 7 __PACKAGE__->meta->make_immutable;
147 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149  
150  
151  
152  
153 6 ▶sub gather_files {
Subroutine gather_files: called CC=1 SCAR=0.0
Pod undocumented
154 6 my ($self) = @_;
155 6 require Dist::Zilla::File::FromCode;
156 $self->add_file(
157 Dist::Zilla::File::FromCode->new(
158 name => 'README',
159 6 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
160 6 return $self->_generate_content;
161 },
162 6 ),
163 );
164 6 return;
165 }
166  
167 # Internal Methods
168  
169 6 ▶sub _generate_content {
Subroutine _generate_content: called CC=1 SCAR=0.0
170 6 my ($self) = @_;
171  
172 # each section should end with exactly one trailing newline
173 6 return join qq[\n], $self->_description_section, $self->_installer_section, $self->_copyright_section;
174 }
175  
176 6 ▶sub _description_section {
Subroutine _description_section: called CC=1 SCAR=0.0
177 6 my ($self) = @_;
178 6 return $self->_heading . qq[\n\n] . $self->_description . qq[\n];
179 }
180  
181 6 ▶sub _installer_section {
Subroutine _installer_section: called CC=3 SCAR=11.7
182 6 my ($self) = @_;
183 6 my $out = q[];
184 6 $out .= qq[INSTALLATION\n\n];
185 6 $out .= $self->_install_auto;
186  
187 6 ▶ my $manual_instructions = ( $self->has_installer ) ? $self->_configured_installer : $self->_auto_installer;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_installer ? : 0 6 6
188  
189 6 ▶ if ( defined $manual_instructions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $manual_instructions) { } 0 6 6
190 0 $out .= "Should you wish to install this module manually, the procedure is\n\n";
191 0 $out .= $manual_instructions;
192 }
193 else {
194 6 $self->log('No install method detected. Omitting Manual Installation Instructions');
195 }
196 6 return $out;
197 }
198  
199 6 ▶sub _copyright_section {
Subroutine _copyright_section: called CC=2 SCAR=7.6
200 6 my ($self) = @_;
201 6 ▶ if ( my $copy = $self->_copyright_from_pod ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $copy = $self->_copyright_from_pod) 0 6 6
202 0 return $copy . qq[\n];
203 }
204 6 return $self->_copyright_from_dist;
205 }
206  
207 6 ▶sub _auto_installer {
Subroutine _auto_installer: called CC=3 SCAR=11.7
208 6 my ($self) = @_;
209 6 $self->log_debug('Autodetecting installer');
210 6 ▶ if ( first { $_->name =~ /\AMakefile.PL\z/msx } @{ $self->zilla->files } ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 2 branches
BranchTrueFalseTotal
if (&first(sub { $_->name =~ /\AMakefile.PL\z/msx; } , @{$self->zilla->files;})) { } 0 6 6
elsif (&first(sub { $_->name =~ /\ABuild.PL\z/msx; } , @{$self->zilla->files;})) { } 0 6 6
211 0 return $self->_install_eumm;
212 }
213 6 ▶ elsif ( first { $_->name =~ /\ABuild.PL\z/msx } @{ $self->zilla->files } ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 0 return $self->_install_mb;
215 }
216 6 return;
217 }
218  
219 0 ▶sub _configured_installer {
Subroutine _configured_installer: not called CC=3 SCAR=24.8
220 0 my ($self) = @_;
221 0 $self->log_debug('Using configured installer');
222  
223 0 my @sections;
224 0 for my $installer ( $self->_installers ) {
225 0 my $method = $installers{$installer};
226 0 push @sections, $self->$method();
227 }
228 0 ▶ return unless @sections;
Branch: 1 branch
BranchTrueFalseTotal
unless @sections 0 0 0
229 0 return join qq[\nor\n\n], @sections;
230 }
231  
232 12 ▶sub _source_pod {
Subroutine _source_pod: called CC=2 SCAR=6.9
233 12 my ($self) = @_;
234 12 ▶ return $self->{_pod_cache} if exists $self->{_pod_cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_pod_cache"} 6 6 12
235 6 my $chars = $self->source_file->content;
236  
237 6 require Encode;
238 6 require Pod::Elemental;
239 6 require Pod::Elemental::Transformer::Pod5;
240 6 require Pod::Elemental::Transformer::Nester;
241 6 require Pod::Elemental::Selectors;
242  
243 6 my $octets = Encode::encode( 'UTF-8', $chars, Encode::FB_CROAK() );
244 6 my $document = Pod::Elemental->read_string($octets);
245 6 Pod::Elemental::Transformer::Pod5->new->transform_node($document);
246  
247 6 my $nester = Pod::Elemental::Transformer::Nester->new(
248 {
249 top_selector => Pod::Elemental::Selectors::s_command('head1'),
250 content_selectors =>
251 [ Pod::Elemental::Selectors::s_flat(), Pod::Elemental::Selectors::s_command( [qw(head2 head3 head4 over item back)] ), ],
252 },
253 );
254 6 $nester->transform_node($document);
255  
256 6 $self->{_pod_cache} = $document;
257 6 return $document;
258 }
259  
260 6 ▶sub _podtext_nodes {
Subroutine _podtext_nodes: called CC=1 SCAR=0.0
261 6 my ( undef, @nodes ) = @_;
262 6 require Pod::Text;
263 6 my $parser = Pod::Text->new( loose => 1 );
264 6 $parser->output_string( \( my $text ) );
265 6 $parser->parse_string_document( join qq[\n], '=pod', q[], map { $_->as_pod_string } @nodes );
266  
267 # strip extra indent;
268 6 $text =~ s{^[ ]{4}}{}msxg;
269 6 $text =~ s{\n+\z}{}msx;
270 6 return $text;
271 }
272  
273 6 ▶sub _heading {
Subroutine _heading: called CC=1 SCAR=0.0
274 6 my ($self) = @_;
275 6 require PPI::Document; # Historic version of dzil doesn't load PPI on its own...
276 6 my $document = $self->ppi_document_for_file( $self->source_file );
277 6 return PPIx::DocumentName->extract($document);
278 }
279  
280 6 ▶sub _description {
Subroutine _description: called CC=5 SCAR=16.4
281 6 my ($self) = @_;
282 6 my $pod = $self->_source_pod;
283 6 my (@nodes) = @{ $pod->children };
284  
285 6 my @found;
286  
287 6 require Pod::Elemental::Selectors;
288  
289 6 for my $node_number ( 0 .. $#nodes ) {
290 12 ▶ next unless Pod::Elemental::Selectors::s_command( head1 => $nodes[$node_number] );
Branch: 1 branch
BranchTrueFalseTotal
unless Pod::Elemental::Selectors::s_command("head1", $nodes[$node_number]) 6 6 12
291 6 ▶ next unless uc $self->description_label eq uc $nodes[$node_number]->content;
Branch: 1 branch
BranchTrueFalseTotal
unless uc $self->description_label eq uc $nodes[$node_number]->content 0 6 6
292 6 push @found, $nodes[$node_number];
293 }
294 6 ▶ if ( not @found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@found) 0 6 6
295 0 $self->log( $self->description_label . ' not found in ' . $self->source_file->name );
296 0 return q[];
297 }
298 6 return $self->_podtext_nodes( map { @{ $_->children } } @found );
299 }
300  
301 6 ▶sub _copyright_from_dist {
Subroutine _copyright_from_dist: called CC=1 SCAR=0.0
302  
303 # Construct a copyright even if the POD doesn't have one
304 6 my ($self) = @_;
305 6 my $notice = $self->zilla->license->notice;
306 6 return qq[COPYRIGHT AND LICENSE\n\n$notice];
307 }
308  
309 6 ▶sub _copyright_from_pod {
Subroutine _copyright_from_pod: called CC=5 SCAR=16.5
310 6 my ($self) = @_;
311 6 my $pod = $self->_source_pod;
312 6 my (@nodes) = @{ $pod->children };
313  
314 6 my @found;
315  
316 6 require Pod::Elemental::Selectors;
317  
318 6 for my $node_number ( 0 .. $#nodes ) {
319 12 ▶ next unless Pod::Elemental::Selectors::s_command( head1 => $nodes[$node_number] );
Branch: 1 branch
BranchTrueFalseTotal
unless Pod::Elemental::Selectors::s_command("head1", $nodes[$node_number]) 6 6 12
320 6 ▶ next unless $nodes[$node_number]->content =~ /COPYRIGHT|LICENSE/imsx;
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes[$node_number]->content =~ /COPYRIGHT|LICENSE/imsx 6 0 6
321 0 push @found, $nodes[$node_number];
322 }
323 6 ▶ if ( not @found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@found) 6 0 6
324 6 $self->log( 'COPYRIGHT/LICENSE not found in ' . $self->source_file->name );
325 6 return;
326 }
327 0 return $self->_podtext_nodes(@found);
328 }
329  
330 6 ▶sub _install_auto {
Subroutine _install_auto: called CC=1 SCAR=0.0
331 6 return <<"EOFAUTO";
332 This is a Perl module distribution. It should be installed with whichever
333 tool you use to manage your installation of Perl, e.g. any of
334  
335 cpanm .
336 cpan .
337 cpanp -i .
338  
339 Consult http://www.cpan.org/modules/INSTALL.html for further instruction.
340 EOFAUTO
341 }
342  
343 0 ▶sub _install_eumm {
Subroutine _install_eumm: not called CC=1 SCAR=6.9
344 0 return <<"EOFEUMM";
345 perl Makefile.PL
346 make
347 make test
348 make install
349 EOFEUMM
350 }
351  
352 0 ▶sub _install_mb {
Subroutine _install_mb: not called CC=1 SCAR=6.9
353 0 return <<"EOFMB";
354 perl Build.PL
355 ./Build
356 ./Build test
357 ./Build install
358 EOFMB
359 }
360  
361 7 1;
362  
363 __END__
↑ Summary