File Coverage

blib/lib/Dist/Zilla/Role/PluginBundle/Easy.pm
Criterion Covered Total %
statement 31 51 60.7
branch 1 20 5.0
condition 0 2 0.0
subroutine 8 10 80.0
pod 3 4 75.0
total 43 87 49.4


line stmt bran cond sub pod time code
1             # ABSTRACT: something that bundles a bunch of plugins easily
2             # This plugin was originally contributed by Christopher J. Madsen
3              
4             use Moose::Role;
5 11     11   7596 with 'Dist::Zilla::Role::PluginBundle';
  11         10543  
  11         122  
6              
7             use Dist::Zilla::Pragmas;
8 11     11   59630  
  11         28  
  11         85  
9             use Module::Runtime 'use_module';
10 11     11   86 use namespace::autoclean;
  11         24  
  11         96  
11 11     11   688  
  11         27  
  11         90  
12             #pod =head1 SYNOPSIS
13             #pod
14             #pod package Dist::Zilla::PluginBundle::Example;
15             #pod use Moose;
16             #pod with 'Dist::Zilla::Role::PluginBundle::Easy';
17             #pod
18             #pod sub configure {
19             #pod my $self = shift;
20             #pod
21             #pod $self->add_plugins('VersionFromModule');
22             #pod $self->add_bundle('Basic');
23             #pod }
24             #pod
25             #pod =head1 DESCRIPTION
26             #pod
27             #pod This role builds upon the PluginBundle role, adding methods to take most of the
28             #pod grunt work out of creating a bundle. It supplies the C<bundle_config> method
29             #pod for you. In exchange, you must supply a C<configure> method, which will store
30             #pod the bundle's configuration in the C<plugins> attribute by calling
31             #pod C<add_plugins> and/or C<add_bundle>.
32             #pod
33             #pod =cut
34              
35             use MooseX::Types::Moose qw(Str ArrayRef HashRef);
36 11     11   1799  
  11         109895  
  11         173  
37             use String::RewritePrefix 0.005
38             rewrite => {
39 11         182 -as => '_plugin_class',
40             prefixes => {
41             '=' => '',
42             '%' => 'Dist::Zilla::Stash::',
43             '' => 'Dist::Zilla::Plugin::',
44             },
45             },
46             rewrite => {
47             -as => '_bundle_class',
48             prefixes => {
49             '' => 'Dist::Zilla::PluginBundle::',
50             '@' => 'Dist::Zilla::PluginBundle::',
51             '=' => ''
52             },
53             };
54 11     11   60474  
  11         2267  
