Cmd/Role.pm

filter: Statementstmt 99.2% 136 / 137 Branchbran 86.9% 40 / 46 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 96.6% 29 / 30 Podpod 100% 2 / 2 totaltotal 94.5% 208 / 220 CC 25 SCAR 18.6
CC
25
Cov
95%
_initialize_from_cmd
17.0
__ANON__
3.2
_build_command_commands
3.0
CRAP 6.5
« Cmd/Tester.pm ↑ Summary Cmd.pm »
1 package MooX::Cmd::Role;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = "0.016_001";
7  
8 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Module::Runtime qw/ use_module /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Text::ParseWords 'shellwords';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use List::MoreUtils qw/first_index first_result/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Params::Util qw/_ARRAY/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 MooX::Cmd::Role - MooX cli app commands do this
23  
24 =head1 SYNOPSIS
25  
26 =head2 using role and want behavior as MooX::Cmd
27  
28 package MyFoo;
29
30 with MooX::Cmd::Role;
31
32 sub _build_command_execute_from_new { 1 }
33  
34 package main;
35  
36 my $cmd = MyFoo->new_with_cmd;
37  
38 =head2 using role and don't execute immediately
39  
40 package MyFoo;
41  
42 with MooX::Cmd::Role;
43 use List::MoreUtils qw/ first_idx /;
44  
45 sub _build_command_base { "MyFoo::Command" }
46  
47 sub _build_command_execute_from_new { 0 }
48  
49 sub execute {
50 my $self = shift;
51 my $chain_idx = first_idx { $self == $_ } @{$self->command_chain};
52 my $next_cmd = $self->command_chain->{$chain_idx+1};
53 $next_cmd->owner($self);
54 $next_cmd->execute;
55 }
56  
57 package main;
58  
59 my $cmd = MyFoo->new_with_cmd;
60 $cmd->command_chain->[-1]->run();
61  
62 =head2 explicit expression of some implicit stuff
63  
64 package MyFoo;
65  
66 with MooX::Cmd::Role;
67  
68 sub _build_command_base { "MyFoo::Command" }
69  
70 sub _build_command_execute_method_name { "run" }
71  
72 sub _build_command_execute_from_new { 0 }
73  
74 package main;
75  
76 my $cmd = MyFoo->new_with_cmd;
77 $cmd->command_chain->[-1]->run();
78  
79 =head1 DESCRIPTION
80  
81 MooX::Cmd::Role is made for modern, flexible Moo style to tailor cli commands.
82  
83 =head1 ATTRIBUTES
84  
85 =head2 command_args
86  
87 ARRAY-REF of args on command line
88  
89 =cut
90  
91 6 has 'command_args' => (is => "ro");
92  
93 =head2 command_chain
94  
95 ARRAY-REF of commands lead to this instance
96  
97 =cut
98  
99 6 has 'command_chain' => (is => "ro");
100  
101 =head2 command_chain_end
102  
103 COMMAND accesses the finally detected command in chain
104  
105 =cut
106  
107 6 has 'command_chain_end' => (is => "lazy");
108  
109 22 ▶sub _build_command_chain_end { $_[0]->command_chain->[-1] }
Subroutine _build_command_chain_end: called CC=1 SCAR=0.0
110  
111 =head2 command_name
112  
113 ARRAY-REF the name of the command lead to this command
114  
115 =cut
116  
117 6 has 'command_name' => (is => "ro");
118  
119 =head2 command_commands
120  
121 HASH-REF names of other commands
122  
123 =cut
124  
125 6 has 'command_commands' => (is => "lazy");
126  
127 sub _build_command_commands
128 {
129 44 ▶ my ($class, $params) = @_;
Subroutine _build_command_commands: called CC=3 SCAR=11.0
130 44 ▶ defined $params->{command_base} or $params->{command_base} = $class->_build_command_base($params);
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{"command_base"} or $params->{"command_base"} = $class->_build_command_base($params) 42 2 44
131 44 my $base = $params->{command_base};
132  
133 # I have no clue why 'only' and 'except' seems to not fulfill what I need or are bugged in M::P - Getty
134 my @cmd_plugins = grep {
135 44 my $plug_class = _mkcommand($_, $base);
136 63 index($plug_class, ":") == -1;
137 } Module::Pluggable::Object->new(
138 search_path => $base,
139 require => 0,
140 )->plugins;
141  
142 44 my %cmds = map { _mkcommand($_, $base) => $_ } @cmd_plugins;
143 44 ▶ scalar keys %cmds == scalar @cmd_plugins
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %cmds == scalar @cmd_plugins or croak(q[Can't compute unambiguous list of commands from '] . join("', '", @cmd_plugins) . q[']) 0 44 44
144 or croak "Can't compute unambiguous list of commands from '" . join("', '", @cmd_plugins) . "'";
145  
146 44 \%cmds;
147 }
148  
149 =head2 command_base
150  
151 STRING base of command plugins
152  
153 =cut
154  
155 6 has command_base => (is => "lazy");
156  
157 42 ▶sub _build_command_base { $_[0] . '::Cmd'; }
Subroutine _build_command_base: called CC=1 SCAR=0.0
158  
159 =head2 command_execute_method_name
160  
161 STRING name of the method to invoke to execute a command, default "execute"
162  
163 =cut
164  
165 6 has command_execute_method_name => (is => "lazy");
166  
167 17 ▶sub _build_command_execute_method_name { "execute" }
Subroutine _build_command_execute_method_name: called CC=1 SCAR=0.0
168  
169 =head2 command_execute_return_method_name
170  
171 STRING I have no clue what that is good for ...
172  
173 =cut
174  
175 6 has command_execute_return_method_name => (is => "lazy");
176  
177 29 ▶sub _build_command_execute_return_method_name { "execute_return" }
Subroutine _build_command_execute_return_method_name: called CC=1 SCAR=0.0
178  
179 =head2 command_creation_method_name
180  
181 STRING name of constructor
182  
183 =cut
184  
185 6 has command_creation_method_name => (is => "lazy");
186  
187 2 ▶sub _build_command_creation_method_name { "new_with_cmd" }
Subroutine _build_command_creation_method_name: called CC=1 SCAR=0.0
188  
189 =head2 command_creation_chain_methods
190  
191 ARRAY-REF names of methods to chain for creating object (from L</command_creation_method_name>)
192  
193 =cut
194  
195 6 has command_creation_chain_methods => (is => "lazy");
196  
197 39 ▶sub _build_command_creation_chain_methods { ['new_with_options', 'new'] }
Subroutine _build_command_creation_chain_methods: called CC=1 SCAR=0.0
198  
199 =head2 command_execute_from_new
200  
201 BOOL true when constructor shall invoke L</command_execute_method_name>, false otherwise
202  
203 =cut
204  
205 6 has command_execute_from_new => (is => "lazy");
206  
207 0 ▶sub _build_command_execute_from_new { 0 }
Subroutine _build_command_execute_from_new: not called CC=1 SCAR=6.9
208  
209 =head1 METHODS
210  
211 =head2 new_with_cmd
212  
213 initializes by searching command line args for commands and invoke them
214  
215 =cut
216  
217 2 ▶sub new_with_cmd { goto &_initialize_from_cmd; }
Subroutine new_with_cmd: called CC=1 SCAR=0.0
Pod documented
218  
219 sub _mkcommand
220 {
221 141 ▶ my ($package, $base) = @_;
Subroutine _mkcommand: called CC=2 SCAR=7.0
222 141 my $bwc = "${base}::";
223 141 my $len_bwc = length($bwc);
224 141 ▶ index($package, $bwc) == 0 and substr($package, 0, $len_bwc, "");
Branch: 1 branch
BranchTrueFalseTotal
index($package, $bwc) == 0 and substr($package, 0, $len_bwc, '') 141 0 141
225 141 lc($package);
226 }
227  
228 6 my @private_init_params =
229 qw(command_base command_execute_method_name command_execute_return_method_name command_creation_chain_methods command_execute_method_name);
230  
231 my $required_method = sub {
232 80 ▶ my ($tgt, $method) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.7
233 80 ▶ $tgt->can($method) or croak("You need an '$method' in " . (blessed $tgt || $tgt));
Branch: 1 branch
BranchTrueFalseTotal
$tgt->can($method) or croak("You need an '${method}' in " . (builtin::blessed($tgt) || $tgt)) 1 79 80
Condition33%1/3
builtin::blessed($tgt) || $tgt
l!l&&r!l&&!r
010
MC/DC0%0/2
builtin::blessed($tgt) || $tgt
builtin::blessed($tgt) $tgt
Truth table33%1/3
A || B
A= builtin::blessed($tgt)B= $tgt
ABresult
1X1
011
000
234 6 };
235  
236 my $call_required_method = sub {
237 30 ▶ my ($tgt, $method, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
238 30 my $m = $required_method->($tgt, $method);
239 30 return $m->($tgt, @args);
240 6 };
241  
242 my $call_optional_method = sub {
243 23 ▶ my ($tgt, $method, @args) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
244 23 ▶ my $m = $tgt->can($method) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $m = $tgt->can($method) or return 4 19 23
245 19 return $m->($tgt, @args);
246 6 };
247  
248 my $call_indirect_method = sub {
249 27 ▶ my ($tgt, $name_getter, @args) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
250 27 my $g = $call_required_method->($tgt, $name_getter);
251 27 my $m = $required_method->($tgt, $g);
252 27 return $m->($tgt, @args);
253 6 };
254  
255 ## no critic qw(ProhibitExcessComplexity)
256 sub _initialize_from_cmd
257 {
258 45 ▶ my ($class, %params) = @_;
Subroutine _initialize_from_cmd: called CC=17 SCAR=28.3
259  
260 45 my @args = shellwords(join ' ', map { quotemeta } @ARGV);
261  
262 45 my (@used_args, $cmd, $cmd_name, $cmd_name_index);
263  
264 45 my %cmd_create_params = %params;
265 45 delete @cmd_create_params{qw(command_commands), @private_init_params};
266  
267 45 ▶ defined $params{command_commands} or $params{command_commands} = $class->_build_command_commands(\%params);
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"command_commands"} or $params{"command_commands"} = $class->_build_command_commands(\%params) 44 1 45
268 45 ▶ if (($cmd_name_index = first_index { $cmd = $params{command_commands}->{$_} } @args) >= 0)
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (($cmd_name_index = &first_index(sub { $cmd = $params{'command_commands'}{$_}; } , @args)) >= 0) { } 23 22 45
269 {
270 23 @used_args = splice @args, 0, $cmd_name_index;
271 23 shift @args; # be careful about relics
272 23 $cmd_name = _mkcommand($cmd, $params{command_base});
273  
274 23 use_module($cmd);
275 defined $cmd_create_params{command_execute_method_name}
276 or $cmd_create_params{command_execute_method_name} =
277 23 ▶ $call_optional_method->($cmd, "_build_command_execute_method_name", \%cmd_create_params);
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd_create_params{"command_execute_method_name"} or $cmd_create_params{"command_execute_method_name"} = &$call_optional_method($cmd, "_build_command_execute_method_name", \%cmd_create_params) 23 0 23
278 defined $cmd_create_params{command_execute_method_name}
279 23 ▶ or $cmd_create_params{command_execute_method_name} = "execute";
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd_create_params{"command_execute_method_name"} or $cmd_create_params{"command_execute_method_name"} = "execute" 4 19 23
280 23 $required_method->($cmd, $cmd_create_params{command_execute_method_name});
281 }
282 else
283 {
284 22 @used_args = @args;
285 22 @args = ();
286 }
287  
288 defined $params{command_creation_chain_methods}
289 44 ▶ or $params{command_creation_chain_methods} = $class->_build_command_creation_chain_methods(\%params);
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"command_creation_chain_methods"} or $params{"command_creation_chain_methods"} = $class->_build_command_creation_chain_methods(\%params) 41 3 44
290 my @creation_chain =
291 _ARRAY($params{command_creation_chain_methods})
292 39 ? @{$params{command_creation_chain_methods}}
293 44 ▶ : ($params{command_creation_chain_methods});
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY($params{'command_creation_chain_methods'}) ? : 39 5 44
294 44 ▶ (my $creation_method = first_result { defined $_ and $class->can($_) } @creation_chain)
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
my $creation_method = &first_result(sub { defined $_ and $class->can($_); } , @creation_chain) or croak("Can't find a creation method on $class") 3 41 44
defined $_ and $class->can($_) 82 1 83
295 or croak "Can't find a creation method on $class";
296  
297 ## no critic qw(RequireLocalizedPunctuationVars)
298 41 @ARGV = @used_args;
299 41 $params{command_args} = [@args];
300 41 $params{command_name} = $cmd_name;
301 41 ▶ defined $params{command_chain} or $params{command_chain} = [];
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"command_chain"} or $params{"command_chain"} = [] 27 14 41
302 41 my $self = $creation_method->($class, %params);
303 41 push @{$self->command_chain}, $self;
304  
305 41 ▶ if ($cmd)
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) { } 22 19 41
306 {
307 22 @ARGV = @args;
308 22 my ($creation_method, $creation_method_name, $cmd_plugin);
309 22 ▶ $cmd->can("_build_command_creation_method_name")
Branch: 1 branch
BranchTrueFalseTotal
$cmd->can("_build_command_creation_method_name") and $creation_method_name = $cmd->_build_command_creation_method_name(\%params) 16 6 22
310 and $creation_method_name = $cmd->_build_command_creation_method_name(\%params);
311 22 ▶ $creation_method_name and $creation_method = $cmd->can($creation_method_name);
Branch: 1 branch
BranchTrueFalseTotal
$creation_method_name and $creation_method = $cmd->can($creation_method_name) 16 6 22
312 22 ▶ if ($creation_method)
Branch: 1 branch
BranchTrueFalseTotal
if ($creation_method) { } 16 6 22
313 {
314 16 @cmd_create_params{qw(command_chain)} = @$self{qw(command_chain)};
315 16 $cmd_plugin = $creation_method->($cmd, %cmd_create_params);
316 $self->{$self->command_execute_return_method_name} =
317 14 [@{$call_indirect_method->($cmd_plugin, "command_execute_return_method_name")}];
318 }
319 else
320 {
321 6 ▶ ($creation_method = first_result { defined $_ and $cmd->can($_) } @creation_chain)
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 2 branches
BranchTrueFalseTotal
$creation_method = &first_result(sub { defined $_ and $cmd->can($_); } , @creation_chain) or croak(q[Can't find a creation method on ] . $cmd) 0 6 6
defined $_ and $cmd->can($_) 12 0 12
322 or croak "Can't find a creation method on " . $cmd;
323 6 $cmd_plugin = $creation_method->($cmd);
324 6 push @{$self->command_chain}, $cmd_plugin;
325  
326 6 my $cemn = $cmd_plugin->can("command_execute_method_name");
327 6 ▶ my $exec_fun = $cemn ? $cemn->() : $self->command_execute_method_name();
Branch: 1 branch
BranchTrueFalseTotal
$cemn ? : 0 6 6
328 $self->command_execute_from_new
329 6 ▶ and $self->{$self->command_execute_return_method_name} =
Branch: 1 branch
BranchTrueFalseTotal
$self->command_execute_from_new and $$self{$self->command_execute_return_method_name} = [&$call_required_method($cmd_plugin, $exec_fun, \@ARGV, $self->command_chain)] 3 3 6
330 [$call_required_method->($cmd_plugin, $exec_fun, \@ARGV, $self->command_chain)];
331 }
332 }
333 else
334 {
335 $self->command_execute_from_new
336 19 ▶ and $self->{$self->command_execute_return_method_name} =
Branch: 1 branch
BranchTrueFalseTotal
$self->command_execute_from_new and $$self{$self->command_execute_return_method_name} = [&$call_indirect_method($self, "command_execute_method_name", \@ARGV, $self->command_chain)] 13 6 19
337 [$call_indirect_method->($self, "command_execute_method_name", \@ARGV, $self->command_chain)];
338 }
339  
340 39 return $self;
341 }
342  
343 =head2 execute_return
344  
345 returns the content of $self->{execute_return}
346  
347 =cut
348  
349 # XXX should be an r/w attribute - can be renamed on loading ...
350 38 ▶sub execute_return { $_[0]->{execute_return} }
Subroutine execute_return: called CC=1 SCAR=0.0
Pod documented
351  
352 =head1 LICENSE AND COPYRIGHT
353  
354 Copyright 2012-2013 Torsten Raudssus, Copyright 2013-2017 Jens Rehsack.
355  
356 This program is free software; you can redistribute it and/or modify it
357 under the terms of either: the GNU General Public License as published
358 by the Free Software Foundation; or the Artistic License.
359  
360 See L<http://dev.perl.org/licenses/> for more information.
361  
362 =cut
363  
364 6 1;
« Cmd/Tester.pm ↑ Summary Cmd.pm »