| 1 | package Minilla::Util; | ||||||||||||||||||||
| 2 | 76 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 76 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 76 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 76 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 76 | ▶ | use File::Basename (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 76 | ▶ | use File::Spec (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 76 | ▶ | use File::Which 'which'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 76 | ▶ | use Minilla::Logger (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 76 | ▶ | use Getopt::Long (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 76 | ▶ | use Cwd(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 76 | ▶ | use parent qw(Exporter); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 76 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 16 | find_dir find_file | ||||||||||||||||||||
| 17 | randstr | ||||||||||||||||||||
| 18 | slurp slurp_utf8 slurp_raw | ||||||||||||||||||||
| 19 | spew spew_utf8 spew_raw | ||||||||||||||||||||
| 20 | edit_file require_optional | ||||||||||||||||||||
| 21 | cmd cmd_perl | ||||||||||||||||||||
| 22 | pod_escape | ||||||||||||||||||||
| 23 | parse_options | ||||||||||||||||||||
| 24 | check_git | ||||||||||||||||||||
| 25 | is_interactive | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 76 | our %EXPORT_TAGS = ( | |||||||||||||||||||
| 29 | all => \@EXPORT_OK | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub randstr { | ||||||||||||||||||
|
Subroutine
randstr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 33 | 0 | my $len = shift; | |||||||||||||||||||
| 34 | 0 | my @chars = ("a".."z","A".."Z",0..9); | |||||||||||||||||||
| 35 | 0 | my $ret = ''; | |||||||||||||||||||
| 36 | 0 | join('', map { $chars[int(rand(scalar(@chars)))] } 1..$len); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 9 | ▶ | sub slurp { | ||||||||||||||||||
|
Subroutine
slurp: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 40 | 9 | my $fname = shift; | |||||||||||||||||||
| 41 | 9 | ▶ | open my $fh, '<', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | or Carp::croak("Can't open '$fname' for reading: '$!'"); | ||||||||||||||||||||
| 43 | 9 | scalar do { local $/; <$fh> } | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | sub slurp_utf8 { | ||||||||||||||||||
|
Subroutine
slurp_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 47 | 0 | my $fname = shift; | |||||||||||||||||||
| 48 | 0 | ▶ | open my $fh, '<:encoding(UTF-8)', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | or Carp::croak("Can't open '$fname' for reading: '$!'"); | ||||||||||||||||||||
| 50 | 0 | scalar do { local $/; <$fh> } | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 11 | ▶ | sub slurp_raw { | ||||||||||||||||||
|
Subroutine
slurp_raw: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 54 | 11 | my $fname = shift; | |||||||||||||||||||
| 55 | 11 | ▶ | open my $fh, '<:raw', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | or Carp::croak("Can't open '$fname' for reading: '$!'"); | ||||||||||||||||||||
| 57 | 11 | scalar do { local $/; <$fh> } | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub spew($$) { | ||||||||||||||||||
|
Subroutine
spew: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 61 | 0 | my $fname = shift; | |||||||||||||||||||
| 62 | 0 | ▶ | open my $fh, '>', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | or Carp::croak("Can't open '$fname' for writing: '$!'"); | ||||||||||||||||||||
| 64 | 0 | print {$fh} $_[0]; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 17 | ▶ | sub spew_raw { | ||||||||||||||||||
|
Subroutine
spew_raw: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 68 | 17 | my $fname = shift; | |||||||||||||||||||
| 69 | 17 | ▶ | open my $fh, '>:raw', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | or Carp::croak("Can't open '$fname' for writing: '$!'"); | ||||||||||||||||||||
| 71 | 17 | print {$fh} $_[0]; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | sub spew_utf8 { | ||||||||||||||||||
|
Subroutine
spew_utf8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 75 | 0 | my $fname = shift; | |||||||||||||||||||
| 76 | 0 | ▶ | open my $fh, '>:encoding(UTF8)', $fname | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | or Carp::croak("Can't open '$fname' for writing: '$!'"); | ||||||||||||||||||||
| 78 | 0 | print {$fh} $_[0]; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 1 | ▶ | sub edit_file { | ||||||||||||||||||
|
Subroutine
edit_file: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 82 | 1 | my ($file) = @_; | |||||||||||||||||||
| 83 | 1 | ▶ | my $editor = $ENV{"EDITOR"} || "vi"; | ||||||||||||||||||
|
Condition50%1/2
$ENV{'EDITOR'} || 'vi'
MC/DC0%0/1
$ENV{'EDITOR'} || 'vi'
$ENV{'EDITOR'}
Truth table50%1/2
A
A= $ENV{'EDITOR'}
| |||||||||||||||||||||
| 84 | 1 | system qq{$editor "$file"}; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 8 | ▶ | sub is_interactive { | ||||||||||||||||||
|
Subroutine
is_interactive: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 88 | 8 | return -t STDIN; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | sub find_file { | ||||||||||||||||||
|
Subroutine
find_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 92 | 0 | my ($file) = @_; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my $dir = Cwd::getcwd(); | |||||||||||||||||||
| 95 | 0 | my %seen; | |||||||||||||||||||
| 96 | 0 | ▶ | while ( -d $dir ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | return undef if $seen{$dir}++; # guard from deep recursion | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 0 | ▶ | if ( -f "$dir/$file" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | return "$dir/$file"; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 0 | $dir = File::Basename::dirname($dir); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | return undef; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub find_dir { | ||||||||||||||||||
|
Subroutine
find_dir: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 108 | 0 | my ($file) = @_; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 0 | my $dir = Cwd::getcwd(); | |||||||||||||||||||
| 111 | 0 | my %seen; | |||||||||||||||||||
| 112 | 0 | ▶ | while ( -d $dir ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | ▶ | return undef if $seen{$dir}++; # guard from deep recursion | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 0 | ▶ | if ( -d "$dir/$file" ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | return "$dir/$file"; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | 0 | $dir = File::Basename::dirname($dir); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | return undef; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 1 | ▶ | sub require_optional { | ||||||||||||||||||
|
Subroutine
require_optional: called CC=5 SCAR=30.3
Pod
undocumented
| |||||||||||||||||||||
| 124 | 1 | my ( $file, $feature, $library ) = @_; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 1 | ▶ | return if exists $INC{$file}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | ▶ | unless ( eval { require $file } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 0 | ▶ | if ( $@ =~ /^Can't locate/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | ▶ | $library ||= do { | ||||||||||||||||||
|
Condition0%0/3
$library ||= do {
local $_ = $file;
s/ \.pm \z//msx;
s[/][::]g;
$_
}
MC/DC0%0/2
$library ||= do {
local $_ = $file;
s/ \.pm \z//msx;
s[/][::]g;
$_
}
$library
do {
local $_ = $file;
s/ \.pm \z//msx;
s[/][::]g;
$_
}
Truth table0%0/3
A ||= B
A= $libraryB= do {
local $_ = $file;
s/ \.pm \z//msx;
s[/][::]g;
$_
}
| |||||||||||||||||||||
| 130 | 0 | local $_ = $file; | |||||||||||||||||||
| 131 | 0 | s/ \.pm \z//xms; | |||||||||||||||||||
| 132 | 0 | s{/}{::}g; | |||||||||||||||||||
| 133 | 0 | $_; | |||||||||||||||||||
| 134 | }; | ||||||||||||||||||||
| 135 | 0 | Carp::croak( "$feature requires $library, but it is not available." | |||||||||||||||||||
| 136 | . " Please install $library using your preferred CPAN client" ); | ||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||
| 139 | 0 | die $@; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | sub cmd_perl { | ||||||||||||||||||
|
Subroutine
cmd_perl: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 145 | 0 | my(@args) = @_; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | my @abs_inc = map { $_ eq '.' ? $_ : File::Spec->rel2abs($_) } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | @INC; | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | cmd($^X, (map { "-I$_" } @abs_inc), @args); | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 0 | ▶ | sub cmd { | ||||||||||||||||||
|
Subroutine
cmd: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 154 | 0 | Minilla::Logger::infof("[%s] \$ %s\n", File::Basename::basename(Cwd::getcwd()), "@_"); | |||||||||||||||||||
| 155 | 0 | ▶ | system(@_) == 0 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 156 | or Minilla::Logger::errorf("Giving up.\n"); | ||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub parse_options { | ||||||||||||||||||
|
Subroutine
parse_options: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 160 | 0 | my ( $args, @spec ) = @_; | |||||||||||||||||||
| 161 | 0 | Getopt::Long::GetOptionsFromArray( $args, @spec ); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | ▶ | sub pod_escape { | ||||||||||||||||||
|
Subroutine
pod_escape: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 165 | 0 | local $_ = shift; | |||||||||||||||||||
| 166 | 0 | my %POD_ESCAPE = ( '<' => 'E<lt>', '>' => 'E<gt>' ); | |||||||||||||||||||
| 167 | 0 | s!([<>])!$POD_ESCAPE{$1}!ge; | |||||||||||||||||||
| 168 | 0 | $_; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | ▶ | sub check_git { | ||||||||||||||||||
|
Subroutine
check_git: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 172 | 0 | ▶ | unless (which 'git') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 173 | 0 | Minilla::Logger::errorf("The \"git\" executable has not been found.\n"); | |||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 76 | 1; | |||||||||||||||||||