Debian Codegraph @

lib/App/Yath/Command.pm

raw
1 package App::Yath::Command;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use File::Spec;
8 use Carp qw/croak/;
9 use Test2::Harness::Util qw/mod2file/;
10
11 use Test2::Harness::Util::HashBase qw/-settings -args/;
12
13 use App::Yath::Options();
14
15 use Test2::Harness::Util::File::JSON();
16
17 sub internal_only { 0 }
18 sub always_keep_dir { 0 }
19 sub summary { "No Summary" }
20 sub description { "No Description" }
21 sub group { "Z-UNFINISHED" }
22 sub doc_args { () }
23 sub only_cmd_opts { 0 }
24
25 sub handle_invalid_option { 0 }
26
27 sub munge_opts { }
28
29 sub name { $_[0] =~ m/([^:=]+)(?:=.*)?$/; $1 || $_[0] }
30
31 sub run {
32 my $self = shift;
33
34 warn "This command is currently empty";
35
36 return 1;
37 }
38
39 sub cli_help {
40 my $class = shift;
41 my %params = @_;
42
43 my $settings = $params{settings} // {};
44 my $script = $settings->harness->script // $0;
45
46 my $cmd = $class->name;
47 my (@args) = $class->doc_args;
48
49 my $options = $params{options};
50 unless ($options) {
51 $options = App::Yath::Options->new;
52 $options->set_command_class($class);
53 }
54
55 my ($pre_opts, $cmd_opts);
56 if ($options) {
57 $pre_opts = $options->pre_docs('cli');
58 $cmd_opts = $options->cmd_docs('cli');
59 }
60
61 my $usage = "Usage: $script";
62
63 my @out;
64
65 if ($pre_opts) {
66 $usage .= ' [YATH OPTIONS]';
67
68 $pre_opts =~ s/^/ /mg;
69 push @out => "[YATH OPTIONS]\n$pre_opts";
70 }
71
72 $usage .= " $cmd";
73
74 if ($cmd_opts) {
75 $usage .= " [COMMAND OPTIONS]";
76
77 $cmd_opts =~ s/^/ /mg;
78 push @out => "[COMMAND OPTIONS]\n$cmd_opts";
79 }
80
81 if (@args) {
82 $usage .= " [COMMAND ARGUMENTS]";
83
84 my @desc;
85 for my $arg (@args) {
86 if (ref($arg)) {
87 my ($name, $text) = @$arg;
88 push @desc => $name;
89 $text =~ s/^/ /mg;
90 push @desc => "$text\n";
91 }
92 else {
93 push @desc => "$arg\n";
94 }
95 }
96
97 my $desc = join "\n" => @desc;
98 $desc =~ s/^/ /mg;
99
100 push @out => "[COMMAND ARGUMENTS]\n$desc";
101 }
102
103 chomp(my $desc = $class->description);
104 unshift @out => ("$cmd - " . $class->summary, $desc, $usage);
105
106 return join("\n\n" => grep { $_ } @out) . "\n";
107 }
108
109 sub generate_pod {
110 my $class = shift;
111
112 my $cmd = $class->name;
113 my (@args) = $class->doc_args;
114
115 my $options = App::Yath::Options->new();
116 require App::Yath;
117 my $ay = App::Yath->new();
118 $options->include($ay->load_options);
119 $options->set_command_class($class);
120 my $pre_opts = $options->pre_docs('pod', 3);
121 my $cmd_opts = $options->cmd_docs('pod', 3);
122
123 my $usage = " \$ yath [YATH OPTIONS] $cmd";
124
125 my @head2s;
126
127 push @head2s => ("=head2 YATH OPTIONS", $pre_opts) if $pre_opts;
128
129 if ($cmd_opts) {
130 $usage .= " [COMMAND OPTIONS]";
131 push @head2s => ("=head2 COMMAND OPTIONS", $cmd_opts);
132 }
133
134 if (@args) {
135 $usage .= " [COMMAND ARGUMENTS]";
136
137 push @head2s => (
138 "=head2 COMMAND ARGUMENTS",
139 "=over 4",
140 (map { ref($_) ? ( "=item $_->[0]", $_->[1] ) : ("=item $_") } @args),
141 "=back"
142 );
143 }
144
145 my @out = (
146 "=head1 NAME",
147 "$class - " . $class->summary,
148 "=head1 DESCRIPTION",
149 $class->description,
150 "=head1 USAGE",
151 $usage,
152 @head2s
153 );
154
155 return join("\n\n" => grep { $_ } @out);
156 }
157
158 sub write_settings_to {
159 my $self = shift;
160 my ($dir, $file) = @_;
161
162 croak "'directory' is a required parameter" unless $dir;
163 croak "'filename' is a required parameter" unless $file;
164
165 my $settings = $self->settings;
166 my $settings_file = Test2::Harness::Util::File::JSON->new(name => File::Spec->catfile($dir, $file));
167 $settings_file->write($settings);
168 return $settings_file->name;
169 }
170
171 sub setup_resources {
172 my $self = shift;
173 my $settings = $self->settings;
174
175 return unless $settings->check_prefix('runner');
176 my $runner = $settings->runner;
177 my $res = $runner->resources or return;
178 return unless @$res;
179
180 for my $res (@$res) {
181 require(mod2file($res)) unless ref $res;
182 $res->setup($settings);
183 }
184 }
185
186 sub setup_plugins {
187 my $self = shift;
188 $_->setup($self->settings) for @{$self->settings->harness->plugins};
189 }
190
191 sub teardown_plugins {
192 my $self = shift;
193 my ($renderers, $logger) = @_;
194 $_->teardown($self->settings, $renderers, $logger) for @{$self->settings->harness->plugins};
195 }
196
197 sub finalize_plugins {
198 my $self = shift;
199 $_->finalize($self->settings) for @{$self->settings->harness->plugins};
200 }
201
202
203 1;
204
205 __END__
206
207 =pod
208
209 =encoding UTF-8
210
211 =head1 NAME
212
213 App::Yath::Command - Base class for yath commands
214
215 =head1 DESCRIPTION
216
217 This is the base class for any/all yath commands. If you wish to add a new yath
218 command you should subclass this package.
219
220 =head1 SYNOPSIS
221
222 package App::Yath::Command::mycommand;
223 use strict;
224 use warnings;
225
226 use App::Yath::Options();
227 use parent 'App::Yath::Command';
228
229 # Include existing option sets
230 include_options(
231 'App::Yath::Options::Debug',
232 'App::Yath::Options::PreCommand',
233 ...,
234 );
235
236 # Add some custom options
237 option_group {prefix => 'mycommand', category => 'mycommand options'} => sub {
238 option foo => (
239 description => "the foo option",
240 default => 0,
241 );
242 };
243
244 # This is used to sort/group commands in the "yath help" output
245 sub group { 'thirdparty' }
246
247 # Brief 1-line summary
248 sub summary { "This is a third party command, it does stuff..." }
249
250 # Longer description of the command (used in yath help mycommand)
251 sub description {
252 return <<" EOT";
253 This command does:
254 This
255 That
256 Those
257 EOT
258 }
259
260 # Entrypoint
261 sub run {
262 my $self = shift;
263
264 my $settings = $self->settings;
265 my $args = $self->args;
266
267 print "Hello Third Party!\n"
268
269 # Return an exit value.
270 return 0;
271 }
272
273 =head1 CLASS METHODS
274
275 =over 4
276
277 =item $string = $cmd_class->cli_help(settings => $settings, options => $options)
278
279 This method generates the command line help for any given command. In general
280 you will NOT want to override this.
281
282 $settings should be an instance of L<Test2::Harness::Settings>.
283
284 $options should be an instance of L<App::Yath::Options> if provided. This
285 method is usually capable of filling in the details when this is omitted.
286
287 =item $multi_line_string = $cmd_class->description()
288
289 Long-form description of the command. Used in C<cli_help()>.
290
291 =item @list = $cmd_class->doc_args()
292
293 A list of argument names to the command, used to generate documentation.
294
295 =item $string = $cmd_class->generate_pod()
296
297 This can be used to generate POD documentation from the command itself using
298 the other fields listed in this section, as well as all applicable command
299 lines options specified in the command.
300
301 =item $string = $cmd_class->group()
302
303 Used for sorting/grouping commands in the C<yath help> output.
304
305 Existing groups:
306
307 ' test' # Space in front to make sure test related command float up
308 'log' # Log processing commands
309 'persist' # Commands related to the persistent runner
310 'zinit' # The init command and related command sink to the bottom.
311
312 Unless your command OBVIOUSLY and CLEARLY belongs in one of the above groups
313 you should probably create your own. Please do not prefix it with a space to
314 make it float, C<' test'> is a special case, you are not that special.
315
316 =item $string = $cmd_class->name()
317
318 Name of the command. By default this is the last part of the package name. You
319 will probably never want to override this.
320
321 =item $short_string = $cmd_class->summary()
322
323 A short summary of what this command is.
324
325 =back
326
327 =head1 OBJECT METHODS
328
329 =over 4
330
331 =item $bool = $cmd->always_keep_dir()
332
333 By default the working directory is deleted when yath exits. Some commands such
334 as L<App::Yath::Command::start> need to keep the directory. Override this
335 method to return true if your command uses the workdir and needs to keep it.
336
337 =item $arrayref = $cmd->args()
338
339 Get an arrayref of command line arguments B<AFTER> options have been
340 process/removed.
341
342 =item $bool = $cmd->internal_only()
343
344 Set this to true if you do not want your command to show up in the help output.
345
346 =item $exit_code = $cmd->run()
347
348 This is the main entrypoint for the command. You B<MUST> override this. This
349 method should return an exit code.
350
351 =item $settings = $cmd->settings()
352
353 Get the settings as populated by the command line options.
354
355 =item $cmd->write_settings_to($directory, $filename)
356
357 A helper method to write the settings to a specified directory and filename.
358 File is written as JSON.
359
360 If you are subclassing another command such as L<App::Yath::Command::test> you
361 may want to override this to a no-op to prevent the settings file from being
362 written, the L<App::Yath::Command:run> command does this.
363
364 =back
365
366 =head1 SOURCE
367
368 The source code repository for Test2-Harness can be found at
369 F<http://github.com/Test-More/Test2-Harness/>.
370
371 =head1 MAINTAINERS
372
373 =over 4
374
375 =item Chad Granum E<lt>[email protected]<gt>
376
377 =back
378
379 =head1 AUTHORS
380
381 =over 4
382
383 =item Chad Granum E<lt>[email protected]<gt>
384
385 =back
386
387 =head1 COPYRIGHT
388
389 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
390
391 This program is free software; you can redistribute it and/or
392 modify it under the same terms as Perl itself.
393
394 See F<http://dev.perl.org/licenses/>
395
396 =cut