| 1 | package Dist::Zilla::Plugin::AutoPrereqs::Perl::Critic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | with qw( | |||||||||||||||||||||||||||
| 11 | Dist::Zilla::Role::PrereqSource | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use CPAN::Meta::YAML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use CPAN::Version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use Moose::Util::TypeConstraints 'enum'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Perl::Critic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | has critic_config => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 26 | default => undef, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 4 | has phase => ( | |||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||
| 31 | isa => enum( [qw(configure build test runtime develop)] ), | ||||||||||||||||||||||||||||
| 32 | default => 'develop', | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 4 | has remove_core_policies => ( | |||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 38 | default => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | has type => ( | |||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||
| 43 | isa => enum( [qw(requires recommends suggests conflicts)] ), | ||||||||||||||||||||||||||||
| 44 | default => 'requires', | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | has _core_policies => ( | ||||||||||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||||||||
| 49 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 50 | lazy => 1, | ||||||||||||||||||||||||||||
| 51 | 6 | ▶ | default => sub { shift->_build_core_policies() }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 4 | ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 4 | has _cache_file => ( | |||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||
| 56 | isa => 'Str', | ||||||||||||||||||||||||||||
| 57 | default => '.perlcritic_package.yml', | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 12 | ▶ | sub register_prereqs { | ||||||||||||||||||||||||||
|
Subroutine
register_prereqs: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 12 | my $type = $self->type; | |||||||||||||||||||||||||||
| 64 | 12 | my $phase = $self->phase; | |||||||||||||||||||||||||||
| 65 | 12 | my $critic_config = $self->critic_config; | |||||||||||||||||||||||||||
| 66 | 12 | my $remove_core_policies = $self->remove_core_policies; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 12 | my %critic_args; | |||||||||||||||||||||||||||
| 69 | 12 | ▶ | if ( defined $critic_config ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 9 | $critic_args{-profile} = $critic_config; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 12 | my $critic = Perl::Critic->new(%critic_args); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | POLICY: | ||||||||||||||||||||||||||||
| 76 | 12 | for my $policy ( $critic->config()->policies() ) { | |||||||||||||||||||||||||||
| 77 | 13 | my $policy_module = ref $policy; | |||||||||||||||||||||||||||
| 78 | 13 | my $policy_version = $policy->VERSION(); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 13 | ▶ | next POLICY if $remove_core_policies and exists $self->_core_policies->{$policy_module}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$remove_core_policies and exists $self->_core_policies->{$policy_module}
MC/DC100%2/2
$remove_core_policies and exists $self->_core_policies->{$policy_module}
$remove_core_policies
exists $self->_core_policies->{$policy_module}
Truth table100%3/3
A and B
A= $remove_core_policiesB= exists $self->_core_policies->{$policy_module}
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 7 | $self->zilla->register_prereqs( { phase => $phase, type => $type }, $policy_module => $policy_version ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 11 | $self->zilla->register_prereqs( { phase => $phase, type => $type }, 'Perl::Critic' => Perl::Critic->VERSION() ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 11 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | sub _build_core_policies { | ||||||||||||||||||||||||||
|
Subroutine
_build_core_policies: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | my $yaml = $self->_read_perl_critic_packages_cache(); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 6 | ▶ | if ( !defined $yaml ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 5 | $yaml = $self->_get_perl_critic_packages_and_create_cache(); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 5 | my $provides = ${$yaml}[0]->{provides}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 5 | return $provides; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | ▶ | sub _get_perl_critic_packages_and_create_cache { | ||||||||||||||||||||||||||
|
Subroutine
_get_perl_critic_packages_and_create_cache: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 105 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | my $cache_file = path( $self->_cache_file ); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 5 | my $url = 'http://cpanmetadb.plackperl.org/v1.0/package/Perl::Critic'; | |||||||||||||||||||||||||||
| 110 | 5 | my $ua = HTTP::Tiny->new; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Download latest Perl::Critic meta data | ||||||||||||||||||||||||||||
| 113 | 5 | $self->log("Downloading '$url'..."); | |||||||||||||||||||||||||||
| 114 | 5 | my $res = $ua->get($url); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | ▶ | if ( !$res->{success} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 1 | $self->log_fatal( 'Unable to download latest package information for Perl::Critic: ' . $res->{reason} . ". Please ensure that your system can access '$url' or disable 'remove_core_policies' in your dist.ini" ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 4 | my $cache_header = "# This is the cache file from\n# " . ref($self) . q{ } . $self->VERSION() . <<'CACHE_HEADER'; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # You can either remove this file and add it to your .gitignore file or | ||||||||||||||||||||||||||||
| 124 | # commit it to Git. The file will be recreated or updated on demand. The | ||||||||||||||||||||||||||||
| 125 | # file is updated based on the version defined in this file. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | CACHE_HEADER | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | my $content = $res->{content}; | |||||||||||||||||||||||||||
| 130 | 4 | ▶ | $cache_file->spew( $cache_header, $content ) or $self->log_fatal("Cannot open file: $cache_file: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | return CPAN::Meta::YAML->read_string($content); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 6 | ▶ | sub _read_perl_critic_packages_cache { | ||||||||||||||||||||||||||
|
Subroutine
_read_perl_critic_packages_cache: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 136 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | my $cache_file = path( $self->_cache_file ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | ▶ | return if !-e $cache_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 2 | my $yaml = CPAN::Meta::YAML->read_string( $cache_file->slurp() ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # installed Perl::Critic is newer then our cached version | ||||||||||||||||||||||||||||
| 145 | 2 | ▶ | return if CPAN::Version->vgt( Perl::Critic->VERSION(), ${$yaml}[0]->{version} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | return $yaml; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | 1; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||