File Coverage

blib/lib/App/spaceless.pm
Criterion Covered Total %
statement 60 87 68.9
branch 21 32 65.6
condition 3 7 42.8
subroutine 12 25 48.0
pod 0 1 0.0
total 96 152 63.1


line stmt bran cond sub pod time code
1             package App::spaceless;
2              
3 2     2   29490 use strict;
  2         5  
  2         88  
4 2     2   11 use warnings;
  2         3  
  2         65  
5 2     2   35 use 5.010001;
  2         7  
  2         73  
6 2     2   9 use Config;
  2         3  
  2         83  
7 2     2   1209 use Shell::Guess;
  2         5164  
  2         80  
8 2     2   1412 use Shell::Config::Generate qw( win32_space_be_gone );
  2         43607  
  2         189  
9 2     2   1568 use Getopt::Long qw( GetOptions );
  2         18218  
  2         11  
10 2     2   1622 use Pod::Usage qw( pod2usage );
  2         94425  
  2         2424  
11              
12             # ABSTRACT: Convert PATH type environment variables to spaceless versions
13             our $VERSION = '0.06'; # VERSION
14              
15              
16             sub _running_shell
17             {
18 3     3   2584 state $shell;
19 3 100       18 $shell = Shell::Guess->running_shell unless defined $shell;
20 3         1335 $shell;
21             }
22              
23             sub main
24             {
25 6     6 0 81614 shift;
26 6         33 local @ARGV = @_;
27 6         16 my $shell;
28             my $file;
29 0         0 my $help;
30 0         0 my $version;
31 0         0 my $trim;
32 6         12 my $cygwin = 1;
33 6         11 my $expand;
34             my $list;
35 0         0 my $squash;
36              
37 6         75 my $config = Shell::Config::Generate->new;
38 6         82 $config->echo_off;
39 6         146 my $sep = quotemeta $Config{path_sep};
40            
41             GetOptions(
42 0     0   0 'csh' => sub { $shell = Shell::Guess->c_shell },
43 2     2   2622 'sh' => sub { $shell = Shell::Guess->bourne_shell },
44 0     0   0 'cmd' => sub { $shell = Shell::Guess->cmd_shell },
45 0     0   0 'command' => sub { $shell = Shell::Guess->command_shell },
46 0     0   0 'fish' => sub { $shell = Shell::Guess->fish_shell },
47 0     0   0 'korn' => sub { $shell = Shell::Guess->korn_shell },
48 0     0   0 'power' => sub { $shell = Shell::Guess->power_shell },
49 0     0   0 'login' => sub { $shell = Shell::Guess->login_shell },
50 0     0   0 'sep=s' => sub { $sep = quotemeta $_[1]; $config->set_path_sep($_[1]) },
  0         0  
51 0     0   0 'sep-in=s' => sub { $sep = quotemeta $_[1] },
52 0     0   0 'sep-out=s' => sub { $config->set_path_sep($_[1]) },
53             'squash|s' => \$squash,
54 0 0   0   0 'no-cygwin' => sub { $cygwin = 0 if $^O eq 'cygwin' },
55 6 50       295 'list|l' => \$list,
56             'expand|x' => \$expand,
57             'trim|t' => \$trim,
58             'f=s' => \$file,
59             'help|h' => \$help,
60             'version|v' => \$version,
61             ) || pod2usage(1);
62              
63 6 50       4762 if($help)
64             {
65 0         0 pod2usage({ -verbose => 2 });
66             }
67            
68 6 100       18 if($version)
69             {
70 2   50     101 say 'App::spaceless version ', ($App::spaceless::VERSION // 'dev');
71 2         27 return 1;
72             }
73              
74 4 100       15 $shell = _running_shell() unless defined $shell;
75            
76 4 50 33 4   46 my $filter = $^O eq 'cygwin' && $shell->is_win32 ? sub { map { Cygwin::posix_to_win_path($_) } @_ } : sub { @_ };
  0         0  
  0         0  
  4         44  
77              
78 4 100       20 @ARGV = ('PATH') unless @ARGV;
79              
80 4 50   0   27 my $to_long = $^O eq 'cygwin' ? sub { Cygwin::win_to_posix_path(Win32::GetLongPathName(Cygwin::posix_to_win_path($_))) } : sub { Win32::GetLongPathName($_[0]) };
  0         0  
  0         0  
81              
82 4 50   0   23 my $mutator = $expand ? sub { map { $to_long->($_) } @_ } : sub { win32_space_be_gone(@_) };
  0         0  
  0         0  
  4         33  
83              
84 4         13 foreach my $var (@ARGV)
85             {
86 25 100       84 my @path = $filter->($mutator->(
87 25 50       47 grep { $trim ? -d $_ : 1 }
88 4   50     50 grep { $cygwin ? 1 : $_ =~ qr{^([A-Za-z]:|/cygdrive/[A-Za-z])} }
89             split /$sep/, $ENV{$var} // ''
90             ));
91            
92 4 50       17 if($squash)
93             {
94 0         0 my %path;
95 0         0 @path = grep { !$path{$_}++ } @path;
  0         0  
96             }
97            
98 4         23 $config->set_path( $var => @path );
99 4 50       61 do { say $_ for @path } if $list;
  0         0  
100             }
101              
102 4 50       16 unless($list)
103             {
104 4 100       13 if(defined $file)
105             {
106 1         7 $config->generate_file($shell, $file);
107             }
108             else
109             {
110 3         46 print $config->generate($shell);
111             }
112             }
113            
114 4         5478 return 0;
115             }
116              
117             1;
118              
119             __END__