File Coverage

blib/lib/Pinto/ArchiveUnpacker.pm
Criterion Covered Total %
statement 45 45 100.0
branch 3 6 50.0
condition 2 5 40.0
subroutine 12 12 100.0
pod 0 1 0.0
total 62 69 89.8


line stmt bran cond sub pod time code
1             # ABSTRACT: Unpack an archive into a temporary directory
2              
3             package Pinto::ArchiveUnpacker;
4              
5 51     51   368 use Moose;
  51         117  
  51         478  
6 51     51   325821 use MooseX::StrictConstructor;
  51         132  
  51         531  
7 51     51   159736 use MooseX::Types::Moose qw(Bool);
  51         121  
  51         543  
8 51     51   220968 use MooseX::MarkAsMethods ( autoclean => 1 );
  51         132  
  51         447  
9              
10 51     51   186204 use Cwd qw(getcwd);
  51         139  
  51         3385  
11 51     51   19222 use Cwd::Guard qw(cwd_guard);
  51         26942  
  51         2698  
12 51     51   379 use Path::Class qw(dir);
  51         117  
  51         2141  
13 51     51   18782 use Archive::Extract;
  51         6124242  
  51         2568  
14 51     51   455 use File::Temp;
  51         152  
  51         4704  
15              
16 51     51   324 use Pinto::Types qw(File);
  51         111  
  51         541  
17 51     51   299116 use Pinto::Util qw(debug throw);
  51         120  
  51         18697  
18              
19             #-----------------------------------------------------------------------------
20              
21             our $VERSION = '0.14'; # VERSION
22              
23             #-----------------------------------------------------------------------------
24              
25             has archive => (
26             is => 'ro',
27             isa => File,
28             required => 1,
29             coerce => 1,
30             );
31              
32             has temp_dir => (
33             is => 'ro',
34             isa => 'File::Temp::Dir',
35             default => sub { File::Temp->newdir( CLEANUP => $_[0]->cleanup ) },
36             lazy => 1,
37             );
38              
39             has cleanup => (
40             is => 'ro',
41             isa => Bool,
42             default => 1,
43             );
44              
45             #-----------------------------------------------------------------------------
46              
47             sub unpack {
48 161     161 0 478 my ($self) = @_;
49              
50 161         4174 my $archive = $self->archive;
51 161         3880 my $temp_dir = $self->temp_dir->dirname;
52 161         3480 my $cwd_guard = cwd_guard(getcwd); # Archive::Extract will chdir
53              
54 161         10853 local $Archive::Extract::PREFER_BIN = 1;
55 161 50 50     1712 local $Archive::Extract::DEBUG = 1 if ( $ENV{PINTO_DEBUG} || 0 ) > 1;
56              
57 161         1635 my $ae = Archive::Extract->new( archive => $archive );
58              
59 161         52165 debug "Unpacking $archive into $temp_dir";
60              
61 161         864 my $ok = $ae->extract( to => $temp_dir );
62 161 50       6398990 throw "Failed to unpack $archive: " . $ae->error if not $ok;
63              
64 161         2607 my @children = dir($temp_dir)->children;
65 161 50 33     110293 return @children == 1 && -d $children[0] ? $children[0] : dir($temp_dir);
66             }
67              
68             #-----------------------------------------------------------------------------
69              
70             __PACKAGE__->meta->make_immutable;
71              
72             #-----------------------------------------------------------------------------
73              
74             1;
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =for :stopwords Jeffrey Ryan Thalhammer
83              
84             =head1 NAME
85              
86             Pinto::ArchiveUnpacker - Unpack an archive into a temporary directory
87              
88             =head1 VERSION
89              
90             version 0.14
91              
92             =head1 AUTHOR
93              
94             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
95              
96             =head1 COPYRIGHT AND LICENSE
97              
98             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
99              
100             This is free software; you can redistribute it and/or modify it under
101             the same terms as the Perl 5 programming language system itself.
102              
103             =cut