File Coverage

blib/lib/ExtUtils/Builder/Action/Command.pm
Criterion Covered Total %
statement 34 34 100.0
branch 12 16 75.0
condition 2 4 50.0
subroutine 7 7 100.0
pod 3 3 100.0
total 58 64 90.6


line stmt bran cond sub pod time code
1             package ExtUtils::Builder::Action::Command;
2             $ExtUtils::Builder::Action::Command::VERSION = '0.020';
3 4     4   378680 use strict;
  4         11  
  4         154  
4 4     4   20 use warnings;
  4         12  
  4         273  
5              
6 4     4   531 use parent 'ExtUtils::Builder::Action::Primitive';
  4         330  
  4         28  
7              
8             sub _preference_map {
9             return {
10 2     2   26 command => 3,
11             execute => 2,
12             code => 1,
13             flatten => 0,
14             };
15             }
16              
17             sub to_code {
18 2     2 1 6 my ($self, %args) = @_;
19 2         604 require Data::Dumper;
20 2 100       6367 if (ref $self->{command}) {
21 1         7 my $serialized = Data::Dumper->new([$self->{command}])->Terse(1)->Indent(0)->Dump;
22 1         67 $serialized =~ s/ \A \[ (.*?) \] \z /$1/xms;
23 1         11 return qq{system($serialized) and die "Could not run command " . join ' ', $serialized};
24             } else {
25 1         19 my $serialized = Data::Dumper->new([$self->{command}])->Terse(1)->Indent(0)->Dump;
26 1         109 return qq{system($serialized) and die "Could not run command $serialized"};
27             }
28             }
29              
30             sub to_command {
31 2     2 1 651 my $self = shift;
32 2 100       21 return ref $self->{command} ? [ @{ $self->{command} } ] : $self->{command};
  1         8  
33             }
34              
35             my $quote = $^O eq 'MSWin32' ? do { require Win32::ShellQuote; \&Win32::ShellQuote::quote_system_list } : sub { @_ };
36             sub execute {
37 3     3 1 36 my ($self, %opts) = @_;
38 3 100       11 if (ref $self->{command}) {
39 2         4 my @command = @{ $self->{command} };
  2         7  
40 2 50       11 my $message = join ' ', map { my $arg = $_; $arg =~ s/ (?= ['#] ) /\\/gx ? "'$arg'" : $arg } @command;
  4         4  
  4         25  
41 2 100       9 print "$message\n" if not $opts{quiet};
42 2 50 50     10 system($quote->(@command)) and die "Could not run command @command" if not $opts{dry_run};
43             } else {
44 1         6 my $command = $self->{command};
45 1 50       6 print "$command\n" if not $opts{quiet};
46 1 50 50     4486 system($command) and die "Could not run command $command" if not $opts{dry_run};
47             }
48 3         138 return;
49             }
50              
51             1;
52              
53             #ABSTRACT: An action object for external commands
54              
55             __END__