| 1 | package Padre::Plugin::Shell::Command; | ||
| 2 | 1 | ▶ | use base 'Padre::Plugin::Shell::Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | use 5.008; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Padre::Constant (); | ||
| 8 | use Padre::Current (); | ||
| 9 | use Padre::Wx (); | ||
| 10 | use File::Which; | ||
| 11 | |||
| 12 | our $VERSION = '0.27'; | ||
| 13 | |||
| 14 | my %actions = ( | ||
| 15 | append => 1, | ||
| 16 | replace => 2, | ||
| 17 | new => 3, | ||
| 18 | ); | ||
| 19 | |||
| 20 | sub plugin_menu { | ||
| 21 | my ($self) = @_; | ||
| 22 | my @menu = (); | ||
| 23 | push @menu, Wx::gettext("Run Command, Append\tAlt+Shift+R") => sub { $self->run_command( $actions{append} ) }; | ||
| 24 | push @menu, Wx::gettext("Run Command, Replace") => sub { $self->run_command( $actions{replace} ) }; | ||
| 25 | push @menu, Wx::gettext("Run Command, New") => sub { $self->run_command( $actions{new} ) }; | ||
| 26 | return @menu; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub get_cmd { | ||
| 30 | my ( $self, $editor ) = @_; | ||
| 31 | my %cmd; | ||
| 32 | |||
| 33 | if ( $editor->GetSelectedText() ) { | ||
| 34 | $cmd{cmd} = $editor->GetSelectedText(); | ||
| 35 | my $start_pos = $editor->GetSelectionStart(); | ||
| 36 | my $start_line = $editor->LineFromPosition($start_pos); | ||
| 37 | my $end_pos = $editor->GetSelectionEnd(); | ||
| 38 | my $end_line = $editor->LineFromPosition($end_pos); | ||
| 39 | $cmd{line_count} = $end_line - $start_line; | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | |||
| 43 | # the command is the current line | ||
| 44 | my $cmd_line = $editor->LineFromPosition( $editor->GetCurrentPos() ); | ||
| 45 | my $start_pos = $editor->PositionFromLine($cmd_line); | ||
| 46 | my $end_pos = $editor->GetLineEndPosition($cmd_line); | ||
| 47 | $editor->SetSelection( $start_pos, $end_pos ); | ||
| 48 | $cmd{cmd} = $editor->GetSelectedText(); | ||
| 49 | $cmd{line_count} = 1; | ||
| 50 | } | ||
| 51 | $cmd{has_shebang} = ( $cmd{cmd} =~ m/^\s*#!/ ) ? 1 : 0; | ||
| 52 | |||
| 53 | # is it a command or should/can it be wrapped in a cat block and run through sh? | ||
| 54 | unless ( $cmd{has_shebang} ) { | ||
| 55 | my ($test) = $cmd{cmd} =~ m/^\s*([^\s]+)/; | ||
| 56 | $test = ( split /[\/\\]/, $test )[-1]; | ||
| 57 | unless ( which($test) ) { | ||
| 58 | if ( which('cat') && which('sh') ) { | ||
| 59 | $cmd{cmd} = "#!/bin/sh\ncat <<EIEIOT\n" . $cmd{cmd} . "\nEIEIOT\n"; | ||
| 60 | $cmd{has_shebang} = 1; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | $cmd{cmd} = ''; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return %cmd; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub run_command { | ||
| 71 | my ( $self, $action ) = @_; | ||
| 72 | |||
| 73 | my $editor = Padre::Current->editor or return; | ||
| 74 | $editor->Freeze; | ||
| 75 | |||
| 76 | $self->update_environment_vars($editor); | ||
| 77 | |||
| 78 | my %cmd = $self->get_cmd($editor); | ||
| 79 | my @cmd_out; | ||
| 80 | |||
| 81 | if ( $cmd{has_shebang} || $cmd{line_count} != 1 ) { | ||
| 82 | my $filename = $self->get_temp_file(); | ||
| 83 | my $fh; | ||
| 84 | if ( open $fh, '>', $filename ) { | ||
| 85 | print $fh $cmd{cmd}; | ||
| 86 | close $fh; | ||
| 87 | `chmod u+x $filename`; | ||
| 88 | |||
| 89 | # Use shebang if there is one, otherwise not. | ||
| 90 | @cmd_out | ||
| 91 | = ( $cmd{has_shebang} ) | ||
| 92 | ? `./$filename 2>&1` | ||
| 93 | : `sh $filename 2>&1`; | ||
| 94 | |||
| 95 | # In case of errors, we don't want the temp filename | ||
| 96 | # showing up in the output as it is pretty ugly and | ||
| 97 | # doesn't add anything to the conversation. | ||
| 98 | if ($?) { | ||
| 99 | $_ =~ s/^[.\/]*$filename/ERR/ for @cmd_out; | ||
| 100 | } | ||
| 101 | |||
| 102 | $self->delete_temp_file($filename); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | |||
| 107 | # It's a one-liner | ||
| 108 | @cmd_out = `$cmd{cmd} 2>&1`; | ||
| 109 | } | ||
| 110 | |||
| 111 | if (@cmd_out) { | ||
| 112 | my $filename = $self->get_temp_file(); | ||
| 113 | my $fh; | ||
| 114 | if ( open $fh, '>', $filename ) { | ||
| 115 | print $fh @cmd_out; | ||
| 116 | close $fh; | ||
| 117 | |||
| 118 | if ( $action == $actions{replace} ) { | ||
| 119 | $self->replace_selection_from_file($filename); | ||
| 120 | } | ||
| 121 | elsif ( $action == $actions{append} ) { | ||
| 122 | $self->append_selection_from_file($filename); | ||
| 123 | } | ||
| 124 | elsif ( $action == $actions{new} ) { | ||
| 125 | $self->new_document_from_file( $filename, 'text/plain' ); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | $self->delete_temp_file($filename); | ||
| 129 | } | ||
| 130 | $editor->Thaw; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ | ||