Commandable/Finder/Packages.pm

filter: Statementstmt 100% 58 / 58 Branchbran 93.7% 15 / 16 Conditioncond 60.0% 12 / 20 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 12 / 12 Podpod 75.0% 3 / 4 totaltotal 84.3% 102 / 121 CC 19 SCAR 19.1
CC
19
Cov
90%
_commands
10.0
new
9.9
packages
2.0
CRAP 6.7
« Commandable/Finder.pm ↑ Summary Commandable/Finder/SubAttributes/Attrs.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019-2024 -- leonerd@leonerd.org.uk
5  
6 package Commandable::Finder::Packages 0.14;
7  
8 9 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use experimental qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use base qw( Commandable::Finder );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use Commandable::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<Commandable::Finder::Packages> - find commands stored per package
21  
22 =head1 SYNOPSIS
23  
24 use Commandable::Finder::Packages;
25  
26 my $finder = Commandable::Finder::Packages->new(
27 base => "MyApp::Command",
28 );
29  
30 my $help_command = $finder->find_command( "help" );
31  
32 foreach my $command ( $finder->find_commands ) {
33 ...
34 }
35  
36 =head1 DESCRIPTION
37  
38 This implementation of L<Commandable::Finder> looks for implementations of
39 commands, where each command is implemented by a different package somewhere
40 in the symbol table.
41  
42 This class uses L<Module::Pluggable> to load packages from the filesystem.
43 As commands are located per package (and not per file), the application can
44 provide special-purpose internal commands by implementing more packages in the
45 given namespace, regardless of which files they come from.
46  
47 =head1 CONSTANTS
48  
49 package My::App::Commands::example;
50  
51 use constant COMMAND_NAME => "example";
52 use constant COMMAND_DESC => "an example of a command";
53  
54 ...
55  
56 Properties about each command are stored as methods (usually constant methods)
57 within each package. Often the L<constant> pragma module is used to create
58 them.
59  
60 The following constant names are used by default:
61  
62 =head2 COMMAND_NAME
63  
64 use constant COMMAND_NAME => "name";
65  
66 Gives a string name for the command.
67  
68 =head2 COMMAND_DESC
69  
70 use constant COMMAND_DESC => "description";
71  
72 Gives a string description for the command.
73  
74 =head2 COMMAND_ARGS
75  
76 use constant COMMAND_ARGS => (
77 { name => "argname", description => "description" },
78 );
79  
80 Gives a list of command argument specifications. Each specification is a HASH
81 reference corresponding to one positional argument, and should contain keys
82 named C<name>, C<description>, and optionally C<optional>.
83  
84 =head2 COMMAND_OPTS
85  
86 use constant COMMAND_OPTS => (
87 { name => "optname", description => "description" },
88 );
89  
90 Gives a list of command option specifications. Each specification is a HASH
91 reference giving one named option, in no particular order, and should contain
92 keys named C<name>, C<description> and optionally C<mode>, C<multi> and
93 C<default>.
94  
95 =cut
96  
97 =head1 CONSTRUCTOR
98  
99 =cut
100  
101 =head2 new
102  
103 $finder = Commandable::Finder::Packages->new( %args )
104  
105 Constructs a new instance of C<Commandable::Finder::Packages>.
106  
107 Takes the following named arguments:
108  
109 =over 4
110  
111 =item base => STR
112  
113 The base of the package namespace to look inside for packages that implement
114 commands.
115  
116 =item name_method => STR
117  
118 Optional. Gives the name of the method inside each command package to invoke
119 to generate the name of the command. Default C<COMMAND_NAME>.
120  
121 =item description_method => STR
122  
123 Optional. Gives the name of the method inside each command package to invoke
124 to generate the description text of the command. Default C<COMMAND_DESC>.
125  
126 =item arguments_method => STR
127  
128 Optional. Gives the name of the method inside each command package to invoke
129 to generate a list of argument specifications. Default C<COMMAND_ARGS>.
130  
131 =item options_method => STR
132  
133 Optional. Gives the name of the method inside each command package to invoke
134 to generate a list of option specifications. Default C<COMMAND_OPTS>.
135  
136 =item code_method => STR
137  
138 Optional. Gives the name of the method inside each command package which
139 implements the actual command behaviour. Default C<run>.
140  
141 =item named_by_package => BOOL
142  
143 Optional. If true, the name of each command will be taken from its package
144 name. with the leading C<base> string removed. If absent or false, the
145 C<name_method> will be used instead.
146  
147 =back
148  
149 If either name or description method are missing from a package, that package
150 is silently ignored.
151  
152 Any additional arguments are passed to the C<configure> method to be used as
153 configuration options.
154  
155 =cut
156  
157 10 ▶sub new ( $class, %args )
Subroutine new: called CC=9 SCAR=22.9
Pod documented
158 {
159 10 ▶ my $base = ( delete $args{base} ) or croak "Require 'base'";
Branch: 1 branch
BranchTrueFalseTotal
my $base = delete $args{"base"} or croak("Require 'base'") 10 0 10
160  
161 10 ▶ my $name_method = ( delete $args{name_method} ) // "COMMAND_NAME";
Condition50%1/2
delete $args{"name_method"} // "COMMAND_NAME"
l!l
010
MC/DC0%0/1
delete $args{"name_method"} // "COMMAND_NAME"
delete $args{"name_method"}
Truth table50%1/2
A
A= delete $args{"name_method"}
Aresult
11
00
162 10 ▶ my $description_method = ( delete $args{description_method} ) // "COMMAND_DESC";
Condition50%1/2
delete $args{"description_method"} // "COMMAND_DESC"
l!l
010
MC/DC0%0/1
delete $args{"description_method"} // "COMMAND_DESC"
delete $args{"description_method"}
Truth table50%1/2
A
A= delete $args{"description_method"}
Aresult
11
00
163 10 ▶ my $arguments_method = ( delete $args{arguments_method} ) // "COMMAND_ARGS";
Condition50%1/2
delete $args{"arguments_method"} // "COMMAND_ARGS"
l!l
010
MC/DC0%0/1
delete $args{"arguments_method"} // "COMMAND_ARGS"
delete $args{"arguments_method"}
Truth table50%1/2
A
A= delete $args{"arguments_method"}
Aresult
11
00
164 10 ▶ my $options_method = ( delete $args{options_method} ) // "COMMAND_OPTS";
Condition50%1/2
delete $args{"options_method"} // "COMMAND_OPTS"
l!l
010
MC/DC0%0/1
delete $args{"options_method"} // "COMMAND_OPTS"
delete $args{"options_method"}
Truth table50%1/2
A
A= delete $args{"options_method"}
Aresult
11
00
165 10 ▶ my $code_method = ( delete $args{code_method} ) // "run"; # App-csvtool
Condition50%1/2
delete $args{"code_method"} // "run"
l!l
010
MC/DC0%0/1
delete $args{"code_method"} // "run"
delete $args{"code_method"}
Truth table50%1/2
A
A= delete $args{"code_method"}
Aresult
11
00
166  
167 10 ▶ undef $name_method if delete $args{named_by_package};
Branch: 1 branch
BranchTrueFalseTotal
if delete $args{"named_by_package"} 1 9 10
168  
169 10 my $mp = Module::Pluggable::Object->new(
170 search_path => $base,
171 require => 1,
172 );
173  
174 10 my $self = bless {
175 mp => $mp,
176 base => $base,
177 methods => {
178 name => $name_method,
179 desc => $description_method,
180 args => $arguments_method,
181 opts => $options_method,
182 code => $code_method,
183 },
184 }, $class;
185  
186 10 ▶ $self->configure( %args ) if %args;
Branch: 1 branch
BranchTrueFalseTotal
if %args 4 6 10
187  
188 10 return $self;
189 }
190  
191 10 ▶sub packages ( $self )
Subroutine packages: called CC=2 SCAR=7.1
Pod undocumented
192 {
193 10 my $name_method = $self->{methods}{name};
194  
195 10 ▶ my $packages = $self->{cache_packages} //= [ $self->{mp}->plugins ];
Condition50%1/2
$self->{"cache_packages"} //= [$self->{"mp"}->plugins]
l!l
010
MC/DC0%0/1
$self->{"cache_packages"} //= [$self->{"mp"}->plugins]
$self->{"cache_packages"}
Truth table50%1/2
A
A= $self->{"cache_packages"}
Aresult
11
00
196  
197 10 return @$packages;
198 }
199  
200 38 ▶sub _commands ( $self )
Subroutine _commands: called CC=10 SCAR=23.0
201 {
202 38 my $name_method = $self->{methods}{name};
203 38 ▶ return $self->{cache_commands} //= do {
Condition66%2/3
$self->{"cache_commands"} //= do { my %commands; foreach my $pkg ($self->packages) { next if defined $name_method and not $pkg->can($name_method); my $name = defined $name_method ? $pkg->$name_method : $pkg =~ s/\Q$self->{"base"}\E:://ru; my $code = $pkg->can($self->{"methods"}{"code"}) or next; my $desc = ($pkg->can($self->{"methods"}{"desc"}) || (next))->($pkg); my $args; if (my $argsmeth = $pkg->can($self->{"methods"}{"args"})) { $args = [map({"Commandable::Command::_Argument"->new(%$_);} $pkg->$argsmeth)]; } ; my $opts; if (my $optsmeth = $pkg->can($self->{"methods"}{"opts"})) { $opts = {map({my $o = "Commandable::Command::_Option"->new(%$_); map {$_, $o;} $o->names;} $pkg->$optsmeth)}; } ; $commands{$name} = "Commandable::Command"->new("name", $name, "description", $desc, "arguments", $args, "options", $opts, "package", $pkg, "code", $code); } ; $self->add_builtin_commands(\%commands); \%commands }
l!l&&r!l&&!r
28100
MC/DC0%0/2
$self->{"cache_commands"} //= do { my %commands; foreach my $pkg ($self->packages) { next if defined $name_method and not $pkg->can($name_method); my $name = defined $name_method ? $pkg->$name_method : $pkg =~ s/\Q$self->{"base"}\E:://ru; my $code = $pkg->can($self->{"methods"}{"code"}) or next; my $desc = ($pkg->can($self->{"methods"}{"desc"}) || (next))->($pkg); my $args; if (my $argsmeth = $pkg->can($self->{"methods"}{"args"})) { $args = [map({"Commandable::Command::_Argument"->new(%$_);} $pkg->$argsmeth)]; } ; my $opts; if (my $optsmeth = $pkg->can($self->{"methods"}{"opts"})) { $opts = {map({my $o = "Commandable::Command::_Option"->new(%$_); map {$_, $o;} $o->names;} $pkg->$optsmeth)}; } ; $commands{$name} = "Commandable::Command"->new("name", $name, "description", $desc, "arguments", $args, "options", $opts, "package", $pkg, "code", $code); } ; $self->add_builtin_commands(\%commands); \%commands }
$self->{"cache_commands"} do { my %commands; foreach my $pkg ($self->packages) { next if defined $name_method and not $pkg->can($name_method); my $name = defined $name_method ? $pkg->$name_method : $pkg =~ s/\Q$self->{"base"}\E:://ru; my $code = $pkg->can($self->{"methods"}{"code"}) or next; my $desc = ($pkg->can($self->{"methods"}{"desc"}) || (next))->($pkg); my $args; if (my $argsmeth = $pkg->can($self->{"methods"}{"args"})) { $args = [map({"Commandable::Command::_Argument"->new(%$_);} $pkg->$argsmeth)]; } ; my $opts; if (my $optsmeth = $pkg->can($self->{"methods"}{"opts"})) { $opts = {map({my $o = "Commandable::Command::_Option"->new(%$_); map {$_, $o;} $o->names;} $pkg->$optsmeth)}; } ; $commands{$name} = "Commandable::Command"->new("name", $name, "description", $desc, "arguments", $args, "options", $opts, "package", $pkg, "code", $code); } ; $self->add_builtin_commands(\%commands); \%commands }
Truth table66%2/3
A //= B
A= $self->{"cache_commands"}B= do { my %commands; foreach my $pkg ($self->packages) { next if defined $name_method and not $pkg->can($name_method); my $name = defined $name_method ? $pkg->$name_method : $pkg =~ s/\Q$self->{"base"}\E:://ru; my $code = $pkg->can($self->{"methods"}{"code"}) or next; my $desc = ($pkg->can($self->{"methods"}{"desc"}) || (next))->($pkg); my $args; if (my $argsmeth = $pkg->can($self->{"methods"}{"args"})) { $args = [map({"Commandable::Command::_Argument"->new(%$_);} $pkg->$argsmeth)]; } ; my $opts; if (my $optsmeth = $pkg->can($self->{"methods"}{"opts"})) { $opts = {map({my $o = "Commandable::Command::_Option"->new(%$_); map {$_, $o;} $o->names;} $pkg->$optsmeth)}; } ; $commands{$name} = "Commandable::Command"->new("name", $name, "description", $desc, "arguments", $args, "options", $opts, "package", $pkg, "code", $code); } ; $self->add_builtin_commands(\%commands); \%commands }
ABresult
1X1
011
000
204 10 my %commands;
205 10 foreach my $pkg ( $self->packages ) {
206 20 ▶ next if defined $name_method and not $pkg->can( $name_method );
Branch: 1 branch
BranchTrueFalseTotal
if defined $name_method and not $pkg->can($name_method) 1 19 20
Condition100%3/3
defined $name_method and not $pkg->can($name_method)
!ll&&!rl&&r
3161
MC/DC100%2/2
defined $name_method and not $pkg->can($name_method)
defined $name_method not $pkg->can($name_method)
Truth table100%3/3
A and not(B)
A= defined $name_methodB= not $pkg->can($name_method)
ABresult
0X0
100
111
207  
208 19 ▶ my $name = defined $name_method
Branch: 1 branch
BranchTrueFalseTotal
defined $name_method ? : 16 3 19
209 ? $pkg->$name_method
210 : ( $pkg =~ s/\Q$self->{base}\E:://r );
211  
212 19 ▶ my $code = $pkg->can( $self->{methods}{code} ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $pkg->can($self->{"methods"}{"code"}) or next 18 1 19
213  
214 18 ▶ my $desc = ( $pkg->can( $self->{methods}{desc} ) or next )->( $pkg );
Condition50%1/2
$pkg->can($self->{'methods'}{'desc'}) || (next)
l!l
180
MC/DC0%0/1
$pkg->can($self->{'methods'}{'desc'}) || (next)
$pkg->can($self->{'methods'}{'desc'})
Truth table50%1/2
A
A= $pkg->can($self->{'methods'}{'desc'})
Aresult
11
00
215  
216 18 my $args;
217 18 ▶ if( my $argsmeth = $pkg->can( $self->{methods}{args} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $argsmeth = $pkg->can($self->{"methods"}{"args"})) 10 8 18
218 $args = [
219 10 map { Commandable::Command::_Argument->new( %$_ ) } $pkg->$argsmeth
220 ];
221 }
222  
223 18 my $opts;
224 18 ▶ if( my $optsmeth = $pkg->can( $self->{methods}{opts} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $optsmeth = $pkg->can($self->{"methods"}{"opts"})) 10 8 18
225 $opts = {
226 10 map { my $o = Commandable::Command::_Option->new( %$_ );
227 25 map { ( $_ => $o ) } $o->names
228 } $pkg->$optsmeth
229 };
230 }
231  
232 18 $commands{ $name } = Commandable::Command->new(
233 name => $name,
234 description => $desc,
235 arguments => $args,
236 options => $opts,
237  
238 package => $pkg,
239 code => $code,
240 );
241 }
242  
243 10 $self->add_builtin_commands( \%commands );
244  
245 10 \%commands;
246 };
247 }
248  
249 3 ▶sub find_commands ( $self )
Subroutine find_commands: called CC=1 SCAR=0.0
Pod documented
250 {
251 3 return values $self->_commands->%*;
252 }
253  
254 35 ▶sub find_command ( $self, $cmd )
Subroutine find_command: called CC=1 SCAR=0.0
Pod documented
255 {
256 35 return $self->_commands->{$cmd};
257 }
258  
259 =head1 AUTHOR
260  
261 Paul Evans <leonerd@leonerd.org.uk>
262  
263 =cut
264  
265 9 0x55AA;
« Commandable/Finder.pm ↑ Summary Commandable/Finder/SubAttributes/Attrs.pm »