blib/lib/Command/Shell.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 56 SCAR 24.8
CC
3
Cov
0%
execute
12.0
help_synopsis
2.0
run
2.0
CRAP 12.0
1 package Command::Shell;
2 use strict;
3 use warnings;
4 use Command::V2;
5  
6 class Command::Shell {
7 is => 'Command::V2',
8 is_abstract => 1,
9 subclassify_by => "_shell_command_subclass",
10 has_input => [
11 delegate_type => { is => 'Text', shell_args_position => 1,
12 doc => 'the class name of the command to be executed' },
13  
14 argv => { is => 'Text', is_many => 1, is_optional => 1, shell_args_position => 2,
15 doc => 'list of command-line arguments to be translated into parameters' },
16 ],
17 has_transient => [
18 delegate => { is => 'Command',
19 doc => 'the command which this adaptor wraps' },
20 _shell_command_subclass => { calculate_from => ['delegate_type'],
21 calculate =>
22 sub {
23 my $delegate_type = shift;
24 my $subclass = $delegate_type . "::Shell";
25 eval "$subclass->class";
26 if ($@) {
27 my $new_subclass = UR::Object::Type->define(
28 class_name => $subclass,
29 is => __PACKAGE__
30 );
31 die "Failed to fabricate subclass $subclass!" unless $new_subclass;
32 }
33 return $subclass;
34 },
35 },
36 ],
37 has_output => [
38 exit_code => => { is => 'Number',
39 doc => 'the exit code to be returned to the shell', }
40 ],
41 doc => 'an adaptor to create and run commands as specified from a standard command-line shell (bash)'
42 };
43  
44 sub help_synopsis {
45 return <<EOS
46  
47 In the "foo" executable:
48  
49 #!/usr/bin/env perl
50 use Foo;
51 exit Command::Shell->run("Foo",@ARGV);
52  
53 The run() static method will construct the appropriate Command::Shell object, have it build its delegate,
54 run the delegate's execution method in an in-memory transaction sandbox, and capture an exit code.
55  
56 If the correct environment variables are set, it will respond to a bash tab-completion request, such that
57 the "foo" script can be used as a self-completer.
58  
59 EOS
60  
61 }
62  
63 sub run {
64 my $class = shift;
65 my $delegate_type = shift;
66 my @argv = @_;
67 my $cmd = $class->create(delegate_type => $delegate_type, argv => \@argv);
68 #print STDERR "created $cmd\n";
69 $cmd->execute;
70 my $exit_code = $cmd->exit_code;
71 $cmd->delete;
72 return $exit_code;
73 }
74  
75 sub execute {
76 my $self = shift;
77 my $delegate_type = $self->delegate_type;
78 eval "use above '$delegate_type'";
79 if ($@) {
80 my $t = UR::Object::Type->get($delegate_type);
81 unless ($t) {
82 die "Failure to use delegate class $delegate_type!:\n$@";
83 }
84 }
85 my @argv = $self->argv;
86  
87 my $exit_code = $delegate_type->_cmdline_run(@argv);
88 $self->exit_code($exit_code);
89 return 1;
90 }
91  
92 # TODO: migrate all methods in Command::V2 which live in the Command::Dispatch::Shell module to this package
93 # Methods which address $self to get to shell-specific things still call $self
94 # Methods which address $self to get to the underlying command should instead call $self->delegate
95  
96 1;
97