| 1 | package Dist::Zilla::Plugin::PkgDist 6.040; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: add a $DIST to your packages | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | with( | |||||||||||||||||||||||||||||||||||||||||
| 6 | 'Dist::Zilla::Role::FileMunger', | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 'Dist::Zilla::Role::FileFinderUser' => { | ||||||||||||||||||||||||||||||||||||||||||
| 8 | default_finders => [ ':InstallModules', ':ExecFiles' ], | ||||||||||||||||||||||||||||||||||||||||||
| 9 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 'Dist::Zilla::Role::PPI', | ||||||||||||||||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Dist::Zilla::Pragmas; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 18 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 19 | #pod This plugin will add a line like the following to each package in each Perl | ||||||||||||||||||||||||||||||||||||||||||
| 20 | #pod module or program (more or less) within the distribution: | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 22 | #pod { our $DIST = 'My-CPAN-Dist'; } # where 'My-CPAN-Dist' is your dist name | ||||||||||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 24 | #pod It will skip any package declaration that includes a newline between the | ||||||||||||||||||||||||||||||||||||||||||
| 25 | #pod C<package> keyword and the package name, like: | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 27 | #pod package | ||||||||||||||||||||||||||||||||||||||||||
| 28 | #pod Foo::Bar; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 30 | #pod This sort of declaration is also ignored by the CPAN toolchain, and is | ||||||||||||||||||||||||||||||||||||||||||
| 31 | #pod typically used when doing monkey patching or other tricky things. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||||||||||||||||
| 33 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub munge_files { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
munge_files: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | $self->munge_file($_) for @{ $self->found_files }; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 9 | ▶ | sub munge_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
munge_file: called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # XXX: for test purposes, for now! evil! -- rjbs, 2010-03-17 | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 9 | ▶ | return if $file->name =~ /^corpus\//; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | return if $file->name =~ /\.t$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | ▶ | return $self->munge_perl($file) if $file->name =~ /\.(?:pm|pl)$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $self->munge_perl($file) if $file->content =~ /^#!(?:.*)perl(?:$|\s)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 9 | ▶ | sub munge_perl { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
munge_perl: called CC=9 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 9 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | my $dist_name = $self->zilla->name; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 9 | my $document = $self->ppi_document_for_file($file); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 9 | ▶ | return unless my $package_stmts = $document->find('PPI::Statement::Package'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | ▶ | if ($self->document_assigns_to_variable($document, '$DIST')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $self->log([ 'skipping %s: assigns to $DIST', $file->name ]); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | my %seen_pkg; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | for my $stmt (@$package_stmts) { | |||||||||||||||||||||||||||||||||||||||||
| 70 | 9 | my $package = $stmt->namespace; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | ▶ | if ($seen_pkg{ $package }++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | $self->log([ 'skipping package re-declaration for %s', $package ]); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | if ($stmt->content =~ /package\s*\n\s*\Q$package/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $self->log([ 'skipping private package %s', $package ]); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # the \x20 hack is here so that when we scan *this* document we don't find | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # an assignment to version; it shouldn't be needed, but it's been annoying | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # enough in the past that I'm keeping it here until tests are better | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | my $perl = "{\n \$$package\::DIST\x20=\x20'$dist_name';\n}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | my $dist_doc = PPI::Document->new(\$perl); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | my @children = $dist_doc->schildren; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | $self->log_debug([ | |||||||||||||||||||||||||||||||||||||||||
| 91 | 'adding $DIST assignment to %s in %s', | ||||||||||||||||||||||||||||||||||||||||||
| 92 | $package, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | $file->name, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | ]); | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # the extra whitespace element ensures we don't swallow up any blanks | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # lines after 'package ...' in the source file that PkgVersion warns about | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # if it's missing. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 7 | ▶ | Carp::carp('error inserting $DIST in ' . $file->name) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$stmt->add_element("PPI::Token::Whitespace"->new("\n")) and $stmt->insert_after($children[0]->clone) and $stmt->insert_after("PPI::Token::Whitespace"->new("\n"))
$stmt->add_element("PPI::Token::Whitespace"->new("\n")) and $stmt->insert_after($children[0]->clone)
MC/DC0%0/3
$stmt->add_element("PPI::Token::Whitespace"->new("\n")) and $stmt->insert_after($children[0]->clone) and $stmt->insert_after("PPI::Token::Whitespace"->new("\n"))
$stmt->add_element("PPI::Token::Whitespace"->new("\n"))
$stmt->insert_after($children[0]->clone)
$stmt->insert_after("PPI::Token::Whitespace"->new("\n"))
Truth table25%1/4
A and B and C
A= $stmt->add_element("PPI::Token::Whitespace"->new("\n"))B= $stmt->insert_after($children[0]->clone)C= $stmt->insert_after("PPI::Token::Whitespace"->new("\n"))
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | unless $stmt->add_element( PPI::Token::Whitespace->new("\n") ) | ||||||||||||||||||||||||||||||||||||||||||
| 101 | and $stmt->insert_after($children[0]->clone) | ||||||||||||||||||||||||||||||||||||||||||
| 102 | and $stmt->insert_after( PPI::Token::Whitespace->new("\n") ); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # the document is no longer correct; it must be reparsed before it can be used again | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | $file->content($document->serialize); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||||||||||||||||