55             requires 'configure';
56              
57             #pod =attr name
58             #pod
59             #pod This is the bundle name, taken from the Section passed to
60             #pod C<bundle_config>.
61             #pod
62             #pod =cut
63              
64             has name => (
65             is => 'ro',
66             isa => Str,
67             required => 1,
68             );
69              
70             #pod =attr payload
71             #pod
72             #pod This hashref contains the bundle's parameters (if any), taken from the
73             #pod Section passed to C<bundle_config>.
74             #pod
75             #pod =cut
76              
77             has payload => (
78             is => 'ro',
79             isa => HashRef,
80             required => 1,
81             );
82              
83             #pod =attr plugins
84             #pod
85             #pod This arrayref contains the configuration that will be returned by
86             #pod C<bundle_config>. You normally modify this by using the
87             #pod C<add_plugins> and C<add_bundle> methods.
88             #pod
89             #pod =cut
90              
91             has plugins => (
92             is => 'ro',
93             isa => ArrayRef,
94             default => sub { [] },
95             );
96              
97             my ($class, $section) = @_;
98              
99 12     12 0 3096 my $self = $class->new($section);
100              
101 12         403 $self->configure;
102              
103 12         2668 return $self->plugins->@*;
104             }
105 12         660  
106             #pod =method add_plugins
107             #pod
108             #pod $self->add_plugins('Plugin1', [ Plugin2 => \%plugin2config ])
109             #pod
110             #pod Use this method to add plugins to your bundle.
111             #pod
112             #pod It is passed a list of plugin specifiers, which can be one of a few things:
113             #pod
114             #pod =for :list
115             #pod * a plugin moniker (like you might provide in your config file)
116             #pod * an arrayref of: C<< [ $moniker, $plugin_name, \%plugin_config ] >>
117             #pod
118             #pod In the case of an arrayref, both C<$plugin_name> and C<\%plugin_config> are
119             #pod optional.
120             #pod
121             #pod The plugins are added to the config in the order given.
122             #pod
123             #pod =cut
124              
125             my ($self, @plugin_specs) = @_;
126              
127             my $prefix = $self->name . '/';
128 12     12 1 366 my $plugins = $self->plugins;
129              
130 12         399 foreach my $this_spec (@plugin_specs) {
131 12         366 my $moniker;
132             my $name;
133 12         50 my $payload;
134 185         3788  
135             if (! ref $this_spec) {
136 185         0 ($moniker, $name, $payload) = ($this_spec, $this_spec, {});
137             } elsif (@$this_spec == 1) {
138 185 50       343 ($moniker, $name, $payload) = ($this_spec->[0], $this_spec->[0], {});
    0          
    0          
139 185         377 } elsif (@$this_spec == 2) {
140             $moniker = $this_spec->[0];
141 0         0 $name = ref $this_spec->[1] ? $moniker : $this_spec->[1];
142             $payload = ref $this_spec->[1] ? $this_spec->[1] : {};
143 0         0 } else {
144 0 0       0 ($moniker, $name, $payload) = @$this_spec;
145 0 0       0 }
146              
147 0         0 push @$plugins, [ $prefix . $name => _plugin_class($moniker) => $payload ];
148             }
149             }
150 185         504  
151             #pod =method add_bundle
152             #pod
153             #pod $self->add_bundle(BundleName => \%bundle_config)
154             #pod
155             #pod Use this method to add all the plugins from another bundle to your bundle. If
156             #pod you omit C<%bundle_config>, an empty hashref will be supplied.
157             #pod
158             #pod =cut
159              
160             my ($self, $bundle, $payload) = @_;
161              
162             my $package = _bundle_class($bundle);
163             $payload ||= {};
164 0     0 1    
165             &use_module(
166 0           $package,
167 0   0       $payload->{':version'} ? $payload->{':version'} : (),
168             );
169              
170             $bundle = "\@$bundle" unless $bundle =~ /^@/;
171 0 0          
172             push $self->plugins->@*,
173             $package->bundle_config({
174 0 0         name => $self->name . '/' . $bundle,
175             package => $package,
176 0           payload => $payload,
177             });
178             }
179              
180             #pod =method config_slice
181             #pod
182             #pod $hash_ref = $self->config_slice(arg1, { arg2 => 'plugin_arg2' })
183             #pod
184             #pod Use this method to extract parameters from your bundle's C<payload> so
185             #pod that you can pass them to a plugin or subsidiary bundle. It supports
186             #pod easy renaming of parameters, since a plugin may expect a parameter
187             #pod name that's too generic to be suitable for a bundle.
188             #pod
189             #pod Each arg is either a key in C<payload>, or a hashref that maps keys in
190             #pod C<payload> to keys in the hash being constructed. If any specified
191             #pod key does not exist in C<payload>, then it is omitted from the result.
192             #pod
193             #pod =cut
194              
195             my $self = shift;
196              
197             my $payload = $self->payload;
198              
199             my %arg;
200 0     0 1    
201             foreach my $arg (@_) {
202 0           if (ref $arg) {
203             while (my ($in, $out) = each %$arg) {
204 0           $arg{$out} = $payload->{$in} if exists $payload->{$in};
205             }
206 0           } else {
207 0 0         $arg{$arg} = $payload->{$arg} if exists $payload->{$arg};
208 0           }
209 0 0         }
210              
211             return \%arg;
212 0 0         }
213              
214             1;
215              
216 0            
217             =pod
218              
219             =encoding UTF-8
220              
221             =head1 NAME
222              
223             Dist::Zilla::Role::PluginBundle::Easy - something that bundles a bunch of plugins easily
224              
225             =head1 VERSION
226              
227             version 6.028
228              
229             =head1 SYNOPSIS
230              
231             package Dist::Zilla::PluginBundle::Example;
232             use Moose;
233             with 'Dist::Zilla::Role::PluginBundle::Easy';
234              
235             sub configure {
236             my $self = shift;
237              
238             $self->add_plugins('VersionFromModule');
239             $self->add_bundle('Basic');
240             }
241              
242             =head1 DESCRIPTION
243              
244             This role builds upon the PluginBundle role, adding methods to take most of the
245             grunt work out of creating a bundle. It supplies the C<bundle_config> method
246             for you. In exchange, you must supply a C<configure> method, which will store
247             the bundle's configuration in the C<plugins> attribute by calling
248             C<add_plugins> and/or C<add_bundle>.
249              
250             =head1 PERL VERSION
251              
252             This module should work on any version of perl still receiving updates from
253             the Perl 5 Porters. This means it should work on any version of perl released
254             in the last two to three years. (That is, if the most recently released
255             version is v5.40, then this module should work on both v5.40 and v5.38.)
256              
257             Although it may work on older versions of perl, no guarantee is made that the
258             minimum required version will not be increased. The version may be increased
259             for any reason, and there is no promise that patches will be accepted to lower
260             the minimum required perl.
261              
262             =head1 ATTRIBUTES
263              
264             =head2 name
265              
266             This is the bundle name, taken from the Section passed to
267             C<bundle_config>.
268              
269             =head2 payload
270              
271             This hashref contains the bundle's parameters (if any), taken from the
272             Section passed to C<bundle_config>.
273              
274             =head2 plugins
275              
276             This arrayref contains the configuration that will be returned by
277             C<bundle_config>. You normally modify this by using the
278             C<add_plugins> and C<add_bundle> methods.
279              
280             =head1 METHODS
281              
282             =head2 add_plugins
283              
284             $self->add_plugins('Plugin1', [ Plugin2 => \%plugin2config ])
285              
286             Use this method to add plugins to your bundle.
287              
288             It is passed a list of plugin specifiers, which can be one of a few things:
289              
290             =over 4
291              
292             =item *
293              
294             a plugin moniker (like you might provide in your config file)
295              
296             =item *
297              
298             an arrayref of: C<< [ $moniker, $plugin_name, \%plugin_config ] >>
299              
300             =back
301              
302             In the case of an arrayref, both C<$plugin_name> and C<\%plugin_config> are
303             optional.
304              
305             The plugins are added to the config in the order given.
306              
307             =head2 add_bundle
308              
309             $self->add_bundle(BundleName => \%bundle_config)
310              
311             Use this method to add all the plugins from another bundle to your bundle. If
312             you omit C<%bundle_config>, an empty hashref will be supplied.
313              
314             =head2 config_slice
315              
316             $hash_ref = $self->config_slice(arg1, { arg2 => 'plugin_arg2' })
317              
318             Use this method to extract parameters from your bundle's C<payload> so
319             that you can pass them to a plugin or subsidiary bundle. It supports
320             easy renaming of parameters, since a plugin may expect a parameter
321             name that's too generic to be suitable for a bundle.
322              
323             Each arg is either a key in C<payload>, or a hashref that maps keys in
324             C<payload> to keys in the hash being constructed. If any specified
325             key does not exist in C<payload>, then it is omitted from the result.
326              
327             =head1 AUTHOR
328              
329             Ricardo SIGNES 😏 <cpan@semiotic.systems>
330              
331             =head1 COPYRIGHT AND LICENSE
332              
333             This software is copyright (c) 2022 by Ricardo SIGNES.
334              
335             This is free software; you can redistribute it and/or modify it under
336             the same terms as the Perl 5 programming language system itself.
337              
338             =cut