File Coverage

lib/Dist/Zilla/Plugin/AutoPrereqs/Perl/Critic.pm
Criterion Covered Total %
statement 74 74 100.0
branch 13 14 92.8
condition 3 3 100.0
subroutine 15 15 100.0
pod 0 1 0.0
total 105 107 98.1


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::AutoPrereqs::Perl::Critic;
2              
3 4     4   304162 use 5.006;
  4         32  
4 4     4   19 use strict;
  4         8  
  4         66  
5 4     4   15 use warnings;
  4         8  
  4         180  
6              
7             our $VERSION = '0.005';
8              
9 4     4   1353 use Moose;
  4         1515542  
  4         23  
10             with qw(
11             Dist::Zilla::Role::PrereqSource
12             );
13              
14 4     4   27327 use CPAN::Meta::YAML;
  4         13252  
  4         306  
15 4     4   1373 use CPAN::Version;
  4         3420  
  4         106  
16 4     4   1683 use HTTP::Tiny;
  4         126088  
  4         168  
17 4     4   32 use Moose::Util::TypeConstraints 'enum';
  4         7  
  4         48  
18 4     4   3963 use Path::Tiny;
  4         24207  
  4         232  
19 4     4   1689 use Perl::Critic;
  4         2386304  
  4         252  
20              
21 4     4   1079 use namespace::autoclean;
  4         23095  
  4         15  
