File Coverage

blib/lib/Pinto/Role/Installer.pm
Criterion Covered Total %
statement 30 31 96.7
branch 5 10 50.0
condition n/a
subroutine 8 8 100.0
pod n/a
total 43 49 87.7


line stmt bran cond sub pod time code
1             # ABSTRACT: Something that installs packages
2              
3             package Pinto::Role::Installer;
4              
5 2     2   1576 use Moose::Role;
  2         6  
  2         20  
6 2     2   10811 use MooseX::Types::Moose qw(Str HashRef Maybe);
  2         6  
  2         24  
7 2     2   9484 use MooseX::MarkAsMethods ( autoclean => 1 );
  2         5  
  2         20  
8              
9 2     2   6943 use Path::Class qw(dir);
  2         5  
  2         131  
10 2     2   12 use File::Which qw(which);
  2         5  
  2         86  
11              
12 2     2   12 use Pinto::Util qw(throw mask_uri_passwords);
  2         7  
  2         108  
13 2     2   12 use Pinto::Constants qw($PINTO_MINIMUM_CPANM_VERSION);
  2         3  
  2         1254  
14              
15             #-----------------------------------------------------------------------------
16              
17             our $VERSION = '0.14'; # VERSION
18              
19             #-----------------------------------------------------------------------------
20              
21             has cpanm_options => (
22             is => 'ro',
23             isa => HashRef [ Maybe [Str] ],
24             default => sub { {} },
25             lazy => 1,
26             );
27              
28             has cpanm_exe => (
29             is => 'ro',
30             isa => Str,
31             builder => '_build_cpanm_exe',
32             lazy => 1,
33             );
34              
35             #-----------------------------------------------------------------------------
36              
37             requires qw( execute targets mirror_uri );
38              
39             #-----------------------------------------------------------------------------
40              
41             with qw( Pinto::Role::Plated );
42              
43             #-----------------------------------------------------------------------------
44              
45             sub _build_cpanm_exe {
46 8     8   24 my ($self) = @_;
47              
48             return dir( $ENV{PINTO_HOME} )->subdir('sbin')->file('cpanm')->stringify
49 8 50       30 if $ENV{PINTO_HOME};
50              
51 8 50       63 my $cpanm_exe = which('cpanm')
52             or throw 'Could not find cpanm in PATH';
53              
54 8         1117 my $cpanm_version_cmd = "$cpanm_exe --version";
55 8         1109870 my $cpanm_version_cmd_output = qx{$cpanm_version_cmd}; ## no critic qw(Backtick)
56 8 50       197 throw "Could not learn version of cpanm: $!" if $?;
57              
58 8 50       313 my ($cpanm_version) = $cpanm_version_cmd_output =~ m{version ([\d.]+)}
59             or throw "Could not parse cpanm version number from $cpanm_version_cmd_output";
60              
61 8 50       226 if ( $cpanm_version < $PINTO_MINIMUM_CPANM_VERSION ) {
62 0         0 throw "Your cpanm ($cpanm_version) is too old. Must have $PINTO_MINIMUM_CPANM_VERSION or newer";
63             }
64              
65 8         899 return $cpanm_exe;
66             }
67              
68             #-----------------------------------------------------------------------------
69              
70             after execute => sub {
71             my ($self) = @_;
72              
73             # Wire cpanm to our repo
74             my $opts = $self->cpanm_options;
75             $opts->{mirror} = $self->mirror_uri;
76             $opts->{'mirror-only'} = '';
77              
78             # Process other cpanm options
79             my @cpanm_opts;
80             for my $opt ( keys %{$opts} ) {
81             my $dashes = ( length $opt == 1 ) ? '-' : '--';
82             my $dashed_opt = $dashes . $opt;
83             my $opt_value = $opts->{$opt};
84             push @cpanm_opts, $dashed_opt;
85             push @cpanm_opts, $opt_value if defined $opt_value && length $opt_value;
86             }
87              
88             # Scrub passwords from the command so they don't appear in the logs
89             my @sanitized_cpanm_opts = map { mask_uri_passwords($_) } @cpanm_opts;
90             $self->info( join ' ', 'Running:', $self->cpanm_exe, @sanitized_cpanm_opts );
91              
92             # Run cpanm
93             0 == system( $self->cpanm_exe, @cpanm_opts, $self->targets )
94             or throw "Installation failed. See the cpanm build log for details";
95             };
96              
97             #-----------------------------------------------------------------------------
98             1;
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =for :stopwords Jeffrey Ryan Thalhammer
107              
108             =head1 NAME
109              
110             Pinto::Role::Installer - Something that installs packages
111              
112             =head1 VERSION
113              
114             version 0.14
115              
116             =head1 AUTHOR
117              
118             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
119              
120             =head1 COPYRIGHT AND LICENSE
121              
122             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
123              
124             This is free software; you can redistribute it and/or modify it under
125             the same terms as the Perl 5 programming language system itself.
126              
127             =cut