File Coverage

blib/lib/Dist/Zilla/Plugin/AutoPrereqs/Perl/Critic.pm
Criterion Covered Total %
statement 51 51 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod 0 1 0.0
total 70 71 98.5


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::AutoPrereqs::Perl::Critic;
2              
3 3     3   349581 use strict;
  3         30  
  3         104  
4 3     3   22 use warnings;
  3         8  
  3         132  
5              
6             our $VERSION = '0.004';
7              
8 3     3   1120 use Moose;
  3         1528675  
  3         31  
9             with qw(
10             Dist::Zilla::Role::PrereqSource
11             );
12              
13 3     3   36301 use CPAN::Meta::YAML;
  3         22552  
  3         380  
14 3     3   1947 use HTTP::Tiny;
  3         147134  
  3         171  
15 3     3   34 use Moose::Util::TypeConstraints 'enum';
  3         10  
  3         45  
16 3     3   3533 use Perl::Critic;
  3         2811040  
  3         194  
17              
18 3     3   1035 use namespace::autoclean;
  3         24792  
  3         20  
19              
20             has critic_config => (
21             is => 'ro',
22             isa => 'Maybe[Str]',
23             default => undef,
24             );
25              
26             has phase => (
27             is => 'ro',
28             isa => enum( [qw(configure build test runtime develop)] ),
29             default => 'develop',
30             );
31              
32             has remove_core_policies => (
33             is => 'ro',
34             isa => 'Bool',
35             default => 1,
36             );
37              
38             has type => (
39             is => 'ro',
40             isa => enum( [qw(requires recommends suggests conflicts)] ),
41             default => 'requires',
42             );
43              
44             has _core_policies => (
45             is => 'ro',
46             isa => 'HashRef',
47             lazy => 1,
48             default => sub { shift->_build_core_policies() },
49             );
50              
51             sub register_prereqs {
52 9     9 0 305036 my ($self) = @_;
53              
54 9         475 my $type = $self->type;
55 9         403 my $phase = $self->phase;
56 9         417 my $critic_config = $self->critic_config;
57 9         452 my $remove_core_policies = $self->remove_core_policies;
58              
59 9         31 my %critic_args = ();
60 9 100       45 if ( defined $critic_config ) {
61 6         29 $critic_args{-profile} = $critic_config;
62             }
63              
64 9         154 my $critic = Perl::Critic->new(%critic_args);
65              
66             POLICY:
67 9         6789770 for my $policy ( $critic->config()->policies() ) {
68 7         1076 my $policy_module = ref $policy;
69 7         146 my $policy_version = $policy->VERSION();
70              
71 7 100 100     327 next POLICY if $remove_core_policies and exists $self->_core_policies->{$policy_module};
72              
73 4         216 $self->zilla->register_prereqs( { phase => $phase, type => $type }, $policy_module => $policy_version );
74             }
75              
76 8         2551 $self->zilla->register_prereqs( { phase => $phase, type => $type }, 'Perl::Critic' => Perl::Critic->VERSION() );
77              
78 8         5628 return;
79             }
80              
81             sub _build_core_policies {
82 3     3   12 my ($self) = @_;
83              
84 3         15 my $url = 'http://cpanmetadb.plackperl.org/v1.0/package/Perl::Critic';
85 3         52 my $ua = HTTP::Tiny->new;
86              
87             # Download latest Perl::Critic meta data
88 3         490 $self->log("Downloading '$url'...");
89 3         2708 my $res = $ua->get($url);
90              
91 3 100       139 if ( $res->{status} ne '200' ) {
92 1         14 $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");
93             }
94              
95 2         40 my $yaml = CPAN::Meta::YAML->read_string( $res->{content} );
96 2         36829 my $provides = ${$yaml}[0]->{provides};
  2         12  
97              
98 2         274 return $provides;
99             }
100              
101             __PACKAGE__->meta->make_immutable;
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Dist::Zilla::Plugin::AutoPrereqs::Perl::Critic - automatically extract Perl::Critic policy prereqs
114              
115             =head1 VERSION
116              
117             Version 0.004
118              
119             =head1 SYNOPSIS
120              
121             # in dist.ini:
122             [AutoPrereqs::Perl::Critic]
123             critic_config = .perlcriticrc ; defaults to not specify a profile
124             phase = develop ; default
125             type = requires ; default
126             remove_core_policies = 1 ; default
127              
128             =head1 DESCRIPTION
129              
130             This plugin will add L<Perl::Critic|Perl::Critic> and all policies used by it,
131             in the installed version, as distribution prerequisites.
132              
133             =head2 critic_config
134              
135             By default no policy is specified which lets L<Perl::Critic|Perl::Critic>
136             find the config itself. This defaults to F<.perlcriticrc> in the current
137             directory.
138              
139             =head2 phase
140              
141             By default, the dependencies are added to the B<develop> B<phase>. This can be
142             changed to every valid phase.
143              
144             =head2 remove_core_policies
145              
146             By default, policies that are included in the latest
147             L<Perl::Critic|Perl::Critic> distribution are not added as dependency. This
148             can be changed by setting B<remove_core_policies> to B<0>.
149              
150             Note: L<Perl::Critic|Perl::Critic> itself is always added as dependency
151             which will come with the core policies.
152              
153             Note: This feature needs HTTP access to B<cpanmetadb.plackperl.org>. Please
154             disable this feature if you're system cannot access that server.
155              
156             =head2 type
157              
158             By default, the dependencies are added as B<type> B<requires>. This can be changed
159             to every valid phase.
160              
161             =head1 SUPPORT
162              
163             =head2 Bugs / Feature Requests
164              
165             Please report any bugs or feature requests through the issue tracker
166             at L<https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic/issues>.
167             You will be notified automatically of any progress on your issue.
168              
169             =head2 Source Code
170              
171             This is open source software. The code repository is available for
172             public review and contribution under the terms of the license.
173              
174             L<https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic>
175              
176             git clone https://github.com/skirmess/Dist-Zilla-Plugin-AutoPrereqs-Perl-Critic.git
177              
178             =head1 AUTHOR
179              
180             Sven Kirmess <sven.kirmess@kzone.ch>
181              
182             =head1 COPYRIGHT AND LICENSE
183              
184             This software is Copyright (c) 2017 by Sven Kirmess.
185              
186             This is free software, licensed under:
187              
188             The (two-clause) FreeBSD License
189              
190             =head1 SEE ALSO
191              
192             L<Dist::Zilla::Plugin::AutoPrereqs|Dist::Zilla::Plugin::AutoPrereqs>,
193             L<Perl::Critic|Perl::Critic>,
194             L<Test::Perl::Critic|Test::Perl::Critic>,
195              
196             =cut
197              
198             # vim: ts=4 sts=4 sw=4 et: syntax=perl