blib/lib/Kook/Commands.pm

filter: Statementstmt 98.1% 215 / 219 Branchbran 76.9% 137 / 178 Conditioncond 57.8% 11 / 19 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 97.7% 44 / 45 Podpod 0.0% 0 / 20 totaltotal 83.9% 412 / 491 CC 107 SCAR 22.3
CC
107
Cov
87%
_mv
18.9
_store
17.5
_cp
13.1
CRAP 9.3
« blib/lib/Kook/Kitchen.pm ↑ Summary blib/lib/Kook/Cookbook.pm »
1 ###
2 ### $Release: 0.0100 $
3 ### $Copyright: copyright(c) 2009-2011 kuwata-lab.com all rights reserved. $
4 ### $License: MIT License $
5 ###
6  
7 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 package Kook::Commands;
12 39 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 our @EXPORT_OK = qw(sys sys_f echo echo_n cp cp_p cp_r cp_pr mkdir mkdir_p rm rm_r rm_f rm_rf rmdir mv store store_p cd edit);
14 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 ▶use File::Basename; # basename()
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use File::Path; # mkpath(), rmtree()
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use Cwd; # getcwd()
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 39 ▶use Kook::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 39 ▶use Kook::Misc ('_report_cmd');
Subroutine BEGIN: called CC=1 SCAR=0.0
21 39 ▶use Kook::Util ('read_file', 'write_file', 'has_metachar', 'flatten', 'glob2');
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 0 ▶sub _msg {
Subroutine _msg: not called CC=1 SCAR=6.9
25 0 my ($msg) = @_;
26 0 return $Kook::Config::MESSAGE_PROMPT . $msg . "\n";
27 }
28  
29 57 ▶sub _pr {
Subroutine _pr: called CC=1 SCAR=0.0
30 57 my ($command) = @_;
31 #print _msg($command);
32 57 print $Kook::Config::COMMAND_PROMPT, $command, "\n";
33 }
34  
35 78 ▶sub _prepare {
Subroutine _prepare: called CC=3 SCAR=11.0
36 78 my ($cmd, @filenames) = @_;
37 78 ▶ _report_cmd("$cmd " . join(' ', @filenames)) if $cmd;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd 78 0 78
38 78 my @arr;
39 #my @fnames = map { has_metachar($_) ? ((@arr = glob2($_)) ? @arr : $_) : $_ } @filenames;
40 78 ▶ my @fnames = map { (@arr = glob2($_)) ? @arr : $_ } flatten(@filenames);
Branch: 1 branch
BranchTrueFalseTotal
(@arr = glob2($_)) ? : 142 6 148
41 78 return @fnames;
42 }
43  
44 41 ▶sub _touch {
Subroutine _touch: called CC=1 SCAR=0.0
45 41 my ($src, $dst) = @_;
46 41 my $mtime = (stat $src)[9];
47 41 utime $mtime, $mtime, $dst;
48 }
49  
50  
51 ## invoke os-depend command. if command failed then die.
52 ## ex.
53 ## sys("gcc -o hello hello.o");
54 59 ▶sub sys {
Subroutine sys: called CC=4 SCAR=13.9
Pod undocumented
55 59 my $command = shift @_;
56 59 ▶ _pr($command) if $Kook::Config::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::VERBOSE 55 4 59
57 59 ▶ return 0 if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 4 55 59
58 55 my $status = system($command);
59 55 ▶ $status == 0 or die "*** command failed (status=$status).\n";
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 or die "*** command failed (status=$status).\n" 54 1 55
60 54 return $status;
61 }
62  
63 ## similar to sys() but never die even when os-command failed.
64 2 ▶sub sys_f {
Subroutine sys_f: called CC=3 SCAR=11.4
Pod undocumented
65 2 my $command = shift @_;
66 2 ▶ _pr($command) if $Kook::Config::VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::VERBOSE 2 0 2
67 2 ▶ return 0 if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 2 2
68 2 return system($command);
69 }
70  
71  
72 ## print argument
73 4 ▶sub echo {
Subroutine echo: called CC=1 SCAR=0.0
Pod undocumented
74 4 _echo("echo", "echo", 0, @_);
75 }
76  
77 ## similar to echo() but not print newline
78 2 ▶sub echo_n {
Subroutine echo_n: called CC=1 SCAR=0.0
Pod undocumented
79 2 _echo("echo", "echo", 1, @_);
80 }
81  
82 6 ▶sub _echo {
Subroutine _echo: called CC=5 SCAR=16.1
83 6 my ($func, $cmd, $n, @filenames) = @_;
84 6 @filenames = _prepare $cmd, @filenames;
85 6 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 6 6
86 6 my $i = 0;
87 6 for (@filenames) {
88 11 ▶ print " " if $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $i++ 5 6 11
89 11 print $_;
90 }
91 6 ▶ print "\n" unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 4 2 6
92 }
93  
94  
95 ## copy files or directories
96 6 ▶sub cp {
Subroutine cp: called CC=1 SCAR=0.0
Pod undocumented
97 6 _cp('cp', 'cp', 0, 0, @_);
98 }
99  
100 6 ▶sub cp_p {
Subroutine cp_p: called CC=1 SCAR=0.0
Pod undocumented
101 6 _cp('cp_p', 'cp -p', 1, 0, @_);
102 }
103  
104 6 ▶sub cp_r {
Subroutine cp_r: called CC=1 SCAR=0.0
Pod undocumented
105 6 _cp('cp_r', 'cp -r', 0, 1, @_);
106 }
107  
108 6 ▶sub cp_pr {
Subroutine cp_pr: called CC=1 SCAR=0.0
Pod undocumented
109 6 _cp('cp_pr', 'cp -pr', 1, 1, @_);
110 }
111  
112 24 ▶sub _cp {
Subroutine _cp: called CC=13 SCAR=25.8
113 24 my ($func, $cmd, $p, $r, @filenames) = @_;
114 24 @filenames = _prepare $cmd, @filenames;
115 24 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 24 24
116 24 my $n = @filenames;
117 24 ▶ $n >= 2 or die "func: at least two file or directory names are required.";
Branch: 1 branch
BranchTrueFalseTotal
$n >= 2 or die "func: at least two file or directory names are required." 24 0 24
118 24 my $dst = pop @filenames;
119 #if ($n == 2) {
120 # -e $src or "$func: $src: no such file or directory.";
121 # if (-d $src) {
122 # $r or "$func: $src: no such file or directory.";
123 # _copy_dir_to_dir($src, $dst, $func, $cmd, $p);
124 # }
125 # elsif (-d $dst) {
126 # _copy_file_to_dir($src, $dst, $func, $cmd, $p);
127 # }
128 # else {
129 # _copy_file_to_file($src, $dst, $func, $cmd, $p);
130 # }
131 #}
132 #else { # $n > 2
133 # -e $dst or "$func: $dst: directory not found.";
134 # -d $dst or "$func: $dst: not a directory.";
135 # for my $src (@filenames) {
136 # -e $src or die "$func: $src: no such file or directory.";
137 # if (-d $src) {
138 # $r or die "$func: $src: cannot copy directory (use 'cp_r' instead).";
139 # _copy_dir_to_dir($src, $dst, $func, $cmd, $p);
140 # }
141 # else {
142 # _copy_file_to_dir($src, $dst, $func, $cmd, $p);
143 # }
144 # }
145 #}
146 24 ▶ if ($n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) { } 14 10 24
147 14 my $src = $filenames[0];
148 14 ▶ ! (-d $src && -f $dst) or die "$func: $src: cannot copy directory to file.\n";
Branch: 1 branch
BranchTrueFalseTotal
(-d $src and -f $dst) and die "${func}: ${src}: cannot copy directory to file.\n" 4 10 14
Condition100%3/3
-d $src and -f $dst
!ll&&!rl&&r
464
MC/DC100%2/2
-d $src and -f $dst
-d $src -f $dst
Truth table100%3/3
A and B
A= -d $srcB= -f $dst
ABresult
0X0
100
111
149 }
150 else { # $n > 2
151 10 ▶ -e $dst or die "$func: $dst: directory not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dst or die "${func}: ${dst}: directory not found.\n" 8 2 10
152 8 ▶ -d $dst or die "$func: $dst: not a directory.\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $dst or die "${func}: ${dst}: not a directory.\n" 8 0 8
153 }
154 18 my $to_dir = -d $dst;
155 18 for my $src (@filenames) {
156 34 ▶ -e $src or die "$func: $src: no such file or directory.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $src or die "${func}: ${src}: no such file or directory.\n" 34 0 34
157 34 ▶ if (-d $src) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $src) { } 10 24 34
elsif ($to_dir) { } 22 2 24
158 10 ▶ $r or die "$func: $src: cannot copy directory (use 'cp_r' instead).\n";
Branch: 1 branch
BranchTrueFalseTotal
$r or die "${func}: ${src}: cannot copy directory (use 'cp_r' instead).\n" 8 2 10
159 8 _copy_dir_to_dir($src, $dst, $func, $p);
160 }
161 elsif ($to_dir) {
162 22 _copy_file_to_dir($src, $dst, $func, $p);
163 }
164 else {
165 2 _copy_file_to_file($src, $dst, $func, $p);
166 }
167 }
168 }
169  
170 58 ▶sub _copy_file_to_file {
Subroutine _copy_file_to_file: called CC=5 SCAR=16.2
171 58 my ($src, $dst, $func, $p) = @_;
172 58 ▶ open(my $in, '<', $src) or die "$func: $src: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $src or die "${func}: ${src}: $!" 58 0 58
173 58 ▶ open(my $out, '>', $dst) or die "$func: $dst: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $dst or die "${func}: ${dst}: $!" 58 0 58
174 58 my ($buf, $size) = (undef, 2*1024*1024);
175 58 ▶ print $out $buf while (read $in, $buf, $size);
Branch: 1 branch
BranchTrueFalseTotal
read $in, $buf, $size and do { print $out $buf } 58 58 116
176 58 close($in);
177 58 close($out);
178 58 ▶ _touch($src, $dst) if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 24 34 58
179 }
180  
181 46 ▶sub _copy_file_to_dir {
Subroutine _copy_file_to_dir: called CC=1 SCAR=0.0
182 46 my ($src, $dst, $func, $p) = @_;
183 46 _copy_file_to_file($src, $dst . '/' . basename($src), $func, $p);
184 }
185  
186 24 ▶sub _copy_dir_to_dir {
Subroutine _copy_dir_to_dir: called CC=10 SCAR=23.1
187 24 my ($src, $dst, $func, $p) = @_;
188 24 ▶ $dst = $dst . '/' . basename($src) if -d $dst;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dst 22 2 24
189 24 ▶ ! -e $dst or die "$func: $dst: already exists.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dst and die "${func}: ${dst}: already exists.\n" 0 24 24
190 24 ▶ mkdir($dst) or die "$func: $dst: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::mkdir $dst or die "${func}: ${dst}: $!" 24 0 24
191 24 ▶ opendir(my $DIR, $src) or die "$func: $src: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $DIR, $src or die "${func}: ${src}: $!" 24 0 24
192 24 my @entries = readdir($DIR);
193 24 closedir($DIR);
194 24 for my $e (@entries) {
195 88 ▶ next if $e eq '.' || $e eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $e eq "." or $e eq ".." 48 40 88
Condition100%3/3
$e eq "." or $e eq ".."
l!l&&r!l&&!r
242440
MC/DC100%2/2
$e eq "." or $e eq ".."
$e eq "." $e eq ".."
Truth table100%3/3
A or B
A= $e eq "."B= $e eq ".."
ABresult
1X1
011
000
196 40 my $fpath = "$src/$e";
197 40 ▶ -d $fpath ? _copy_dir_to_dir($fpath, $dst, $func, $p)
Branch: 1 branch
BranchTrueFalseTotal
-d $fpath ? : 16 24 40
198 : _copy_file_to_dir($fpath, $dst, $func, $p);
199 }
200 24 ▶ _touch($src, $dst) if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 12 12 24
201 }
202  
203  
204 ## create directory
205 3 ▶sub mkdir {
Subroutine mkdir: called CC=1 SCAR=0.0
Pod undocumented
206 3 _mkdir('mkdir', 'mkdir', 0, @_);
207 }
208  
209 2 ▶sub mkdir_p {
Subroutine mkdir_p: called CC=1 SCAR=0.0
Pod undocumented
210 2 _mkdir('mkdir_p', 'mkdir -p', 1, @_);
211 }
212  
213 5 ▶sub _mkdir {
Subroutine _mkdir: called CC=11 SCAR=24.3
214 5 my ($func, $cmd, $p, @dirnames) = @_;
215 5 @dirnames = _prepare($cmd, @dirnames);
216 5 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 5 5
217 5 ▶ @dirnames or die "$func: directory name required.\n";
Branch: 1 branch
BranchTrueFalseTotal
@dirnames or die "${func}: directory name required.\n" 5 0 5
218 5 ▶ if ($p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p) { } 2 3 5
219 2 for my $dname (@dirnames) {
220 3 ▶ -d $dname and next;
Branch: 1 branch
BranchTrueFalseTotal
-d $dname and next 0 3 3
221 3 ▶ -e $dname and die "$func: $dname: already exists.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dname and die "${func}: ${dname}: already exists.\n" 1 2 3
222 2 ▶ mkpath($dname) or die "$func: $dname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkpath($dname) or die "${func}: ${dname}: $!\n" 2 0 2
223 }
224 }
225 else {
226 3 for my $dname (@dirnames) {
227 4 ▶ -e $dname and die "$func: $dname: already exists.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dname and die "${func}: ${dname}: already exists.\n" 1 3 4
228 3 ▶ CORE::mkdir($dname) or die "$func: $dname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::mkdir $dname or die "${func}: ${dname}: $!\n" 2 1 3
229 }
230 }
231 }
232  
233  
234 ## remove files or directories
235 ## ex.
236 ## rm_rf "*/*.o", "doc/*.html", "tmp";
237 3 ▶sub rm {
Subroutine rm: called CC=1 SCAR=0.0
Pod undocumented
238 3 _rm('rm', 'rm', 0, 0, @_);
239 }
240  
241 2 ▶sub rm_r {
Subroutine rm_r: called CC=1 SCAR=0.0
Pod undocumented
242 2 _rm('rm_r', 'rm -r', 1, 0, @_);
243 }
244  
245 2 ▶sub rm_f {
Subroutine rm_f: called CC=1 SCAR=0.0
Pod undocumented
246 2 _rm('rm_f', 'rm -f', 0, 1, @_);
247 }
248  
249 2 ▶sub rm_rf {
Subroutine rm_rf: called CC=1 SCAR=0.0
Pod undocumented
250 2 _rm('rm_rf', 'rm -rf', 1, 1, @_);
251 }
252  
253 9 ▶sub _rm {
Subroutine _rm: called CC=10 SCAR=23.4
254 9 my ($func, $cmd, $r, $f, @filenames) = @_;
255 9 @filenames = _prepare($cmd, @filenames);
256 9 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 9 9
257 9 ▶ @filenames or die "$func: directory name required.\n";
Branch: 1 branch
BranchTrueFalseTotal
@filenames or die "${func}: directory name required.\n" 9 0 9
258 9 for my $fname (@filenames) {
259 15 ▶ if (-d $fname) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $fname) { } 6 9 15
elsif (-e $fname) { } 5 4 9
260 6 ▶ $r or die "$func: $fname: can't remove directory (try 'rm_r' instead).\n";
Branch: 1 branch
BranchTrueFalseTotal
$r or die "${func}: ${fname}: can't remove directory (try 'rm_r' instead).\n" 4 2 6
261 4 ▶ rmtree($fname) or die "$func: $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
rmtree($fname) or die "${func}: ${fname}: $!" 4 0 4
262 }
263 elsif (-e $fname) {
264 5 ▶ unlink($fname) or die "$func: $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $fname or die "${func}: ${fname}: $!" 5 0 5
265 }
266 else {
267 4 ▶ $f or die "$func: $fname: not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
$f or die "${func}: ${fname}: not found.\n" 2 2 4
268 }
269 }
270 }
271  
272  
273 ## remove directory
274 3 ▶sub rmdir {
Subroutine rmdir: called CC=1 SCAR=0.0
Pod undocumented
275 3 _rmdir('rmdir', 'rmdir', @_);
276 }
277  
278 3 ▶sub _rmdir {
Subroutine _rmdir: called CC=7 SCAR=19.8
279 3 my ($func, $cmd, @dirnames) = @_;
280 3 @dirnames = _prepare($cmd, @dirnames);
281 3 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 3 3
282 3 ▶ @dirnames or die "$func: directory name required.\n";
Branch: 1 branch
BranchTrueFalseTotal
@dirnames or die "${func}: directory name required.\n" 3 0 3
283 3 for my $dname (@dirnames) {
284 3 ▶ -e $dname or die "$func: $dname: not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dname or die "${func}: ${dname}: not found.\n" 2 1 3
285 2 ▶ -d $dname or die "$func: $dname: not a directory.";
Branch: 1 branch
BranchTrueFalseTotal
-d $dname or die "${func}: ${dname}: not a directory." 2 0 2
286 2 ▶ CORE::rmdir($dname) or die "$func: $dname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::rmdir $dname or die "${func}: ${dname}: $!\n" 1 1 2
287 }
288 }
289  
290  
291 ## rename or move files or directories
292 ## ex.
293 ## mv "src/*/*.pl", "test/*.pl", "dist";
294 9 ▶sub mv {
Subroutine mv: called CC=1 SCAR=0.0
Pod undocumented
295 9 _mv('mv', 'mv', @_);
296 }
297  
298 9 ▶sub _mv {
Subroutine _mv: called CC=17 SCAR=29.4
299 9 my ($func, $cmd, @filenames) = @_;
300 9 @filenames = _prepare($cmd, @filenames);
301 9 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 9 9
302 9 my $n = @filenames;
303 9 ▶ if ($n < 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n < 2) { } 0 9 9
elsif ($n == 2) { } 7 2 9
304 0 die "$func: at least two file or directory names are required.\n";
305 }
306 elsif ($n == 2) {
307 7 my ($src, $dst) = @filenames;
308 7 ▶ if (! -e $src) { die "$func: $src: not found.\n"; }
Branch: 4 branches
BranchTrueFalseTotal
if (not -e $src) { } 1 6 7
elsif (not -e $dst) { } 2 4 6
elsif (-d $dst) { } 2 2 4
elsif (-d $src) { } 1 1 2
309 2 ▶ elsif (! -e $dst) { rename($src, $dst) or die "$func: $!"; } # any to new
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst or die "${func}: $!" 2 0 2
310 2 ▶ elsif (-d $dst) { rename($src, $dst.'/'.basename($src)) or die "$func: $!"; } # any to dir
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst . '/' . basename($src) or die "${func}: $!" 2 0 2
311 1 elsif (-d $src) { die "$func: $dst: not a directory.\n"; } # dir to file
312 1 ▶ else { rename($src, $dst) or die "$func: $!"; } # file to file
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst or die "${func}: $!" 1 0 1
313 }
314 else {
315 2 my $dst = pop @filenames;
316 2 ▶ -e $dst or die "$func: $dst: directory not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dst or die "${func}: ${dst}: directory not found.\n" 2 0 2
317 2 ▶ -d $dst or die "$func: $dst: not a directory.\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $dst or die "${func}: ${dst}: not a directory.\n" 2 0 2
318 2 for my $src (@filenames) {
319 9 ▶ -e $src or die "$func: $src: not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $src or die "${func}: ${src}: not found.\n" 8 1 9
320 }
321 1 for my $src (@filenames) {
322 4 ▶ rename($src, $dst.'/'.basename($src)) or die "$func: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst . '/' . basename($src) or die "${func}: $!" 4 0 4
323 }
324 }
325 }
326  
327  
328 ## copy files to directory with keeping file path
329 ## ex.
330 ## $dir = "dist/project-1.2.3";
331 ## mkdir_p $dir;
332 ## store "README", "src", "test", "doc", $dir;
333 5 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod undocumented
334 5 _store('store', 'store', 0, @_);
335 }
336  
337 5 ▶sub store_p {
Subroutine store_p: called CC=1 SCAR=0.0
Pod undocumented
338 5 _store('store_p', 'store -p', 1, @_);
339 }
340  
341 10 ▶sub _store {
Subroutine _store: called CC=14 SCAR=28.6
342 10 my ($func, $cmd, $p, @filenames) = @_;
343 10 my @fnames = _prepare($cmd, @filenames);
344 10 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 10 10
345 10 my $n = @fnames;
346 10 ▶ $n >= 2 or die "$func: at least two file or directory names are required.\n";
Branch: 1 branch
BranchTrueFalseTotal
$n >= 2 or die "${func}: at least two file or directory names are required.\n" 8 2 10
347 8 my $basedir = pop @fnames;
348 8 ▶ -e $basedir or die "$func: $basedir: directory not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $basedir or die "${func}: ${basedir}: directory not found.\n" 6 2 8
349 6 ▶ -d $basedir or die "$func: $basedir: not a directory.\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $basedir or die "${func}: ${basedir}: not a directory.\n" 4 2 6
350 4 for my $src (@fnames) {
351 14 ▶ -e $src or die "$func: $src: not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $src or die "${func}: ${src}: not found.\n" 12 2 14
352 }
353 2 for my $src (@fnames) {
354 10 my $dst = $basedir.'/'.$src;
355 10 ▶ if (-d $src) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $src) { } 0 10 10
356 0 ▶ mkpath($dst) or die "$func: $!" unless -d $dst;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dst 0 0 0
Condition0%0/5
-d $dst || (mkpath($dst) || die("${func}: $!"))
l!l&&r!l&&!r
000
mkpath($dst) or die "${func}: $!"
l!l
00
MC/DC0%0/2
-d $dst || (mkpath($dst) || die("${func}: $!"))
-d $dst mkpath($dst)
Truth table0%0/3
A || B
A= -d $dstB= mkpath($dst)
ABresult
1X1
011
000
357 }
358 else {
359 10 my $dirname = dirname($dst);
360 10 ▶ mkpath($dirname) or die "$func: $!" unless -d $dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dirname 4 6 10
Condition60%3/5
-d $dirname || (mkpath($dirname) || die("${func}: $!"))
l!l
640
mkpath($dirname) or die "${func}: $!"
l!l
40
MC/DC0%0/2
-d $dirname || (mkpath($dirname) || die("${func}: $!"))
-d $dirname mkpath($dirname)
Truth table66%2/3
A || B
A= -d $dirnameB= mkpath($dirname)
ABresult
1X1
011
000
361 10 _copy_file_to_file($src, $dst);
362 10 ▶ _touch($src, $dst) if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 5 5 10
363 }
364 }
365 }
366  
367  
368 ## change directory
369 ## ex.
370 ## cd "dist";
371 ## cd "dist", sub { rm "*.o" };
372 5 ▶sub cd {
Subroutine cd: called CC=1 SCAR=0.0
Pod undocumented
373 5 _cd('cd', 'cd', @_);
374 }
375  
376 5 ▶sub _cd {
Subroutine _cd: called CC=8 SCAR=21.1
377 5 my ($func, $cmd, $dir, $closure) = @_;
378 5 my @dirs = _prepare($cmd, $dir);
379 5 ▶ $dir = $dirs[0] or die "$func: directory name required.\n";
Branch: 1 branch
BranchTrueFalseTotal
$dir = $dirs[0] or die "${func}: directory name required.\n" 5 0 5
380 5 ▶ -e $dir or die "$func: $dir: directory not found.\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $dir or die "${func}: ${dir}: directory not found.\n" 4 1 5
381 4 ▶ -d $dir or die "$func: $dir: not a directory.\n";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or die "${func}: ${dir}: not a directory.\n" 3 1 4
382 3 my $cwd = getcwd();
383 3 ▶ CORE::chdir($dir) or die "$func: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $dir or die "${func}: $!" 3 0 3
384 3 ▶ if ($closure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($closure) 1 2 3
385 1 ▶ ref($closure) eq "CODE" or die "$func: 2nd argument should be closure.\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $closure eq "CODE" or die "${func}: 2nd argument should be closure.\n" 1 0 1
386 1 $_ = $dir;
387 1 $closure->();
388 1 _report_cmd("$cmd - # back to $cwd");
389 1 ▶ CORE::chdir $cwd or die "$func: $!";
Branch: 1 branch
BranchTrueFalseTotal
chdir $cwd or die "${func}: $!" 1 0 1
390 }
391 }
392  
393  
394 ## edit files
395 ## ex.
396 ## edit "dist/src/*/*.pl", sub { s/\$RELEASE\$/1.2.3/g; $_ };
397 8 ▶sub edit { # or sub edit (&@) { ...
Subroutine edit: called CC=10 SCAR=23.0
Pod undocumented
398 #my ($closure, @filenames) = @_;
399 8 my $closure = pop @_;
400 8 ▶ ref($closure) eq 'CODE' or die "edit(): last argument should be closure.\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $closure eq "CODE" or die "edit(): last argument should be closure.\n" 7 1 8
401 7 my (@filenames) = @_;
402 7 my @fnames = _prepare('edit', @filenames);
403 7 ▶ return if $Kook::Config::NOEXEC;
Branch: 1 branch
BranchTrueFalseTotal
if $Kook::Config::NOEXEC 0 7 7
404 7 my $errmsg = "edit(): closure should return non-empty string but %s returned.\n";
405 7 for my $fname (@fnames) {
406 29 ▶ next unless -f $fname;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fname 21 8 29
407 8 $_ = read_file($fname);
408 8 my $s = &{$closure}();
409 8 ▶ defined($s) or die sprintf($errmsg, 'undef');
Branch: 1 branch
BranchTrueFalseTotal
defined $s or die sprintf($errmsg, "undef") 7 1 8
410 7 ▶ length($s) > 0 or die sprintf($errmsg, 'empty string');
Branch: 1 branch
BranchTrueFalseTotal
length $s > 0 or die sprintf($errmsg, "empty string") 6 1 7
411 6 ▶ "$s" ne "0" or die sprintf($errmsg, 'zero');
Branch: 1 branch
BranchTrueFalseTotal
"$s" ne 0 or die sprintf($errmsg, "zero") 5 1 6
412 5 ▶ my $is_number = $s =~ /\A\d+(\.\d+)?\Z/ && Dumper($s) !~ /['"]/;
Condition66%2/3
$s =~ /\A\d+(\.\d+)?\Z/ && !(Dumper($s) =~ /['"]/)
!ll&&!rl&&r
302
MC/DC50%1/2
$s =~ /\A\d+(\.\d+)?\Z/ && !(Dumper($s) =~ /['"]/)
$s =~ /\A\d+(\.\d+)?\Z/ !(Dumper($s) =~ /['"]/)
Truth table66%2/3
A && not(B)
A= $s =~ /\A\d+(\.\d+)?\Z/B= !(Dumper($s) =~ /['"]/)
ABresult
0X0
100
111
413 5 ▶ ! $is_number or die sprintf($errmsg, $s);
Branch: 1 branch
BranchTrueFalseTotal
$is_number and die sprintf($errmsg, $s) 2 3 5
414 3 write_file($fname, $s);
415 }
416 }
417  
418  
419  
420 39 1;
« blib/lib/Kook/Kitchen.pm ↑ Summary blib/lib/Kook/Cookbook.pm »