| 1 | package Command::Common; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | |||
| 8 | # Once roles exist this should probably be a role. | ||
| 9 | class Command::Common { | ||
| 10 | is_abstract => 1, | ||
| 11 | valid_signals => [qw(error_die)], | ||
| 12 | }; | ||
| 13 | |||
| 14 | sub create { | ||
| 15 | my $class = shift; | ||
| 16 | my ($rule,%extra) = $class->define_boolexpr(@_); | ||
| 17 | my @params_list = $rule->params_list; | ||
| 18 | my $self = $class->SUPER::create(@params_list, %extra); | ||
| 19 | return unless $self; | ||
| 20 | |||
| 21 | # set non-optional boolean flags to false. | ||
| 22 | # TODO: rename that property meta method if it is not ONLY used for shell args | ||
| 23 | for my $property_meta ($self->_shell_args_property_meta) { | ||
| 24 | my $property_name = $property_meta->property_name; | ||
| 25 | if (!$property_meta->is_optional and !defined($self->$property_name)) { | ||
| 26 | if (defined $property_meta->data_type and $property_meta->data_type =~ /Boolean/i) { | ||
| 27 | $self->$property_name(0); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub shortcut { | ||
| 36 | my $self = shift; | ||
| 37 | return unless $self->can('_shortcut_body'); | ||
| 38 | |||
| 39 | my $result = $self->_shortcut_body; | ||
| 40 | $self->result($result); | ||
| 41 | |||
| 42 | return $result; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub execute { | ||
| 46 | # This is a wrapper for real execute() calls. | ||
| 47 | # All execute() methods are turned into _execute_body at class init, | ||
| 48 | # so this will get direct control when execute() is called. | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | #TODO handle calls to SUPER::execute() from another execute(). | ||
| 52 | |||
| 53 | # handle calls as a class method | ||
| 54 | my $was_called_as_class_method = 0; | ||
| 55 | if (ref($self)) { | ||
| 56 | if ($self->is_executed) { | ||
| 57 | Carp::confess("Attempt to re-execute an already executed command."); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | # called as class method | ||
| 62 | # auto-create an instance and execute it | ||
| 63 | $self = $self->create(@_); | ||
| 64 | return unless $self; | ||
| 65 | $was_called_as_class_method = 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | # handle __errors__ objects before execute | ||
| 69 | if (my @problems = $self->__errors__) { | ||
| 70 | for my $problem (@problems) { | ||
| 71 | my @properties = $problem->properties; | ||
| 72 | $self->error_message("Property " . | ||
| 73 | join(',', map { "'$_'" } @properties) . | ||
| 74 | ': ' . $problem->desc); | ||
| 75 | } | ||
| 76 | my $command_name = $self->command_name; | ||
| 77 | $self->error_message("Please see '$command_name --help' for more information."); | ||
| 78 | $self->delete() if $was_called_as_class_method; | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $result = eval { $self->_execute_body(@_); }; | ||
| 83 | my $error = $@; | ||
| 84 | if ($error or not $result) { | ||
| 85 | my %error_data; | ||
| 86 | |||
| 87 | $error_data{die_message} = defined($error) ? $error:''; | ||
| 88 | $error_data{error_message} = defined($self->error_message) ? $self->error_message:''; | ||
| 89 | $error_data{error_package} = defined($self->error_package) ? $self->error_package:''; | ||
| 90 | $error_data{error_file} = defined($self->error_file) ? $self->error_file:''; | ||
| 91 | $error_data{error_subroutine} = defined($self->error_subroutine) ? $self->error_subroutine:''; | ||
| 92 | $error_data{error_line} = defined($self->error_line) ? $self->error_line:''; | ||
| 93 | $self->__signal_observers__('error_die', %error_data); | ||
| 94 | die $error if $error; | ||
| 95 | } | ||
| 96 | |||
| 97 | $self->is_executed(1); | ||
| 98 | $self->result($result); | ||
| 99 | |||
| 100 | return $self if $was_called_as_class_method; | ||
| 101 | return $result; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _execute_body { | ||
| 105 | # default implementation in the base class | ||
| 106 | |||
| 107 | # Override "execute" or "_execute_body" to implement the body of the command. | ||
| 108 | # See above for details of internal implementation. | ||
| 109 | |||
| 110 | my $self = shift; | ||
| 111 | my $class = ref($self) || $self; | ||
| 112 | if ($class eq __PACKAGE__) { | ||
| 113 | die "The execute() method is not defined for $_[0]!"; | ||
| 114 | } | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | # Translates a true/false value from the command module's execute() | ||
| 119 | # from Perl (where positive means success), to shell (where 0 means success) | ||
| 120 | # Also, execute() could return a negative value; this is converted to | ||
| 121 | # positive and used as the shell exit code. NOTE: This means execute() | ||
| 122 | # returning 0 and -1 mean the same thing | ||
| 123 | sub exit_code_for_return_value { | ||
| 124 | my $self = shift; | ||
| 125 | my $return_value = shift; | ||
| 126 | if (! $return_value) { | ||
| 127 | $return_value = 1; | ||
| 128 | } elsif ($return_value < 0) { | ||
| 129 | $return_value = 0 - $return_value; | ||
| 130 | } else { | ||
| 131 | $return_value = 0 | ||
| 132 | } | ||
| 133 | return $return_value; | ||
| 134 | } |