File Coverage

lib/Dist/Zilla/PluginBundle/Code.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 29 30 96.6


line stmt bran cond sub pod time code
1             # vim: ts=4 sts=4 sw=4 et: syntax=perl
2             #
3             # Copyright (c) 2020-2023 Sven Kirmess
4             #
5             # Permission to use, copy, modify, and distribute this software for any
6             # purpose with or without fee is hereby granted, provided that the above
7             # copyright notice and this permission notice appear in all copies.
8             #
9             # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10             # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11             # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12             # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13             # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14             # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15             # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16              
17 2     2   1796 use 5.006;
  2         12  
18 2     2   11 use strict;
  2         3  
  2         43  
19 2     2   18 use warnings;
  2         4  
  2         100  
20              
21             package Dist::Zilla::PluginBundle::Code;
22              
23             our $VERSION = '0.008';
24              
25 2     2   565 use Moose;
  2         445954  
  2         15  
26 2     2   13907 use namespace::autoclean;
  2         6  
  2         24  
27              
28             with 'Dist::Zilla::Role::PluginBundle';
29              
30 2     2   741 use MooseX::Types::Moose qw(CodeRef HashRef Str);
  2         55875  
  2         21  
31              
32             has bundle_config => (
33             is => 'ro',
34             isa => CodeRef,
35             reader => '_bundle_config',
36             required => 1,
37             );
38              
39             has name => (
40             is => 'ro',
41             isa => Str,
42             required => 1,
43             );
44              
45             has payload => (
46             is => 'ro',
47             isa => HashRef,
48             required => 1,
49             );
50              
51             sub bundle_config {
52 2     2 0 2766371 my ( $class, $section ) = @_;
53              
54 2         9 $section->{bundle_config} = delete $section->{payload}->{bundle_config};
55              
56 2         124 my $self = $class->new($section);
57              
58 1         52 my $code_ref = $self->_bundle_config;
59 1         5 return $self->$code_ref;
60             }
61              
62             __PACKAGE__->meta->make_immutable;
63              
64             1;
65              
66             __END__
67              
68             =pod
69              
70             =encoding UTF-8
71              
72             =head1 NAME
73              
74             Dist::Zilla::PluginBundle::Code - a dynamic bundle
75              
76             =head1 VERSION
77              
78             Version 0.008
79              
80             =head1 SYNOPSIS
81              
82             package Dist::Zilla::PluginBundle::MyBundle;
83              
84             use Moose;
85             with 'Dist::Zilla::Role::PluginBundle';
86              
87             sub bundle_config {
88             my ( $class, $section ) = @_;
89              
90             my @plugins;
91             push @plugins, [
92             'SomeUniqueName',
93             'Dist::Zilla::PluginBundle::Code',
94             {
95             bundle_config => sub {
96             my ($self) = @_;
97             print $self->payload->{some_arg}, "\n";
98              
99             # Dist::Zilla expects you to return plugins and bundles
100             # or nothing.
101             return;
102             },
103             some_arg => 'some value',
104             another_arg => 'another value',
105             },
106             ];
107              
108             return @plugins;
109             }
110              
111             =head1 DESCRIPTION
112              
113             This bundle implements the L<Dist::Zilla::Role::PluginBundle> role. It
114             can be returned from another bundle by the C<bundle_config> method.
115              
116             L<Dist::Zilla> will then run the C<bundle_config> method of this code bundle.
117             This is mainly useful if your bundle wants to return a plugin that runs
118             in the initialization phase (e.g. L<Dist::Zilla::Plugin::lib>) and run a
119             function after that plugin run.
120              
121             This, being a bundle, has no access to the C<zilla> object nor the various
122             C<log> methods. But you can access the C<name> and C<payload> attributes.
123             The C<payload> attribute is a hash ref that contains the arguments given to
124             the code bundle.
125              
126             B<Note:> Bundles that consume the L<Dist::Zilla::Role::PluginBundle::Easy>
127             role immediately resolve other bundles themselves, there is no way to push
128             a bundle back onto the stack. Therefore it doesn't make sense to use this
129             code bundle inside L<Dist::Zilla::Role::PluginBundle::Easy> bundles.
130              
131             =head1 SUPPORT
132              
133             =head2 Bugs / Feature Requests
134              
135             Please report any bugs or feature requests through the issue tracker
136             at L<https://github.com/skirmess/Dist-Zilla-Plugin-Code/issues>.
137             You will be notified automatically of any progress on your issue.
138              
139             =head2 Source Code
140              
141             This is open source software. The code repository is available for
142             public review and contribution under the terms of the license.
143              
144             L<https://github.com/skirmess/Dist-Zilla-Plugin-Code>
145              
146             git clone https://github.com/skirmess/Dist-Zilla-Plugin-Code.git
147              
148             =head1 AUTHOR
149              
150             Sven Kirmess <sven.kirmess@kzone.ch>
151              
152             =head1 SEE ALSO
153              
154             L<Dist::Zilla>, L<Dist::Zilla::Role::PluginBundle>
155              
156             =cut