File Coverage

blib/lib/Alien/Build/Plugin/Core/Download.pm
Criterion Covered Total %
statement 65 74 87.8
branch 16 24 66.6
condition 2 3 66.6
subroutine 8 8 100.0
pod 1 1 100.0
total 92 110 83.6


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::Core::Download;
2              
3 48     48   232964 use strict;
  48         108  
  48         1580  
4 48     48   377 use warnings;
  48         155  
  48         1370  
5 48     48   904 use 5.008004;
  48         160  
6 48     48   974 use Alien::Build::Plugin;
  48         200  
  48         366  
7 48     48   1157 use Path::Tiny ();
  48         11888  
  48         1469  
8 48     48   1208 use Alien::Build::Util qw( _mirror );
  48         132  
  48         38269  
9              
10             # ABSTRACT: Core download plugin
11             our $VERSION = '2.45'; # VERSION
12              
13              
14             sub _hook
15             {
16 12     12   46 my($build) = @_;
17              
18 12         115 my $res = $build->fetch;
19              
20 12 100       85 if($res->{type} =~ /^(?:html|dir_listing)$/)
21             {
22 2         6 my $type = $res->{type};
23 2         8 $type =~ s/_/ /;
24 2         10 $build->log("decoding $type");
25 2         18 $res = $build->decode($res);
26             }
27              
28 12 100       55 if($res->{type} eq 'list')
29             {
30 3         21 $res = $build->prefer($res);
31 3 50       8 die "no matching files in listing" if @{ $res->{list} } == 0;
  3         14  
32 3         8 my $version = $res->{list}->[0]->{version};
33 3         7 my($pick, @other) = map { $_->{url} } @{ $res->{list} };
  303         525  
  3         10  
34 3 50       15 if(@other > 8)
35             {
36 3         15 splice @other, 7;
37 3         10 push @other, '...';
38             }
39 3         22 $build->log("candidate *$pick");
40 3         26 $build->log("candidate $_") for @other;
41 3         16 $res = $build->fetch($pick);
42              
43 3 50       15 if($version)
44             {
45 0         0 $version =~ s/\.+$//;
46 0         0 $build->log("setting version based on archive to $version");
47 0         0 $build->runtime_prop->{version} = $version;
48             }
49             }
50              
51 12 50       52 if($res->{type} eq 'file')
52             {
53 12         30 my $alienfile = $res->{filename};
54 12         96 $build->log("downloaded $alienfile");
55 12 100       113 if($res->{content})
    50          
56             {
57 4         35 my $tmp = Alien::Build::TempDir->new($build, "download");
58 4         1511 my $path = Path::Tiny->new("$tmp/$alienfile");
59 4         141 $path->spew_raw($res->{content});
60 4         1935 $build->install_prop->{download} = $path->stringify;
61 4         13 $build->install_prop->{complete}->{download} = 1;
62 4         23 return $build;
63             }
64             elsif($res->{path})
65             {
66 8 100 66     81 if(defined $res->{tmp} && !$res->{tmp})
67             {
68 7 50       136 if(-e $res->{path})
69             {
70 7         45 $build->install_prop->{download} = $res->{path};
71 7         37 $build->install_prop->{complete}->{download} = 1;
72             }
73             else
74             {
75 0         0 die "not a file or directory: @{[ $res->{path} ]}";
  0         0  
76             }
77             }
78             else
79             {
80 1         14 my $from = Path::Tiny->new($res->{path});
81 1         45 my $tmp = Alien::Build::TempDir->new($build, "download");
82 1         357 my $to = Path::Tiny->new("$tmp/@{[ $from->basename ]}");
  1         10  
83 1 50       72 if(-d $res->{path})
84             {
85             # Please note: _mirror and Alien::Build::Util are ONLY
86             # allowed to be used by core plugins. If you are writing
87             # a non-core plugin it may be removed. That is why it
88             # is private.
89 0         0 _mirror $from, $to;
90             }
91             else
92             {
93 1         9 require File::Copy;
94 1 50       5 File::Copy::copy(
95             "$from" => "$to",
96             ) || die "copy $from => $to failed: $!";
97             }
98 1         391 $build->install_prop->{download} = $to->stringify;
99 1         4 $build->install_prop->{complete}->{download} = 1;
100             }
101 8         196 return $build;
102             }
103 0         0 die "file without content or path";
104             }
105 0         0 die "unknown fetch response type: @{[ $res->{type} ]}";
  0         0  
106             }
107              
108             sub init
109             {
110 337     337 1 974 my($self, $meta) = @_;
111              
112 337         1739 $meta->default_hook(download => \&_hook);
113             }
114              
115             1;
116              
117             __END__