blib/lib/Dist/Zilla/Plugin/Manifest/Write.pm

filter: Statementstmt 95.3% 226 / 237 Branchbran 72.9% 35 / 48 Conditioncond 65.3% 17 / 26 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 97.5% 39 / 40 Podpod 100% 3 / 3 totaltotal 88.5% 326 / 368 CC 43 SCAR 12.0
CC
43
Cov
89%
_file_comment
7.0
__package_name
6.0
__ANON__
6.0
CRAP 3.3
↑ Summary eg/ManifestWithFileSize.pm »
1 # ---------------------------------------------------------------------- copyright and license ---
2 #
3 # file: lib/Dist/Zilla/Plugin/Manifes/Write.pm
4 #
5 # Copyright © 2015, 2016 Van de Bugger.
6 #
7 # This file is part of perl-Dist-Zilla-Plugin-Manifest-Write.
8 #
9 # perl-Dist-Zilla-Plugin-Manifest-Write is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by the Free Software
11 # Foundation, either version 3 of the License, or (at your option) any later version.
12 #
13 # perl-Dist-Zilla-Plugin-Manifest-Write is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 # PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # perl-Dist-Zilla-Plugin-Manifest-Write. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # ---------------------------------------------------------------------- copyright and license ---
21  
22 #pod =for :this This is C<Dist::Zilla::Plugin::Manifest::Write> module documentation. Read this if you are going to hack or
23 #pod extend C<Manifest::Write>.
24 #pod
25 #pod =for :those If you want to have annotated distribution manifest, read the L<plugin user
26 #pod manual|Dist::Zilla::Plugin::Manifest::Write::Manual>. General topics like getting source, building, installing, bug
27 #pod reporting and some others are covered in the F<README>.
28 #pod
29 #pod =head1 DESCRIPTION
30 #pod
31 #pod In order to add a manifest file to the distribution, C<Dist::Zilla::Plugin::Manifest::Write> class consumes
32 #pod C<Dist::Zilla::Role::FileGatherer> role. To meet the role requirements, the class implements
33 #pod C<gather_files> method. Other methods are supporting helpers for this one.
34 #pod
35 #pod Most of attributes are initialized by builders for easier customization by subclassing. Code is
36 #pod also divided into small methods for the same purpose.
37 #pod
38 #pod =head1 SEE ALSO
39 #pod
40 #pod =for :list
41 #pod = L<Dist::Zilla>
42 #pod = L<Dist::Zilla::Role>
43 #pod = L<Dist::Zilla::Role::Plugin>
44 #pod = L<Dist::Zilla::Role::FileInjector>
45 #pod = L<Dist::Zilla::Role::FileGatherer>
46 #pod = L<Dist::Zilla::Plugin::Manifest>
47 #pod = L<Dist::Zilla::Plugin::Manifest::Write::Manual>
48 #pod
49 #pod =cut
50  
51 # --------------------------------------------------------------------------------------------------
52  
53 package Dist::Zilla::Plugin::Manifest::Write;
54  
55 3 ▶use Moose; # Let `perlcritic` shut up: it complains on code (`$VERSION`) before `strict`.
Subroutine BEGIN: called CC=1 SCAR=0.0
56 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # PODNAME: Dist::Zilla::Plugin::Manifest::Write
59 # ABSTRACT: Have annotated distribution manifest
60 3 our $VERSION = 'v0.9.7'; # VERSION
61  
62 3 with 'Dist::Zilla::Role::FileGatherer';
63 3 with 'Dist::Zilla::Role::BeforeBuild';
64 3 with 'Dist::Zilla::Role::BeforeArchive';
65 3 with 'Dist::Zilla::Role::ErrorLogger';
66 3 with 'Dist::Zilla::Role::FileFinderUser' => {
67 finder_arg_names => [ qw{ exclude_files } ],
68 default_finders => [ ':NoFiles' ],
69 };
70  
71 3 ▶use Dist::Zilla::File::FromCode;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 3 ▶use ExtUtils::Manifest qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
73 3 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 3 ▶use Module::Util qw{ is_valid_module_name };
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use Path::Tiny qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶use Set::Object qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
78 3 ▶use String::RewritePrefix;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 3 ▶use Dist::Zilla::Role::File 5.023 (); # Hint for `AutoPrereqs`.
Subroutine BEGIN: called CC=1 SCAR=0.0
81 # We do not consume the role but just require the specified version. Before this version
82 # `_added_by` was a `Str`, not `ArrayRef`.
83  
84 # --------------------------------------------------------------------------------------------------
85  
86 # File deeds:
87 3 Readonly our $SOURCE => 1;
88 3 Readonly our $META => 2;
89 3 Readonly our $OTHER => 3;
90 # File breeds:
91 3 Readonly our $ADDED => 4;
92 3 Readonly our $BUILT => 5;
93  
94 # --------------------------------------------------------------------------------------------------
95  
96 #pod =head1 FUNCTIONS
97 #pod
98 #pod I would expect to find these functions in C<Dist::Zilla>. Actually, C<Dist::Zilla::Util> defines
99 #pod the function C<expand_config_package_name>, but that function "is likely to change or go away" and
100 #pod there is no reverse transformation.
101 #pod
102 #pod =cut
103  
104 # --------------------------------------------------------------------------------------------------
105  
106 #pod =func __plugin_moniker
107 #pod
108 #pod $str = __plugin_moniker( 'Dist::Zilla::Plugin::Name' ); # 'Name'
109 #pod $str = __plugin_moniker( 'Non::Standard::Name' ); # '=Non::Standard::Name'
110 #pod $str = __plugin_moniker( $plugin );
111 #pod
112 #pod The function takes either reference to a plugin object or a string, package name, and returns
113 #pod C<Dist::Zilla> plugin moniker: If its package name begins with C<Dist::Zilla::Plugin::>, this
114 #pod common prefix is dropped, otherwise the package name prepended with C<=>.
115 #pod
116 #pod =cut
117  
118 {
119  
120 3 my $package2moniker = {
121 'Dist::Zilla::Plugin::' => '',
122 'Dist::Zilla::Role::' => '*',
123 '' => '=',
124 };
125  
126 98 ▶sub __plugin_moniker($) { ## no critic ( ProhibitSubroutinePrototypes )
Subroutine __plugin_moniker: called CC=2 SCAR=7.0
127 98 my ( $arg ) = @_;
128 98 ▶ if ( my $blessed = blessed( $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $blessed = builtin::blessed($arg)) 98 0 98
129 98 $arg = $blessed;
130 };
131 98 return String::RewritePrefix->rewrite( $package2moniker, $arg );
132 };
133  
134 }
135  
136 # --------------------------------------------------------------------------------------------------
137  
138 #pod =func __package_name
139 #pod
140 #pod $str = __package_name( 'Name' ); # returns 'Dist::Zilla::Plugin::Name'
141 #pod $str = __package_name( '=Name' ); # returns 'Name'
142 #pod $str = __package_name( $plugin );
143 #pod
144 #pod This is operation opposite to C<__plugin_moniker>. It takes either reference to plugin object, or
145 #pod string, plugin moniker, and returns package name.
146 #pod
147 #pod This function is similar to C<expand_config_package_name> from C<Dist::Zilla::Util>, with minor
148 #pod difference: this function works with plugins only (not with plugin bundles and stashes), and
149 #pod accepts also reference to plugin object.
150 #pod
151 #pod =cut
152  
153 {
154  
155 3 my $moniker2package = {
156 '=' => '',
157 '' => 'Dist::Zilla::Plugin::',
158 };
159  
160 0 ▶sub __package_name($) { ## no critic ( ProhibitSubroutinePrototypes )
Subroutine __package_name: not called CC=2 SCAR=17.9
161 0 my ( $arg ) = @_;
162 0 ▶ if ( my $blessed = blessed( $arg ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $blessed = builtin::blessed($arg)) 0 0 0
163 0 return $blessed;
164 };
165 0 return String::RewritePrefix->rewrite( $moniker2package, $arg );
166 };
167  
168 }
169  
170 # --------------------------------------------------------------------------------------------------
171  
172 #pod =Method BUILDARGS
173 #pod
174 #pod The method splits values of C<source_providers> option into separate plugin names using whitespaces
175 #pod as delimiters, combines result of splitting with C<source_provider> option values, then filters out
176 #pod empty values. Resulting C<ArrayRef> saved as C<source_providers> options.
177 #pod
178 #pod The same for C<metainfo_providers> and C<metainfo_provider> options.
179 #pod
180 #pod =cut
181  
182 3 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
183 21 my ( $orig, $class, $args ) = @_;
184 21 for my $type ( qw{ source metainfo } ) {
185 42 my $provider = $type . '_provider';
186 42 my $providers = $type . '_providers';
187 42 ▶ if ( exists( $args->{ $provider } ) or exists( $args->{ $providers } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$provider} or exists $args->{$providers}) 23 19 42
Condition100%3/3
exists $args->{$provider} or exists $args->{$providers}
l!l&&r!l&&!r
22119
MC/DC100%2/2
exists $args->{$provider} or exists $args->{$providers}
exists $args->{$provider} exists $args->{$providers}
Truth table100%3/3
A or B
A= exists $args->{$provider}B= exists $args->{$providers}
ABresult
1X1
011
000
188 $args->{ $providers } = [
189 grep( # Filter out empty values.
190 37 { $_ ne '' }
191 23 ▶ @{ $args->{ $provider } or [] }, # Get singular option values as-is.
Condition100%2/2
$args->{$provider} || []
l!l
221
MC/DC100%1/1
$args->{$provider} || []
$args->{$provider}
Truth table100%2/2
A
A= $args->{$provider}
Aresult
11
00
192 23 ▶ map( { split( m{\s+}x, $_ ) } @{ $args->{ $providers } or [] } )
Condition100%2/2
$args->{$providers} || []
l!l
122
MC/DC100%1/1
$args->{$providers} || []
$args->{$providers}
Truth table100%2/2
A
A= $args->{$providers}
Aresult
11
00
193 # Split plural option values.
194 )
195 ];
196 };
197 };
198 21 return $class->$orig( $args );
199 3 };
200  
201 # --------------------------------------------------------------------------------------------------
202  
203 3 has _heading => (
204 isa => 'Str',
205 is => 'ro',
206 lazy => 1,
207 builder => '_build_heading',
208 );
209  
210 20 ▶sub _build_heading {
Subroutine _build_heading: called CC=1 SCAR=0.0
211 20 my ( $self ) = @_;
212 20 return sprintf( "# This file was generated with %s %s.", blessed( $self ), $self->VERSION );
213 };
214  
215 # --------------------------------------------------------------------------------------------------
216  
217 #pod =attr manifest
218 #pod
219 #pod Name of manifest file to write.
220 #pod
221 #pod C<Str>, read-only. Default value is C<'MANIFEST'>.
222 #pod
223 #pod =cut
224  
225 3 has manifest => (
226 isa => 'Str',
227 is => 'ro',
228 default => 'MANIFEST',
229 );
230  
231 3 has _manifest_file => (
232 isa => 'Dist::Zilla::Role::File',
233 is => 'ro',
234 lazy => 1,
235 init_arg => undef,
236 builder => '_build_manifest_file',
237 );
238  
239 20 ▶sub _build_manifest_file {
Subroutine _build_manifest_file: called CC=1 SCAR=0.0
240 20 my ( $self ) = @_;
241 20 my $zilla = $self->zilla;
242 return Dist::Zilla::File::FromCode->new( {
243 name => $self->manifest,
244 code_return_type => 'bytes',
245 20 ▶ code => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
246 20 my @list;
247 20 my $files = Set::Object->new( @{ $zilla->files } );
248 20 $files->remove( @{ $self->found_files } );
249 # Process all files in alphbetical order.
250 20 for my $file ( sort( { $a->name cmp $b->name } @{ $files } ) ) {
251 97 push( @list, {
252 name => $self->_file_name( $file ),
253 comment => $self->_file_comment( $file )
254 } );
255 };
256 20 $self->abort_if_error(); # `_file_comment` methods may generate errors.
257 # Find width of filename column.
258 20 my $width = List::Util::max( map( { length( $_->{ name } ) } @list ) );
259 # Output formats.
260 20 my $body = "%*s # %s";
261 return
262 join(
263 "\n",
264 $self->_heading,
265 20 map( { sprintf( $body, - $width, $_->{ name }, $_->{ comment } ) } @list ),
266 ) . "\n";
267 },
268 20 } );
269 };
270  
271 # --------------------------------------------------------------------------------------------------
272  
273 #pod =attr manifest_skip
274 #pod
275 #pod Name of manifest.skip file to write.
276 #pod
277 #pod C<Str>, read-only. Default value is C<'MANIFEST.SKIP'>.
278 #pod
279 #pod =cut
280  
281 3 has manifest_skip => (
282 isa => 'Str',
283 is => 'ro',
284 default => 'MANIFEST.SKIP',
285 );
286  
287  
288 3 has _manifest_skip_file => (
289 isa => 'Maybe[Dist::Zilla::Role::File]',
290 is => 'ro',
291 lazy => 1,
292 init_arg => undef,
293 builder => '_build_manifest_skip',
294 );
295  
296 20 ▶sub _build_manifest_skip {
Subroutine _build_manifest_skip: called CC=2 SCAR=6.9
297 20 my ( $self ) = @_;
298 20 ▶ return if $self->manifest_skip eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->manifest_skip eq "" 1 19 20
299 return Dist::Zilla::File::FromCode->new( {
300 name => $self->manifest_skip,
301 code_return_type => 'bytes',
302 19 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
303 19 my $default = Path::Tiny::path( $ExtUtils::Manifest::DEFAULT_MSKIP );
304 return join( "\n",
305 $self->_heading,
306 '',
307 19 map( { "^" . quotemeta( $_->name ) . "\$" } @{ $self->found_files } ),
308 '',
309 # `ExtUtils::Manifest` recognizes `#!include_default` directive. Unfortunately,
310 # `Module::Manifest` does not. So I have to copy default `MANIFEST.SKIP`.
311 "# The rest is a copy of $default file:",
312 '',
313 $default->slurp_utf8(),
314 '# end of file #'
315 ) . "\n";
316 },
317 19 } );
318 };
319  
320 # --------------------------------------------------------------------------------------------------
321  
322 #pod =attr source_providers
323 #pod
324 #pod List of plugin names. Enlisted plugins are considered as source file providers. A file added to
325 #pod distribution by any of these plugins is considered as source file.
326 #pod
327 #pod C<ArrayRef[Str]>, read-only, default value is empty array. Init argument (and config file
328 #pod multi-value option) name is C<source_provider>. (C<BUILDARGS> also handles C<source_providers>
329 #pod option.)
330 #pod
331 #pod =cut
332  
333 3 has source_providers => (
334 isa => 'ArrayRef[Str]',
335 is => 'ro',
336 required => 1,
337 lazy => 1,
338 builder => '_build_source_providers',
339 );
340  
341 2 ▶sub _build_source_providers {
Subroutine _build_source_providers: called CC=1 SCAR=0.0
342 2 return [];
343 };
344  
345 # --------------------------------------------------------------------------------------------------
346  
347 #pod =attr metainfo_providers
348 #pod
349 #pod Like C<source_providers> but enlists meta info file providers.
350 #pod
351 #pod C<ArrayRef[Str]>, read-only, default value is C<CPANFile>, C<Manifest>, C<MetaYAML>, C<MetaJSON>,
352 #pod and the plugin itself. Init argument (and config file multi-value option) name is
353 #pod C<metainfo_provider>. (C<BUILDARGS> also handles C<metainfo_providers> option.)
354 #pod
355 #pod Note: Do not confuse C<Manifest::Write>'s term I<metainfo providers> with C<Dist::Zilla>'s
356 #pod C<MetaProvider> role. Plugins do C<MetaProvider> role provide I<metadata>, while C<Manifest::Write>
357 #pod is interested in plugins which adds I<files> containing metadata to the distribution (such plugins
358 #pod do C<FileInjector> role, not C<MetaProvider>).
359 #pod
360 #pod =cut
361  
362 3 has metainfo_providers => (
363 isa => 'ArrayRef[Str]',
364 is => 'ro',
365 required => 1,
366 lazy => 1,
367 builder => '_build_metainfo_providers',
368 );
369  
370 17 ▶sub _build_metainfo_providers {
Subroutine _build_metainfo_providers: called CC=2 SCAR=7.0
371 17 my ( $self ) = @_;
372 17 my @list = ( qw{ CPANFile Manifest MetaYAML MetaJSON }, $self->plugin_name );
373 17 ▶ if ( $self->strict >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict >= 0) 17 0 17
374 17 @list = grep( { $self->_is_injector( $_ ) } @list );
375 };
376 17 return \@list;
377 };
378  
379 85 ▶sub _is_injector {
Subroutine _is_injector: called CC=2 SCAR=7.0
380 85 my ( $self, $name ) = @_;
381 85 my $plugin = $self->zilla->plugin_named( $name );
382 85 ▶ return $plugin && $plugin->does( 'Dist::Zilla::Role::FileInjector' );
Condition66%2/3
$plugin && $plugin->does('Dist::Zilla::Role::FileInjector')
!ll&&!rl&&r
53032
MC/DC50%1/2
$plugin && $plugin->does('Dist::Zilla::Role::FileInjector')
$plugin $plugin->does('Dist::Zilla::Role::FileInjector')
Truth table66%2/3
A && B
A= $pluginB= $plugin->does('Dist::Zilla::Role::FileInjector')
ABresult
0X0
100
111
383 };
384  
385 # --------------------------------------------------------------------------------------------------
386  
387 #pod =attr strict
388 #pod
389 #pod Strictness of checking source and metainfo provider names: -1 (no checks), 0 (some mistakes are
390 #pod fatal, some are not), or 1 (all mistakes are fatal).
391 #pod
392 #pod C<Int>, read-only. Default is 1.
393 #pod
394 #pod See L<Dist::Zilla::Plugin::Manifest::Write::Manual/"strict">.
395 #pod
396 #pod =cut
397  
398 3 has strict => (
399 isa => 'Int',
400 is => 'ro',
401 default => 1,
402 );
403  
404 # --------------------------------------------------------------------------------------------------
405  
406 #pod =attr show_mungers
407 #pod
408 #pod If C<1>, file mungers will be included into annotation. By default mungers are not included.
409 #pod
410 #pod C<Bool>, read-only. Default is C<0>.
411 #pod
412 #pod =cut
413  
414 3 has show_mungers => (
415 isa => 'Bool',
416 is => 'ro',
417 default => 0,
418 );
419  
420 # --------------------------------------------------------------------------------------------------
421  
422 #pod =attr deeds
423 #pod
424 #pod This attribute maps internal file deed constants (C<SOURCE>, C<META>, C<OTHER>) to user-visible
425 #pod names used in manifest (project name, C<metainfo>, and C<3rd party> respectively).
426 #pod
427 #pod C<HashRef[Str]>, read-only.
428 #pod
429 #pod =cut
430  
431 3 has deeds => (
432 isa => 'HashRef[Str]',
433 is => 'ro',
434 lazy => 1,
435 builder => '_build_deeds',
436 init_arg => undef,
437 );
438  
439 20 ▶sub _build_deeds {
Subroutine _build_deeds: called CC=1 SCAR=0.0
440 20 my ( $self ) = @_;
441 return {
442 20 $SOURCE => $self->zilla->name,
443 $META => 'metainfo',
444 $OTHER => '3rd party',
445 };
446 };
447  
448 # --------------------------------------------------------------------------------------------------
449  
450 #pod =attr breeds
451 #pod
452 #pod This attribute maps internal file deed constants (C<$ADDED> and C<$BUILT>) to user-visible names
453 #pod used in manifest. By default user-visible breed names are the same as internal identifiers.
454 #pod
455 #pod C<HashRef[Str]>, read-only.
456 #pod
457 #pod =cut
458  
459 3 has breeds => (
460 isa => 'HashRef[Str]',
461 is => 'ro',
462 lazy => 1,
463 builder => '_build_breeds',
464 init_arg => undef,
465 );
466  
467 20 ▶sub _build_breeds {
Subroutine _build_breeds: called CC=1 SCAR=0.0
468 20 my ( $self ) = @_;
469 return {
470 20 $ADDED => 'added',
471 $BUILT => 'built',
472 };
473 };
474  
475 # --------------------------------------------------------------------------------------------------
476  
477 #pod =attr _providers
478 #pod
479 #pod This attribute maps provider names to file deeds. It makes C<_file_deed> method implementation
480 #pod simpler and faster.
481 #pod
482 #pod C<HashRef[Str]>, read-only, not an init arg.
483 #pod
484 #pod =cut
485  
486 3 has _providers => (
487 isa => 'HashRef[Int]',
488 is => 'ro',
489 required => 1,
490 lazy => 1,
491 builder => '_build_providers',
492 init_arg => undef,
493 );
494  
495 21 ▶sub _build_providers {
Subroutine _build_providers: called CC=5 SCAR=16.1
496 21 my ( $self ) = @_;
497 21 my $providers = {};
498 21 for my $provider ( @{ $self->source_providers } ) {
499 29 $providers->{ $provider } = $SOURCE;
500 };
501 21 for my $provider ( @{ $self->metainfo_providers } ) {
502 36 ▶ if ( exists( $providers->{ $provider } ) and $providers->{ $provider } != $META ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $providers->{$provider} and $providers->{$provider} != $Dist::Zilla::Plugin::Manifest::Write::META) { } 1 35 36
Condition66%2/3
exists $providers->{$provider} and $providers->{$provider} != $Dist::Zilla::Plugin::Manifest::Write::META
!ll&&!rl&&r
3501
MC/DC50%1/2
exists $providers->{$provider} and $providers->{$provider} != $Dist::Zilla::Plugin::Manifest::Write::META
exists $providers->{$provider} $providers->{$provider} != $Dist::Zilla::Plugin::Manifest::Write::META
Truth table66%2/3
A and B
A= exists $providers->{$provider}B= $providers->{$provider} != $Dist::Zilla::Plugin::Manifest::Write::META
ABresult
0X0
100
111
503 # The same plugin name may be specified in the same option multiple times.
504 1 $self->log_error( [
505 "%s cannot be a source provider and a metainfo provider simultaneously",
506 $provider
507 ] );
508 } else {
509 35 $providers->{ $provider } = $META;
510 };
511 };
512 21 return $providers;
513 };
514  
515 # --------------------------------------------------------------------------------------------------
516  
517 #pod =attr _dw
518 #pod
519 #pod Max length of user-visible deed names.
520 #pod
521 #pod C<Int>, read-only, not an init arg.
522 #pod
523 #pod =cut
524  
525 3 has _dw => (
526 isa => 'Int',
527 is => 'ro',
528 lazy => 1,
529 builder => '_build_dw',
530 init_arg => undef,
531 );
532  
533 20 ▶sub _build_dw {
Subroutine _build_dw: called CC=1 SCAR=0.0
534 20 my ( $self ) = @_;
535 20 return List::Util::max( map( { length( $_ ) } values( %{ $self->deeds } ) ) );
536 };
537  
538 # --------------------------------------------------------------------------------------------------
539  
540 61 ▶around found_files => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
541 61 my ( $orig, $self ) = @_;
542 61 my $found = $self->$orig();
543 61 ▶ push( @$found, $self->_manifest_skip_file ) if defined $self->_manifest_skip_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_manifest_skip_file 59 2 61
544 61 return $found;
545 3 };
546  
547 # --------------------------------------------------------------------------------------------------
548  
549 #pod =method before_build
550 #pod
551 #pod This method is called by C<Dist::Zilla> automatically before build. The method checks validity of
552 #pod source and metainfo provider names.
553 #pod
554 #pod =cut
555  
556 21 ▶sub before_build {
Subroutine before_build: called CC=6 SCAR=17.9
Pod documented
557 21 my ( $self ) = @_;
558 21 ▶ if ( $self->strict >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict >= 0) 20 1 21
559 20 ▶ my $log_warning = $self->strict > 0 ? 'log_error' : 'log';
Branch: 1 branch
BranchTrueFalseTotal
$self->strict > 0 ? : 19 1 20
560 20 my $zilla = $self->zilla;
561 20 for my $provider ( @{ $self->source_providers }, @{ $self->metainfo_providers } ) {
562 62 ▶ if ( my $plugin = $zilla->plugin_named( $provider ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin = $zilla->plugin_named($provider)) { } 58 4 62
563 58 ▶ if ( not $plugin->does( 'Dist::Zilla::Role::FileInjector' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin->does("Dist::Zilla::Role::FileInjector")) 1 57 58
564 1 $self->log_error( [ "%s does not do FileInjector role", $provider ] );
565 };
566 } else {
567 4 $self->$log_warning( [ "%s is not a plugin", $provider ] );
568 };
569 };
570 20 $self->_providers(); # Initiate building the attribute. It can report errors.
571 20 $self->abort_if_error();
572 };
573 20 return;
574 };
575  
576 # --------------------------------------------------------------------------------------------------
577  
578 #pod =method before_archive
579 #pod
580 #pod This method is called by C<Dist::Zilla> automatically before build the archive. The method prunes
581 #pod files found by file finders specified in the C<exclude_files> option.
582 #pod
583 #pod =cut
584  
585 2 ▶sub before_archive {
Subroutine before_archive: called CC=2 SCAR=6.9
Pod documented
586 2 my ( $self ) = @_;
587 2 my $zilla = $self->zilla;
588 2 for my $file ( @{ $self->found_files } ) {
589 5 $zilla->prune_file( $file );
590 };
591 2 return;
592 };
593  
594 # --------------------------------------------------------------------------------------------------
595  
596 #pod =method gather_files
597 #pod
598 #pod This is the main method of the class. It adds a file with name C<< $self->manifest >> to the
599 #pod distribution. File content is specified by C<CodeRef> to postpone actual file creation. Being
600 #pod evaluated, the code iterates through all the files in distribution in alphabetical order, and
601 #pod fulfills the manifest with filenames and comments.
602 #pod
603 #pod =cut
604  
605 20 ▶sub gather_files {
Subroutine gather_files: called CC=2 SCAR=6.9
Pod documented
606 20 my ( $self, $arg ) = @_;
607 20 $self->add_file( $self->_manifest_file );
608 20 ▶ $self->add_file( $self->_manifest_skip_file ) if defined $self->_manifest_skip_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_manifest_skip_file 19 1 20
609 20 return;
610 };
611  
612 # --------------------------------------------------------------------------------------------------
613  
614 #pod =method _file_name
615 #pod
616 #pod $str = $self->_file_name( $file );
617 #pod
618 #pod Returns filename to be used in manifest. If filename does not include special characters (spaces,
619 #pod backslashes (C<\>), apostrophes (C<'>), hashes (C<#>)), it is the same as real filename, otherwise
620 #pod filename encoded like Perl single-quoted string: backslashes and apostrophes are escaped, and
621 #pod entire filename is enclosed into apostrophes.
622 #pod
623 #pod =cut
624  
625 97 ▶sub _file_name {
Subroutine _file_name: called CC=2 SCAR=6.9
626 97 my ( $self, $file ) = @_;
627 97 my $name = $file->name;
628 97 ▶ if ( $name =~ m{[\ '\\#]}x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /[\ '\\#]/x) 5 92 97
629 5 $name =~ s{([\\'])}{\\$1}gx;
630 5 $name = "'" . $name . "'";
631 };
632 97 return $name;
633 };
634  
635 # --------------------------------------------------------------------------------------------------
636  
637 #pod =method _file_comment
638 #pod
639 #pod $str = $self->_file_comment( $file ); # Without leading sharp.
640 #pod
641 #pod The method returns comment to be used with the specified file. Comment should not include leading
642 #pod sharp character (C<#>).
643 #pod
644 #pod =cut
645  
646 97 ▶sub _file_comment {
Subroutine _file_comment: called CC=6 SCAR=19.4
647 97 my ( $self, $file ) = @_;
648 97 my $history = $self->_file_history( $file );
649 97 my $deed = $self->_file_deed( $file, $history );
650 97 my $breed = $self->_file_breed( $file, $history );
651 97 my $adder = $self->_file_adder( $file, $history );
652 97 my @mungers = $self->_file_mungers( $file, $history );
653 my $comment =
654 sprintf(
655 "%*s file %s by %s",
656 $self->_dw,
657 $self->deeds->{ $deed } || $deed || '(*UNKNOWN*)',
658 97 ▶ $self->breeds->{ $breed } || $breed || '(*UNKNOWN*)',
Branch: 1 branch
BranchTrueFalseTotal
@mungers ? : 1 96 97
Condition40%4/10
$self->deeds->{$deed} || $deed || '(*UNKNOWN*)'
l!l
970
$self->deeds->{$deed} or $deed
l!l&&r!l&&!r
9700
$self->breeds->{$breed} || $breed || '(*UNKNOWN*)'
l!l
970
$self->breeds->{$breed} or $breed
l!l&&r!l&&!r
9700
MC/DC0%0/2
$self->deeds->{$deed} || $deed || '(*UNKNOWN*)'
$self->deeds->{$deed} $deed
MC/DC0%0/2
$self->breeds->{$breed} || $breed || '(*UNKNOWN*)'
$self->breeds->{$breed} $breed
Truth table33%1/3
A or B
A= $self->deeds->{$deed}B= $deed
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $self->breeds->{$breed}B= $breed
ABresult
1X1
011
000
659 $adder
660 ) .
661 (
662 @mungers ? (
663 ' and munged by ' . join( ', ', @mungers )
664 ) : (
665 ''
666 )
667 );
668 97 return $comment;
669 };
670  
671 # --------------------------------------------------------------------------------------------------
672  
673 #pod =method _file_history
674 #pod
675 #pod $arrayref = $self->_file_history( $file );
676 #pod
677 #pod The method calls C<_file_added_by> then does post-processing: all C<filename set> records are
678 #pod filtered out as insignificant and makes sure the log is not empty.
679 #pod
680 #pod =cut
681  
682 97 ▶sub _file_history {
Subroutine _file_history: called CC=2 SCAR=7.1
683 97 my ( $self, $file ) = @_;
684 97 my $added_by = $file->{ added_by };
685 97 my $history = $self->_file_added_by( $file );
686 # Filter out 'filename set' entries.
687 97 $history = [ grep( { $_->{ action } ne 'filename set' } @$history ) ];
688 # Just in case make sure history is not empty.
689 97 ▶ if ( not @$history ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$history) 0 97 97
690 0 $self->log_error( [ '%s file history is empty', $file->name ] );
691 };
692 97 return $history;
693 };
694  
695 # --------------------------------------------------------------------------------------------------
696  
697 #pod =method _file_added_by
698 #pod
699 #pod $arrayref = $self->_file_added_by( $file );
700 #pod
701 #pod The method parses file's C<added_by> log. Internally, C<added_by> log is a list of strings. Here
702 #pod are few examples:
703 #pod
704 #pod content added by COPYING (Dist::Zilla::Plugin::GenerateFile line 114)
705 #pod filename set by GatherFromManifest (Dist::Zilla::Plugin::GatherFromManifest line 125)
706 #pod encoded_content added by GatherFromManifest (Dist::Zilla::Plugin::GatherFromManifest line 126)
707 #pod text from coderef added by MetaJSON (Dist::Zilla::Plugin::MetaJSON line 83)
708 #pod content set by TemplateFiles (Dist::Zilla::Plugin::TemplateFiles line 35)
709 #pod content set by OurPkgVersion (Dist::Zilla::Plugin::OurPkgVersion line 82)
710 #pod content set by PodWeaver (Dist::Zilla::Plugin::PodWeaver line 175)
711 #pod
712 #pod Thus, each string in C<added_by> log follows the format:
713 #pod
714 #pod <action> by <name> (<package> line <number>)
715 #pod
716 #pod The method parses these strings and returns a more convenient for further processing form:
717 #pod
718 #pod [ { action => …, name => …, package => …, line => … }, { … }, … ]
719 #pod
720 #pod Do not call this method directly, use C<_file_history> instead.
721 #pod
722 #pod =cut
723  
724 97 ▶sub _file_added_by {
Subroutine _file_added_by: called CC=4 SCAR=15.0
725 97 my ( $self, $file ) = @_;
726 97 my $added_by = $file->{ added_by };
727 # ^ Do not use accessor — it will convert array of strings into single string.
728 97 my $history = [];
729 97 my $n = 0;
730 97 for my $entry ( @$added_by ) {
731 146 ++ $n;
732 146 ▶ if ( $entry =~ m{\A (.*?) \s by \s (.*) \s \( ([a-z_0-9:]+) \s line \s (\d+) \) \z}ix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /\A (.*?) \s by \s (.*) \s \( ([a-z_0-9:]+) \s line \s (\d+) \) \z/xi) { } 146 0 146
733 146 my ( $action, $name, $package, $line ) = ( $1, $2, $3, $4 );
734 146 push(
735 @$history,
736 { action => $action, name => $name, package => $package, line => $line }
737 );
738 } else {
739 $self->log_error( [
740 "Can't parse entry #%d in file %s added_by log:\n%s",
741 $n, $file->name,
742 join(
743 "\n",
744 map(
745 0 ▶ { ( $_ == $n ? '>>> ' : ' ' ) . $added_by->[ $_ - 1 ] }
Branch: 1 branch
BranchTrueFalseTotal
$_ == $n ? : 0 0 0
746 1 .. @$added_by
747 )
748 )
749 ] );
750 };
751 };
752 97 return $history;
753 };
754  
755 # --------------------------------------------------------------------------------------------------
756  
757 #pod =method _file_deed
758 #pod
759 #pod $str = $self->_file_deed( $file, $history ); # $SOURCE, $META, or $OTHER.
760 #pod
761 #pod Returns internal identifier of file deed.
762 #pod
763 #pod =cut
764  
765 97 ▶sub _file_deed {
Subroutine _file_deed: called CC=3 SCAR=11.6
766 97 my ( $self, $file, $history ) = @_;
767 97 my $deed;
768 97 ▶ if ( my $first = $history->[ 0 ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = $history->[0]) { } 97 0 97
769 97 ▶ $deed = $self->_providers->{ $first->{ name } } || $OTHER;
Condition66%2/3
$self->_providers->{$first->{'name'}} || $Dist::Zilla::Plugin::Manifest::Write::OTHER
l!l&&r!l&&!r
76210
MC/DC0%0/2
$self->_providers->{$first->{'name'}} || $Dist::Zilla::Plugin::Manifest::Write::OTHER
$self->_providers->{$first->{'name'}} $Dist::Zilla::Plugin::Manifest::Write::OTHER
Truth table66%2/3
A || B
A= $self->_providers->{$first->{'name'}}B= $Dist::Zilla::Plugin::Manifest::Write::OTHER
ABresult
1X1
011
000
770 } else {
771 0 $self->log_error( [ "can't find file %s deed: file history is empty", $file->name ] );
772 };
773 97 return $deed;
774 };
775  
776 # --------------------------------------------------------------------------------------------------
777  
778 #pod =method _file_breed
779 #pod
780 #pod $str = $self->_file_breed( $file, $history ); # ADDED or BUILT.
781 #pod
782 #pod Returns internal identifier of file breed, either C<$ADDED> or C<$BUILT>.
783 #pod
784 #pod Current implementation checks file object class: if it is a C<Dist::Zilla::File::OnDisk>, the file
785 #pod is added to distribution, otherwise the file is built.
786 #pod
787 #pod =cut
788  
789 97 ▶sub _file_breed {
Subroutine _file_breed: called CC=2 SCAR=6.9
790 97 my ( $self, $file, $history ) = @_;
791 97 my $breed;
792 97 ▶ if ( $file->isa( 'Dist::Zilla::File::OnDisk' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->isa('Dist::Zilla::File::OnDisk')) { } 43 54 97
793 43 $breed = $ADDED;
794 } else {
795 54 $breed = $BUILT;
796 };
797 97 return $breed;
798 };
799  
800 # --------------------------------------------------------------------------------------------------
801  
802 #pod =method _file_adder
803 #pod
804 #pod $str = $self->_file_adder( $file, $history );
805 #pod
806 #pod Returns moniker of the plugin added the file to the distribution.
807 #pod
808 #pod =cut
809  
810 97 ▶sub _file_adder {
Subroutine _file_adder: called CC=4 SCAR=15.0
811 97 my ( $self, $file, $history ) = @_;
812 97 my $adder = '(*UNKNOWN*)';
813 97 ▶ if ( my $first = $history->[ 0 ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = $history->[0]) { } 97 0 97
814 97 my $name = $first->{ name };
815 97 ▶ if ( my $plugin = $self->zilla->plugin_named( $name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin = $self->zilla->plugin_named($name)) { } 97 0 97
816 97 $adder = __plugin_moniker( $plugin );
817 # Just in case make sure found plugin does `FileInjector` role.
818 97 ▶ $plugin->does( 'Dist::Zilla::Role::FileInjector' ) or
Branch: 1 branch
BranchTrueFalseTotal
$plugin->does("Dist::Zilla::Role::FileInjector") or $self->log_error(["oops: found file adder %s does not do FileInjector role", $name]) 97 0 97
819 $self->log_error( [
820 "oops: found file adder %s does not do FileInjector role", $name
821 ] );
822 } else {
823 0 $self->log_error( [
824 "can't find file %s adder: %s is not a plugin", $file->name, $name
825 ] );
826 };
827 } else {
828 0 $self->log_error( [ "can't find file %s adder: file history is empty", $file->name ] );
829 };
830 97 return $adder;
831 };
832  
833 # --------------------------------------------------------------------------------------------------
834  
835 #pod =method _file_mungers
836 #pod
837 #pod @list = $self->_file_mungers( $file, $history );
838 #pod
839 #pod If C<show_mungers> attribute is C<true>, returns list of monikers of the plugins munged the file.
840 #pod Otherwise returns empty list.
841 #pod
842 #pod =cut
843  
844 97 ▶sub _file_mungers {
Subroutine _file_mungers: called CC=3 SCAR=11.0
845 97 my ( $self, $file, $history ) = @_;
846 97 my @mungers;
847 97 ▶ if ( $self->show_mungers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_mungers) 4 93 97
848 4 for my $i ( 1 .. @$history - 1 ) {
849 1 push( @mungers, $self->_file_munger( $file, $history->[ $i ] ) );
850 };
851 };
852 97 return @mungers;
853 };
854  
855 # --------------------------------------------------------------------------------------------------
856  
857 #pod =method _file_munger
858 #pod
859 #pod $str = $self->_file_munger( $file, $history->[ $n ] );
860 #pod
861 #pod The method is supposed to return a moniker of plugin munged the file. But… see
862 #pod L<Dist::Zilla::Plugin::Manifest::Write/"Correctness of Information">.
863 #pod
864 #pod =cut
865  
866 1 ▶sub _file_munger {
Subroutine _file_munger: called CC=2 SCAR=7.6
867 1 my ( $self, $file, $entry ) = @_;
868 # Try to find a plugin with given name.
869 1 ▶ if ( my $plugin = $self->zilla->plugin_named( $entry->{ name } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plugin = $self->zilla->plugin_named($entry->{"name"})) 1 0 1
870 # Bingo! Return (correct) plugin moniker.
871 1 return __plugin_moniker( $plugin );
872 };
873 # Oops, bad luck. We have:
874 # * a plugin name which is not a plugin name but moniker of *some* plugin.
875 # * a package name which can be a plugin package name or not.
876 # We have to guess.
877 # BTW, I have tried to mark guessed monikers with a question mark, but *all* file mungers
878 # will carry this mark. Looks ugly,so I rejected it.
879 0 return __plugin_moniker( $entry->{ package } );
880 };
881  
882 # --------------------------------------------------------------------------------------------------
883  
884 #pod =method mvp_multivalue_args
885 #pod
886 #pod This method tells C<Dist::Zilla> that C<source_provider>, C<source_providers>,
887 #pod C<metainfo_provider>, and C<metainfo_providers> are multi-value options (i. e. can be specified in
888 #pod several times).
889 #pod
890 #pod =cut
891  
892 21 ▶around mvp_multivalue_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
893 21 my ( $orig, $self ) = @_;
894 return (
895 21 $self->$orig(),
896 qw{ source_provider source_providers metainfo_provider metainfo_providers }
897 );
898 3 };
899  
900 # --------------------------------------------------------------------------------------------------
901  
902 3 __PACKAGE__->meta->make_immutable;
903  
904 3 1;
905  
906 # --------------------------------------------------------------------------------------------------
907  
908 #pod =head1 SYNOPSIS
909 #pod
910 #pod package ManifestWithFileSize;
911 #pod
912 #pod use Moose;
913 #pod use namespace::autoclean;
914 #pod extends 'Dist::Zilla::Plugin::Manifest::Write';
915 #pod our $VERSION = '0.007';
916 #pod
917 #pod # Overload any method or modify it with all the Moose power, e. g.:
918 #pod around _file_comment => sub {
919 #pod my ( $orig, $self, $file ) = @_;
920 #pod my $comment = $self->$orig( $file );
921 #pod if ( $file->name ne $self->manifest ) { # Avoid infinite recursion.
922 #pod $comment .= sprintf( ' (%d bytes)', length( $file->encoded_content ) );
923 #pod };
924 #pod return $comment;
925 #pod };
926 #pod
927 #pod __PACKAGE__->meta->make_immutable;
928 #pod 1;
929 #pod
930 #pod =example Manifest with File Size
931 #pod
932 #pod A module shown in Synopsis is a real example. Its result looks like:
933 #pod
934 #pod # This file was generated with ManifestWithFileSize 0.007.
935 #pod MANIFEST # metainfo file built by =ManifestWithFileSize
936 #pod dist.ini # Dummy file added by GatherDir (239 bytes)
937 #pod lib/Dummy.pm # Dummy file added by GatherDir (22 bytes)
938 #pod
939 #pod
940 #pod =head1 COPYRIGHT AND LICENSE
941 #pod
942 #pod Copyright (C) 2015, 2016 Van de Bugger
943 #pod
944 #pod License GPLv3+: The GNU General Public License version 3 or later
945 #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
946 #pod
947 #pod This is free software: you are free to change and redistribute it. There is
948 #pod NO WARRANTY, to the extent permitted by law.
949 #pod
950 #pod
951 #pod =cut
952  
953 # ------------------------------------------------------------------------------------------------
954 #
955 # file: doc/what.pod
956 #
957 # This file is part of perl-Dist-Zilla-Plugin-Manifest-Write.
958 #
959 # ------------------------------------------------------------------------------------------------
960  
961 #pod =encoding UTF-8
962 #pod
963 #pod =head1 WHAT?
964 #pod
965 #pod C<Dist-Zilla-Plugin-Manifest-Write> (or C<Manifest::Write> for brevity) is a plugin for C<Dist::Zilla>, a replacement
966 #pod for standard plugin C<Manifest>. C<Manifest::Write> writes I<annotated> distribution manifest: each
967 #pod filename is followed by a comment explaining origin of the file: if it is a part of software, meta
968 #pod information, or 3rd-party file. Also it can B<I<exclude> built files from distribution>, e. g.
969 #pod extra tests have to be built (to run) but need not be distributed.
970 #pod
971 #pod =cut
972  
973 # end of file #
974  
975  
976 # end of file #
977  
978 __END__
↑ Summary eg/ManifestWithFileSize.pm »