File Coverage

lib/Dist/Zilla/Util/BundleInfo.pm
Criterion Covered Total %
statement 18 91 19.7
branch 0 20 0.0
condition n/a
subroutine 6 17 35.2
pod 1 1 100.0
total 25 129 19.3


line stmt bran cond sub pod time code
1 3     3   34371 use 5.008; # pragma utf8
  3         12  
  3         125  
2 3     3   15 use strict;
  3         6  
  3         95  
3 3     3   13 use warnings;
  3         5  
  3         83  
4 3     3   1725 use utf8;
  3         25  
  3         13  
5              
6             package Dist::Zilla::Util::BundleInfo;
7              
8             our $VERSION = '1.001003';
9              
10             # ABSTRACT: Load and interpret a bundle
11              
12             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13              
14 3     3   1759 use Moo 1.000008 qw( has );
  3         39306  
  3         14  
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32             sub _coerce_bundle_name {
33 0     0     my ($name) = @_;
34 0           require Dist::Zilla::Util;
35 0           return Dist::Zilla::Util->expand_config_package_name($name);
36             }
37              
38              
39              
40              
41              
42              
43              
44             sub _isa_bundle {
45 0     0     my ($name) = @_;
46 0           require Module::Runtime;
47 0           Module::Runtime::require_module($name);
48 0 0         if ( not $name->can('bundle_config') ) {
49 0           require Carp;
50 0           Carp::croak("$name is not a bundle, as it does not have a bundle_config method");
51             }
52             }
53              
54              
55              
56              
57              
58              
59              
60              
61              
62              
63             has bundle_name => (
64             is => ro =>,
65             required => 1,
66             coerce => sub { _coerce_bundle_name( $_[0] ) },
67             isa => sub { _isa_bundle( $_[0] ) },
68             );
69              
70              
71              
72              
73              
74              
75              
76              
77              
78              
79              
80             has bundle_dz_name => (
81             is => ro =>,
82             lazy => 1,
83             builder => sub {
84 0     0     return $_[0]->bundle_name;
85             },
86             );
87              
88              
89              
90              
91              
92              
93              
94              
95              
96              
97              
98              
99              
100              
101              
102              
103              
104              
105              
106              
107              
108              
109              
110              
111              
112              
113              
114              
115              
116              
117              
118              
119              
120             has bundle_payload => (
121             is => ro =>,
122             lazy => 1,
123             builder => sub {
124 0     0     [];
125             },
126             );
127              
128             has _loaded_module => (
129             is => ro =>,
130             lazy => 1,
131             builder => sub {
132 0     0     require Module::Runtime;
133 0           Module::Runtime::require_module( $_[0]->bundle_name );
134 0           return $_[0]->bundle_name;
135             },
136             );
137              
138             has _mvp_alias_rmap => (
139             is => ro =>,
140             lazy => 1,
141             builder => sub {
142 0     0     my ($self) = @_;
143 0 0         return {} unless $self->_loaded_module->can('mvp_aliases');
144 0           my $rmap = {};
145 0           my $fmap = $self->_loaded_module->mvp_aliases;
146 0           for my $key ( keys %{$fmap} ) {
  0            
147 0           my $value = $fmap->{$key};
148 0 0         $rmap->{$value} = [] if not exists $rmap->{$value};
149 0           push @{ $rmap->{$value} }, $key;
  0            
150             }
151 0           return $rmap;
152             },
153             );
154              
155             sub _mvp_alias_for {
156 0     0     my ( $self, $alias ) = @_;
157 0 0         return unless exists $self->_mvp_alias_rmap->{$alias};
158 0           return @{ $self->_mvp_alias_rmap->{$alias} };
  0            
159             }
160             has _mvp_multivalue_args => (
161             is => ro =>,
162             lazy => 1,
163             builder => sub {
164 0 0   0     return {} unless $_[0]->_loaded_module->can('mvp_multivalue_args');
165 0           my $map = {};
166 0           for my $arg ( $_[0]->_loaded_module->mvp_multivalue_args ) {
167 0           $map->{$arg} = 1;
168 0           for my $alias ( $_[0]->_mvp_alias_for($arg) ) {
169 0           $map->{$alias} = 1;
170             }
171             }
172 0           return $map;
173             },
174             );
175              
176             sub _property_is_mvp_multi {
177 0     0     my ( $self, $property ) = @_;
178 0           return exists $self->_mvp_multivalue_args->{$property};
179             }
180              
181             sub _array_to_hash {
182 0     0     my ( $self, @orig_payload ) = @_;
183 0           my $payload = {};
184 0           my ( $key_i, $value_i ) = ( 0, 1 );
185 0           while ( $value_i <= $#orig_payload ) {
186 0           my ($key) = $orig_payload[$key_i];
187 0           my ($value) = $orig_payload[$value_i];
188 0 0         if ( $self->_property_is_mvp_multi($key) ) {
189 0 0         $payload->{$key} = [] if not exists $payload->{$key};
190 0           push @{ $payload->{$key} }, $value;
  0            
191 0           next;
192             }
193 0 0         if ( exists $payload->{$key} ) {
194 0           require Carp;
195 0           Carp::carp( "Multiple specification of non-multivalue key $key for bundle" . $self->bundle_name );
196 0 0         if ( not ref $payload->{$key} ) {
197 0           $payload->{$key} = [ $payload->{$key} ];
198             }
199 0           push @{ $payload->{$key} }, $value;
  0            
200 0           next;
201             }
202 0           $payload->{$key} = $value;
203             }
204             continue {
205 0           $key_i += 2;
206 0           $value_i += 2;
207             }
208 0           return $payload;
209             }
210              
211              
212              
213              
214              
215              
216              
217              
218             sub plugins {
219 0     0 1   my ( $self, ) = @_;
220 0           my $payload = $self->bundle_payload;
221 0           my $bundle = $self->bundle_name;
222 0           my $bundle_dz_name = $self->bundle_dz_name;
223 0           require Dist::Zilla::Util::BundleInfo::Plugin;
224 0           my @out;
225 0 0         if ( 'ARRAY' eq ref $payload ) {
226 0           $payload = $self->_array_to_hash( @{$payload} );
  0            
227             }
228 0           for my $plugin ( $bundle->bundle_config( { name => $bundle_dz_name, payload => $payload } ) ) {
229 0           push @out, Dist::Zilla::Util::BundleInfo::Plugin->inflate_bundle_entry($plugin);
230             }
231 0           return @out;
232             }
233              
234 3     3   6414 no Moo;
  3         5  
  3         13  
