blib/lib/Module/Rename.pm

filter: Statementstmt 99.2% 133 / 134 Branchbran 88.8% 32 / 36 Conditioncond 100% 21 / 21 MC/DCmcdc 100% 13 / 13 Subroutinesub 100% 18 / 18 Podpod 28.5% 2 / 7 totaltotal 95.6% 219 / 229 CC 30 SCAR 14.6
CC
30
Cov
97%
__ANON__
8.0
find_and_rename
7.0
__ANON__
5.0
CRAP 4.3
↑ Summary
1 ###########################################
2 package Module::Rename;
3 ###########################################
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec qw( splitdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Sysadm::Install qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec::Functions qw( abs2rel splitdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = "0.04";
15  
16 ###########################################
17 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
18 ###########################################
19 2 my($class, %options) = @_;
20  
21 2 my $self = {
22 name_old => undef,
23 name_new => undef,
24 dir_exclude => ['blib'],
25 dir_ignore => ['CVS'],
26 wipe_empty_subdirs => 0,
27 use_git => 0,
28 %options,
29 };
30  
31 2 ▶ if( $self->{use_git} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_git"}) 1 1 2
32 1 $self->{ git_bin } = bin_find( "git" );
33 1 ▶ if( !defined $self->{ git_bin } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"git_bin"}) 0 1 1
34 0 die "No git executable found";
35 }
36 1 push @{ $self->{dir_exclude} }, ".git";
37 }
38  
39 2 $self->{dir_exclude_hash} = { map { $_ => 1 } @{$self->{dir_exclude}} };
40 2 $self->{dir_ignore_hash} = { map { $_ => 1 } @{$self->{dir_ignore}} };
41  
42 2 ($self->{look_for} = $self->{name_old}) =~ s#::#/#g;
43 2 ($self->{replace_by} = $self->{name_new}) =~ s#::#/#g;
44  
45 2 ($self->{pmfile} = $self->{name_old}) =~ s#.*::##g;
46 2 $self->{pmfile} .= ".pm";
47  
48 2 ($self->{new_pmfile} = $self->{name_new}) =~ s#.*::##g;
49 2 $self->{new_pmfile} .= ".pm";
50  
51 2 bless $self, $class;
52 }
53  
54 ###########################################
55 2 ▶sub longest_common_path {
Subroutine longest_common_path: called CC=3 SCAR=11.0
Pod undocumented
56 ###########################################
57 2 my( $self, $file1, $file2 ) = @_;
58  
59 2 my @common = ();
60  
61 2 my @dirs1 = splitdir( dirname $file1 );
62 2 my @dirs2 = splitdir( dirname $file2 );
63  
64 2 for my $dir1_part ( @dirs1 ) {
65 6 my $dir2_part = shift @dirs2;
66 6 ▶ if( $dir1_part eq $dir2_part ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir1_part eq $dir2_part) { } 5 1 6
67 5 push @common, $dir1_part;
68 } else {
69 1 last;
70 }
71 }
72  
73 2 return File::Spec->catfile( @common );
74 }
75  
76 ###########################################
77 8 ▶sub move {
Subroutine move: called CC=4 SCAR=13.9
Pod undocumented
78 ###########################################
79 8 my($self, $old_path, $new_path) = @_;
80  
81 8 ▶ if( $old_path ne $new_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_path ne $new_path) 6 2 8
82 6 ▶ if ($self->{use_git} and !-d $old_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_git'} and not -d $old_path) { } 2 4 6
Condition100%3/3
$self->{"use_git"} and not -d $old_path
!ll&&!rl&&r
312
MC/DC100%2/2
$self->{"use_git"} and not -d $old_path
$self->{"use_git"} not -d $old_path
Truth table100%3/3
A and not(B)
A= $self->{"use_git"}B= not -d $old_path
ABresult
0X0
100
111
83 # make sure we launch the git command inside the git workspace
84 2 my $common = $self->longest_common_path( $old_path, $new_path );
85 2 cd $common;
86 2 tap("git", "mv",
87 abs2rel( $old_path, $common ),
88 abs2rel( $new_path, $common ),
89 );
90 2 cdback;
91 } else {
92 4 mv $old_path, $new_path;
93 }
94 }
95 }
96  
97 ###########################################
98 2 ▶sub find_and_rename {
Subroutine find_and_rename: called CC=7 SCAR=19.5
Pod documented
99 ###########################################
100 2 my($self, $start_dir) = @_;
101  
102 2 my @files = ();
103 2 my %empty_subdirs = ();
104  
105 28 ▶ find(sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
106 28 ▶ if(-d and $self->dir_empty($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and $self->dir_empty($_)) 1 27 28
Condition100%3/3
-d $_ and $self->dir_empty($_)
!ll&&!rl&&r
15121
MC/DC100%2/2
-d $_ and $self->dir_empty($_)
-d $_ $self->dir_empty($_)
Truth table100%3/3
A and B
A= -d $_B= $self->dir_empty($_)
ABresult
0X0
100
111
107 1 INFO "$File::Find::name is an empty subdir";
108 1 $empty_subdirs{$File::Find::name}++;
109 }
110 28 ▶ if(-d and exists $self->{dir_exclude_hash}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and exists $self->{"dir_exclude_hash"}{$_}) 1 27 28
Condition100%3/3
-d $_ and exists $self->{"dir_exclude_hash"}{$_}
!ll&&!rl&&r
15121
MC/DC100%2/2
-d $_ and exists $self->{"dir_exclude_hash"}{$_}
-d $_ exists $self->{"dir_exclude_hash"}{$_}
Truth table100%3/3
A and B
A= -d $_B= exists $self->{"dir_exclude_hash"}{$_}
ABresult
0X0
100
111
111 1 $File::Find::prune = 1;
112 1 return;
113 }
114 27 ▶ return unless -f $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 12 15 27
115 push @files, $File::Find::name if
116 $File::Find::name =~ /$self->{look_for}/ or
117 15 ▶ $_ eq $self->{pmfile};
Branch: 1 branch
BranchTrueFalseTotal
if $File::Find::name =~ /$self->{"look_for"}/ or $_ eq $self->{"pmfile"} 4 11 15
Condition100%3/3
$File::Find::name =~ /$self->{"look_for"}/ or $_ eq $self->{"pmfile"}
l!l&&r!l&&!r
2211
MC/DC100%2/2
$File::Find::name =~ /$self->{"look_for"}/ or $_ eq $self->{"pmfile"}
$File::Find::name =~ /$self->{"look_for"}/ $_ eq $self->{"pmfile"}
Truth table100%3/3
A or B
A= $File::Find::name =~ /$self->{"look_for"}/B= $_ eq $self->{"pmfile"}
ABresult
1X1
011
000
118 15 $self->file_process($_, $File::Find::name);
119 2 }, $start_dir);
120
121 2 for my $file (@files) {
122  
123 4 my $newfile = $file;
124  
125 4 ▶ if($file =~ /$self->{look_for}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /$self->{'look_for'}/) { } 2 2 4
126 2 $newfile =~ s/$self->{look_for}/$self->{replace_by}/;
127 } else {
128 # We found a module file outside the regular
129 # dir structure, just replace it within this directory
130 2 $newfile =~ s/$self->{pmfile}/$self->{new_pmfile}/;
131 }
132  
133 4 INFO "mv $file $newfile";
134 4 my $dir = dirname($newfile);
135 4 ▶ mkd $dir unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 2 2 4
136 4 $self->move($file, $newfile);
137 }
138  
139 2 (my $dashed_look_for = $self->{name_old}) =~ s#::#-#g;
140 2 (my $dashed_replace_by = $self->{name_new}) =~ s#::#-#g;
141  
142 # Rename any top directory files like Foo-Bar-0.01
143 2 my @rename_candidates = ($start_dir);
144 81 ▶ find(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
145 81 ▶ if(/$dashed_look_for/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$dashed_look_for/) 2 79 81
146 2 push @rename_candidates, $File::Find::name;
147 }
148 2 }, $start_dir);
149 2 for my $item (@rename_candidates) {
150 4 (my $newitem = $item) =~ s/$dashed_look_for/$dashed_replace_by/;
151 4 $self->move($item, $newitem);
152 }
153  
154 # Even the start_dir could have to be modified.
155 2 $start_dir =~ s/$dashed_look_for/$dashed_replace_by/;
156  
157 # Update empty_subdirs with the latest name changes
158 2 %empty_subdirs = map { s/$dashed_look_for/$dashed_replace_by/; $_; }
159 %empty_subdirs;
160  
161 2 ▶ if( $self->{wipe_empty_subdirs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"wipe_empty_subdirs"}) 2 0 2
162 2 my @dirs = ();
163 # Delete all empty dirs
164 30 ▶ find(sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
165 30 ▶ if( exists $self->{dir_exclude_hash}->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dir_exclude_hash"}{$_}) 1 29 30
166 1 $File::Find::prune = 1;
167 }
168  
169 30 ▶ if(-d and $self->dir_empty($_) and
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_ and $self->dir_empty($_) and not exists $empty_subdirs{$File::Find::name}) 2 28 30
Condition100%6/6
-d $_ and $self->dir_empty($_) and not exists $empty_subdirs{$File::Find::name}
!ll&&!rl&&r
2712
-d $_ and $self->dir_empty($_)
!ll&&!rl&&r
15123
MC/DC100%3/3
-d $_ and $self->dir_empty($_) and not exists $empty_subdirs{$File::Find::name}
-d $_ $self->dir_empty($_) not exists $empty_subdirs{$File::Find::name}
Truth table100%4/4
A and B and not(C)
A= -d $_B= $self->dir_empty($_)C= not exists $empty_subdirs{$File::Find::name}
ABCresult
0XX0
10X0
1100
1111
170 ! exists $empty_subdirs{$File::Find::name}
171 ) {
172 2 WARN "$File::Find::name is empty and can go away";
173 2 push @dirs, $File::Find::name;
174 }
175 2 }, $start_dir);
176 2 for my $dir ( @dirs ) {
177 2 rmf $dir;
178 }
179 }
180 }
181  
182 ###########################################
183 28 ▶sub dir_empty {
Subroutine dir_empty: called CC=3 SCAR=11.0
Pod undocumented
184 ###########################################
185 28 my($self, $dir) = @_;
186  
187 28 ▶ opendir DIR, $dir or LOGDIE "Cannot open dir $dir";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or LOGDIE("Cannot open dir $dir") 28 0 28
188 28 ▶ my @items = grep { $_ ne "." and $_ ne ".." } readdir DIR;
Condition100%3/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
282872
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
189 28 closedir DIR;
190  
191 28 @items = grep { ! exists $self->{dir_ignore_hash}->{$_} } @items;
192
193 28 return ! scalar @items;
194 }
195  
196 ###########################################
197 15 ▶sub file_process {
Subroutine file_process: called CC=3 SCAR=11.0
Pod undocumented
198 ###########################################
199 15 my($self, $file, $path) = @_;
200  
201 15 my $out = "";
202  
203 15 ▶ open FILE, "<$file" or LOGDIE "Can't open $file ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGDIE("Can't open $file ($!)") 15 0 15
204 15 ▶ while(<FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 358 15 373
205 358 DEBUG "Looking for /$self->{name_old}/";
206 358 s/($self->{name_old})\b/$self->rep($1,$self->{name_new})/ge;
207 358 DEBUG "Looking for /$self->{look_for}/";
208 358 s/($self->{look_for})\b/$self->rep($1,$self->{replace_by})/ge;
209 358 $out .= $_;
210 }
211 15 close FILE;
212  
213 15 blurt $out, $file;
214 }
215  
216 ###########################################
217 52 ▶sub rep {
Subroutine rep: called CC=1 SCAR=0.0
Pod undocumented
218 ###########################################
219 52 my($self, $found, $replace) = @_;
220  
221 52 INFO "$File::Find::name ($.): $found => $replace";
222 52 return $replace;
223 }
224  
225 2 1;
226  
227 __END__
↑ Summary