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   194901 use strict;
  64         145  
  64         1974  
4 64     64   310 use warnings;
  64         157  
  64         1720  
5 64     64   1071 use 5.008004;
  64         221  
6 64     64   358 use Exporter qw( import );
  64         123  
  64         2478  
7 64     64   2674 use Path::Tiny qw( path );
  64         32476  
  64         3254  
8 64     64   430 use Config;
  64         137  
  64         62827  
9              
10             # ABSTRACT: Private utility functions for Alien::Build
11             our $VERSION = '2.45'; # 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   23665 my($src_root, $dst_root, $opt) = @_;
26 39         126 ($src_root, $dst_root) = map { path($_) } ($src_root, $dst_root);
  78         1460  
27 39   100     1109 $opt ||= {};
28              
29 39         905 require Alien::Build;
30 39         219 require File::Find;
31 39         3217 require File::Copy;
32              
33             File::Find::find({
34             wanted => sub {
35 214 50   214   15354 next unless -e $File::Find::name;
36 214         904 my $src = path($File::Find::name)->relative($src_root);
37 214 100 100     42543 return if $opt->{filter} && "$src" !~ $opt->{filter};
38 188 100       507 return if "$src" eq '.';
39 153         717 my $dst = $dst_root->child("$src");
40 153         4983 $src = $src->absolute($src_root);
41 153 100       10741 if(-l "$src")
    100          
    50          
42             {
43 3 50       56 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       207 if(-e "$dst")
50 0         0 { unlink "$dst" }
51 3         54 my $target = readlink "$src";
52 3 50       39 Alien::Build->log("ln -s $target $dst") if $opt->{verbose};
53 3 50       8 symlink($target, $dst) || die "unable to symlink $target => $dst";
54             }
55             elsif(-d "$src")
56             {
57 57 100       5166 if($opt->{empty_directory})
58             {
59 9 50       29 unless(-d $dst)
60             {
61 9 50       160 Alien::Build->log("mkdir $dst") if $opt->{verbose};
62 9 50       23 mkdir($dst) || die "unable to create directory $dst: $!";
63             }
64             }
65             }
66             elsif(-f "$src")
67             {
68 93 100       4989 unless(-d $dst->parent)
69             {
70 51 100       4355 Alien::Build->log("mkdir -p @{[ $dst->parent ]}") if $opt->{verbose};
  13         66  
71 51         230 $dst->parent->mkpath;
72             }
73             # TODO: rmtree if a directory?
74 93 50       14097 if(-e "$dst")
75 0         0 { unlink "$dst" }
76 93 100       2206 Alien::Build->log("cp $src $dst") if $opt->{verbose};
77 93 50       366 File::Copy::cp("$src", "$dst") || die "copy error $src => $dst: $!";
78 93 0 33     33960 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         13300 no_chdir => 1,
87             }, "$src_root");
88              
89 39         1126 ();
90             }
91              
92             sub _dump
93             {
94 29 50   29   5694 if(eval { require YAML })
  29         4263  
95             {
96 0         0 return YAML::Dump(@_);
97             }
98             else
99             {
100 29         1676 require Data::Dumper;
101 29         12105 return Data::Dumper::Dumper(@_);
102             }
103             }
104              
105             sub _destdir_prefix
106             {
107 16     16   12030 my($destdir, $prefix) = @_;
108 16 50       77 $prefix =~ s{^/?([a-z]):}{$1}i if $^O eq 'MSWin32';
109 16         62 path($destdir)->child($prefix)->stringify;
110             }
111              
112             sub _perl_config
113             {
114 6     6   14 my($key) = @_;
115 6         35 $Config{$key};
116             }
117              
118             sub _ssl_reqs
119             {
120             return {
121 12     12   4058 'Net::SSLeay' => '1.49',
122             'IO::Socket::SSL' => '1.56',
123             };
124             }
125              
126             sub _has_ssl
127             {
128 6     6   2832 my %reqs = %{ _ssl_reqs() };
  6         23  
129 6         20 eval {
130 6         1371 require Net::SSLeay;
131 6 50       23424 die "need Net::SSLeay $reqs{'Net::SSLeay'}" unless Net::SSLeay->VERSION($reqs{'Net::SSLeay'});
132 6         1558 require IO::Socket::SSL;
133 6 50       109839 die "need IO::Socket::SSL $reqs{'IO::Socket::SSL'}" unless IO::Socket::SSL->VERSION($reqs{'IO::Socket::SSL'});
134             };
135 6         46 $@ eq '';
136             }
137              
138             1;
139              
140             __END__