Build/PPK/Deptool.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 87 CC 5 SCAR 19.9
CC
5
Cov
0%
fetch_dist
20.0
load_index
6.0
new
2.0
CRAP 7.3
1 package Build::PPK::Deptool;
2  
3 # Copyright (c) 2018, cPanel, L.L.C.
4 # All rights reserved.
5 # http://cpanel.net/
6 #
7 # This is free software; you can redistribute it and/or modify it under the same
8 # terms as Perl itself. See the LICENSE file for further details.
9  
10 use strict;
11 use warnings;
12  
13 use Carp;
14  
15 sub new {
16 my ( $class, %opts ) = @_;
17  
18 return bless {
19 'dist_index' => $class->load_index( $opts{'dist_index'} ),
20 'prog_index' => $class->load_index( $opts{'prog_index'} )
21 }, $class;
22 }
23  
24 sub load_index {
25 my ( $class, $file ) = @_;
26 my %index;
27  
28 open( my $fh, '<', $file ) or die("Unable to open $file: $!");
29  
30 while ( my $line = readline($fh) ) {
31 chomp $line;
32  
33 $line =~ s/^\s+//;
34 $line =~ s/\s+$//;
35  
36 next if $line =~ /^#/;
37  
38 my ( $target, $data ) = split /\s*:\s*/, $line, 2;
39 my @deps = split /\s+/, $data;
40  
41 $index{$target} = \@deps;
42 }
43  
44 close $fh;
45  
46 return \%index;
47 }
48  
49 sub fetch_dist {
50 my ( $self, %args ) = @_;
51 my $url = $self->{'dist_index'}->{ $args{'dist'} }->[0];
52  
53 die("Could not find URL for dependency $args{'dist'}") unless $url;
54  
55 my %HANDLERS = (
56 qr(^git://) => sub {
57 require Build::PPK::Deptool::Git;
58  
59 return Build::PPK::Deptool::Git->fetch_dist(@_);
60 },
61  
62 qr(^http://) => sub {
63 require Build::PPK::Deptool::HTTP;
64  
65 return Build::PPK::Deptool::HTTP->fetch_dist(@_);
66 },
67  
68 qr(^cpan://) => sub {
69 require Build::PPK::Deptool::CPAN;
70  
71 return Build::PPK::Deptool::CPAN->fetch_dist(@_);
72 }
73 );
74  
75 foreach my $pattern ( keys %HANDLERS ) {
76 next unless $url =~ $pattern;
77  
78 my %fetch_args = (
79 'url' => $url,
80 'dist' => $args{'dist'},
81 'path' => $args{'path'}
82 );
83  
84 $args{'callback'}->(%fetch_args) if $args{'callback'};
85  
86 return $HANDLERS{$pattern}->(%fetch_args);
87 }
88  
89 confess("Could not find an appropriate handler to fetch dependency $url");
90 }
91  
92 sub targets {
93 my ($self) = @_;
94  
95 return sort keys %{ $self->{'prog_index'} };
96 }
97  
98 sub target_deps {
99 my ( $self, %args ) = @_;
100 my %deps;
101  
102 foreach my $target ( @{ $args{'targets'} } ) {
103 foreach my $dep ( @{ $self->{'prog_index'}->{$target} } ) {
104 $deps{$dep} = 1;
105 }
106 }
107  
108 return sort keys %deps;
109 }
110  
111 1;