blib/lib/Path/Tiny/Archive/Tar.pm

filter: Statementstmt 94.4% 51 / 54 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 4 / 4 totaltotal 86.1% 81 / 94 CC 13 SCAR 17.3
CC
13
Cov
86%
tar
10.7
untar
4.1
tgz
2.0
CRAP 5.6
↑ Summary
1 package Path::Tiny::Archive::Tar;
2  
3 # ABSTRACT: Tar/untar add-on for file path utility
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Archive::Tar ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Compress::Raw::Zlib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 3 our $VERSION = '0.003';
15  
16 3 our %EXPORT_TAGS = ( const =>[qw(
17 COMPRESSION_NONE
18 COMPRESSION_GZIP
19 COMPRESSION_GZIP_DEFAULT
20 COMPRESSION_GZIP_NONE
21 COMPRESSION_GZIP_FASTEST
22 COMPRESSION_GZIP_BEST
23 COMPRESSION_BZIP2
24 )] );
25 3 our @EXPORT_OK = @{ $EXPORT_TAGS{const} };
26  
27  
28 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 push(@Path::Tiny::ISA, __PACKAGE__);
30 }
31  
32 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 COMPRESSION_NONE => undef,
34 # Hack for Archive::Tar: true so don't use default compression and pass
35 # this level to IO::Zlib. And hack for Compress::Zlib: not digit so use
36 # default compression (letters may specify other compression modes).
37 COMPRESSION_GZIP => '-',
38 COMPRESSION_GZIP_DEFAULT => '-',
39 # Hack for Archive::Tar: zero but true so pass 0 level to IO::Zlib.
40 COMPRESSION_GZIP_NONE => '00',
41 COMPRESSION_GZIP_FASTEST => Compress::Raw::Zlib::Z_BEST_SPEED,
42 COMPRESSION_GZIP_BEST => Compress::Raw::Zlib::Z_BEST_COMPRESSION,
43 COMPRESSION_BZIP2 => Archive::Tar::COMPRESS_BZIP,
44 3 };
45  
46  
47 4 ▶sub tar {
Subroutine tar: called CC=10 SCAR=23.7
Pod documented
48 4 my ($self, $dest, $level) = @_;
49  
50 4 $dest = path($dest);
51  
52 4 my $tar = Archive::Tar->new;
53  
54 4 ▶ if ($self->is_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_file) { } 3 1 4
elsif ($self->is_dir) { } 1 0 1
55 3 my $file = Archive::Tar::File->new(file => $self->stringify());
56  
57 3 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 3 3
58  
59 3 $file->name($self->basename);
60 3 $file->prefix('');
61  
62 3 ▶ $tar->add_files($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
$tar->add_files($file) or return 3 0 3
63 }
64 elsif ($self->is_dir) {
65 1 my $paths = $self->iterator({ recurse => 1 });
66  
67 1 ▶ while (my $path = $paths->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = &$paths()) 2 1 3
68 2 my $file = Archive::Tar::File->new(file => $path->stringify());
69  
70 2 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 2 2
71  
72 2 $file->rename($path->relative($self));
73  
74 2 ▶ $tar->add_files($file) or return;
Branch: 1 branch
BranchTrueFalseTotal
$tar->add_files($file) or return 2 0 2
75 }
76 }
77 else {
78 0 return;
79 }
80  
81 4 ▶ $tar->write($dest->stringify(), defined $level ? $level : ()) or return;
Branch: 2 branches
BranchTrueFalseTotal
$tar->write($dest->stringify, defined $level ? $level : ()) or return 4 0 4
defined $level ? : 1 3 4
82  
83 4 return $dest;
84 }
85  
86  
87 0 ▶sub tgz {
Subroutine tgz: not called CC=1 SCAR=6.9
Pod documented
88 0 return $_[0]->tar($_[1], COMPRESSION_GZIP);
89 }
90  
91  
92 0 ▶sub tbz2 {
Subroutine tbz2: not called CC=1 SCAR=6.9
Pod documented
93 0 return $_[0]->tar($_[1], COMPRESSION_BZIP2);
94 }
95  
96  
97 2 ▶sub untar {
Subroutine untar: called CC=4 SCAR=14.0
Pod documented
98 2 my ($self, $dest) = @_;
99  
100 2 $dest = path($dest);
101  
102 2 my $files = Archive::Tar->iter($self->stringify());
103  
104 2 ▶ return unless $files;
Branch: 1 branch
BranchTrueFalseTotal
unless $files 0 2 2
105  
106 2 ▶ while (my $file = $files->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$files()) 3 2 5
107 3 ▶ $file->extract($dest->child($file->full_path)->stringify()) or return;
Branch: 1 branch
BranchTrueFalseTotal
$file->extract($dest->child($file->full_path)->stringify) or return 3 0 3
108 }
109  
110 2 return $dest;
111 }
112  
113  
114 3 1;
115  
116 __END__
↑ Summary