File Coverage

blib/lib/Dist/Zilla/Role/PluginLoader/Configurable.pm
Criterion Covered Total %
statement 36 36 100.0
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 53 54 98.1


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