Role/Hooker.pm

filter: Statementstmt 95.0% 57 / 60 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 89.0% 81 / 91 CC 9 SCAR 11.3
CC
9
Cov
90%
hook
6.1
_prologue
3.0
_line_directive
2.0
CRAP 3.1
↑ Summary Plugin/Hook.pm »
1 # ---------------------------------------------------------------------- copyright and license ---
2 #
3 # file: lib/Dist/Zilla/Role/Hooker.pm
4 #
5 # Copyright © 2015, 2016, 2018 Van de Bugger.
6 #
7 # This file is part of perl-Dist-Zilla-Plugin-Hook.
8 #
9 # perl-Dist-Zilla-Plugin-Hook is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU General Public License as published by the Free Software Foundation,
11 # either version 3 of the License, or (at your option) any later version.
12 #
13 # perl-Dist-Zilla-Plugin-Hook is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 # PURPOSE. See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # perl-Dist-Zilla-Plugin-Hook. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # SPDX-License-Identifier: GPL-3.0-or-later
21 #
22 # ---------------------------------------------------------------------- copyright and license ---
23  
24 #pod =for :this This is C<Hooker> role documentation. Read this if you are going to hack or extend
25 #pod C<Dist-Zilla-Plugin-Hook>, or use the role in your plugin.
26 #pod
27 #pod =for :those If you want to write C<Dist::Zilla> plugin directly in F<dist.ini>, read the L<manual|Dist::Zilla::Plugin::Hook::Manual>. General
28 #pod topics like getting source, building, installing, bug reporting and some others are covered in the
29 #pod F<README>.
30 #pod
31 #pod =head1 DESCRIPTION
32 #pod
33 #pod C<Dist-Zilla-Plugin-Hook> is a set of plugins: C<Hook::Init>, C<Hook::BeforeBuild>, C<Hook::GatherFiles>,
34 #pod etc. All these plugins are just stubs, they contains almost no code. They just use services
35 #pod provided by the C<Hooker> role. The role is an engine for all C<Hook> plugins.
36 #pod
37 #pod =head1 SEE ALSO
38 #pod
39 #pod =for :list
40 #pod = L<Dist::Zilla>
41 #pod = L<Dist::Zilla::Role>
42 #pod = L<Dist::Zilla::Role::Plugin>
43 #pod = L<Dist::Zilla::Plugin::Hook::Manual>
44 #pod
45 #pod =cut
46  
47 package Dist::Zilla::Role::Hooker;
48  
49 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 23 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # ABSTRACT: Run Perl code written in your plugin's F<dist.ini> section
54 23 our $VERSION = 'v0.8.4'; # VERSION
55  
56 23 with 'Dist::Zilla::Role::Plugin';
57 23 with 'Dist::Zilla::Role::ErrorLogger' => { -version => 0.005 };
58  
59 # --------------------------------------------------------------------------------------------------
60  
61 #pod =attr code
62 #pod
63 #pod Perl code to execute, list of lines (without newline characters).
64 #pod
65 #pod C<ArrayRef[Str]>, read-only. Default value is empty array (i. e. no code).
66 #pod
67 #pod Note: C<init_arg> attribute property set to ".". In F<dist.ini> file the Perl code should be
68 #pod specified using this notation:
69 #pod
70 #pod [Hook::Role]
71 #pod . = …Perl code…
72 #pod
73 #pod =cut
74  
75 has code => (
76 is => 'ro',
77 isa => 'ArrayRef[Str]',
78 auto_deref => 1,
79 init_arg => '.',
80 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 23 );
82  
83 # --------------------------------------------------------------------------------------------------
84  
85 #pod =method hook
86 #pod
87 #pod $ret = $self->hook( @args );
88 #pod @ret = $self->hook( @args );
89 #pod $self->hook( @args );
90 #pod
91 #pod This is the primary method of the role. The method executes Perl code specified in C<code>
92 #pod attribute (prepended with C<_prologue>) with string form of C<eval>. The method passes arguments
93 #pod specified by the caller to the code, and passes the code return value back to the caller. Calling
94 #pod context (list, scalar, or void) is preserved. The method also hides all the lexical variables
95 #pod (except the variables documented below) from code. The method intercepts warnings generated in code
96 #pod and logs them; warnings do not stop executing.
97 #pod
98 #pod Following lexical variables are exposed to the code intentionally:
99 #pod
100 #pod =begin :list
101 #pod
102 #pod = C<@_>
103 #pod C<hook> arguments, self-reference is already shifted!
104 #pod
105 #pod = C<$arg>
106 #pod The same as C<$_[ 0 ]>.
107 #pod
108 #pod = C<$self>
109 #pod = C<$plugin>
110 #pod Reference to the plugin object executing the code (such as C<Hook::Init> or C<Hook::BeforeBuild>).
111 #pod
112 #pod = C<$dist>
113 #pod = C<$zilla>
114 #pod Reference to C<Dist::Zilla> object, the same as C<< $self->zilla >>.
115 #pod
116 #pod =end :list
117 #pod
118 #pod If code dies, the method logs error message and aborts C<Dist::Zilla>.
119 #pod
120 #pod =cut
121  
122 18 ▶sub hook { ## no critic ( RequireArgUnpacking )
Subroutine hook: called CC=6 SCAR=18.1
Pod documented
123 18 my $self = shift( @_ );
124 18 ▶ if ( not $self->code ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->code) 0 18 18
125 0 return;
126 };
127 18 my $zilla = $self->zilla; # `eval` sees these variables.
128 18 my $plugin = $self;
129 18 my $dist = $zilla;
130 18 my $arg = $_[ 0 ];
131 my $code = # Declaration is not yet completed, `eval` will not see it.
132 18 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 #~ local $SIG{ __DIE__ }; # TODO: Should I cancel die handler, if any is set?
134 1 ▶ local $SIG{ __WARN__ } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 1 my $msg = "$_[ 0 ]";
136 1 chomp( $msg );
137 1 $self->log( $msg );
138 18 };
139 eval( ## no critic ( ProhibitStringyEval, RequireCheckingReturnValueOfEval )
140 18 join(
141 "\n",
142 $self->_line_directive( 'prologue' ), # Make error repot nice.
143 $self->_prologue,
144 $self->_line_directive( $self->plugin_name ), # Make error repot nice.
145 $self->code,
146 )
147 );
148 18 };
149 18 my $want = wantarray();
150 18 my ( $err, @ret );
151 {
152 18 local $@ = $@; # Keep outer `$@` intact.
153 18 ▶ if ( $want ) { # Let us keep calling context.
Branch: 2 branches
BranchTrueFalseTotal
if ($want) { } 0 18 18
elsif (defined $want) { } 2 16 18
154 0 @ret = $code->( @_ );
155 } elsif ( defined( $want ) ) {
156 2 $ret[ 0 ] = $code->( @_ );
157 } else {
158 16 $code->( @_ );
159 };
160 18 $err = "$@"; # Stringify `$@`.
161 }
162 18 ▶ if ( $err ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err ne "") 8 10 18
163 8 chomp( $err );
164 8 $self->abort( $err );
165 };
166 10 ▶ return $want ? @ret : $ret[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$want ? : 0 10 10
167 };
168  
169 # --------------------------------------------------------------------------------------------------
170  
171 #pod =method _line_directive
172 #pod
173 #pod $dir = $self->_line_directive( $filename, $linenumber );
174 #pod $dir = $self->_line_directive( $filename );
175 #pod
176 #pod The method returns Perl line directive, like
177 #pod
178 #pod #line 1 "filename.ext"
179 #pod
180 #pod The method takes care about quotes. Perl line directive does not allow any quotes (escaped or not)
181 #pod in filename, so directive
182 #pod
183 #pod #line 1 "\"Assa\" project.txt"
184 #pod
185 #pod will be ignored. To avoid this, C<line_directive> replaces quotes in filename with apostrophes, e.
186 #pod g.:
187 #pod
188 #pod #line 1 "'Assa' project.txt"
189 #pod
190 #pod If line number is not specified, 1 will be used.
191 #pod
192 #pod =cut
193  
194 36 ▶sub _line_directive {
Subroutine _line_directive: called CC=2 SCAR=7.0
195 36 my ( $self, $file, $line ) = @_;
196 36 $file =~ s{"}{'}gx; # Perl `#line` directive does not allow quotes in filename.
197 # TODO: Issue a warning if filename contains double quote?
198 36 ▶ if ( not defined( $line ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 36 0 36
199 36 $line = 1;
200 };
201 36 return sprintf( '#line %d "%s"', $line, $file );
202 };
203  
204 # --------------------------------------------------------------------------------------------------
205  
206 #pod =method _prologue
207 #pod
208 #pod @code = $self->_prologue;
209 #pod
210 #pod The method returns prologue code.
211 #pod
212 #pod Prologue code is extracted from C<Dist::Zilla> plugin named C<prologue>.
213 #pod
214 #pod =cut
215  
216 18 ▶sub _prologue {
Subroutine _prologue: called CC=3 SCAR=11.0
217 18 my ( $self ) = @_;
218 18 my $zilla = $self->zilla;
219 18 my $prologue = $zilla->plugin_named( 'prologue' );
220 18 ▶ if ( $prologue and $prologue->meta->does_role( 'Dist::Zilla::Role::Hooker' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prologue and $prologue->meta->does_role("Dist::Zilla::Role::Hooker")) 4 14 18
Condition66%2/3
$prologue and $prologue->meta->does_role("Dist::Zilla::Role::Hooker")
!ll&&!rl&&r
1404
MC/DC50%1/2
$prologue and $prologue->meta->does_role("Dist::Zilla::Role::Hooker")
$prologue $prologue->meta->does_role("Dist::Zilla::Role::Hooker")
Truth table66%2/3
A and B
A= $prologueB= $prologue->meta->does_role("Dist::Zilla::Role::Hooker")
ABresult
0X0
100
111
221 4 return $prologue->code;
222 };
223 14 return ();
224 };
225  
226 # --------------------------------------------------------------------------------------------------
227  
228 #pod =method mvp_multivalue_args
229 #pod
230 #pod The method tells C<Dist::Zilla> that dot (C<.>) is a multi-value option.
231 #pod
232 #pod =cut
233  
234 21 ▶around mvp_multivalue_args => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
235 21 my ( $orig, $self ) = @_;
236 21 return ( $self->$orig(), qw{ . } );
237 23 };
238  
239 # --------------------------------------------------------------------------------------------------
240  
241 21 ▶around mvp_aliases => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
242 21 my ( $orig, $self ) = @_;
243 21 my $aliases = $self->$orig();
244 21 $aliases->{ hook } = '.';
245 21 return $aliases;
246 23 };
247  
248 23 1;
249  
250 # --------------------------------------------------------------------------------------------------
251  
252 #pod =head1 COPYRIGHT AND LICENSE
253 #pod
254 #pod Copyright (C) 2015, 2016, 2018 Van de Bugger
255 #pod
256 #pod License GPLv3+: The GNU General Public License version 3 or later
257 #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
258 #pod
259 #pod This is free software: you are free to change and redistribute it. There is
260 #pod NO WARRANTY, to the extent permitted by law.
261 #pod
262 #pod
263 #pod =cut
264  
265 # ------------------------------------------------------------------------------------------------
266 #
267 # file: doc/what.pod
268 #
269 # Copyright © 2015, 2016, 2018 Van de Bugger.
270 #
271 # This file is part of perl-Dist-Zilla-Plugin-Hook.
272 #
273 # SPDX-License-Identifier: GPL-3.0-or-later
274 #
275 # ------------------------------------------------------------------------------------------------
276  
277 #pod =encoding UTF-8
278 #pod
279 #pod =head1 WHAT?
280 #pod
281 #pod C<Dist-Zilla-Plugin-Hook> (or just C<Hook>) is a set of C<Dist-Zilla> plugins. Every plugin executes Perl
282 #pod code inlined into F<dist.ini> at particular stage of build process.
283 #pod
284 #pod =cut
285  
286 # end of file #
287  
288  
289 # end of file #
290  
291 __END__
↑ Summary Plugin/Hook.pm »