File Coverage

blib/lib/Enbld/Home.pm
Criterion Covered Total %
statement 66 68 97.0
branch 16 18 88.8
condition n/a
subroutine 15 16 93.7
pod 0 3 0.0
total 97 105 92.3


line stmt bran cond sub pod time code
1             package Enbld::Home;
2              
3 4     4   884 use strict;
  4         4  
  4         91  
4 4     4   11 use warnings;
  4         4  
  4         78  
5              
6 4     4   10 use Carp;
  4         4  
  4         179  
7 4     4   14 use File::Spec;
  4         7  
  4         96  
8 4     4   16 use File::Path qw/make_path remove_tree/;
  4         5  
  4         155  
9 4     4   12 use autodie;
  4         7  
  4         36  
10 4     4   12812 use Time::Piece;
  4         20980  
  4         19  
11 4     4   252 use Time::Seconds;
  4         4  
  4         2920  
12              
13             require Enbld::Feature;
14             require Enbld::Error;
15             require Enbld::Exception;
16              
17             our $dirs = {
18             home => undef,
19             dists => undef,
20             depository => undef,
21             build => undef,
22             conf => undef,
23             log => undef,
24             etc => undef,
25             extlib => undef,
26             library => undef,
27             deploy_path => undef,
28             };
29              
30             sub initialize {
31 36     36 0 15046 my $pkg = shift;
32              
33 36         156 $pkg->_create_home_directory;
34 36 100       202 $pkg->_set_deploy_directory if Enbld::Feature->is_deploy_mode;
35 36         132 $pkg->_delete_old_build_directory;
36              
37 36         93 return $dirs->{home};
38             }
39              
40             sub AUTOLOAD {
41 666     666   2539 my $method = our $AUTOLOAD;
42 666         1959 $method =~ s/.*:://;
43              
44 666 100       1498 unless ( exists $dirs->{$method} ) {
45 1         2 my $err = "Can't execute Enbld::Home's method.";
46 1         6 croak( Enbld::Exception->new( $err, $method ));
47             }
48              
49 665 100       1069 unless ( $dirs->{$method} ) {
50 1         1 my $err = "Not initialized Enbld home's path yet.";
51 1         5 croak( Enbld::Exception->new( $err ));
52             }
53              
54 664         3816 return $dirs->{$method};
55             }
56              
57             sub create_build_directory {
58              
59             make_path( $dirs->{build} = File::Spec->catdir(
60 29     29 0 3510 $dirs->{home}, 'build', time
61             ));
62              
63 29         88 return $dirs->{build};
64             }
65              
66             sub install_path {
67              
68 48 100   48 0 266 return Enbld::Home->deploy_path if Enbld::Feature->is_deploy_mode;
69 45         224 return Enbld::Home->home;
70              
71             }
72              
73             sub _create_home_directory {
74 36     36   61 my $pkg = shift;
75              
76             $dirs->{home} = $ENV{PERL_ENBLD_HOME} ? $ENV{PERL_ENBLD_HOME} :
77 36 100       197 File::Spec->catdir( $ENV{HOME}, '.enbld' );
78              
79 36         59 my $error;
80 36         5084 make_path( $dirs->{home}, { error => \$error } );
81              
82 36 50       93 if ( @{ $error } ) {
  36         137  
83 0         0 die( Enbld::Error->new(
84             "Can't create Enbld's home directory:$dirs->{home}\n" .
85             "Please check write permission for $dirs->{home}",
86             ));
87             }
88              
89 36 50       429 if ( ! -w $dirs->{home} ) {
90 0         0 die( Enbld::Error->new(
91             "No permission to write directory:$dirs->{home}" .
92             "Please check write permission for $dirs->{home}"
93             ));
94             }
95              
96 36         24587 make_path( $dirs->{$_} = File::Spec->catdir( $dirs->{home}, $_ )) for
97             qw/dists build depository conf etc log extlib/;
98              
99 36         117 $dirs->{library} = $dirs->{home};
100              
101 36         81 return $dirs->{home};
102             }
103              
104             sub _set_deploy_directory {
105              
106 4     4   42 my $deploy_path = Enbld::Feature->deploy_path;
107              
108 4         11 $dirs->{deploy_path} = $deploy_path;
109 4         11 $dirs->{library} = $deploy_path;
110              
111 4         441 return $dirs->{deploy_path};
112             }
113              
114             sub _delete_old_build_directory {
115              
116 36     36   159 opendir my $dh, $dirs->{build};
117 36         6461 my @list = grep { !/^\.{1,2}$/ } readdir $dh;
  75         306  
118 36         152 closedir $dh;
119              
120 36         2917 my $limit = localtime;
121 36         3729 $limit -= ONE_MONTH;
122              
123 36         2374 foreach my $build_dir ( @list ) {
124 3 100       20 next if ( $build_dir !~ /\d{10}/ ); # for skip .DS_Store
125              
126 2 100       5 if ( $limit->epoch > $build_dir ) {
127 1         214 remove_tree( File::Spec->catdir( $dirs->{build}, $build_dir ) );
128             }
129             }
130             }
131              
132       0     sub DESTROY {
133             # do nothing.
134             }
135              
136             1;