| 1 | package CPANfile::Parse::PPI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Parse I<cpanfile>s with PPI | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 12 | our $VERSION = '0.06'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use List::Util qw(first any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use PPI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | my $strict; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has meta => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 28 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 21 | 28 | ▶ | die if 'HASH' ne ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 12 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | has modules => ( | ||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||
| 27 | 28 | ▶ | isa => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 28 | 28 | ▶ | die if 'ARRAY' ne ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 12 | ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 31 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 31 | my ($class, $file_or_code) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 31 | my ($meta, @modules) = _parse( $file_or_code ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | return { | ||||||||||||||||||||||||||||
| 39 | 28 | modules => \@modules, | |||||||||||||||||||||||||||
| 40 | meta => $meta, | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 12 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | $strict = 1 if grep{ $_ eq '-strict' }@_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 31 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=26 SCAR=32.6
| |||||||||||||||||||||||||||||
| 49 | 31 | my ($file_or_code) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 31 | my $doc = PPI::Document->new( $file_or_code ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # 'feature' and 'on' are handled separately | ||||||||||||||||||||||||||||
| 54 | 31 | my @bindings = qw( | |||||||||||||||||||||||||||
| 55 | mirror osname | ||||||||||||||||||||||||||||
| 56 | requires recommends conflicts suggests | ||||||||||||||||||||||||||||
| 57 | test_requires author_requires configure_requires build_requires | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | my $requires = $doc->find( | ||||||||||||||||||||||||||||
| 61 | 3777 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 62 | 3777 | ▶ | $_[1]->isa('PPI::Token::Word') and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 432 | my $content = $_[1]->content; | |||||||||||||||||||||||||||
| 64 | 432 | ▶ | first { $content eq $_ } @bindings; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 31 | ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 31 | ▶ | return if !$requires; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 31 | my @modules; | |||||||||||||||||||||||||||
| 72 | 31 | my $meta = {}; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | REQUIRED: | ||||||||||||||||||||||||||||
| 75 | 31 | ▶ | for my $required ( @{ $requires || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$requires || []
MC/DC0%0/1
$requires || []
$requires
Truth table50%1/2
A
A= $requires
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # 'mirror' can be an attribute for "requires" as well as a keyword | ||||||||||||||||||||||||||||
| 78 | # _scan_attrs should have removed all 'mirrors' that are used as | ||||||||||||||||||||||||||||
| 79 | # an attribute for 'requires'. So skip those PPI nodes... | ||||||||||||||||||||||||||||
| 80 | 304 | ▶ | next REQUIRED if !$required; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 304 | my $value = $required->snext_sibling; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 304 | my $type = $required->content; | |||||||||||||||||||||||||||
| 86 | 304 | my %on_feature = ( | |||||||||||||||||||||||||||
| 87 | on => '', | ||||||||||||||||||||||||||||
| 88 | feature => '', | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 304 | ▶ | if ( $type eq 'mirror' or $type eq 'osname' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "mirror" or $type eq "osname"
MC/DC50%1/2
$type eq "mirror" or $type eq "osname"
$type eq "mirror"
$type eq "osname"
Truth table66%2/3
A or B
A= $type eq "mirror"B= $type eq "osname"
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | push @{ $meta->{$type} }, $value->content if $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 304 | ▶ | if ( -1 != index $type, '_' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 2 | (my $stage, $type) = split /_/, $type, 2; | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | $stage = 'develop' if $stage eq 'author'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 2 | $on_feature{on} = $stage; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 304 | my %attr = _scan_attrs( $required, $type ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 304 | ▶ | next REQUIRED if !$value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 302 | ▶ | my $can_string = $value->can('string') ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 302 | ▶ | my $prereq = $can_string ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | $value->string : | ||||||||||||||||||||||||||||
| 108 | $value->content; | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | #next REQUIRED if $prereq eq 'perl'; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 302 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value->isa("PPI::Token::Symbol") or $prereq =~ /\A[^A-Za-z]/
MC/DC100%2/2
$value->isa("PPI::Token::Symbol") or $prereq =~ /\A[^A-Za-z]/
$value->isa("PPI::Token::Symbol")
$prereq =~ /\A[^A-Za-z]/
Truth table100%3/3
A or B
A= $value->isa("PPI::Token::Symbol")B= $prereq =~ /\A[^A-Za-z]/
| |||||||||||||||||||||||||||||
| 113 | $value->isa('PPI::Token::Symbol') || | ||||||||||||||||||||||||||||
| 114 | $prereq =~ m{\A[^A-Za-z]} | ||||||||||||||||||||||||||||
| 115 | ) { | ||||||||||||||||||||||||||||
| 116 | 6 | ▶ | carp 'Cannot handle dynamic code' if !$strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 6 | ▶ | croak 'Cannot handle dynamic code' if $strict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | next REQUIRED; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 296 | my $parent_node = $value; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | PARENT: | ||||||||||||||||||||||||||||
| 125 | 296 | while ( 1 ) { | |||||||||||||||||||||||||||
| 126 | 593 | $parent_node = $parent_node->parent; | |||||||||||||||||||||||||||
| 127 | 593 | ▶ | last PARENT if !$parent_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 593 | ▶ | last PARENT if $parent_node->isa('PPI::Document'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 435 | ▶ | if ( $parent_node->isa('PPI::Structure::Block') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 139 | $parent_node = $parent_node->parent; | |||||||||||||||||||||||||||
| 132 | my ($on_feature) = $parent_node->find_first( | ||||||||||||||||||||||||||||
| 133 | 161 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | # need to create token var because 'any' messes up $_ | ||||||||||||||||||||||||||||
| 135 | 161 | my $token = $_[1]; | |||||||||||||||||||||||||||
| 136 | $token->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||
| 137 | && ( | ||||||||||||||||||||||||||||
| 138 | 146 | ▶ | any { $token->content eq $_ } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 161 | ▶ | (qw{on feature}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 139 | ); | |||||||||||||||||||||||||||
| 143 | 139 | ▶ | if ($on_feature) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 138 | my $word = $on_feature->snext_sibling; | |||||||||||||||||||||||||||
| 145 | 138 | ▶ | my $condition | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | = $word->can('string') | ||||||||||||||||||||||||||||
| 147 | ? $word->string | ||||||||||||||||||||||||||||
| 148 | : $word->content; | ||||||||||||||||||||||||||||
| 149 | 138 | $on_feature{ $on_feature->content } = $condition; | |||||||||||||||||||||||||||
| 150 | 138 | last PARENT; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||||||||||
| 154 | 1 | next PARENT; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 296 | my $version = ''; | |||||||||||||||||||||||||||
| 161 | 296 | my $sibling = $value->snext_sibling; | |||||||||||||||||||||||||||
| 162 | SIBLING: | ||||||||||||||||||||||||||||
| 163 | 296 | while ( 1 ) { | |||||||||||||||||||||||||||
| 164 | 365 | ▶ | last SIBLING if !$sibling; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 296 | ▶ | do { $sibling = $sibling->snext_sibling; next SIBLING } if !$sibling->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 227 | my $value = $sibling->snext_sibling; | |||||||||||||||||||||||||||
| 169 | 227 | ▶ | last SIBLING if !$value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 222 | ▶ | $version = $value->can('string') ? $value->string : $value->content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 222 | last SIBLING; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | push @modules, { | ||||||||||||||||||||||||||||
| 177 | name => $prereq, | ||||||||||||||||||||||||||||
| 178 | version => $version, | ||||||||||||||||||||||||||||
| 179 | type => $type, | ||||||||||||||||||||||||||||
| 180 | stage => $on_feature{on}, | ||||||||||||||||||||||||||||
| 181 | feature => $on_feature{feature}, | ||||||||||||||||||||||||||||
| 182 | 296 | %attr, | |||||||||||||||||||||||||||
| 183 | }; | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 28 | return $meta, @modules; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 304 | ▶ | sub _scan_attrs { | ||||||||||||||||||||||||||
|
Subroutine
_scan_attrs: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 190 | 304 | my ($required, $type) = @_; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 304 | ▶ | return if $type ne 'requires' && $type ne 'recommends'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type ne "requires" and $type ne "recommends"
MC/DC100%2/2
$type ne "requires" and $type ne "recommends"
$type ne "requires"
$type ne "recommends"
Truth table100%3/3
A and B
A= $type ne "requires"B= $type ne "recommends"
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 299 | my $sibling = $required->snext_sibling; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 299 | my %attr; | |||||||||||||||||||||||||||
| 197 | my @to_delete; | ||||||||||||||||||||||||||||
| 198 | 299 | my $delete; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 299 | ▶ | while ( $sibling ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1108 | my $content = $sibling->content; | |||||||||||||||||||||||||||
| 202 | 1108 | ▶ | if ( $content eq 'mirror' or $content eq 'dist' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$content eq "mirror" or $content eq "dist"
MC/DC100%2/2
$content eq "mirror" or $content eq "dist"
$content eq "mirror"
$content eq "dist"
Truth table100%3/3
A or B
A= $content eq "mirror"B= $content eq "dist"
| |||||||||||||||||||||||||||||
| 203 | 8 | $delete = 1; | |||||||||||||||||||||||||||
| 204 | 8 | my $value_node = $sibling->snext_sibling->snext_sibling; | |||||||||||||||||||||||||||
| 205 | 8 | ▶ | $attr{$content} = $value_node->can('string') ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | $value_node->string : | ||||||||||||||||||||||||||||
| 207 | $value_node->content; | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1108 | ▶ | push @to_delete, $sibling if $delete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 1108 | $sibling = $sibling->snext_sibling; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 299 | $_->remove for @to_delete; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 299 | return %attr; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 12 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||