22              
23             has critic_config => (
24             is => 'ro',
25             isa => 'Maybe[Str]',
26             default => undef,
27             );
28              
29             has phase => (
30             is => 'ro',
31             isa => enum( [qw(configure build test runtime develop)] ),
32             default => 'develop',
33             );
34              
35             has remove_core_policies => (
36             is => 'ro',
37             isa => 'Bool',
38             default => 1,
39             );
40              
41             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             default => sub { shift->_build_core_policies() },
52             );
53              
54             has _cache_file => (
55             is => 'ro',
56             isa => 'Str',
57             default => '.perlcritic_package.yml',
58             );
59              
60             sub register_prereqs {
61 12     12 0 291441 my ($self) = @_;
62              
63 12         462 my $type = $self->type;
64 12         408 my $phase = $self->phase;
65 12         411 my $critic_config = $self->critic_config;
66 12         466 my $remove_core_policies = $self->remove_core_policies;
67              
68 12         25 my %critic_args;
69 12 100       39 if ( defined $critic_config ) {
70 9         32 $critic_args{-profile} = $critic_config;
71             }
72              
73 12         151 my $critic = Perl::Critic->new(%critic_args);
74              
75             POLICY:
76 12         5683533 for my $policy ( $critic->config()->policies() ) {
77 13         812 my $policy_module = ref $policy;
78 13         194 my $policy_version = $policy->VERSION();
79              
80 13 100 100     456 next POLICY if $remove_core_policies and exists $self->_core_policies->{$policy_module};
81              
82 7         228 $self->zilla->register_prereqs( { phase => $phase, type => $type }, $policy_module => $policy_version );
83             }
84              
85 11         3607 $self->zilla->register_prereqs( { phase => $phase, type => $type }, 'Perl::Critic' => Perl::Critic->VERSION() );
86              
87 11         4536 return;
88             }
89              
90             sub _build_core_policies {
91 6     6   16 my ($self) = @_;
92              
93 6         26 my $yaml = $self->_read_perl_critic_packages_cache();
94              
95 6 100       344 if ( !defined $yaml ) {
96 5         22 $yaml = $self->_get_perl_critic_packages_and_create_cache();
97             }
98              
99 5         39928 my $provides = ${$yaml}[0]->{provides};
  5         19  
100              
101 5         270 return $provides;
102             }
103              
104             sub _get_perl_critic_packages_and_create_cache {
105 5     5   17 my ($self) = @_;
106              
107 5         242 my $cache_file = path( $self->_cache_file );
108              
109 5         165 my $url = 'http://cpanmetadb.plackperl.org/v1.0/package/Perl::Critic';
110 5         59 my $ua = HTTP::Tiny->new;
111              
112             # Download latest Perl::Critic meta data
113 5         605 $self->log("Downloading '$url'...");
114 5         2307 my $res = $ua->get($url);
115              
116 5 100       149 if ( $res->{status} ne '200' ) {
117 1         8 $self->log_fatal("Unable to download latest package information for Perl::Critic. Please ensure that your system can access '$url' or disable 'remove_core_policies' in your dist.ini");
118             }
119              
120 4         50 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         19 my $content = $res->{content};
130 4 50       26 $cache_file->spew( $cache_header, $content ) or $self->log_fatal("Cannot open file: $cache_file: $!");
131              
132 4         2178 return CPAN::Meta::YAML->read_string($content);
133             }
134              
135             sub _read_perl_critic_packages_cache {
136 6     6   19 my ($self) = @_;
137              
138 6         213 my $cache_file = path( $self->_cache_file );
139              
140 6 100       289 return if !-e $cache_file;
141              
142 2         104 my $yaml = CPAN::Meta::YAML->read_string( $cache_file->slurp() );
143              
144             # installed Perl::Critic is newer then our cached version
145 2 100       20866 return if CPAN::Version->vgt( Perl::Critic->VERSION(), ${$yaml}[0]->{version} );
  2         32  
146              
147 1         100 return $yaml;
148             }
149              
150             __PACKAGE__->meta->make_immutable;
151              
152             1;
153              
154             __END__
155              
156             =pod
157              
158             =encoding UTF-8
159              
160             =head1 NAME
161              
162             Dist::Zilla::Plugin::AutoPrereqs::Perl::Critic - automatically extract Perl::Critic policy prereqs
163              
164             =head1 VERSION
165              
166             Version 0.005
167              
168             =head1 SYNOPSIS
169              
170             # in dist.ini:
171             [AutoPrereqs::Perl::Critic]
172             critic_config = .perlcriticrc ; defaults to not specify a profile
173             phase = develop ; default
174             type = requires ; default
175             remove_core_policies = 1 ; default
176              
177             =head1 DESCRIPTION
178              
179             This plugin will add L<Perl::Critic|Perl::Critic> and all policies used by it,
180             in the installed version, as distribution prerequisites.
181              
182             =head2 critic_config
183              
184             By default no policy is specified which lets L<Perl::Critic|Perl::Critic>
185             find the config itself. This defaults to F<.perlcriticrc> in the current
186             directory.
187              
188             =head2 phase
189              
190             By default, the dependencies are added to the B<develop> B<phase>. This can be
191             changed to every valid phase.
192              
193             =head2 remove_core_policies
194              
195             By default, policies that are included in the latest
196             L<Perl::Critic|Perl::Critic> distribution are not added as dependency. This
197             can be changed by setting B<remove_core_policies> to B<0>.
198              
199             Note: L<Perl::Critic|Perl::Critic> itself is always added as dependency
200             which will come with the core policies.
201              
202             Note: This feature needs HTTP access to B<cpanmetadb.plackperl.org>. Please
203             disable this feature if you're system cannot access that server.
204              
205             Note: To reduce network traffic and remove the delay caused by the network
206             access the cache file F<.perlcritic_package.yml> is generated. You can either
207             add this file to your F<.gitignore> file or add it to Git. It will be updated
208             as soon as the system runs a newer version of L<Perl::Critic|Perl::Critic>
209             then the one that is mentioned in the cache file.
210              
211             =head2 type
212              
213             By default, the dependencies are added as B<type> B<requires>. This can be changed
214             to every valid phase.
215              
216             =head1 SUPPORT
217              
218             =head2 Bugs / Feature Requests
219              
220             Please report any bugs or feature requests through the issue tracker
221             at L<https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic/issues>.
222             You will be notified automatically of any progress on your issue.
223              
224             =head2 Source Code
225              
226             This is open source software. The code repository is available for
227             public review and contribution under the terms of the license.
228              
229             L<https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic>
230              
231             git clone https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic.git
232              
233             =head1 AUTHOR
234              
235             Sven Kirmess <sven.kirmess@kzone.ch>
236              
237             =head1 COPYRIGHT AND LICENSE
238              
239             This software is Copyright (c) 2017 by Sven Kirmess.
240              
241             This is free software, licensed under:
242              
243             The (two-clause) FreeBSD License
244              
245             =head1 SEE ALSO
246              
247             L<Dist::Zilla::Plugin::AutoPrereqs|Dist::Zilla::Plugin::AutoPrereqs>,
248             L<Perl::Critic|Perl::Critic>,
249             L<Test::Perl::Critic|Test::Perl::Critic>
250              
251             =cut
252              
253             # vim: ts=4 sts=4 sw=4 et: syntax=perl