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 13 25 52.0
pod 0 1 0.0
total 97 152 63.8


line stmt bran cond sub pod time code
1             package App::spaceless;
2              
3 2     2   36484 use strict;
  2         5  
  2         53  
4 2     2   11 use warnings;
  2         4  
  2         61  
5 2     2   47 use 5.010001;
  2         7  
6 2     2   8 use Config;
  2         4  
  2         90  
7 2     2   2807 use Shell::Guess;
  2         4467  
  2         66  
8 2     2   1728 use Shell::Config::Generate qw( win32_space_be_gone );
  2         5797  
  2         119  
9 2     2   2384 use Getopt::Long qw( GetOptions );
  2         23537  
  2         10  
10 2     2   2303 use Pod::Usage qw( pod2usage );
  2         108346  
  2         2040  
11              
12             # ABSTRACT: Convert PATH type environment variables to spaceless versions
13             our $VERSION = '0.07'; # VERSION
14              
15              
16             sub _running_shell
17             {
18 3     3   2504 state $shell;
19 3 100       20 $shell = Shell::Guess->running_shell unless defined $shell;
20 3         1644 $shell;
21             }
22              
23             sub main
24             {
25 6     6 0 84276 shift;
26 6         22 local @ARGV = @_;
27 6         14 my $shell;
28             my $file;
29 0         0 my $help;
30 0         0 my $version;
31 0         0 my $trim;
32 6         10 my $cygwin = 1;
33 6         13 my $expand;
34             my $list;
35 0         0 my $squash;
36              
37 6         62 my $config = Shell::Config::Generate->new;
38 6         109 $config->echo_off;
39 6         103 my $sep = quotemeta $Config{path_sep};
40            
41             GetOptions(
42 0     0   0 'csh' => sub { $shell = Shell::Guess->c_shell },
43 2     2   2656 '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       263 '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       4767 if($help)
64             {
65 0         0 pod2usage({ -verbose => 2 });
66             }
67            
68 6 100       19 if($version)
69             {
70 2   50     99 say 'App::spaceless version ', ($App::spaceless::VERSION // 'dev');
71 2         22 return 1;
72             }
73              
74 4 100       18 $shell = _running_shell() unless defined $shell;
75            
76 4 50 33 4   30 my $filter = $^O eq 'cygwin' && $shell->is_win32 ? sub { map { Cygwin::posix_to_win_path($_) } @_ } : sub { @_ };
  0         0  
  0         0  
  4         35  
77              
78 4 100       20 @ARGV = ('PATH') unless @ARGV;
79              
80 4 50   0   23 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   4   21 my $mutator = $expand ? sub { map { $to_long->($_) } @_ } : sub { win32_space_be_gone(grep { -e $_ } @_) };
  0         0  
  0         0  
  4         6  
  24         420  
83              
84 4         11 foreach my $var (@ARGV)
85             {
86             my @path = $filter->($mutator->(
87 25 100       93 grep { $trim ? -d $_ : 1 }
88 25 50       49 grep { $cygwin ? 1 : $_ =~ qr{^([A-Za-z]:|/cygdrive/[A-Za-z])} }
89 4   50     40 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         21 $config->set_path( $var => @path );
99 4 50       50 do { say $_ for @path } if $list;
  0         0  
100             }
101              
102 4 50       12 unless($list)
103             {
104 4 100       31 if(defined $file)
105             {
106 1         6 $config->generate_file($shell, $file);
107             }
108             else
109             {
110 3         23 print $config->generate($shell);
111             }
112             }
113            
114 4         1186 return 0;
115             }
116              
117             1;
118              
119             __END__