| 1 | package ShipIt::Util; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 5 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 6 | 2 | our @EXPORT_OK = qw(slurp write_file bool_prompt edit_file $term make_var tempdir_obj | |||||||||||||||||||||||||||
| 7 | find_subclasses in_dir); | ||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Term::ReadLine (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Path (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $term = eval { | |||||||||||||||||||||||||||
| 14 | 2 | Term::ReadLine->new("prompt"); | |||||||||||||||||||||||||||
| 15 | }; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 7 | ▶ | sub slurp { | ||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 7 | my ($file) = @_; | |||||||||||||||||||||||||||
| 19 | 7 | ▶ | open (my $fh, $file) or confess "Failed to open $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 7 | return do { local $/; binmode $fh; <$fh>; } | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | sub write_file { | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 7 | my ($file, $contents) = @_; | |||||||||||||||||||||||||||
| 25 | 7 | ▶ | open (my $fh, ">", $file) or confess "Failed to open $file for write: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 7 | binmode $fh; | |||||||||||||||||||||||||||
| 27 | 7 | print $fh $contents; | |||||||||||||||||||||||||||
| 28 | 7 | ▶ | close($fh) or confess "Close failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 7 | ▶ | die "assert" unless -s $file == length($contents); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 7 | return 1; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub bool_prompt { | ||||||||||||||||||||||||||
|
Subroutine
bool_prompt: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 0 | my ($q, $def) = @_; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | $def = uc($def || ""); | ||||||||||||||||||||||||||
|
Condition0%0/2
$def || ''
MC/DC0%0/1
$def || ''
$def
Truth table0%0/2
A
A= $def
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | die "bogus default" unless $def =~ /^[YN]?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return $def if !$term; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | my $opts = " [y/n]"; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | $opts = " [Y/n]" if $def eq "Y"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $opts = " [y/N]" if $def eq "N"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $to_bool = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 42 | 0 | my $yn = shift; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | return 1 if $yn =~ /^y/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | return 0 if $yn =~ /^n/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | return undef; | |||||||||||||||||||||||||||
| 46 | 0 | }; | |||||||||||||||||||||||||||
| 47 | 0 | while (1) { | |||||||||||||||||||||||||||
| 48 | 0 | my $ans = $term->readline("$q$opts "); | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $bool = $to_bool->($ans || $def); | ||||||||||||||||||||||||||
|
Condition0%0/3
$ans || $def
MC/DC0%0/2
$ans || $def
$ans
$def
Truth table0%0/3
A || B
A= $ansB= $def
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return $bool if defined $bool; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | warn "Please answer 'y' or 'n'\n"; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub edit_file { | ||||||||||||||||||||||||||
|
Subroutine
edit_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $editor = $ENV{"EDITOR"} || "vi"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{'EDITOR'} || 'vi'
MC/DC0%0/1
$ENV{'EDITOR'} || 'vi'
$ENV{'EDITOR'}
Truth table0%0/2
A
A= $ENV{'EDITOR'}
| |||||||||||||||||||||||||||||
| 58 | 0 | system($editor, $file); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub make_var { | ||||||||||||||||||||||||||
|
Subroutine
make_var: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | my $var = shift; | |||||||||||||||||||||||||||
| 63 | 0 | my $file = slurp("Makefile"); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | return undef unless $file =~ /^\Q$var\E\s*=\s*(.+)\s*/m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return $1; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub find_subclasses { | ||||||||||||||||||||||||||
|
Subroutine
find_subclasses: not called CC=4 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | # search for any other custom project type modules | ||||||||||||||||||||||||||||
| 70 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 71 | 0 | my @classes = (); | |||||||||||||||||||||||||||
| 72 | 0 | for my $dir (@INC) { | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | for my $file (glob("$dir/" . join("/", split(/::/, $class)) . "/*.pm")) { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($file =~ /\/(\w+)\.pm/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | push(@classes, "$class" . "::" . "$1"); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | return @classes; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # returns either $obj or ($obj->dir, $obj), when in list context. | ||||||||||||||||||||||||||||
| 83 | # when $obj goes out of scope, all temp directory contents are wiped. | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub tempdir_obj { | ||||||||||||||||||||||||||
|
Subroutine
tempdir_obj: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $dir = File::Temp::tempdir() or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | die "Failed to create temp directory: $!\n"; | ||||||||||||||||||||||||||||
| 87 | 0 | my $obj = bless { | |||||||||||||||||||||||||||
| 88 | dir => $dir, | ||||||||||||||||||||||||||||
| 89 | }, "ShipIt::Util::TempDir"; | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return wantarray ? ($dir, $obj) : $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # run a coderef in another directory, then return to old directory, | ||||||||||||||||||||||||||||
| 94 | # even if $code dies. | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub in_dir { | ||||||||||||||||||||||||||
|
Subroutine
in_dir: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 96 | 0 | my ($dir, $code) = @_; | |||||||||||||||||||||||||||
| 97 | 0 | my $old_cwd = getcwd; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | chdir($dir) or die "chdir to dir $dir failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | my $rv = eval { $code->(); }; | |||||||||||||||||||||||||||
| 100 | 0 | my $err = $@; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | chdir($old_cwd) or die "chdir back to $old_cwd failed: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | die $err if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return $rv; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ############################################################################ | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | package ShipIt::Util::TempDir; | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub directory { $_[0]{dir} }; | ||||||||||||||||||||||||||
|
Subroutine
directory: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | File::Path::rmtree($self->{dir}) if $self->{dir} && -d $self->{dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"dir"} and -d $self->{"dir"}
MC/DC0%0/2
$self->{"dir"} and -d $self->{"dir"}
$self->{"dir"}
-d $self->{"dir"}
Truth table0%0/3
A and B
A= $self->{"dir"}B= -d $self->{"dir"}
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | 1; | |||||||||||||||||||||||||||