File Coverage

blib/lib/Shipwright/Source/Compressed.pm
Criterion Covered Total %
statement 27 71 38.0
branch 0 22 0.0
condition 0 12 0.0
subroutine 9 13 69.2
pod 2 2 100.0
total 38 120 31.6


line stmt bran cond sub pod time code
1             package Shipwright::Source::Compressed;
2              
3 3     3   1196 use warnings;
  3         7  
  3         89  
4 3     3   14 use strict;
  3         5  
  3         64  
5 3     3   13 use File::Spec::Functions qw/catfile catdir/;
  3         6  
  3         146  
6              
7 3     3   15 use base qw/Shipwright::Source::Base/;
  3         5  
  3         786  
8 3     3   1327 use Archive::Extract;
  3         300345  
  3         48  
9 3     3   130 use File::Temp qw/tempdir/;
  3         7  
  3         161  
10 3     3   17 use File::Copy::Recursive qw/rmove/;
  3         6  
  3         110  
11 3     3   16 use Shipwright::Util;
  3         7  
  3         230  
12 3     3   16 use Cwd qw/getcwd/;
  3         6  
  3         1499  
13              
14             =head2 run
15              
16             =cut
17              
18             sub run {
19 0     0 1   my $self = shift;
20              
21 0 0         $self->name( $self->just_name( $self->path ) ) unless $self->name;
22 0 0         $self->version( $self->just_version( $self->path ) ) unless $self->version;
23 0           $self->log->info( 'running source ' . $self->name . ': ' . $self->source );
24              
25 0           $self->_update_version( $self->name, $self->version );
26              
27             $self->_update_url( $self->name, 'file:' . $self->source )
28 0 0         unless $self->{_no_update_url};
29              
30 0           my $newer = $self->_cmd; # if we really get something new
31              
32 0           my $ret = $self->SUPER::run(@_);
33              
34             # follow only if --follow and we really added new stuff.
35 0 0 0       $self->_follow( catdir( $self->directory, $self->name ) )
36             if $self->follow && $newer;
37 0           return catdir( $self->directory, $self->name );
38             }
39              
40             =head2 path
41              
42             the decompressed source path
43              
44             =cut
45              
46             sub path {
47 0     0 1   my $self = shift;
48              
49             # we memoize path info so we don't need to extract on each call.
50 0 0         return $self->{_path} if $self->{_path};
51              
52 0           my $source = $self->source;
53 0           my $ae = Archive::Extract->new( archive => $source );
54             # this's to check if $source is valid, aka. it only contains one directory.
55 0           my $tmp_dir = tempdir( 'shipwright_tmp_XXXXXX', CLEANUP => 1, TMPDIR => 1 );
56 0           $ae->extract( to => $tmp_dir );
57 0           my $files = $ae->files;
58              
59             # 1st file in Crypt-DH-0.07.tar.gz is "./"
60 0 0         shift @$files if $files->[0] =~ /^\.[\/\\]$/;
61 0           my $base_dir = $files->[0];
62             # some compressed file has name like ./PerlImagick-6.67/
63 0           $base_dir =~ s!^\.[/\\]!!;
64              
65             # sunnavy found that the 1st file is not the directory name when extracting
66             # HTML-Strip-1.06.tar.gz, which is weird but valid compressed file.
67 0           $base_dir =~ s![/\\].*!!;
68              
69 0 0         if ( @$files != grep { /^(?:\.[\/\\])?\Q$base_dir\E/ } @$files ) {
  0            
70 0           confess_or_die 'only support compressed file which contains only one directory: '
71             . $base_dir;
72             }
73              
74 0           $self->{_path} = $base_dir;
75              
76 0           return $base_dir;
77             }
78              
79             sub _cmd {
80 0     0     my $self = shift;
81 0           my $arg;
82              
83 0           my ( $from, $to );
84 0           $from = catdir( $self->directory, $self->path );
85 0           $to = catdir( $self->directory, $self->name );
86              
87             # if it already exists, assuming we have processed it already, don't do it
88             # again
89 0 0         return if -e $to;
90              
91 0           my $ae = Archive::Extract->new( archive => $self->source );
92              
93             return sub {
94 0     0     $ae->extract( to => $self->directory );
95              
96 0 0 0       if ( -e catfile( $from, 'dist.ini' )
      0        
      0        
97             && !-e catfile( $from, 'configure' )
98             && !-e catfile( $from, 'Makefile.PL' )
99             && !-e catfile( $from, 'Build.PL' ) )
100             {
101             # assume it's a Dist::Zilla dist
102 0 0         if ( $from eq $to ) {
103 0           rmove( $from, $from . '-tmp' );
104             }
105              
106 0           my $old = getcwd();
107 0           chdir $from . '-tmp';
108 0           run_cmd( [ $ENV{SHIPWRIGHT_DZIL}, 'build', '--in', $to ] );
109 0           chdir $old;
110             }
111              
112 0 0         if ( $from ne $to ) {
113 0           rmove( $from, $to );
114             }
115 0           };
116             }
117              
118             1;
119              
120             __END__