File Coverage

blib/lib/Ixchel/Actions/perl.pm
Criterion Covered Total %
statement 17 115 14.7
branch 0 40 0.0
condition 0 6 0.0
subroutine 6 10 60.0
pod 2 4 50.0
total 25 175 14.2


line stmt bran cond sub pod time code
1             package Ixchel::Actions::perl;
2              
3 1     1   132884 use 5.006;
  1         5  
4 1     1   11 use strict;
  1         2  
  1         33  
5 1     1   5 use warnings;
  1         2  
  1         139  
6 1     1   537 use Ixchel::functions::perl_module_via_pkg;
  1         9  
  1         128  
7 1     1   16 use Ixchel::functions::install_cpanm;
  1         15  
  1         78  
8 1     1   7 use base 'Ixchel::Actions::base';
  1         8  
  1         55  
9              
10             =head1 NAME
11              
12             Ixchel::Actions::perl - Handles making sure desired Perl modules are installed as specified by the config.
13              
14             =head1 VERSION
15              
16             Version 0.4.0
17              
18             =cut
19              
20             our $VERSION = '0.4.0';
21              
22             =head1 CLI SYNOPSIS
23              
24             ixchel -a perl [B<--notest>] [B<--reinstall>] [B<--force>]
25              
26             =head1 CODE SYNOPSIS
27              
28             my $results=$ixchel->action(action=>'perl', opts=>{}'});
29              
30             if ($results->{ok}) {
31             print $results->{status_text};
32             }else{
33             die('Action errored... '.joined("\n", @{$results->{errors}}));
34             }
35              
36             =head1 DESCRIPTION
37              
38             The modules to be installed are determined by the config.
39              
40             - .perl.cpanm :: Always install cpanm even if all modules could
41             be installed via packages.
42             - Default :: 0
43              
44             - .perl.modules :: Array of modules to install.
45             - Default :: []
46              
47             - .perl.pkgs_always_try :: If .perl.modules should first try to be installed
48             via packages.
49             - Default :: 1
50              
51             - .perl.pkgs_optional :: An array of modules that can optionally be installed via
52             via packages. This is only meaningful if .perl.pkgs_always_try is set to 0,
53             meaning .perl.modules is only being handled via cpanm.
54             - Default :: []
55              
56             - .perl.pkgs_require :: An array of modules that must be install via packages and
57             will not later be tried via cpanm.
58             - Default :: []
59              
60             =head1 FLAGS
61              
62             =head2 --notest
63              
64             When calling cpanm, add --notest to it.
65              
66             =head2 --reinstall
67              
68             When calling cpanm, add --reinstall to it.
69              
70             =head2 --force
71              
72             When calling cpanm, add --force to it.
73              
74             =head1 RESULT HASH REF
75              
76             .errors :: A array of errors encountered.
77             .status_text :: A string description of what was done and the results.
78             .ok :: Set to zero if any of the above errored.
79              
80             =cut
81              
82       0 0   sub new_extra { }
83              
84             sub action_extra {
85 0     0 0   my $self = $_[0];
86              
87             # if we've already installed cpanm or not
88 0           my $installed_cpanm = 0;
89 0 0         if ( $self->{config}{perl}{cpanm} ) {
90 0           eval {
91 0           install_cpanm;
92 0           $installed_cpanm = 1;
93             };
94 0 0         if ($@) {
95 0           $self->status_add( status => 'Failed to install cpanm via packages ... ' . $@, error => 1 );
96             } else {
97 0           $self->status_add( status => 'cpanm installed' );
98             }
99             } ## end if ( $self->{config}{perl}{cpanm} )
100              
101             # a list of modules installed
102 0           my %installed;
103             my %tried_via_packages;
104              
105 0           $self->status_add( status => 'pkgs_require: ' . join( ',', @{ $self->{config}{perl}{pkgs_require} } ), );
  0            
106              
107             # handle ones that must be installed via pkgs
108 0           my @failed_pkg_required;
109             my @pkgs_installed;
110 0 0         if ( ref( $self->{config}{perl}{pkgs_require} ) eq 'ARRAY' ) {
111 0           $self->status_add( status => 'pkgs_require: ' . join( ',', @{ $self->{config}{perl}{pkgs_require} } ), );
  0            
112 0           foreach my $module ( @{ $self->{config}{perl}{pkgs_require} } ) {
  0            
113 0           my $status;
114 0           eval { $status = perl_module_via_pkg( module => $module ); };
  0            
115 0 0         if ($@) {
116 0           $self->status_add(
117             status => 'Failed to install ' . $module . ' via packages',
118             error => 1
119             );
120 0           $tried_via_packages{$module} = 1;
121 0           push( @failed_pkg_required, $module );
122             } else {
123 0           $self->{results}{status_text} = $self->{results}{status_text} . $status;
124 0           $self->status_add( status => $module . ' installed' );
125 0           $installed{$module} = 1;
126 0           push( @pkgs_installed, $module );
127             }
128             } ## end foreach my $module ( @{ $self->{config}{perl}{pkgs_require...}})
129             } ## end if ( ref( $self->{config}{perl}{pkgs_require...}))
130              
131             # a list of modules to install
132 0           my @modules;
133              
134             # handle ones that must be installed via pkgs
135 0 0         if ( ref( $self->{config}{perl}{pkgs_optional} ) eq 'ARRAY' ) {
136 0           $self->status_add( status => 'pkgs_optional: ' . join( ',', @{ $self->{config}{perl}{pkgs_require} } ), );
  0            
137 0           foreach my $module ( @{ $self->{config}{perl}{pkgs_optional} } ) {
  0            
138 0           my $status;
139 0           eval { $status = perl_module_via_pkg( module => $module ); };
  0            
140 0 0         if ($@) {
141             # not an error here as it using packages is optional and will be used later.
142 0           push( @modules, $module );
143 0           $self->status_add( status => 'Failed to install ' . $module . ' via packages', );
144 0           $tried_via_packages{$module} = 1;
145             } else {
146 0           $self->{results}{status_text} = $self->{results}{status_text} . $status;
147 0           $self->status_add( status => $module . ' installed' );
148 0           $installed{$module} = 1;
149 0           push( @pkgs_installed, $module );
150             }
151             } ## end foreach my $module ( @{ $self->{config}{perl}{pkgs_optional...}})
152             } ## end if ( ref( $self->{config}{perl}{pkgs_optional...}))
153              
154 0 0         if ( ref( $self->{config}{perl}{modules} ) eq 'ARRAY' ) {
155 0           push( @modules, @{ $self->{config}{perl}{modules} } );
  0            
156             }
157              
158 0           $self->status_add( status => 'modules: ' . join( ',', @modules ) );
159              
160 0           my @installed_via_cpanm;
161             my @cpanm_failed;
162 0           foreach my $module (@modules) {
163 0 0 0       if ( $self->{config}{perl}{pkgs_always_try}
      0        
164             && !defined( $installed{$module} )
165             && !defined( $tried_via_packages{$module} ) )
166             {
167 0           my $status;
168 0           eval { $status = perl_module_via_pkg( module => $module ); };
  0            
169 0 0         if ($@) {
170             # not an error here as it using packages is optional and will be used later.
171 0           push( @modules, $module );
172 0           $self->status_add( status => 'Failed to install ' . $module . ' via packages', );
173             } else {
174 0           $self->{results}{status_text} = $self->{results}{status_text} . $status;
175 0           $self->status_add( status => $module . ' installed' );
176 0           $installed{$module} = 1;
177             }
178             } ## end if ( $self->{config}{perl}{pkgs_always_try...})
179              
180             # if not already installed, try it via cpanm
181 0 0         if ( !defined( $installed{$module} ) ) {
182 0 0         if ( !$installed_cpanm ) {
183 0           eval {
184 0           install_cpanm;
185 0           $installed_cpanm = 1;
186             };
187 0 0         if ($@) {
188 0           $self->status_add( status => 'Failed to install cpanm via packages ... ' . $@, error => 1 );
189             # can't proceced beyond here as cpanm is required
190 0           return undef;
191             } else {
192 0           $self->status_add( status => 'cpanm installed' );
193             }
194             } ## end if ( !$installed_cpanm )
195              
196             #
197 0           my @cpanm_args = ('cpanm');
198 0 0         if ( $self->{opts}{notest} ) {
199 0           push( @cpanm_args, '--notest' );
200             }
201 0 0         if ( $self->{opts}{reinstall} ) {
202 0           push( @cpanm_args, '--reinstall' );
203             }
204 0 0         if ( $self->{opts}{force} ) {
205 0           push( @cpanm_args, '--force' );
206             }
207 0           push( @cpanm_args, $module );
208 0           $self->status_add( status => 'running... ' . join( ' ', @cpanm_args ), );
209 0           system(@cpanm_args);
210             # set this here just in case it is defined multiple times we can skip it if it fails once
211 0           $installed{$module} = 1;
212 0 0         if ( $? != 0 ) {
213 0           $self->status_add(
214             status => 'cpanm failed: ' . join( ' ', @cpanm_args ),
215             error => 1,
216             );
217 0           push( @cpanm_failed, $module );
218             } else {
219 0           push( @installed_via_cpanm, $module );
220             }
221              
222             } ## end if ( !defined( $installed{$module} ) )
223             } ## end foreach my $module (@modules)
224 0           $self->status_add( status => 'Installed: ' . join( ',', keys( %{installed} ) ) );
225 0 0         if ( defined( $installed_via_cpanm[0] ) ) {
226 0           $self->status_add( status => 'Installed Via CPANM: ' . join( ',', @installed_via_cpanm ) );
227             }
228 0 0         if ( defined( $pkgs_installed[0] ) ) {
229 0           $self->status_add( status => 'Installed Via CPANM: ' . join( ',', @pkgs_installed ) );
230             }
231 0 0         if ( defined( $cpanm_failed[0] ) ) {
232 0           $self->status_add( status => 'CPANM Failed: ' . join( ',', @cpanm_failed ) );
233             }
234 0 0         if ( defined( $failed_pkg_required[0] ) ) {
235 0           $self->status_add( status => 'Failed Required By Pkg: ' . join( ',', @failed_pkg_required ) );
236             }
237              
238 0           return undef;
239             } ## end sub action_extra
240              
241             sub short {
242 0     0 1   return 'Install Perl modules specified by the config.';
243             }
244              
245             sub opts_data {
246 0     0 1   return '
247             notest
248             force
249             reinstall
250             ';
251             }
252              
253             1;