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