File Coverage

blib/lib/Dist/Zilla/Plugin/PkgDist.pm
Criterion Covered Total %
statement 38 40 95.0
branch 12 18 66.6
condition 2 6 33.3
subroutine 6 6 100.0
pod 0 3 0.0
total 58 73 79.4


line stmt bran cond sub pod time code
1             # ABSTRACT: add a $DIST to your packages
2              
3             use Moose;
4 2     2   1978 with(
  2         6  
  2         19  
5             'Dist::Zilla::Role::FileMunger',
6             'Dist::Zilla::Role::FileFinderUser' => {
7             default_finders => [ ':InstallModules', ':ExecFiles' ],
8             },
9             'Dist::Zilla::Role::PPI',
10             );
11              
12             use Dist::Zilla::Pragmas;
13 2     2   14400  
  2         5  
  2         22  
14             use namespace::autoclean;
15 2     2   16  
  2         5  
  2         18  
16             #pod =head1 DESCRIPTION
17             #pod
18             #pod This plugin will add a line like the following to each package in each Perl
19             #pod module or program (more or less) within the distribution:
20             #pod
21             #pod { our $DIST = 'My-CPAN-Dist'; } # where 'My-CPAN-Dist' is your dist name
22             #pod
23             #pod It will skip any package declaration that includes a newline between the
24             #pod C<package> keyword and the package name, like:
25             #pod
26             #pod package
27             #pod Foo::Bar;
28             #pod
29             #pod This sort of declaration is also ignored by the CPAN toolchain, and is
30             #pod typically used when doing monkey patching or other tricky things.
31             #pod
32             #pod =cut
33              
34             my ($self) = @_;
35              
36 1     1 0 5 $self->munge_file($_) for @{ $self->found_files };
37             }
38 1         2  
  1         6  
39             my ($self, $file) = @_;
40              
41             # XXX: for test purposes, for now! evil! -- rjbs, 2010-03-17
42 9     9 0 2293 return if $file->name =~ /^corpus\//;
43              
44             return if $file->name =~ /\.t$/i;
45 9 50       38 return $self->munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i;
46             return $self->munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/;
47 9 50       28 return;
48 9 50       28 }
49 0 0       0  
50 0         0 my ($self, $file) = @_;
51              
52             my $dist_name = $self->zilla->name;
53              
54 9     9 0 24 my $document = $self->ppi_document_for_file($file);
55              
56 9         260 return unless my $package_stmts = $document->find('PPI::Statement::Package');
57              
58 9         37 if ($self->document_assigns_to_variable($document, '$DIST')) {
59             $self->log([ 'skipping %s: assigns to $DIST', $file->name ]);
60 9 100       2172 return;
61             }
62 8 100       5803  
63 3         16 my %seen_pkg;
64 3         1057  
65             for my $stmt (@$package_stmts) {
66             my $package = $stmt->namespace;
67 5         13  
68             if ($seen_pkg{ $package }++) {
69 5         13 $self->log([ 'skipping package re-declaration for %s', $package ]);
70 9         1594 next;
71             }
72 9 100       249  
73 1         6 if ($stmt->content =~ /package\s*\n\s*\Q$package/) {
74 1         272 $self->log([ 'skipping private package %s', $package ]);
75             next;
76             }
77 8 100       22  
78 1         52 # the \x20 hack is here so that when we scan *this* document we don't find
79 1         328 # an assignment to version; it shouldn't be needed, but it's been annoying
80             # enough in the past that I'm keeping it here until tests are better
81             my $perl = "{\n \$$package\::DIST\x20=\x20'$dist_name';\n}\n";
82              
83             my $dist_doc = PPI::Document->new(\$perl);
84             my @children = $dist_doc->schildren;
85 7         323  
86             $self->log_debug([
87 7         32 'adding $DIST assignment to %s in %s',
88 7         13015 $package,
89             $file->name,
90 7         117 ]);
91              
92             # the extra whitespace element ensures we don't swallow up any blanks
93             # lines after 'package ...' in the source file that PkgVersion warns about
94             # if it's missing.
95             Carp::carp('error inserting $DIST in ' . $file->name)
96             unless $stmt->add_element( PPI::Token::Whitespace->new("\n") )
97             and $stmt->insert_after($children[0]->clone)
98             and $stmt->insert_after( PPI::Token::Whitespace->new("\n") );
99 7 50 33     212 }
      33        
100              
101             # the document is no longer correct; it must be reparsed before it can be used again
102             $file->content($document->serialize);
103             }
104              
105             __PACKAGE__->meta->make_immutable;
106 5         1218 1;
107              
108              
109             =pod
110              
111             =encoding UTF-8
112              
113             =head1 NAME
114              
115             Dist::Zilla::Plugin::PkgDist - add a $DIST to your packages
116              
117             =head1 VERSION
118              
119             version 6.028
120              
121             =head1 DESCRIPTION
122              
123             This plugin will add a line like the following to each package in each Perl
124             module or program (more or less) within the distribution:
125              
126             { our $DIST = 'My-CPAN-Dist'; } # where 'My-CPAN-Dist' is your dist name
127              
128             It will skip any package declaration that includes a newline between the
129             C<package> keyword and the package name, like:
130              
131             package
132             Foo::Bar;
133              
134             This sort of declaration is also ignored by the CPAN toolchain, and is
135             typically used when doing monkey patching or other tricky things.
136              
137             =head1 PERL VERSION
138              
139             This module should work on any version of perl still receiving updates from
140             the Perl 5 Porters. This means it should work on any version of perl released
141             in the last two to three years. (That is, if the most recently released
142             version is v5.40, then this module should work on both v5.40 and v5.38.)
143              
144             Although it may work on older versions of perl, no guarantee is made that the
145             minimum required version will not be increased. The version may be increased
146             for any reason, and there is no promise that patches will be accepted to lower
147             the minimum required perl.
148              
149             =head1 AUTHOR
150              
151             Ricardo SIGNES 😏 <cpan@semiotic.systems>
152              
153             =head1 COPYRIGHT AND LICENSE
154              
155             This software is copyright (c) 2022 by Ricardo SIGNES.
156              
157             This is free software; you can redistribute it and/or modify it under
158             the same terms as the Perl 5 programming language system itself.
159              
160             =cut