File Coverage

lib/Dist/Zilla/Plugin/Code/Initialization.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


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   1741 use 5.006;
  2         10  
18 2     2   11 use strict;
  2         5  
  2         40  
19 2     2   8 use warnings;
  2         4  
  2         152  
20              
21             package Dist::Zilla::Plugin::Code::Initialization;
22              
23             our $VERSION = '0.008';
24              
25 2     2   551 use Moose;
  2         448706  
  2         31  
26 2     2   14432 use namespace::autoclean;
  2         5  
  2         15  
27              
28             with 'Dist::Zilla::Role::Plugin';
29              
30 2     2   953 use Config::MVP 2.200012 (); # https://github.com/rjbs/Config-MVP/issues/13
  2         200  
  2         55  
31 2     2   458 use MooseX::Types::Moose qw(CodeRef);
  2         56449  
  2         14  
32              
33             has initialization => (
34             is => 'ro',
35             isa => 'CodeRef',
36             reader => '_initialization',
37             required => 1,
38             );
39              
40             around plugin_from_config => sub {
41             my $orig = shift @_;
42             my $plugin_class = shift @_;
43              
44             my $instance = $plugin_class->$orig(@_);
45              
46             my $code_ref = $instance->_initialization;
47             $instance->$code_ref(@_);
48              
49             return $instance;
50             };
51              
52             __PACKAGE__->meta->make_immutable;
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Dist::Zilla::Plugin::Code::Initialization - something that runs when plugins are initialized
65              
66             =head1 VERSION
67              
68             Version 0.008
69              
70             =head1 SYNOPSIS
71              
72             =head2 Dist::Zilla::Role::PluginBundle
73              
74             package Dist::Zilla::PluginBundle::MyBundle;
75              
76             use Moose;
77             with 'Dist::Zilla::Role::PluginBundle';
78              
79             sub bundle_config {
80             my ( $class, $section ) = @_;
81              
82             my @plugins;
83             push @plugins, [
84             'SomeUniqueName',
85             'Dist::Zilla::Plugin::Code::Initialization',
86             {
87             initialization => sub {
88             my ($self) = @_;
89             $self->log('Hello world');
90             },
91             },
92             ];
93              
94             return @plugins;
95             }
96              
97             =head2 Dist::Zilla::Role::PluginBundle::Easy
98              
99             package Dist::Zilla::PluginBundle::MyBundle;
100              
101             use Moose;
102             with 'Dist::Zilla::Role::PluginBundle::Easy';
103              
104             sub configure {
105             my ( $self ) = @_;
106              
107             $self->add_plugins([
108             'Code::Initialization',
109             {
110             initialization => sub {
111             my ($self) = @_;
112             $self->log('Hello world');
113             },
114             },
115             ]);
116              
117             return;
118             }
119              
120             =head1 DESCRIPTION
121              
122             This plugin implements the L<Dist::Zilla::Role::Plugin> role and attaches
123             to the C<plugin_from_config> method, that is fired when the plugins and
124             bundles get initialized. (This plugin runs at the same time as bundles.)
125              
126             =head1 SUPPORT
127              
128             =head2 Bugs / Feature Requests
129              
130             Please report any bugs or feature requests through the issue tracker
131             at L<https://github.com/skirmess/Dist-Zilla-Plugin-Code/issues>.
132             You will be notified automatically of any progress on your issue.
133              
134             =head2 Source Code
135              
136             This is open source software. The code repository is available for
137             public review and contribution under the terms of the license.
138              
139             L<https://github.com/skirmess/Dist-Zilla-Plugin-Code>
140              
141             git clone https://github.com/skirmess/Dist-Zilla-Plugin-Code.git
142              
143             =head1 AUTHOR
144              
145             Sven Kirmess <sven.kirmess@kzone.ch>
146              
147             =head1 SEE ALSO
148              
149             L<Dist::Zilla>, L<Dist::Zilla::Role::Plugin>
150              
151             =cut