| 1 | package Dist::Zilla::Plugin::SyncCPANfile; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Sync a cpanfile with the prereqs listed in dist.ini | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | #use v5.10; | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 6 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use version; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use CPAN::Audit; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 6 | with qw( | |||||||||||||||||||||||||||||||
| 20 | Dist::Zilla::Role::AfterBuild | ||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 6 | has cpan_audit => ( | |||||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 25 | isa => 'Bool', | ||||||||||||||||||||||||||||||||
| 26 | default => 0, | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 6 | has filename => ( | |||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 32 | default => 'cpanfile', | ||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | has comment => ( | ||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 37 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||||||
| 38 | 15 | ▶ | default => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||
| 39 | [ | ||||||||||||||||||||||||||||||||
| 40 | 15 | ▶ | ( sprintf 'This file is generated by %s v%s', __PACKAGE__, __PACKAGE__->VERSION // '<internal>' ), | ||||||||||||||||||||||||||||||
|
Condition50%1/2
__PACKAGE__->VERSION // "<internal>"
MC/DC0%0/1
__PACKAGE__->VERSION // "<internal>"
__PACKAGE__->VERSION
Truth table50%1/2
A
A= __PACKAGE__->VERSION
| |||||||||||||||||||||||||||||||||
| 41 | 'Do not edit this file directly. To change prereqs, edit the `dist.ini` file.', | ||||||||||||||||||||||||||||||||
| 42 | ] | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | 6 | ); | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 17 | ▶ | sub mvp_multivalue_args { qw( comment ) } | ||||||||||||||||||||||||||||||
|
Subroutine
mvp_multivalue_args: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 17 | ▶ | sub after_build { | ||||||||||||||||||||||||||||||
|
Subroutine
after_build: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 49 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 17 | my $content = $self->_get_cpanfile(); | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # need to write it to disk if we're in a | ||||||||||||||||||||||||||||||||
| 54 | # phase that is not filemunge | ||||||||||||||||||||||||||||||||
| 55 | 17 | path( $self->filename )->spew_raw( $content ); | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 17 | ▶ | sub _get_cpanfile { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_cpanfile: called CC=20 SCAR=31.2
| |||||||||||||||||||||||||||||||||
| 59 | 17 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 17 | my $audit = CPAN::Audit->new; | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 17 | my $zilla = $self->zilla; | |||||||||||||||||||||||||||||||
| 64 | 17 | my $prereqs = $zilla->prereqs; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 17 | my @types = qw(requires recommends suggests conflicts); | |||||||||||||||||||||||||||||||
| 67 | 17 | my @phases = qw(runtime build test configure develop); | |||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 17 | my $str = join "\n", ( map { "# $_" } @{ $self->comment } ), '', ''; | |||||||||||||||||||||||||||||||
| 70 | 17 | for my $phase (@phases) { | |||||||||||||||||||||||||||||||
| 71 | 85 | ▶ | my $prefix = $phase eq 'runtime' ? '' : (sprintf "\non '%s' => sub {\n", $phase ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 85 | ▶ | my $postfix = $phase eq 'runtime' ? '' : "};\n"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 85 | ▶ | my $indent = $phase eq 'runtime' ? '' : ' '; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 85 | for my $type (@types) { | |||||||||||||||||||||||||||||||
| 76 | 340 | my $req = $prereqs->requirements_for($phase, $type); | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 340 | ▶ | next unless $req->required_modules; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 17 | $str .= $prefix; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 17 | for my $module ( sort $req->required_modules ) { | |||||||||||||||||||||||||||||||
| 83 | 19 | ▶ | my $version = $req->requirements_for_module( $module ) || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->requirements_for_module($module) || 0
MC/DC100%1/1
$req->requirements_for_module($module) || 0
$req->requirements_for_module($module)
Truth table100%2/2
A
A= $req->requirements_for_module($module)
| |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 19 | my ($min_version, $advisories); | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | if ( $self->cpan_audit ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 9 | ($min_version, $advisories) = _audit( $audit, $module, $version ); | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 19 | ▶ | if ( $advisories && $version =~ m{(>|<|>=|<=|!=|==)} ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$advisories and $version =~ /(>|<|>=|<=|!=|==)/
MC/DC100%2/2
$advisories and $version =~ /(>|<|>=|<=|!=|==)/
$advisories
$version =~ /(>|<|>=|<=|!=|==)/
Truth table100%3/3
A and B
A= $advisoriesB= $version =~ /(>|<|>=|<=|!=|==)/
| |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # this seems to be a version range, so check if the latest fixed version would be accepted | ||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | if ( defined $min_version && !$req->accepts_module( $module, $min_version ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $min_version and not $req->accepts_module($module, $min_version)
MC/DC0%0/2
defined $min_version and not $req->accepts_module($module, $min_version)
defined $min_version
not $req->accepts_module($module, $min_version)
Truth table33%1/3
A and not(B)
A= defined $min_versionB= not $req->accepts_module($module, $min_version)
| |||||||||||||||||||||||||||||||||
| 95 | 0 | $self->log( "Range '$version' for $module does not include latest fixed version ($min_version)!" ); | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | elsif ( defined $min_version ) { | ||||||||||||||||||||||||||||||||
| 98 | 0 | $self->log( "Current version range includes vulnerable versions. Consider updating the minimum to $min_version" ) #if $affected_version_allowed; | |||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | elsif ( $advisories ) { | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # this branch is used when no version range is given but a version number | ||||||||||||||||||||||||||||||||
| 104 | 3 | ▶ | my $vuln_version_requested = $min_version && ( | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$min_version && 'version'->new($version) < 'version'->new($min_version)
MC/DC0%0/2
$min_version && 'version'->new($version) < 'version'->new($min_version)
$min_version
'version'->new($version) < 'version'->new($min_version)
Truth table33%1/3
A && B
A= $min_versionB= 'version'->new($version) < 'version'->new($min_version)
| |||||||||||||||||||||||||||||||||
| 105 | version->new( $version ) < version->new( $min_version ) | ||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 3 | ▶ | if ( $version == 0 && $vuln_version_requested ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$version == 0 and $vuln_version_requested
MC/DC0%0/2
$version == 0 and $vuln_version_requested
$version == 0
$vuln_version_requested
Truth table66%2/3
A and B
A= $version == 0B= $vuln_version_requested
| |||||||||||||||||||||||||||||||||
| 109 | 0 | $version = $min_version; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | elsif ( $vuln_version_requested ) { | ||||||||||||||||||||||||||||||||
| 112 | 0 | $self->log( "Prereq $module $version is vulnerable" ); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 19 | $str .= sprintf qq~%s%s "%s" => "%s";\n~, | |||||||||||||||||||||||||||||||
| 117 | $indent, | ||||||||||||||||||||||||||||||||
| 118 | $type, | ||||||||||||||||||||||||||||||||
| 119 | $module, | ||||||||||||||||||||||||||||||||
| 120 | $version; | ||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 17 | $str .= $postfix; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 17 | return $str; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | sub _audit { | ||||||||||||||||||||||||||||||
|
Subroutine
_audit: called CC=6 SCAR=18.9
| |||||||||||||||||||||||||||||||||
| 131 | 9 | my ($audit, $module, $version) = @_; | |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 9 | my $result = $audit->command( 'module', $module, $version ); | |||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | my ($module_data) = values %{ $result->{dists} || {} }; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$result->{'dists'} || {}
MC/DC0%0/1
$result->{'dists'} || {}
$result->{'dists'}
Truth table50%1/2
A
A= $result->{'dists'}
| |||||||||||||||||||||||||||||||||
| 135 | 9 | ▶ | my @advisories = @{ $module_data->{advisories} || [] }; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$module_data->{'advisories'} || []
MC/DC100%1/1
$module_data->{'advisories'} || []
$module_data->{'advisories'}
Truth table100%2/2
A
A= $module_data->{'advisories'}
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 9 | my @versions; | |||||||||||||||||||||||||||||||
| 138 | 9 | for my $advisory ( @advisories ) { | |||||||||||||||||||||||||||||||
| 139 | 22 | ▶ | my ($fixed_version) = ( $advisory->{fixed_versions} // '' ) =~ m{(v?[0-9]+(?:\.[0-9]+){0,2})}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$advisory->{"fixed_versions"} // ""
MC/DC0%0/1
$advisory->{"fixed_versions"} // ""
$advisory->{"fixed_versions"}
Truth table50%1/2
A
A= $advisory->{"fixed_versions"}
| |||||||||||||||||||||||||||||||||
| 140 | 22 | ▶ | next if !$fixed_version; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | 0 | my $version_object = version->new( $fixed_version ); | |||||||||||||||||||||||||||||||
| 143 | 0 | push @versions, $version_object; | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 0 | my ($min_version) = sort { $b <=> $a } @versions; | |||||||||||||||||||||||||||||||
| 147 | 9 | return ( $min_version, scalar @advisories ); | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 6 | 1; | |||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||||||