File Coverage

blib/lib/Dist/Zilla/PluginBundle/Filter.pm
Criterion Covered Total %
statement 43 44 97.7
branch 8 14 57.1
condition 3 8 37.5
subroutine 10 10 100.0
pod 0 2 0.0
total 64 78 82.0


line stmt bran cond sub pod time code
1             # ABSTRACT: use another bundle, with some plugins removed
2              
3             use Moose;
4 3     3   3156 with 'Dist::Zilla::Role::PluginBundle';
  3         6  
  3         44  
5              
6             use Dist::Zilla::Pragmas;
7 3     3   23892  
  3         13  
  3         54  
8             use namespace::autoclean;
9 3     3   29  
  3         7  
  3         39  
10             use List::Util 1.33 qw(any);
11 3     3   452 use Module::Runtime qw(use_module);
  3         106  
  3         367  
12 3     3   26 use Dist::Zilla::Util;
  3         8  
  3         34  
13 3     3   704  
  3         9  
  3         2056  
14             #pod =head1 SYNOPSIS
15             #pod
16             #pod In your F<dist.ini>:
17             #pod
18             #pod [@Filter]
19             #pod -bundle = @Basic
20             #pod -version = 5.031
21             #pod -remove = ShareDir
22             #pod -remove = UploadToCPAN
23             #pod option = for_basic
24             #pod
25             #pod =head1 DESCRIPTION
26             #pod
27             #pod This plugin bundle actually wraps and modifies another plugin bundle. It
28             #pod includes all the configuration for the bundle named in the C<-bundle> attribute,
29             #pod but removes all the entries whose package is given in the C<-remove> attributes.
30             #pod
31             #pod A minimum required version of the bundle can be specified with the C<-version>
32             #pod attribute.
33             #pod
34             #pod Options not prefixed with C<-> will be passed to the bundle to be filtered.
35             #pod
36             #pod B<NOTE:> When you filter a bundle you B<SHOULD NOT> include it directly in
37             #pod your C<dist.ini> file. This plugin will take care of including it for you.
38             #pod
39             #pod =head1 SEE ALSO
40             #pod
41             #pod Core Dist::Zilla plugins: L<@Basic|Dist::Zilla::PluginBundle::Basic>.
42             #pod
43             #pod Dist::Zilla roles: L<PluginBundle|Dist::Zilla::Role::PluginBundle>.
44             #pod
45             #pod =cut
46              
47              
48 1     1 0 230 my ($self, $section) = @_;
49             my $class = (ref $self) || $self;
50              
51 2     2 0 457 my $config = {};
52 2   33     14  
53             my $has_filter_args = any { /^-/ } keys %{ $section->{payload} };
54 2         10 for my $key (keys %{ $section->{payload} }) {
55             my $val = $section->{payload}->{$key};
56 2     4   11 my $target = $has_filter_args && ($key !~ /^-/)
  4         17  
  2         22  
57 2         9 ? 'bundle'
  2         8  
58 4         11 : 'filter';
59 4 50 33     31 $key =~ s/^-// if $target eq 'filter';
60             $config->{$target}->{$key} = $val;
61             }
62 4 50       19  
63 4         14 Carp::croak("no bundle given for bundle filter")
64             unless my $bundle = $config->{filter}->{bundle};
65              
66             my $pkg = Dist::Zilla::Util->expand_config_package_name($bundle);
67 2 50       15  
68             my $version = $config->{filter}->{version};
69 2         17  
70             unless (eval { &use_module($pkg, $version ? $version : ()); 1 }) {
71 2         67 # XXX Naughty! -- rjbs, 2013-07-23
72             Config::MVP::Section->missing_package($pkg, $bundle);
73 2 50       5 }
  2 50       23  
  2         72  
74              
75 0         0 my @plugins = $pkg->bundle_config({
76             name => $section->{name}, # not 100% sure about this -- rjbs, 2010-03-06
77             package => $pkg,
78             payload => $config->{bundle} || {},
79             });
80              
81             return @plugins unless my $remove = $config->{filter}->{remove};
82 2   50     34  
83             for my $i (reverse 0 .. $#plugins) {
84 2 50       16 splice @plugins, $i, 1 if any(sub {
85             $plugins[$i][1] eq Dist::Zilla::Util->expand_config_package_name($_)
86 2         10 }, @$remove);
87             }
88 50     50   479  
89 34 100       891 return @plugins;
90             }
91              
92 2         68 __PACKAGE__->meta->make_immutable;
93             1;
94              
95              
96             =pod
97              
98             =encoding UTF-8
99              
100             =head1 NAME
101              
102             Dist::Zilla::PluginBundle::Filter - use another bundle, with some plugins removed
103              
104             =head1 VERSION
105              
106             version 6.028
107              
108             =head1 SYNOPSIS
109              
110             In your F<dist.ini>:
111              
112             [@Filter]
113             -bundle = @Basic
114             -version = 5.031
115             -remove = ShareDir
116             -remove = UploadToCPAN
117             option = for_basic
118              
119             =head1 DESCRIPTION
120              
121             This plugin bundle actually wraps and modifies another plugin bundle. It
122             includes all the configuration for the bundle named in the C<-bundle> attribute,
123             but removes all the entries whose package is given in the C<-remove> attributes.
124              
125             A minimum required version of the bundle can be specified with the C<-version>
126             attribute.
127              
128             Options not prefixed with C<-> will be passed to the bundle to be filtered.
129              
130             B<NOTE:> When you filter a bundle you B<SHOULD NOT> include it directly in
131             your C<dist.ini> file. This plugin will take care of including it for you.
132              
133             =head1 PERL VERSION
134              
135             This module should work on any version of perl still receiving updates from
136             the Perl 5 Porters. This means it should work on any version of perl released
137             in the last two to three years. (That is, if the most recently released
138             version is v5.40, then this module should work on both v5.40 and v5.38.)
139              
140             Although it may work on older versions of perl, no guarantee is made that the
141             minimum required version will not be increased. The version may be increased
142             for any reason, and there is no promise that patches will be accepted to lower
143             the minimum required perl.
144              
145             =head1 SEE ALSO
146              
147             Core Dist::Zilla plugins: L<@Basic|Dist::Zilla::PluginBundle::Basic>.
148              
149             Dist::Zilla roles: L<PluginBundle|Dist::Zilla::Role::PluginBundle>.
150              
151             =head1 AUTHOR
152              
153             Ricardo SIGNES 😏 <cpan@semiotic.systems>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2022 by Ricardo SIGNES.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut