File Coverage

blib/lib/Alien/Build/Util.pm
Criterion Covered Total %
statement 68 77 88.3
branch 31 50 62.0
condition 7 11 63.6
subroutine 13 13 100.0
pod n/a
total 119 151 78.8


line stmt bran cond sub pod time code
1             package Alien::Build::Util;
2              
3 64     64   182656 use strict;
  64         132  
  64         1806  
4 64     64   291 use warnings;
  64         153  
  64         1497  
5 64     64   1176 use 5.008004;
  64         196  
6 64     64   326 use Exporter qw( import );
  64         160  
  64         1812  
7 64     64   2508 use Path::Tiny qw( path );
  64         31552  
  64         2815  
8 64     64   400 use Config;
  64         155  
  64         57179  
9              
10             # ABSTRACT: Private utility functions for Alien::Build
11             our $VERSION = '2.47'; # VERSION
12              
13              
14             our @EXPORT_OK = qw( _mirror _dump _destdir_prefix _perl_config _ssl_reqs _has_ssl );
15              
16             # usage: _mirror $source_directory, $dest_direction, \%options
17             #
18             # options:
19             # - filter -> regex for files that should match
20             # - empty_directory -> if true, create all directories, including empty ones.
21             # - verbose -> turn on verbosity
22              
23             sub _mirror
24             {
25 39     39   19693 my($src_root, $dst_root, $opt) = @_;
26 39         161 ($src_root, $dst_root) = map { path($_) } ($src_root, $dst_root);
  78         1430  
27 39   100     1021 $opt ||= {};
28              
29 39         740 require Alien::Build;
30 39         225 require File::Find;
31 39         2469 require File::Copy;
32              
33             File::Find::find({
34             wanted => sub {
35 214 50   214   13260 next unless -e $File::Find::name;
36 214         814 my $src = path($File::Find::name)->relative($src_root);
37 214 100 100     38939 return if $opt->{filter} && "$src" !~ $opt->{filter};
38 188 100       469 return if "$src" eq '.';
39 153         648 my $dst = $dst_root->child("$src");
40 153         4444 $src = $src->absolute($src_root);
41 153 100       9831 if(-l "$src")
    100          
    50          
42             {
43 3 50       51 unless(-d $dst->parent)
44             {
45 0 0       0 Alien::Build->log("mkdir -p @{[ $dst->parent ]}") if $opt->{verbose};
  0         0  
46 0         0 $dst->parent->mkpath;
47             }
48             # TODO: rmtree if a directory?
49 3 50       184 if(-e "$dst")
50 0         0 { unlink "$dst" }
51 3         52 my $target = readlink "$src";
52 3 50       40 Alien::Build->log("ln -s $target $dst") if $opt->{verbose};
53 3 50       7 symlink($target, $dst) || die "unable to symlink $target => $dst";
54             }
55             elsif(-d "$src")
56             {
57 57 100       4509 if($opt->{empty_directory})
58             {
59 9 50       30 unless(-d $dst)
60             {
61 9 50       159 Alien::Build->log("mkdir $dst") if $opt->{verbose};
62 9 50       21 mkdir($dst) || die "unable to create directory $dst: $!";
63             }
64             }
65             }
66             elsif(-f "$src")
67             {
68 93 100       4086 unless(-d $dst->parent)
69             {
70 51 100       3912 Alien::Build->log("mkdir -p @{[ $dst->parent ]}") if $opt->{verbose};
  13         62  
71 51         197 $dst->parent->mkpath;
72             }
73             # TODO: rmtree if a directory?
74 93 50       11987 if(-e "$dst")
75 0         0 { unlink "$dst" }
76 93 100       2077 Alien::Build->log("cp $src $dst") if $opt->{verbose};
77 93 50       328 File::Copy::cp("$src", "$dst") || die "copy error $src => $dst: $!";
78 93 0 33     30236 if($] < 5.012 && -x "$src" && $^O ne 'MSWin32')
      33        
79             {
80             # apparently Perl 5.8 and 5.10 do not preserver perms
81 0         0 my $mode = [stat "$src"]->[2] & oct(777);
82 0         0 eval { chmod $mode, "$dst" };
  0         0  
83             }
84             }
85             },
86 39         11030 no_chdir => 1,
87             }, "$src_root");
88              
89 39         977 ();
90             }
91              
92             sub _dump
93             {
94 29 50   29   5473 if(eval { require YAML })
  29         3813  
95             {
96 0         0 return YAML::Dump(@_);
97             }
98             else
99             {
100 29         1724 require Data::Dumper;
101 29         11789 return Data::Dumper::Dumper(@_);
102             }
103             }
104              
105             sub _destdir_prefix
106             {
107 16     16   11229 my($destdir, $prefix) = @_;
108 16 50       60 $prefix =~ s{^/?([a-z]):}{$1}i if $^O eq 'MSWin32';
109 16         48 path($destdir)->child($prefix)->stringify;
110             }
111              
112             sub _perl_config
113             {
114 6     6   22 my($key) = @_;
115 6         47 $Config{$key};
116             }
117              
118             sub _ssl_reqs
119             {
120             return {
121 12     12   3875 'Net::SSLeay' => '1.49',
122             'IO::Socket::SSL' => '1.56',
123             };
124             }
125              
126             sub _has_ssl
127             {
128 6     6   2622 my %reqs = %{ _ssl_reqs() };
  6         28  
129 6         16 eval {
130 6         1161 require Net::SSLeay;
131 6 50       21263 die "need Net::SSLeay $reqs{'Net::SSLeay'}" unless Net::SSLeay->VERSION($reqs{'Net::SSLeay'});
132 6         1631 require IO::Socket::SSL;
133 6 50       101367 die "need IO::Socket::SSL $reqs{'IO::Socket::SSL'}" unless IO::Socket::SSL->VERSION($reqs{'IO::Socket::SSL'});
134             };
135 6         41 $@ eq '';
136             }
137              
138             1;
139              
140             __END__