| 1 | package MooX::Cmd::Role; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: MooX cli app commands do this | ||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | our $VERSION = '1.001'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Module::Runtime qw/ use_module /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Regexp::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 8 | ▶ | use Text::ParseWords 'shellwords'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Module::Pluggable::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use List::MoreUtils qw/first_index first_result/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Params::Util qw/_ARRAY/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | has 'command_args' => (is => "ro"); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | has 'command_chain' => (is => "ro"); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 8 | has 'command_chain_end' => (is => "lazy"); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 26 | ▶ | sub _build_command_chain_end { $_[0]->command_chain->[-1] } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_chain_end: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8 | has 'command_name' => (is => "ro"); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 8 | has 'command_commands' => (is => "lazy"); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub _build_command_commands | ||||||||||||||||||||||||||||
| 37 | 62 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_command_commands: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 38 | 62 | my ($class, $params) = @_; | |||||||||||||||||||||||||||
| 39 | 62 | ▶ | defined $params->{command_base} or $params->{command_base} = $class->_build_command_base($params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 62 | my $base = $params->{command_base}; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # I have no clue why 'only' and 'except' seems to not fulfill what I need or are bugged in M::P - Getty | ||||||||||||||||||||||||||||
| 43 | my @cmd_plugins = grep { | ||||||||||||||||||||||||||||
| 44 | 62 | my $plug_class = _mkcommand($_, $base); | |||||||||||||||||||||||||||
| 45 | 90 | index($plug_class, ":") == -1; | |||||||||||||||||||||||||||
| 46 | } Module::Pluggable::Object->new( | ||||||||||||||||||||||||||||
| 47 | search_path => $base, | ||||||||||||||||||||||||||||
| 48 | require => 0, | ||||||||||||||||||||||||||||
| 49 | )->plugins; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 62 | my %cmds = map { _mkcommand($_, $base) => $_ } @cmd_plugins; | |||||||||||||||||||||||||||
| 52 | 62 | ▶ | scalar keys %cmds == scalar @cmd_plugins | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | or croak "Can't compute unambiguous list of commands from '" . join("', '", @cmd_plugins) . "'"; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 62 | \%cmds; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 8 | has command_base => (is => "lazy"); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 60 | ▶ | sub _build_command_base { $_[0] . '::Cmd'; } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_base: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 8 | has command_execute_method_name => (is => "lazy"); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 28 | ▶ | sub _build_command_execute_method_name { "execute" } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_execute_method_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 8 | has command_execute_return_method_name => (is => "lazy"); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 39 | ▶ | sub _build_command_execute_return_method_name { "execute_return" } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_execute_return_method_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 8 | has command_creation_method_name => (is => "lazy"); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | sub _build_command_creation_method_name { "new_with_cmd" } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_creation_method_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 8 | has command_creation_chain_methods => (is => "lazy"); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 56 | ▶ | sub _build_command_creation_chain_methods { ['new_with_options', 'new'] } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_creation_chain_methods: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 8 | has command_execute_from_new => (is => "lazy"); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _build_command_execute_from_new { 0 } | ||||||||||||||||||||||||||
|
Subroutine
_build_command_execute_from_new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | ▶ | sub new_with_cmd { goto &_initialize_from_cmd; } | ||||||||||||||||||||||||||
|
Subroutine
new_with_cmd: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub _mkcommand | ||||||||||||||||||||||||||||
| 92 | 195 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_mkcommand: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 93 | 195 | my ($package, $base) = @_; | |||||||||||||||||||||||||||
| 94 | 195 | my $bwc = "${base}::"; | |||||||||||||||||||||||||||
| 95 | 195 | my $len_bwc = length($bwc); | |||||||||||||||||||||||||||
| 96 | 195 | ▶ | index($package, $bwc) == 0 and substr($package, 0, $len_bwc, ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 195 | lc($package); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 8 | my @private_init_params = | |||||||||||||||||||||||||||
| 101 | qw(command_base command_execute_method_name command_execute_return_method_name command_creation_chain_methods command_execute_method_name); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 108 | ▶ | my $required_method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||
| 104 | 108 | my ($tgt, $method) = @_; | |||||||||||||||||||||||||||
| 105 | 108 | ▶ | $tgt->can($method) or croak("You need an '$method' in " . (blessed $tgt || $tgt)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($tgt) || $tgt
MC/DC0%0/2
builtin::blessed($tgt) || $tgt
builtin::blessed($tgt)
$tgt
Truth table33%1/3
A || B
A= builtin::blessed($tgt)B= $tgt
| |||||||||||||||||||||||||||||
| 106 | 8 | }; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 40 | ▶ | my $call_required_method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 40 | my ($tgt, $method, @args) = @_; | |||||||||||||||||||||||||||
| 110 | 40 | my $m = $required_method->($tgt, $method); | |||||||||||||||||||||||||||
| 111 | 40 | return $m->($tgt, @args); | |||||||||||||||||||||||||||
| 112 | 8 | }; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 31 | ▶ | my $call_optional_method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 115 | 31 | my ($tgt, $method, @args) = @_; | |||||||||||||||||||||||||||
| 116 | 31 | ▶ | my $m = $tgt->can($method) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 27 | return $m->($tgt, @args); | |||||||||||||||||||||||||||
| 118 | 8 | }; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 37 | ▶ | my $call_indirect_method = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 37 | my ($tgt, $name_getter, @args) = @_; | |||||||||||||||||||||||||||
| 122 | 37 | my $g = $call_required_method->($tgt, $name_getter); | |||||||||||||||||||||||||||
| 123 | 37 | my $m = $required_method->($tgt, $g); | |||||||||||||||||||||||||||
| 124 | 37 | return $m->($tgt, @args); | |||||||||||||||||||||||||||
| 125 | 8 | }; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | ## no critic qw(ProhibitExcessComplexity) | ||||||||||||||||||||||||||||
| 128 | sub _initialize_from_cmd | ||||||||||||||||||||||||||||
| 129 | 63 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_initialize_from_cmd: called CC=17 SCAR=28.3
| |||||||||||||||||||||||||||||
| 130 | 63 | my ($class, %params) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 63 | my @args = shellwords(join ' ', map { quotemeta } @ARGV); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 63 | my (@used_args, $cmd, $cmd_name, $cmd_name_index); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 63 | my %cmd_create_params = %params; | |||||||||||||||||||||||||||
| 137 | 63 | delete @cmd_create_params{qw(command_commands), @private_init_params}; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 63 | ▶ | defined $params{command_commands} or $params{command_commands} = $class->_build_command_commands(\%params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 62 | ▶ | if (($cmd_name_index = first_index { $cmd = $params{command_commands}->{$_} } @args) >= 0) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 31 | @used_args = splice @args, 0, $cmd_name_index; | |||||||||||||||||||||||||||
| 143 | 31 | shift @args; # be careful about relics | |||||||||||||||||||||||||||
| 144 | 31 | $cmd_name = _mkcommand($cmd, $params{command_base}); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 31 | use_module($cmd); | |||||||||||||||||||||||||||
| 147 | defined $cmd_create_params{command_execute_method_name} | ||||||||||||||||||||||||||||
| 148 | or $cmd_create_params{command_execute_method_name} = | ||||||||||||||||||||||||||||
| 149 | 31 | ▶ | $call_optional_method->($cmd, "_build_command_execute_method_name", \%cmd_create_params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | defined $cmd_create_params{command_execute_method_name} | ||||||||||||||||||||||||||||
| 151 | 31 | ▶ | or $cmd_create_params{command_execute_method_name} = "execute"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 31 | $required_method->($cmd, $cmd_create_params{command_execute_method_name}); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | else | ||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 31 | @used_args = @args; | |||||||||||||||||||||||||||
| 157 | 31 | @args = (); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | defined $params{command_creation_chain_methods} | ||||||||||||||||||||||||||||
| 161 | 61 | ▶ | or $params{command_creation_chain_methods} = $class->_build_command_creation_chain_methods(\%params); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | my @creation_chain = | ||||||||||||||||||||||||||||
| 163 | _ARRAY($params{command_creation_chain_methods}) | ||||||||||||||||||||||||||||
| 164 | 56 | ? @{$params{command_creation_chain_methods}} | |||||||||||||||||||||||||||
| 165 | 61 | ▶ | : ($params{command_creation_chain_methods}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 61 | ▶ | (my $creation_method = first_result { defined $_ and $class->can($_) } @creation_chain) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 167 | or croak "Can't find a creation method on $class"; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | ## no critic qw(RequireLocalizedPunctuationVars) | ||||||||||||||||||||||||||||
| 170 | 58 | @ARGV = @used_args; | |||||||||||||||||||||||||||
| 171 | 58 | $params{command_args} = [@args]; | |||||||||||||||||||||||||||
| 172 | 58 | $params{command_name} = $cmd_name; | |||||||||||||||||||||||||||
| 173 | 58 | ▶ | defined $params{command_chain} or $params{command_chain} = []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 58 | my $self = $creation_method->($class, %params); | |||||||||||||||||||||||||||
| 175 | 55 | push @{$self->command_chain}, $self; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 55 | ▶ | if ($cmd) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||||||||
| 179 | 30 | @ARGV = @args; | |||||||||||||||||||||||||||
| 180 | 30 | my ($creation_method, $creation_method_name, $cmd_plugin); | |||||||||||||||||||||||||||
| 181 | 30 | ▶ | $cmd->can("_build_command_creation_method_name") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | and $creation_method_name = $cmd->_build_command_creation_method_name(\%params); | ||||||||||||||||||||||||||||
| 183 | 30 | ▶ | $creation_method_name and $creation_method = $cmd->can($creation_method_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 30 | ▶ | if ($creation_method) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | { | ||||||||||||||||||||||||||||
| 186 | 24 | @cmd_create_params{qw(command_chain)} = @$self{qw(command_chain)}; | |||||||||||||||||||||||||||
| 187 | 24 | $cmd_plugin = $creation_method->($cmd, %cmd_create_params); | |||||||||||||||||||||||||||
| 188 | $self->{$self->command_execute_return_method_name} = | ||||||||||||||||||||||||||||
| 189 | 19 | [@{$call_indirect_method->($cmd_plugin, "command_execute_return_method_name")}]; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | else | ||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 6 | ▶ | ($creation_method = first_result { defined $_ and $cmd->can($_) } @creation_chain) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 194 | or croak "Can't find a creation method on " . $cmd; | ||||||||||||||||||||||||||||
| 195 | 6 | $cmd_plugin = $creation_method->($cmd); | |||||||||||||||||||||||||||
| 196 | 6 | push @{$self->command_chain}, $cmd_plugin; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 6 | my $cemn = $cmd_plugin->can("command_execute_method_name"); | |||||||||||||||||||||||||||
| 199 | 6 | ▶ | my $exec_fun = $cemn ? $cemn->() : $self->command_execute_method_name(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | $self->command_execute_from_new | ||||||||||||||||||||||||||||
| 201 | 6 | ▶ | and $self->{$self->command_execute_return_method_name} = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | [$call_required_method->($cmd_plugin, $exec_fun, \@ARGV, $self->command_chain)]; | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | else | ||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||
| 207 | $self->command_execute_from_new | ||||||||||||||||||||||||||||
| 208 | 25 | ▶ | and $self->{$self->command_execute_return_method_name} = | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | [$call_indirect_method->($self, "command_execute_method_name", \@ARGV, $self->command_chain)]; | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 50 | return $self; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # XXX should be an r/w attribute - can be renamed on loading ... | ||||||||||||||||||||||||||||
| 217 | 48 | ▶ | sub execute_return { $_[0]->{execute_return} } | ||||||||||||||||||||||||||
|
Subroutine
execute_return: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 8 | 1; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||