Installer.pm

filter: Statementstmt 72.2% 117 / 162 Branchbran 48.1% 26 / 54 Conditioncond 37.1% 13 / 35 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 66.6% 16 / 24 Podpod 100% 2 / 2 totaltotal 59.3% 178 / 300 CC 43 SCAR 42.0
CC
43
Cov
62%
install
129.6
list_available
12.0
_cpan_install
12.0
CRAP 67.0
↑ Summary Installer/Utils/Config.pm »
1 package OPM::Installer;
2  
3 # ABSTRACT: Install ticketsystem (Znuny/OTOBO) add ons
4  
5 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '1.0.1'; # VERSION
11  
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Capture::Tiny qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Module::Runtime qw(use_module is_module_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Types::Standard qw(ArrayRef Str Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use OPM::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use OPM::Installer::Utils::TS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use OPM::Installer::Utils::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use OPM::Installer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 has package => ( is => 'ro', isa => Str );
25 2 has version => ( is => 'ro', isa => Str, lazy => 1, default => \&_build_version );
26 1 ▶has prove => ( is => 'ro', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 has manager => ( is => 'ro', lazy => 1, default => \&_build_manager );
28 2 has repositories => ( is => 'ro', isa => ArrayRef[Str] );
29 2 has conf => ( is => 'ro' );
30 2 has force => ( is => 'ro', isa => Bool );
31 2 has sudo => ( is => 'ro', isa => Bool );
32 0 ▶has utils_ts => ( is => 'ro', lazy => 1, default => sub{ OPM::Installer::Utils::TS->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
33 1 ▶has verbose => ( is => 'ro', isa => Bool, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
34 0 ▶has logger => ( is => 'ro', lazy => 1, default => sub { OPM::Installer::Logger->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
35  
36 0 ▶sub list_available {
Subroutine list_available: not called CC=3 SCAR=24.8
Pod documented
37 0 my ( $self, %params ) = @_;
38  
39 0 my %file_opts;
40 0 ▶ if ( $params{repositories} and ref $params{repositories} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"repositories"} and ref $params{"repositories"} eq "ARRAY") 0 0 0
Condition0%0/3
$params{"repositories"} and ref $params{"repositories"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"repositories"} and ref $params{"repositories"} eq "ARRAY"
$params{"repositories"} ref $params{"repositories"} eq "ARRAY"
Truth table0%0/3
A and B
A= $params{"repositories"}B= ref $params{"repositories"} eq "ARRAY"
ABresult
0X0
100
111
41 0 $file_opts{repositories} = $params{repositories};
42 }
43  
44 0 my $package_utils = OPM::Installer::Utils::File->new(
45 %file_opts,
46 package => 'DummyPackage', # ::File needs a package set
47 version => $self->version,
48 );
49  
50 0 return $package_utils->list_available;
51 }
52  
53 5 ▶sub install {
Subroutine install: called CC=33 SCAR=48.6
Pod documented
54 5 my $self = shift;
55  
56 5 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 5 0 5
57 5 unshift @_, 'package';
58 }
59  
60 5 my %params = @_;
61  
62 5 my %file_opts;
63 5 ▶ if ( $self->repositories ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->repositories) 5 0 5
64 5 $file_opts{repositories} = $self->repositories;
65 }
66  
67 5 ▶ if ( $params{repositories} and ref $params{repositories} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"repositories"} and ref $params{"repositories"} eq "ARRAY") 0 5 5
Condition33%1/3
$params{"repositories"} and ref $params{"repositories"} eq "ARRAY"
!ll&&!rl&&r
500
MC/DC0%0/2
$params{"repositories"} and ref $params{"repositories"} eq "ARRAY"
$params{"repositories"} ref $params{"repositories"} eq "ARRAY"
Truth table33%1/3
A and B
A= $params{"repositories"}B= ref $params{"repositories"} eq "ARRAY"
ABresult
0X0
100
111
68 0 $file_opts{repositories} = $params{repositories};
69 }
70  
71 5 my $version_string = "";
72 5 ▶ if ( $params{version} and $params{version_exact} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"version"} and $params{"version_exact"}) 0 5 5
Condition33%1/3
$params{"version"} and $params{"version_exact"}
!ll&&!rl&&r
500
MC/DC0%0/2
$params{"version"} and $params{"version_exact"}
$params{"version"} $params{"version_exact"}
Truth table33%1/3
A and B
A= $params{"version"}B= $params{"version_exact"}
ABresult
0X0
100
111
73 0 $file_opts{version} = $params{version};
74 0 $version_string = $params{version};
75 }
76  
77 5 ▶ say sprintf "Try to install %s %s...", $params{package} || $self->package, $version_string if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 5 5
Condition0%0/3
$params{'package'} || $self->package
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{'package'} || $self->package
$params{'package'} $self->package
Truth table0%0/3
A || B
A= $params{'package'}B= $self->package
ABresult
1X1
011
000
78
79 5 ▶ my $installed_version = $self->utils_ts->is_installed( package => $params{package} || $self->package );
Condition33%1/3
$params{'package'} || $self->package
l!l&&r!l&&!r
500
MC/DC0%0/2
$params{'package'} || $self->package
$params{'package'} $self->package
Truth table33%1/3
A || B
A= $params{'package'}B= $self->package
ABresult
1X1
011
000
80 5 ▶ if ( $installed_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed_version) 1 4 5
81 my $message = sprintf 'Addon %s is installed (%s)',
82 1 ▶ $params{package} || $self->package, $installed_version;
Condition33%1/3
$params{'package'} || $self->package
l!l&&r!l&&!r
100
MC/DC0%0/2
$params{'package'} || $self->package
$params{'package'} $self->package
Truth table33%1/3
A || B
A= $params{'package'}B= $self->package
ABresult
1X1
011
000
83  
84 1 $self->logger->debug( message => $message );
85 1 say $message;
86  
87 1 ▶ if ( $params{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"version"}) 0 1 1
88 my $check = $self->utils_ts->_check_version(
89 installed => $installed_version,
90 requested => $params{version},
91 0 );
92  
93 0 ▶ return 1 if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 0 0 0
94 }
95 }
96  
97 my $package_utils = OPM::Installer::Utils::File->new(
98 %file_opts,
99 5 ▶ package => $params{package} || $self->package,
Condition33%1/3
$params{'package'} || $self->package
l!l&&r!l&&!r
500
MC/DC0%0/2
$params{'package'} || $self->package
$params{'package'} $self->package
Truth table33%1/3
A || B
A= $params{'package'}B= $self->package
ABresult
1X1
011
000
100 framework_version => $self->version,
101 verbose => $self->verbose,
102 );
103  
104 5 my $package_path = $package_utils->resolve_path;
105  
106 5 ▶ if ( !$package_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package_path) 1 4 5
107 my $message = sprintf "Could not find a .opm file for %s%s (framework version %s)",
108 $params{package} || $self->package,
109 1 ▶ ( $file_opts{version} ? " $file_opts{version}" : "" ),
Branch: 1 branch
BranchTrueFalseTotal
$file_opts{'version'} ? : 0 1 1
Condition33%1/3
$params{'package'} || $self->package
l!l&&r!l&&!r
100
MC/DC0%0/2
$params{'package'} || $self->package
$params{'package'} $self->package
Truth table33%1/3
A || B
A= $params{'package'}B= $self->package
ABresult
1X1
011
000
110 $self->version;
111  
112 1 $self->logger->error( fatal => $message );
113 1 say $message;
114 1 return;
115 }
116  
117 4 my $parsed = OPM::Parser->new(
118 opm_file => $package_path,
119 );
120  
121 4 $parsed->parse;
122  
123 4 ▶ if ( $parsed->error_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed->error_string) 1 3 4
124 1 my $message = sprintf "Cannot parse $package_path: %s", $parsed->error_string;
125 1 $self->logger->error( fatal => $message );
126 1 say $message;
127 1 return;
128 }
129  
130 3 ▶ if ( !$self->_check_matching_versions( $parsed, $self->version ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_matching_versions($parsed, $self->version)) 0 3 3
131 0 my $message = sprintf 'framework versions of %s (%s) doesn\'t match ticketsystem version %s',
132 $parsed->name,
133 join ( ', ', $parsed->framework ),
134 $self->version;
135  
136 0 $self->logger->error( fatal => $message );
137 0 say $message;
138 0 return;
139 }
140  
141 3 ▶ if ( $self->utils_ts->is_installed( package => $parsed->name, version => $parsed->version ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->utils_ts->is_installed("package", $parsed->name, "version", $parsed->version)) 2 1 3
142 2 my $message = sprintf 'Addon %s is up to date (%s)',
143 $parsed->name, $parsed->version;
144  
145 2 $self->logger->debug( message => $message );
146 2 say $message;
147 2 return 1;
148 }
149  
150 1 ▶ say sprintf "Working on %s...", $parsed->name if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
151 1 $self->logger->debug( message => sprintf "Working on %s...", $parsed->name );
152  
153 1 ▶ my @dependencies = @{ $parsed->dependencies || [] };
Condition50%1/2
$parsed->dependencies || []
l!l
10
MC/DC0%0/1
$parsed->dependencies || []
$parsed->dependencies
Truth table50%1/2
A
A= $parsed->dependencies
Aresult
11
00
154 0 my @cpan_deps = grep{ $_->{type} eq 'CPAN' }@dependencies;
155 0 my @addon_deps = grep{ $_->{type} eq 'Addon' }@dependencies;
156  
157 0 my $found_dependencies = join ', ', map{ $_->{name} }@dependencies;
158 1 ▶ say sprintf "Found dependencies: %s", $found_dependencies if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
159 1 $self->logger->debug( message => sprintf "Found dependencies: %s", $found_dependencies );
160  
161 1 for my $cpan_dep ( @cpan_deps ) {
162 0 my $module = $cpan_dep->{name};
163 0 my $version = $cpan_dep->{version};
164  
165 0 ▶ next CPANDEP if !is_module_name( $module );
Branch: 1 branch
BranchTrueFalseTotal
unless &is_module_name($module) 0 0 0
166  
167 0 ▶ use_module( $module, $version ) and next;
Branch: 1 branch
BranchTrueFalseTotal
&use_module($module, $version) and next 0 0 0
168  
169 0 $self->_cpan_install( %{$cpan_dep} );
170 }
171  
172 1 for my $addon_dep ( @addon_deps ) {
173 0 my $module = $addon_dep->{name};
174 0 my $version = $addon_dep->{version};
175  
176 0 ▶ $self->utils_ts->is_installed( %{$addon_dep} ) and next;
Branch: 1 branch
BranchTrueFalseTotal
$self->utils_ts->is_installed(%{$addon_dep;}) and next 0 0 0
177  
178 0 my $success = $self->install( package => $module, version => $version );
179 0 ▶ if ( !$success && !$self->force ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success or $self->force) 0 0 0
Condition0%0/3
$success or $self->force
l!l&&r!l&&!r
000
MC/DC0%0/2
$success or $self->force
$success $self->force
Truth table0%0/3
A or B
A= $successB= $self->force
ABresult
1X1
011
000
180 0 return;
181 }
182 }
183  
184 1 ▶ if ( $self->prove ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->prove) 0 1 1
185 # TODO: run unittests
186 }
187  
188 1 my $content = io( $package_path )->slurp;
189  
190 1 my $message = sprintf "Install %s ...", $parsed->name;
191 1 ▶ say $message if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
192 1 $self->logger->debug( message => $message );
193  
194 1 $self->manager->PackageInstall( String => $content );
195  
196 1 return 1;
197 }
198  
199 0 ▶sub _cpan_install {
Subroutine _cpan_install: not called CC=3 SCAR=24.8
200 0 my ( $self, %params) = @_;
201  
202 0 my $dist = $params{name};
203 0 ▶ my @sudo = $self->sudo ? 'sudo' : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->sudo ? : 0 0 0
204 0 ▶ my ($out, $err, $exit) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 0 system @sudo, 'cpanm', $dist;
206 0 };
207  
208 0 ▶ if ( $out !~ m{Successfully installed } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out =~ /Successfully installed /u) 0 0 0
209 0 die "Installation of dependency failed ($dist)! - ($err)";
210 }
211  
212 0 return;
213 }
214  
215 0 ▶sub _build_manager {
Subroutine _build_manager: not called CC=1 SCAR=6.9
216 0 my $self = shift;
217  
218 0 return $self->utils_ts->manager;
219 }
220  
221 0 ▶sub _build_utils_ts {
Subroutine _build_utils_ts: not called CC=1 SCAR=6.9
222 0 OPM::Installer::Utils::TS->new;
223 }
224  
225 0 ▶sub _build_version {
Subroutine _build_version: not called CC=1 SCAR=6.9
226 0 shift->utils_ts->framework_version;
227 }
228  
229 11 ▶sub _check_matching_versions {
Subroutine _check_matching_versions: called CC=7 SCAR=19.5
230 11 my ($self, $parsed, $addon_version) = @_;
231  
232 11 my ($major, $minor, $patch) = split /\./, $addon_version;
233  
234 11 my $check_ok;
235  
236 FRAMEWORK:
237 11 for my $required_framework ( @{ $parsed->framework } ) {
238 29 my ($r_major, $r_minor, $r_patch) = split /\./, $required_framework;
239  
240 29 ▶ next FRAMEWORK if $r_major != $major;
Branch: 1 branch
BranchTrueFalseTotal
if $r_major != $major 7 22 29
241 22 ▶ next FRAMEWORK if lc $r_minor ne 'x' && $r_minor != $minor;
Branch: 1 branch
BranchTrueFalseTotal
if lc $r_minor ne "x" and $r_minor != $minor 14 8 22
Condition100%3/3
lc $r_minor ne "x" and $r_minor != $minor
!ll&&!rl&&r
1714
MC/DC100%2/2
lc $r_minor ne "x" and $r_minor != $minor
lc $r_minor ne "x" $r_minor != $minor
Truth table100%3/3
A and B
A= lc $r_minor ne "x"B= $r_minor != $minor
ABresult
0X0
100
111
242 8 ▶ next FRAMEWORK if lc $r_patch ne 'x' && $r_patch != $patch;
Branch: 1 branch
BranchTrueFalseTotal
if lc $r_patch ne "x" and $r_patch != $patch 2 6 8
Condition100%3/3
lc $r_patch ne "x" and $r_patch != $patch
!ll&&!rl&&r
512
MC/DC100%2/2
lc $r_patch ne "x" and $r_patch != $patch
lc $r_patch ne "x" $r_patch != $patch
Truth table100%3/3
A and B
A= lc $r_patch ne "x"B= $r_patch != $patch
ABresult
0X0
100
111
243  
244 6 $check_ok = 1;
245 6 last FRAMEWORK;
246 }
247  
248 11 return $check_ok;
249 }
250  
251 2 1;
252  
253 __END__
↑ Summary Installer/Utils/Config.pm »