File Coverage

blib/lib/CodeGen/Cpppp/Platform.pm
Criterion Covered Total %
statement 27 35 77.1
branch 7 12 58.3
condition 5 6 83.3
subroutine 7 8 87.5
pod n/a
total 46 61 75.4


line stmt bran cond sub pod time code
1             package CodeGen::Cpppp::Platform;
2              
3             our $VERSION = '0.005'; # VERSION
4             # ABSTRACT: Utility functions for abstracting the host OS
5              
6 3     3   253911 use v5.20;
  3         10  
7 3     3   16 use warnings;
  3         5  
  3         494  
8 3     3   17 use Carp;
  3         6  
  3         213  
9 3     3   474 use experimental 'signatures', 'lexical_subs', 'postderef';
  3         3284  
  3         20  
10 3     3   643 use Exporter 'import';
  3         10  
  3         2816  
11              
12             our @EXPORT_OK= qw( format_commandline );
13              
14              
15             # could use String::ShellQuote, but aiming for fewer deps
16             # TODO: move this to a platform-specific utility module
17             sub _unix_shellquote {
18 7 50   7   9368668 return "''" unless length $_[0];
19 7 50       74 return $_[0] unless $_[0] =~ m|[^-\w!%+,./:@^]|;
20 0         0 (my $x= $_[0]) =~ s/'/'\\''/g;
21 0         0 $x= "'$x'";
22 0         0 $x =~ s/^''//;
23 0         0 $x =~ s/''$//;
24 0         0 $x;
25             }
26              
27             # This utility function returns a string which could be passed to the
28             # shell to re-run the current command.
29             sub _unix_format_commandline {
30             # Make a map of which options have arguments
31 2     2   5 my %have_arg;
32             # This is defined by bin/cpppp. Use it, if available, else we don't know
33             # which options have arguments and everything goes to its own line.
34 2         12 for (keys %main::option_spec) {
35 22 100       67 /^([^=]+)=/ or next;
36 10         117 $have_arg{$_}= 1 for split '|', $1;
37             }
38 2         6 my @lines;
39 2 50       8 for (@main::original_argv? @main::original_argv : @ARGV) {
40 6         12 my $escaped= _unix_shellquote($_);
41 6 100 66     43 if (@lines && $lines[-1] =~ /^-+(.*)/ && $have_arg{$1}) {
      100        
42 2         8 $lines[-1] .= ' ' . $escaped;
43             } else {
44 4         12 push @lines, $escaped;
45             }
46             }
47 2         23 return $0 . ' ' . join(" \\\n ", @lines);
48             }
49              
50             sub _win32_format_commandline {
51 0     0     require Win32::ShellQuote;
52             # Not sure if Win32 can wrap a command, so just drop the whole thing on one line.
53 0 0         my @argv= ( $0, @main::original_argv? @main::original_argv : @ARGV );
54 0           return Win32::ShellQuote::quote_native(@argv);
55             }
56              
57             if ($^O eq 'Win32') {
58             *format_commandline= \&_win32_format_commandline;
59             } else {
60             *format_commandline= \&_unix_format_commandline;
61             }
62              
63             1;
64              
65             __END__