File Coverage

blib/lib/Dist/Zilla/Plugin/if/not.pm
Criterion Covered Total %
statement 32 32 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1 6     6   11144104 use 5.008; # utf8
  6         18  
2 6     6   30 use strict;
  6         8  
  6         132  
3 6     6   36 use warnings;
  6         8  
  6         202  
4 6     6   522 use utf8;
  6         14  
  6         58  
5              
6             package Dist::Zilla::Plugin::if::not;
7              
8             our $VERSION = '0.002002';
9              
10             # ABSTRACT: Only load a plugin if a condition is false
11              
12             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13              
14 6     6   870 use Moose qw( has around with );
  6         293119  
  6         57  
15 6     6   25652 use Dist::Zilla::Util qw();
  6         9115  
  6         113  
16 6     6   25 use Eval::Closure qw( eval_closure );
  6         7  
  6         2536  
17              
18             with 'Dist::Zilla::Role::PluginLoader::Configurable';
19              
20             has conditions => ( is => 'ro', lazy_build => 1 );
21 2     2   60 sub _build_conditions { return [] }
22              
23             around dump_config => sub {
24             my ( $orig, $self, @args ) = @_;
25             my $config = $self->$orig(@args);
26             my $localconf = $config->{ +__PACKAGE__ } = {};
27              
28             $localconf->{conditions} = $self->conditions;
29              
30             $localconf->{ q[$] . __PACKAGE__ . '::VERSION' } = $VERSION
31             unless __PACKAGE__ eq ref $self;
32              
33             return $config;
34             };
35              
36             around mvp_aliases => sub {
37             my ( $orig, $self, @rest ) = @_;
38             my $hash = $self->$orig(@rest);
39             $hash = {
40             %{$hash},
41             q{?} => 'conditions',
42             q[condition] => 'conditions',
43             };
44             return $hash;
45             };
46             around mvp_multivalue_args => sub {
47             my ( $orig, $self, @args ) = @_;
48             return ( qw( conditions ), $self->$orig(@args) );
49             };
50              
51              
52              
53              
54              
55              
56              
57              
58             sub check_conditions {
59 7     7 1 11 my ($self) = @_;
60              
61 7         12 my $env = {};
62             ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
63 7         222 $env->{q[$root]} = \$self->zilla->root;
64 7         468 $env->{q[$zilla]} = \$self->zilla;
65 7         31 my $code = join q[ and ], @{ $self->conditions }, q[1];
  7         204  
66 7         40 my $closure = eval_closure(
67             source => qq[sub { \n] . $code . qq[ }\n],
68             environment => $env,
69             );
70             ## use critic;
71 7         1710 return !$closure->();
72             }
73              
74             around 'load_plugins' => sub {
75             my ( $orig, $self, $loader ) = @_;
76             return unless $self->check_conditions;
77             return $self->$orig($loader);
78             };
79              
80 6     6   31 no Moose;
  6         8  
  6         33  
81              
82             __PACKAGE__->meta->make_immutable;
83              
84             1;
85              
86             __END__
87              
88             =pod
89              
90             =encoding UTF-8
91              
92             =head1 NAME
93              
94             Dist::Zilla::Plugin::if::not - Only load a plugin if a condition is false
95              
96             =head1 VERSION
97              
98             version 0.002002
99              
100             =head1 METHODS
101              
102             =head2 C<check_conditions>
103              
104             This is identical to L<< C<if>|Dist::Zilla::Plugin::if >> except this condition
105             returns inverted.
106              
107             =head1 AUTHOR
108              
109             Kent Fredric <kentnl@cpan.org>
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             This software is copyright (c) 2017 by Kent Fredric <kentfredric@gmail.com>.
114              
115             This is free software; you can redistribute it and/or modify it under
116             the same terms as the Perl 5 programming language system itself.
117              
118             =cut