blib/lib/Shipwright/Util.pm

filter: Statementstmt 80.8% 93 / 115 Branchbran 70.4% 31 / 44 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 91.3% 21 / 23 Podpod 100% 13 / 13 totaltotal 73.8% 161 / 218 CC 30 SCAR 20.9
CC
30
Cov
73%
run_cmd
20.0
user_home
12.0
share_root
9.3
CRAP 8.1
« blib/lib/Shipwright/Script/Defaultbranch.pm ↑ Summary blib/lib/Shipwright/Source/Git.pm »
1 package Shipwright::Util;
2  
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use File::Spec::Functions qw/catfile catdir splitpath splitdir tmpdir rel2abs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Cwd qw/abs_path getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Shipwright; # we need this to find where Shipwright.pm lives
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 our @EXPORT = qw/load_yaml load_yaml_file dump_yaml dump_yaml_file run_cmd
13 select_fh shipwright_root share_root user_home confess_or_die
14 shipwright_user_root parent_dir find_module/;
15  
16 15 our ( $SHIPWRIGHT_ROOT, $SHARE_ROOT );
17  
18 1 ▶sub load_yaml {
Subroutine load_yaml: called CC=1 SCAR=0.0
Pod documented
19 1 goto &YAML::Tiny::Load;
20 }
21  
22 4 ▶sub load_yaml_file {
Subroutine load_yaml_file: called CC=1 SCAR=0.0
Pod documented
23 4 goto &YAML::Tiny::LoadFile;
24 }
25  
26 1 ▶sub dump_yaml {
Subroutine dump_yaml: called CC=1 SCAR=0.0
Pod documented
27 1 goto &YAML::Tiny::Dump;
28 }
29  
30 3 ▶sub dump_yaml_file {
Subroutine dump_yaml_file: called CC=1 SCAR=0.0
Pod documented
31 3 goto &YAML::Tiny::DumpFile;
32 }
33  
34  
35 =head1 LIST
36  
37 =head2 General Helpers
38  
39 =head3 load_yaml, load_yaml_file, dump_yaml, dump_yaml_file
40  
41 they are just dropped in from YAML::Tiny
42  
43 =head3 confess_or_die
44  
45 =cut
46  
47 9 ▶sub confess_or_die {
Subroutine confess_or_die: called CC=2 SCAR=8.1
Pod documented
48 9 ▶ if ( $ENV{SHIPWRIGHT_DEVEL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'SHIPWRIGHT_DEVEL'}) { } 0 9 9
49 0 goto &confess;
50 }
51 else {
52 9 die @_,"\n";
53 }
54 }
55  
56 =head3 parent_dir
57  
58 return the dir's parent dir, the arg must be a dir path
59  
60 =cut
61  
62 0 ▶sub parent_dir {
Subroutine parent_dir: not called CC=1 SCAR=6.9
Pod documented
63 0 my $dir = shift;
64 0 my @dirs = splitdir($dir);
65 0 pop @dirs;
66 0 return catdir(@dirs);
67 }
68  
69  
70 =head3 run_cmd
71  
72 a wrapper of run3 sub in IPC::Run3.
73  
74 =cut
75  
76 9 ▶sub run_cmd {
Subroutine run_cmd: called CC=13 SCAR=29.9
Pod documented
77 9 my $cmd = shift;
78 9 my $ignore_failure = shift;
79  
80 9 ▶ if ( ref $cmd eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq "CODE") 7 2 9
81 7 my @returns;
82 7 ▶ if ( $ignore_failure ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore_failure) { } 6 1 7
83 6 @returns = eval { $cmd->() };
84 }
85 else {
86 1 @returns = $cmd->();
87 }
88 7 ▶ return wantarray ? @returns : $returns[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 3 7
89 }
90  
91 2 my $log = Log::Log4perl->get_logger('Shipwright::Util');
92  
93 2 my ( $out, $err );
94 2 $log->info( "running cmd: " . join ' ', @$cmd );
95 2 select_fh('null');
96 2 run3( $cmd, undef, \$out, \$err );
97 2 select_fh('stdout');
98  
99 2 ▶ $log->debug("output:\n$out") if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 1 1 2
100 2 ▶ $log->error("err:\n$err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 1 2
101  
102 2 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 1 1 2
103 1 $log->error(
104 'failed to run ' . join( ' ', @$cmd ) . " with exit number $?" );
105 1 ▶ unless ($ignore_failure) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_failure) 0 1 1
106 0 ▶ $out = "\n$out" if length $out;
Branch: 1 branch
BranchTrueFalseTotal
if length $out 0 0 0
107 0 ▶ $err = "\n$err" if length $err;
Branch: 1 branch
BranchTrueFalseTotal
if length $err 0 0 0
108 0 my $suggest = '';
109 0 ▶ if ( $err && $err =~ /Can't locate (\S+)\.pm in \@INC/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and $err =~ /Can't locate (\S+)\.pm in \@INC/) 0 0 0
Condition0%0/3
$err and $err =~ /Can't locate (\S+)\.pm in \@INC/
!ll&&!rl&&r
000
MC/DC0%0/2
$err and $err =~ /Can't locate (\S+)\.pm in \@INC/
$err $err =~ /Can't locate (\S+)\.pm in \@INC/
Truth table0%0/3
A and B
A= $errB= $err =~ /Can't locate (\S+)\.pm in \@INC/
ABresult
0X0
100
111
110 0 my $module = $1;
111 0 $module =~ s!/!::!g;
112 0 $suggest = "install $module first";
113 }
114  
115 0 my $cwd = getcwd;
116 0 confess_or_die <<"EOF";
117 command failed: @$cmd
118 \$?: $?
119 cwd: $cwd
120 stdout was: $out
121 stderr was: $err
122 suggest: $suggest
123 EOF
124 }
125  
126 }
127  
128 2 ▶ return wantarray ? ( $out, $err ) : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
129  
130 }
131  
132 =head3 select_fh
133  
134 wrapper for the select in core
135  
136 =cut
137  
138 15 my ( $null_fh, $stdout_fh, $cpan_fh, $cpan_log_path, $cpan_fh_flag );
139  
140 # use $cpan_fh_flag to record if we've selected cpan_fh before, so so,
141 # we don't need to warn that any more.
142  
143 15 open $null_fh, '>', '/dev/null';
144  
145 15 $cpan_log_path = catfile( tmpdir(), 'shipwright_cpan.log');
146  
147 15 open $cpan_fh, '>>', $cpan_log_path;
148 15 $stdout_fh = select;
149  
150 8 ▶sub select_fh {
Subroutine select_fh: called CC=5 SCAR=16.1
Pod documented
151 8 my $type = shift;
152  
153 8 ▶ if ( $type eq 'null' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'null') { } 3 5 8
elsif ($type eq 'stdout') { } 2 3 5
elsif ($type eq 'cpan') { } 2 1 3
154 3 select $null_fh;
155 }
156 elsif ( $type eq 'stdout' ) {
157 2 select $stdout_fh;
158 }
159 elsif ( $type eq 'cpan' ) {
160 2 ▶ warn "CPAN related output will be at $cpan_log_path\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $cpan_fh_flag 1 1 2
161 unless $cpan_fh_flag;
162 2 $cpan_fh_flag = 1;
163 2 select $cpan_fh;
164 }
165 else {
166 1 confess_or_die "unknown type: $type";
167 }
168 }
169  
170 =head3 find_module
171  
172 Takes perl modules name space and name of a module in the space.
173 Finds and returns matching module name using case insensitive search, for
174 example:
175  
176 find_module('Shipwright::Backend', 'svn');
177 # returns 'Shipwright::Backend::SVN'
178  
179 find_module('Shipwright::Backend', 'git');
180 # returns 'Shipwright::Backend::Git'
181  
182 Returns undef if there is no module matching criteria.
183  
184 =cut
185  
186 12 ▶sub find_module {
Subroutine find_module: called CC=4 SCAR=13.9
Pod documented
187 12 my $space = shift;
188 12 my $name = shift;
189  
190 12 my @space = split /::/, $space;
191 12 my @globs = map File::Spec->catfile($_, @space, '*.pm'), @INC;
192 12 foreach my $glob ( @globs ) {
193 14 ▶ foreach my $module ( map { /([^\\\/]+)\.pm$/; $1 } glob $glob ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
194 38 ▶ return join '::', @space, $module
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq lc $module 12 26 38
195 if lc $name eq lc $module;
196 }
197 }
198 0 return;
199 }
200  
201 =head2 PATHS
202  
203 =head3 shipwright_root
204  
205 Returns the root directory that Shipwright has been installed into.
206 Uses %INC to figure out where Shipwright.pm is.
207  
208 =cut
209  
210 6 ▶sub shipwright_root {
Subroutine shipwright_root: called CC=2 SCAR=6.9
Pod documented
211 6 ▶ unless ($SHIPWRIGHT_ROOT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Shipwright::Util::SHIPWRIGHT_ROOT) 4 2 6
212 4 my $dir = ( splitpath( $INC{"Shipwright.pm"} ) )[1];
213 4 $SHIPWRIGHT_ROOT = rel2abs($dir);
214 }
215 6 return ($SHIPWRIGHT_ROOT);
216 }
217  
218 =head3 share_root
219  
220 Returns the 'share' directory of the installed Shipwright module. This is
221 currently only used to store the initial files in project.
222  
223 =cut
224  
225 5 ▶sub share_root {
Subroutine share_root: called CC=6 SCAR=22.3
Pod documented
226 5 ▶ unless ($SHARE_ROOT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Shipwright::Util::SHARE_ROOT) 4 1 5
227 4 my @root = splitdir( shipwright_root() );
228  
229 4 ▶ if ( $root[-2] ne 'blib'
Branch: 1 branch
BranchTrueFalseTotal
if ($root[-2] ne 'blib' and $root[-1] eq 'lib' and !($^O =~ /MSWin/) || $root[-2] ne 'site') { } 0 4 4
Condition22%2/9
$root[-2] ne "blib" and $root[-1] eq "lib" and !($^O =~ /MSWin/) || $root[-2] ne "site"
!ll&&!rl&&r
400
$root[-2] ne "blib" and $root[-1] eq "lib"
!ll&&!rl&&r
400
not $^O =~ /MSWin/ or $root[-2] ne "site"
l!l&&r!l&&!r
000
MC/DC0%0/4
$root[-2] ne "blib" and $root[-1] eq "lib" and !($^O =~ /MSWin/) || $root[-2] ne "site"
$root[-2] ne "blib" $root[-1] eq "lib" not $^O =~ /MSWin/ $root[-2] ne "site"
Truth table20%1/5
A and B and not(C) or D
A= $root[-2] ne "blib"B= $root[-1] eq "lib"C= not $^O =~ /MSWin/D= $root[-2] ne "site"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
230 && $root[-1] eq 'lib'
231 && ( $^O !~ /MSWin/ || $root[-2] ne 'site' ) )
232 {
233  
234 # so it's -Ilib in the Shipwright's source dir
235 0 $root[-1] = 'share';
236 }
237 else {
238 4 push @root, qw/auto share dist Shipwright/;
239 }
240  
241 4 $SHARE_ROOT = catdir(@root);
242 }
243  
244 5 return ($SHARE_ROOT);
245  
246 }
247  
248 =head3 user_home
249  
250 return current user's home directory
251  
252 =cut
253  
254 0 ▶sub user_home {
Subroutine user_home: not called CC=3 SCAR=24.8
Pod documented
255 0 ▶ return $ENV{HOME} if $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HOME"} 0 0 0
256  
257 0 my $home = eval { (getpwuid $<)[7] };
258 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
259 0 confess_or_die "can't find user's home, please set it by env HOME";
260 }
261 else {
262 0 return $home;
263 }
264 }
265  
266 =head3 shipwright_user_root
267  
268 the user's own shipwright root where we put internal files in.
269 it's ~/.shipwright by default.
270 it can be overwritten by $ENV{SHIPWRIGHT_USER_ROOT}
271  
272 =cut
273  
274 12 ▶sub shipwright_user_root {
Subroutine shipwright_user_root: called CC=2 SCAR=9.2
Pod documented
275 12 ▶ return $ENV{SHIPWRIGHT_USER_ROOT} || catdir( user_home, '.shipwright' );
Condition33%1/3
$ENV{'SHIPWRIGHT_USER_ROOT'} || catdir(user_home(), '.shipwright')
l!l&&r!l&&!r
1200
MC/DC0%0/2
$ENV{'SHIPWRIGHT_USER_ROOT'} || catdir(user_home(), '.shipwright')
$ENV{'SHIPWRIGHT_USER_ROOT'} catdir(user_home(), '.shipwright')
Truth table33%1/3
A || B
A= $ENV{'SHIPWRIGHT_USER_ROOT'}B= catdir(user_home(), '.shipwright')
ABresult
1X1
011
000
276 }
277  
278 15 1;
279  
280 __END__
« blib/lib/Shipwright/Script/Defaultbranch.pm ↑ Summary blib/lib/Shipwright/Source/Git.pm »