File Coverage

lib/Dist/Zilla/Role/PluginLoader/Configurable.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1 1     1   1268 use 5.010; # _Pulp__5010_qr_m_propagate_properly
  1         3  
  1         44  
2 1     1   5 use strict;
  1         2  
  1         38  
3 1     1   6 use warnings;
  1         12  
  1         32  
4 1     1   951 use utf8;
  1         9  
  1         5  
5              
6             package Dist::Zilla::Role::PluginLoader::Configurable;
7              
8             our $VERSION = '0.001001';
9              
10             # ABSTRACT: A role for plugins that load user defined and configured plugins
11              
12             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
13              
14 1     1   455 use Moose::Role qw( has around with );
  0            
  0            
15             use Dist::Zilla::Util;
16             use Dist::Zilla::Util::ConfigDumper qw( config_dumper );
17             with 'Dist::Zilla::Role::PrereqSource', 'Dist::Zilla::Role::PluginLoader';
18              
19             has dz_plugin => ( is => ro =>, required => 1 );
20              
21             has dz_plugin_name => ( is => ro =>, lazy => 1, lazy_build => 1 );
22             sub _build_dz_plugin_name { my ($self) = @_; return $self->dz_plugin; }
23              
24             has dz_plugin_minversion => ( is => ro =>, lazy => 1, lazy_build => 1 );
25             sub _build_dz_plugin_minversion { return 0 }
26              
27             has dz_plugin_arguments => ( is => ro =>, lazy => 1, lazy_build => 1 );
28             sub _build_dz_plugin_arguments { return [] }
29              
30             has prereq_to => ( is => ro =>, lazy => 1, lazy_build => 1 );
31             sub _build_prereq_to { return ['develop.requires'] }
32              
33             sub mvp_aliases {
34             return {
35             q{>} => 'dz_plugin_arguments',
36             q[dz_plugin_argument] => 'dz_plugin_arguments',
37             };
38             }
39              
40             sub mvp_multivalue_args {
41             return qw( dz_plugin_arguments prereq_to );
42             }
43              
44             sub load_plugins {
45             my ( $self, $loader ) = @_;
46             $loader->load_ini( $self->dz_plugin, $self->dz_plugin_name, $self->dz_plugin_arguments );
47             return;
48             }
49              
50             my $re_phases = qr/configure|build|test|runtime|develop/msx;
51             my $re_relation = qr/requires|recommends|suggests|conflicts/msx;
52             my $re_prereq = qr/\A($re_phases)[.]($re_relation)\z/msx;
53              
54             sub register_prereqs {
55             my ($self) = @_;
56             my $prereqs = $self->zilla->prereqs;
57              
58             my @targets;
59              
60             for my $prereq ( @{ $self->prereq_to } ) {
61             next if 'none' eq $prereq;
62             if ( my ( $phase, $relation ) = $prereq =~ $re_prereq ) {
63             push @targets, $prereqs->requirements_for( $phase, $relation );
64             }
65             }
66             for my $target (@targets) {
67             $target->add_string_requirement( Dist::Zilla::Util->expand_config_package_name( $self->dz_plugin ),
68             $self->dz_plugin_minversion );
69             }
70             return;
71             }
72              
73             no Moose::Role;
74              
75             1;
76              
77             __END__
78              
79             =pod
80              
81             =encoding UTF-8
82              
83             =head1 NAME
84              
85             Dist::Zilla::Role::PluginLoader::Configurable - A role for plugins that load user defined and configured plugins
86              
87             =head1 VERSION
88              
89             version 0.001001
90              
91             =head1 METHODS
92              
93             =head2 C<mvp_aliases>
94              
95             =over 4
96              
97             =item * C<dz_plugin_arguments=> can be written as C<< >= >> or C<< dz_plugin_argument= >>
98              
99             =back
100              
101             =head2 C<mvp_multivalue_args>
102              
103             All of the following support multiple declaration:
104              
105             =over 4
106              
107             =item * C<dz_plugin_arguments>
108              
109             =item * C<prereq_to>
110              
111             =back
112              
113             =head2 C<load_plugins>
114              
115             This is where by default the child plugin itself is loaded.
116              
117             If you want to make the loading of a child plugin conditional, wrapping
118             this method is recommended as follows:
119              
120             around load_plugins => sub {
121             my ( $orig, $self, $loader ) = @_;
122             # conditional code here
123             return if $dont_load_them;
124             return $self->$orig($loader);
125             };
126              
127             You can also do more fancy things with C<$loader>, but it is not advised.
128              
129             =head2 C<register_prereqs>
130              
131             By default, registers L</dz_plugin_package> version L</dz_plugin_minimumversion>
132             as C<develop.requires> ( as per L</prereq_to> ).
133              
134             =head1 ATTRIBUTES
135              
136             =head2 C<dz_plugin>
137              
138             B<REQUIRED>
139              
140             The C<plugin> identifier.
141              
142             For instance, C<[GatherDir / Foo]> and C<[GatherDir]> approximation would both set this field to
143              
144             dz_plugin => 'GatherDir'
145              
146             =head2 C<dz_plugin_name>
147              
148             The "Name" for the C<plugin>.
149              
150             For instance, C<[GatherDir / Foo]> would set this value as
151              
152             dz_plugin_name => "Foo"
153              
154             and C<[GatherDir]> approximation would both set this field to
155              
156             dz_plugin_name => "Foo"
157              
158             In C<Dist::Zilla>, C<[GatherDir]> is equivalent to C<[GatherDir / GatherDir]>.
159              
160             Likewise, if you do not specify C<dz_plugin_name>, the value of C<dz_plugin> will be used.
161              
162             =head2 C<dz_plugin_minversion>
163              
164             The minimum version of C<dz_plugin> to use.
165              
166             At present, this B<ONLY> affects C<prereq> generation.
167              
168             =head2 C<dz_plugin_arguments>
169              
170             A C<mvp_multivalue_arg> attribute that creates an array of arguments
171             to pass on to the created plugin.
172              
173             For convenience, this attribute has an alias of '>' ( mnemonic "Forward" ), so that the following example:
174              
175             [GatherDir]
176             include_dotfiles = 1
177             exclude_file = bad
178             exclude_file = bad2
179              
180             Would be written
181              
182             [YourPlugin]
183             dz_plugin = GatherDir
184             >= include_dotfiles = 1
185             >= exclude_file = bad
186             >= exclude_file = bad2
187              
188             Or in crazy long form
189              
190             [YourPlugin]
191             dz_plugin = GatherDir
192             dz_plugin_argument = include_dotfiles = 1
193             dz_plugin_argument = exclude_file = bad
194             dz_plugin_argument = exclude_file = bad2
195              
196             =head2 C<prereq_to>
197              
198             This determines where dependencies get injected.
199              
200             Default is:
201              
202             develop.requires
203              
204             And a special value
205              
206             none
207              
208             Prevents dependency injection.
209              
210             This attribute may be specified multiple times.
211              
212             =head1 AUTHOR
213              
214             Kent Fredric <kentfredric@gmail.com>
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2014 by Kent Fredric <kentfredric@gmail.com>.
219              
220             This is free software; you can redistribute it and/or modify it under
221             the same terms as the Perl 5 programming language system itself.
222              
223             =cut