File Coverage

blib/lib/Alien/Build/Plugin/Fetch/Wget.pm
Criterion Covered Total %
statement 49 50 98.0
branch 8 12 66.6
condition 3 6 50.0
subroutine 12 12 100.0
pod 1 1 100.0
total 73 81 90.1


line stmt bran cond sub pod time code
1             package Alien::Build::Plugin::Fetch::Wget;
2              
3 2     2   254017 use strict;
  2         29  
  2         66  
4 2     2   20 use warnings;
  2         6  
  2         69  
5 2     2   246 use Alien::Build::Plugin;
  2         2017  
  2         16  
6 2     2   213 use File::Temp qw( tempdir );
  2         6  
  2         125  
7 2     2   12 use Path::Tiny qw( path );
  2         6  
  2         99  
8 2     2   14 use File::Which qw( which );
  2         4  
  2         126  
9 2     2   16 use Capture::Tiny qw( capture );
  2         15  
  2         152  
10 2     2   351 use File::chdir;
  2         2723  
  2         1459  
11              
12             # ABSTRACT: Plugin for fetching files using wget
13             our $VERSION = '0.01_01'; # TRIAL VERSION
14             $VERSION = eval $VERSION;
15              
16             has wget_command => sub { defined $ENV{WGET} ? which($ENV{WGET}) : which('wget') };
17              
18             sub init
19             {
20 1     1 1 25 my($self, $meta) = @_;
21            
22             $meta->register_hook(
23             fetch => sub {
24 3     3   17424 my($build, $url) = @_;
25 3   66     17 $url ||= $meta->prop->{start_url};
26              
27 3         27 my($scheme) = $url =~ /^([a-z0-9]+):/i;
28            
29 3 50 33     15 if($scheme eq 'http' || $scheme eq 'https')
30             {
31 3         13 local $CWD = tempdir( CLEANUP => 1 );
32            
33 3         956 my($stdout, $stderr) = $self->_execute(
34             $build,
35             $self->wget_command,
36             '-k', '--content-disposition', '-S',
37             $url,
38             );
39              
40 2         9 my($path) = path('.')->children;
41 2 50       256 die "no file found after wget" unless $path;
42 2         17 my($type) = $stderr =~ /Content-Type:\s*(.*?)$/m;
43 2 50       11 $type =~ s/;.*$// if $type;
44 2 100       7 if($type eq 'text/html')
45             {
46             return {
47 1         7 type => 'html',
48             base => $url,
49             content => scalar $path->slurp,
50             };
51             }
52             else
53             {
54             return {
55 1         7 type => 'file',
56             filename => $path->basename,
57             path => $path->absolute->stringify,
58             };
59             }
60             }
61             else
62             {
63 0         0 die "scheme $scheme is not supported by the Fetch::Wget plugin";
64             }
65             },
66 1 50       6 ) if $self->wget_command;
67             }
68              
69             sub _execute
70             {
71 3     3   39 my($self, $build, @command) = @_;
72 3         27 $build->log("+ @command");
73             my($stdout, $stderr, $err) = capture {
74 3     3   2183 system @command;
75 3         955 $?;
76 3         184 };
77 3 100       1610 if($err)
78             {
79 1         3 chomp $stderr;
80 1         8 $stderr = [split /\n/, $stderr]->[-1];
81 1         13 die "error in wget fetch: $stderr";
82             }
83 2         10 ($stdout, $stderr);
84             }
85              
86             1;
87              
88             __END__