235              
236             1;
237              
238             __END__
239              
240             =pod
241              
242             =encoding UTF-8
243              
244             =head1 NAME
245              
246             Dist::Zilla::Util::BundleInfo - Load and interpret a bundle
247              
248             =head1 VERSION
249              
250             version 1.001003
251              
252             =head1 SYNOPSIS
253              
254             use Dist::Zilla::Util::BundleInfo;
255              
256             # [@RJBS]
257             # -myparam = foo
258             # param = bar
259             # param = quux
260             #
261             my $info = Dist::Zilla::Util::BundleInfo->new(
262             bundle_name => '@RJBS',
263             bundle_payload => [
264             '-myparam' => 'foo',
265             'param' => 'bar',
266             'param' => 'quux'
267             ]
268             );
269             for my $plugin ( $info->plugins ) {
270             print $plugin->to_dist_ini; # emit each plugin in order in dist.ini format.
271             }
272              
273             =head1 METHODS
274              
275             =head2 C<plugins>
276              
277             Returns a list of L<< C<::BundleInfo::Plugin>|Dist::Zilla::Util::BundleInfo::Plugin >> instances
278             representing the configuration data for each section returned by the bundle.
279              
280             =head1 ATTRIBUTES
281              
282             =head2 C<bundle_name>
283              
284             The name of the bundle to get info from
285              
286             ->new( bundle_name => '@RJBS' )
287             ->new( bundle_name => 'Dist::Zilla::PluginBundle::RJBS' )
288              
289             =head2 C<bundle_dz_name>
290              
291             The name to pass to the bundle in the C<name> parameter.
292              
293             This is synonymous to the value of C<Foo> in
294              
295             [@Bundle / Foo]
296              
297             =head2 C<bundle_payload>
298              
299             The parameter list to pass to the bundle.
300              
301             This is synonymous with the properties passed in C<dist.ini>
302              
303             {
304             foo => 'bar',
305             quux => 'do',
306             multivalue => [ 'a' , 'b', 'c' ]
307             }
308              
309             C<==>
310              
311             [
312             'foo' => 'bar',
313             'quux' => 'do',
314             'multivalue' => 'a',
315             'multivalue' => 'b',
316             'multivalue' => 'c',
317             ]
318              
319             C<==>
320              
321             foo = bar
322             quux = do
323             multivalue = a
324             multivalue = b
325             multivalue = c
326              
327             =head1 PRIVATE FUNCTIONS
328              
329             =head2 C<_coerce_bundle_name>
330              
331             _coerce_bundle_name('@Foo') # Dist::Zilla::PluginBundle::Foo
332              
333             =head2 C<_isa_bundle>
334              
335             _isa_bundle('Foo::Bar::Baz') # fatals if Foo::Bar::Baz can't do ->bundle_config
336              
337             =begin MetaPOD::JSON v1.1.0
338              
339             {
340             "namespace":"Dist::Zilla::Util::BundleInfo",
341             "interface":"class",
342             "inherits":"Moo::Object"
343             }
344              
345              
346             =end MetaPOD::JSON
347              
348             =head1 AUTHOR
349              
350             Kent Fredric <kentfredric@gmail.com>
351              
352             =head1 COPYRIGHT AND LICENSE
353              
354             This software is copyright (c) 2014 by Kent Fredric <kentfredric@gmail.com>.
355              
356             This is free software; you can redistribute it and/or modify it under
357             the same terms as the Perl 5 programming language system itself.
358              
359             =cut