File Coverage

blib/lib/App/EditorTools/CommandBase/Install.pm
Criterion Covered Total %
statement 39 52 75.0
branch 4 14 28.5
condition 3 5 60.0
subroutine 11 13 84.6
pod 1 1 100.0
total 58 85 68.2


line stmt bran cond sub pod time code
1             package App::EditorTools::CommandBase::Install;
2              
3             # ABSTRACT: Base class for installing bindings for editors
4              
5 7     7   29769 use strict;
  7         113  
  7         251  
6 7     7   39 use warnings;
  7         13  
  7         196  
7              
8 7     7   43 use File::Basename;
  7         20  
  7         802  
9 7     7   39 use File::Path qw(mkpath);
  7         13  
  7         438  
10 7     7   7162 use File::Slurp;
  7         116629  
  7         728  
11 7     7   7597 use File::ShareDir qw(dist_file);
  7         61326  
  7         719  
12 7     7   75 use App::Cmd::Setup -command;
  7         18  
  7         90  
13              
14             our $VERSION = '1.00';
15              
16             sub execute {
17 8     8 1 68 my ( $self, $opt, $arg ) = @_;
18              
19 8         55 print STDERR "Installing script to:\n";
20 8   100     180 print STDERR $opt->{dest} || 'STDOUT';
21 8         160 print STDERR "\n";
22              
23 8 100       165 return if $opt->{dryrun};
24              
25 2 50       9 if ( $opt->{dest} ) {
26 0         0 $self->_mkdir( $opt->{dest} );
27              
28             # TODO: overwriting?
29 0 0       0 open my $fh, ">", $opt->{dest}
30             or die "Unable to write to $opt->{dest}: $!";
31 0         0 $opt->{dest} = $fh;
32             }
33              
34 2   33     40 $self->_print( $opt->{dest} || *STDOUT );
35 2         446 return;
36             }
37              
38             sub _print {
39 2     2   8 my ( $self, $fh ) = @_;
40              
41 2         14 return print $fh $self->_get_script( $self->_script );
42             }
43              
44             sub _get_script {
45 2     2   7 my ( $self, $script ) = @_;
46              
47 2         138 my $file = File::Spec->catfile( dirname( $INC{'App/EditorTools.pm'} ),
48             qw( .. .. share ), $script );
49              
50 2 50       124 $file = dist_file( 'App-EditorTools', $script )
51             unless -r $file;
52              
53 2         562 return $self->_intro . read_file($file);
54             }
55              
56             sub _mkdir {
57 0     0   0 my ( $self, $path ) = @_;
58              
59 0         0 my $dir = dirname $path;
60              
61 0 0       0 unless ( -d $dir ) {
62 0 0       0 mkpath($dir)
63             || die "Unable to create directory $dir: $!\n";
64             }
65              
66 0         0 return 1;
67             }
68              
69             sub _count {
70 0     0   0 my (@a) = @_;
71              
72 0         0 my $total = 0;
73 0         0 for my $a (@a) {
74 0 0       0 $total += 1 if defined $a;
75             }
76              
77 0         0 return $total;
78             }
79              
80             sub _confirm_one_opt {
81 10     10   24 my ( $self, $opt ) = @_;
82              
83 10         102 my %hash = %$opt;
84 10         44 return grep( { defined $_ } @hash{qw{dest local global print}} ) <= 1;
  40         160  
85             }
86              
87             1;
88              
89             __END__