blib/lib/App/codefork.pm

filter: Statementstmt 70.5% 84 / 119 Branchbran 36.2% 21 / 58 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 85.7% 12 / 14 Podpod 100% 5 / 5 totaltotal 55.7% 126 / 226 CC 42 SCAR 40.8
CC
42
Cov
56%
run
210.0
_collect_dir
13.7
apply_changes
8.6
CRAP 59.2
↑ Summary
1 package App::codefork;
2 # ABSTRACT: Worlds dumbest code forker
3 2 our $VERSION = '1.000';
4 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Text::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 has config => (
9 is => 'ro',
10 required => 1,
11 1 ▶ coerce => sub { path($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 );
13  
14 has dir => (
15 is => 'ro',
16 0 ▶ default => sub { path('.')->absolute },
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 1 ▶ coerce => sub { path($_[0])->absolute },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 2 );
19  
20 has debug => (
21 is => 'ro',
22 1 ▶ default => sub { $ENV{CODEFORK_DEBUG} ? 1 : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'CODEFORK_DEBUG'} ? : 0 1 1
23 2 );
24  
25 2 has mods => (
26 is => 'lazy',
27 );
28  
29  
30 1 ▶sub _build_mods {
Subroutine _build_mods: called CC=3 SCAR=11.2
31 1 my ($self) = @_;
32 1 my @lines = $self->config->lines({ chomp => 1 });
33 return [ map {
34 1 ▶ if (/\|/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\|/) { } 2 1 3
elsif (/%/) { } 1 0 1
35 2 [ replace => split(/\|/, $_, 2) ]
36 } elsif (/%/) {
37 1 [ word => split(/%/, $_, 2) ]
38 } else {
39 ()
40 0 }
41 } @lines ];
42 }
43  
44 1 ▶sub collect_changes {
Subroutine collect_changes: called CC=2 SCAR=6.9
Pod documented
45 1 my ($self) = @_;
46 1 $self->log('Parsing config...');
47 1 $self->log($_->[0].': '.$_->[1].' => '.$_->[2]) for @{$self->mods};
48 1 my @changes;
49 1 $self->_collect_dir($self->dir, '', \@changes);
50 1 return \@changes;
51 }
52  
53  
54 2 ▶sub _collect_dir {
Subroutine _collect_dir: called CC=13 SCAR=26.2
55 2 my ($self, $dir, $new_dir_prefix, $changes) = @_;
56 2 my @mods = @{$self->mods};
57  
58 2 for my $entry (sort $dir->children) {
59 3 my $basename = $entry->basename;
60 3 ▶ next if $basename eq '.git' || $basename eq '.svn';
Branch: 1 branch
BranchTrueFalseTotal
if $basename eq ".git" or $basename eq ".svn" 0 3 3
Condition33%1/3
$basename eq ".git" or $basename eq ".svn"
l!l&&r!l&&!r
003
MC/DC0%0/2
$basename eq ".git" or $basename eq ".svn"
$basename eq ".git" $basename eq ".svn"
Truth table33%1/3
A or B
A= $basename eq ".git"B= $basename eq ".svn"
ABresult
1X1
011
000
61  
62 3 my $new_basename = $basename;
63 3 for my $mod (@mods) {
64 9 ▶ if ($mod->[0] eq 'replace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->[0] eq "replace") 6 3 9
65 6 $new_basename =~ s/$mod->[1]/$mod->[2]/g;
66 }
67 }
68  
69 3 my $old_rel = $entry->relative($self->dir)->stringify;
70 3 ▶ my $new_rel = length($new_dir_prefix)
Branch: 1 branch
BranchTrueFalseTotal
length $new_dir_prefix ? : 1 2 3
71 ? "$new_dir_prefix/$new_basename"
72 : $new_basename;
73  
74 3 ▶ if ($entry->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->is_dir) { } 1 2 3
75 1 $self->_collect_dir($entry, $new_rel, $changes);
76 } else {
77 2 my $content = $entry->slurp_raw;
78 2 my $new_content = $content;
79 2 for my $mod (@mods) {
80 6 my ($cmd, $from, $to) = @$mod;
81 6 ▶ if ($cmd eq 'replace') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd eq 'replace') { } 4 2 6
elsif ($cmd eq 'word') { } 2 0 2
82 4 $new_content =~ s/$from/$to/g;
83 } elsif ($cmd eq 'word') {
84 2 $new_content =~ s/([^A-Za-z0-9]+)$from([^A-Za-z0-9]+)/$1$to$2/g;
85 }
86 }
87  
88 2 ▶ if ($new_rel ne $old_rel || $new_content ne $content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_rel ne $old_rel or $new_content ne $content) 2 0 2
Condition33%1/3
$new_rel ne $old_rel or $new_content ne $content
l!l&&r!l&&!r
200
MC/DC0%0/2
$new_rel ne $old_rel or $new_content ne $content
$new_rel ne $old_rel $new_content ne $content
Truth table33%1/3
A or B
A= $new_rel ne $old_relB= $new_content ne $content
ABresult
1X1
011
000
89 2 push @$changes, {
90 old_path => $old_rel,
91 new_path => $new_rel,
92 old_content => $content,
93 new_content => $new_content,
94 };
95 }
96 }
97 }
98 }
99  
100 1 ▶sub generate_diff {
Subroutine generate_diff: called CC=6 SCAR=20.4
Pod documented
101 1 my ($self, $changes) = @_;
102 1 ▶ $changes //= $self->collect_changes;
Condition33%1/3
$changes //= $self->collect_changes
l!l&&r!l&&!r
100
MC/DC0%0/2
$changes //= $self->collect_changes
$changes $self->collect_changes
Truth table33%1/3
A //= B
A= $changesB= $self->collect_changes
ABresult
1X1
011
000
103 1 my $output = '';
104 1 for my $change (@$changes) {
105 2 my $renamed = $change->{old_path} ne $change->{new_path};
106 2 my $modified = $change->{old_content} ne $change->{new_content};
107  
108 2 ▶ if ($modified) {
Branch: 2 branches
BranchTrueFalseTotal
if ($modified) { } 2 0 2
elsif ($renamed) { } 0 0 0
109 my $diff_text = diff(
110 \$change->{old_content},
111 \$change->{new_content},
112 {
113 2 STYLE => 'Unified',
114 FILENAME_A => "a/$change->{old_path}",
115 FILENAME_B => "b/$change->{new_path}",
116 },
117 );
118 2 ▶ if ($renamed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($renamed) 2 0 2
119 2 my $header = "rename from $change->{old_path}\nrename to $change->{new_path}\n";
120 2 $diff_text =~ s/^(---\s)/$header$1/m;
121 }
122 2 $output .= $diff_text;
123 } elsif ($renamed) {
124 0 $output .= "diff $change->{old_path} $change->{new_path}\n";
125 0 $output .= "rename from $change->{old_path}\n";
126 0 $output .= "rename to $change->{new_path}\n";
127 }
128 }
129 1 return $output;
130 }
131  
132  
133 1 ▶sub apply_changes {
Subroutine apply_changes: called CC=7 SCAR=21.5
Pod documented
134 1 my ($self, $changes) = @_;
135 1 ▶ $changes //= $self->collect_changes;
Condition33%1/3
$changes //= $self->collect_changes
l!l&&r!l&&!r
100
MC/DC0%0/2
$changes //= $self->collect_changes
$changes $self->collect_changes
Truth table33%1/3
A //= B
A= $changesB= $self->collect_changes
ABresult
1X1
011
000
136 1 for my $change (@$changes) {
137 2 my $old = $self->dir->child($change->{old_path});
138 2 my $new = $self->dir->child($change->{new_path});
139 2 my $renamed = $change->{old_path} ne $change->{new_path};
140 2 my $modified = $change->{old_content} ne $change->{new_content};
141  
142 2 ▶ $new->parent->mkpath if $renamed;
Branch: 1 branch
BranchTrueFalseTotal
if $renamed 2 0 2
143  
144 2 ▶ if ($modified) {
Branch: 2 branches
BranchTrueFalseTotal
if ($modified) { } 2 0 2
elsif ($renamed) { } 0 0 0
145 2 $self->log("modify: $change->{new_path}");
146 2 $new->spew_raw($change->{new_content});
147 2 ▶ if ($renamed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($renamed) 2 0 2
148 2 $self->log("rename: $change->{old_path} -> $change->{new_path}");
149 2 $old->remove;
150 }
151 } elsif ($renamed) {
152 0 $self->log("rename: $change->{old_path} -> $change->{new_path}");
153 0 $old->move($new);
154 }
155 }
156 1 return scalar @$changes;
157 }
158  
159  
160 8 ▶sub log {
Subroutine log: called CC=2 SCAR=7.2
Pod documented
161 8 my ($self, $text) = @_;
162 8 ▶ print STDERR $text."\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 8 8
163 }
164  
165  
166 0 ▶sub run {
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
167 0 my ($class, @args) = @_;
168  
169 0 my $command = 'diff';
170 0 my $config = 'fork.conf';
171 0 my $output_file;
172  
173 0 ▶ if (@args && $args[0] eq 'apply') {
Branch: 3 branches
BranchTrueFalseTotal
if (@args and $args[0] eq 'apply') { } 0 0 0
elsif (@args and $args[0] eq 'output') { } 0 0 0
elsif (@args) { } 0 0 0
Condition0%0/3
@args and $args[0] eq "apply"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0] eq "apply"
@args $args[0] eq "apply"
Truth table0%0/3
A and B
A= @argsB= $args[0] eq "apply"
ABresult
0X0
100
111
174 0 $command = 'apply';
175 0 shift @args;
176 0 ▶ $config = shift @args if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 0 0
177 ▶ } elsif (@args && $args[0] eq 'output') {
Condition0%0/3
@args and $args[0] eq "output"
!ll&&!rl&&r
000
MC/DC0%0/2
@args and $args[0] eq "output"
@args $args[0] eq "output"
Truth table0%0/3
A and B
A= @argsB= $args[0] eq "output"
ABresult
0X0
100
111
178 0 $command = 'output';
179 0 shift @args;
180 0 ▶ if (@args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 2) 0 0 0
181 0 $config = shift @args;
182 }
183 0 $output_file = shift @args;
184 0 ▶ die "output command requires a filename\n" unless defined $output_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output_file 0 0 0
185 } elsif (@args) {
186 0 $config = shift @args;
187 }
188  
189 0 ▶ die "Config file '$config' not found\n" unless -f $config;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $config 0 0 0
190  
191 0 my $app = $class->new(config => $config);
192 0 my $changes = $app->collect_changes;
193  
194 0 ▶ if ($command eq 'diff') {
Branch: 3 branches
BranchTrueFalseTotal
if ($command eq 'diff') { } 0 0 0
elsif ($command eq 'apply') { } 0 0 0
elsif ($command eq 'output') { } 0 0 0
195 0 my $diff = $app->generate_diff($changes);
196 0 ▶ if (length $diff) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $diff) { } 0 0 0
197 0 print $diff;
198 } else {
199 0 print "No changes.\n";
200 }
201 } elsif ($command eq 'apply') {
202 0 my $count = $app->apply_changes($changes);
203 0 print "Applied $count change(s).\n";
204 } elsif ($command eq 'output') {
205 0 my $diff = $app->generate_diff($changes);
206 0 path($output_file)->spew_utf8($diff);
207 0 print "Wrote diff to $output_file\n";
208 }
209 }
210  
211  
212  
213 2 1;
214  
215 __END__
↑ Summary