lib/App/GitFind/FileProcessor.pm

filter: Statementstmt 30.1% 25 / 83 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 35.0% 7 / 20 Podpod 100% 7 / 7 totaltotal 28.0% 39 / 139 CC 17 SCAR 25.7
CC
17
Cov
23%
_process
42.0
dot_relative_path
20.0
process_AND
12.0
CRAP 13.0
« support/cmdline.yp ↑ Summary lib/App/GitFind/Class.pm »
1 package App::GitFind::FileProcessor;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.000002';
8  
9 2 ▶use parent 'App::GitFind::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Class::Tiny qw(expr searchbase);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Imports
13 2 ▶use App::GitFind::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 #use App::GitFind::Actions qw(argdetails);
15 #use App::GitFind::FileStatLs ();
16 2 ▶use File::Spec; # TODO use facilities from A::GF::PathClassMicro instead?
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #use Git::Raw;
18  
19 # === Documentation === {{{1
20  
21 =head1 NAME
22  
23 App::GitFind::FileProcessor - Test a file against a set of criteria
24  
25 =head1 SYNOPSIS
26  
27 my $hrArgs = App::GitFind::cmdline::Parse(\@ARGV);
28 my $runner = App::GitFind::FileProcessor->new(-expr => $hrArgs->{expr});
29 $runner->process($some_entry_or_other);
30  
31 =cut
32  
33 # }}}1
34 # === Main interpreter === {{{1
35  
36 =head2 process
37  
38 Process a single file, represented as an L<App::GitFind::Entry> instance.
39 Returns the Boolean result of the expression. Note that the specific
40 exit codes from C<-exec> and similar actions are lost. Usage:
41  
42 $runner->process([-entry=>]$entry);
43  
44 =cut
45  
46 0 ▶sub process {
Subroutine process: not called CC=1 SCAR=6.9
Pod documented
47 0 my ($self, %args) = getparameters('self',[qw(entry)], @_);
48 0 @_ = ($self, $args{entry}, $self->expr);
49 0 goto &_process;
50 } #process()
51  
52 # Internal: $self->_process($entry, $expr). Called recursively to
53 # handle subexpressions.
54 0 ▶sub _process {
Subroutine _process: not called CC=6 SCAR=37.4
55 0 my ($self, $entry, $expr) = @_;
56 0 my $func; # What will handle the expression
57 my @arg; # Args to $func
58  
59 0 ▶ vlog { Processing => ddc($expr) } 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
60  
61 0 ▶ die "Invalid expression: " . ddc($expr) unless ref $expr eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $expr eq "HASH" 0 0 0
62  
63 0 ▶ if($expr->{code}) { # Basic elements
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->{'code'}) { } 0 0 0
64 0 $func = $expr->{code};
65 0 @arg = $entry;
66 0 ▶ push @arg, @{$expr->{params}} if $expr->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $expr->{"params"} 0 0 0
67  
68 } else { # SEQ, AND, OR, NOT
69 die "Logical expression has more than one key: " . ddc($expr)
70 0 ▶ if scalar keys %{$expr} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$expr;} > 1 0 0 0
71 0 my $operation = (keys %{$expr})[0];
72 0 $func = $self->can("process_$operation");
73 # TODO remove the can() check once everything is implemented
74 0 @arg = ($entry, $expr->{$operation});
75 }
76  
77 0 ▶ die "I don't know how to process the expression: " . ddc($expr)
Branch: 1 branch
BranchTrueFalseTotal
unless $func 0 0 0
78 unless $func;
79  
80 0 return $self->$func(@arg);
81 } #_process()
82  
83 =head2 callback
84  
85 Returns a callback that will process an entry. Usage:
86  
87 my $callback = $processor->callback([$log]); # Create the callback
88 $callback->($entry); # Invoke the callback
89  
90 The optional C<$log> parameter, if truthy, provides extra debug output.
91  
92 =cut
93  
94 0 ▶sub callback {
Subroutine callback: not called CC=2 SCAR=17.9
Pod documented
95 0 my ($self, $log) = @_;
96 0 my $expr = $self->expr;
97 0 ▶ if($log) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log) { } 0 0 0
98 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 my $entry = $_[0];
100 0 ▶ vlog { '>>>', $entry->path } 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my $matched = $self->_process($entry, $expr);
102 0 ▶ vlog { '<<<', $matched ? 'matched' : 'did not match' } 3;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$matched ? : 0 0 0
103 0 return $matched;
104 }
105  
106 0 } else { # not logging
107 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 return $self->_process(shift, $expr);
109 # Future optimization? Use goto &_process instead?
110 }
111 0 }
112 } #callback
113  
114 # }}}1
115 # === Helpers === {{{1
116  
117 =head2 dot_relative_path
118  
119 Given a path, adds C<./> or C<.\> unless it is an absolute path. This is
120 a hacked workaround for the issue noted in
121 L<https://bitbucket.org/shlomif/perl-file-find-object/issues/3/not-consistently-present-in-returned-paths>.
122  
123 =cut
124  
125 # Make a regex that will match ./ or .\ in a platform-independent way
126 2 my $_dotslash = File::Spec->catfile('.',''); # platform-independent ./
127 2 my $_ddotslash = File::Spec->catfile('..',''); # platform-independent ./
128 2 $_dotslash = qr{^(?:\Q$_dotslash\E|\Q$_ddotslash\E)};
129  
130 0 ▶sub dot_relative_path {
Subroutine dot_relative_path: not called CC=4 SCAR=30.0
Pod documented
131 0 my $path = $_[1]->path; # $_[1] is an Entry
132 0 ▶ return $path if File::Spec->file_name_is_absolute($path);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($path) 0 0 0
133 0 ▶ return $path if $path =~ $_dotslash || $path eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$_dotslash/ or $path eq "." 0 0 0
Condition0%0/3
$path =~ /$_dotslash/ or $path eq "."
l!l&&r!l&&!r
000
MC/DC0%0/2
$path =~ /$_dotslash/ or $path eq "."
$path =~ /$_dotslash/ $path eq "."
Truth table0%0/3
A or B
A= $path =~ /$_dotslash/B= $path eq "."
ABresult
1X1
011
000
134 0 return File::Spec->catfile('.',$path);
135 } #dot_relative_path()
136  
137 # }}}1
138 # === Logical operators === {{{1
139  
140 =head2 process_NOT
141  
142 Process a negation of a single expression. Usage:
143  
144 $runner->process_NOT($entry, $expr);
145  
146 Even though the name of the parameter is C<exprs> for consistency with
147 AND, OR, and SEQ, only a single expression is allowed.
148  
149 =cut
150  
151 0 ▶sub process_NOT {
Subroutine process_NOT: not called CC=2 SCAR=17.9
Pod documented
152 0 my ($self, $entry, $expr) = @_;
153 0 ▶ croak "I can't take an array of expressions" if ref $expr eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expr eq "ARRAY" 0 0 0
154  
155 0 return !$self->_process($entry, $expr);
156 } #process_NOT()
157  
158 =head2 process_AND
159  
160 Process a conjunction of expressions. Usage:
161  
162 $runner->process_AND($entry, [$expr1, ...]);
163  
164 =cut
165  
166 0 ▶sub process_AND {
Subroutine process_AND: not called CC=3 SCAR=24.8
Pod documented
167 0 my ($self, $entry, $lrExprs) = @_;
168 0 my $retval;
169  
170 0 for(@$lrExprs) {
171 0 $retval = $self->_process($entry, $_);
172 0 ▶ last unless $retval; # Short-circuit
Branch: 1 branch
BranchTrueFalseTotal
unless $retval 0 0 0
173 }
174 0 return $retval;
175 } #process_AND()
176  
177 =head2 process_OR
178  
179 Process a disjunction of expressions. Usage:
180  
181 $runner->process_OR($entry, [$expr1, ...]);
182  
183 =cut
184  
185 0 ▶sub process_OR {
Subroutine process_OR: not called CC=3 SCAR=24.8
Pod documented
186 0 my ($self, $entry, $lrExprs) = @_;
187 0 my $retval;
188  
189 0 for(@$lrExprs) {
190 0 $retval = $self->_process($entry, $_);
191 0 ▶ last if $retval; # Short-circuit
Branch: 1 branch
BranchTrueFalseTotal
if $retval 0 0 0
192 }
193 0 return $retval;
194 } #process_OR()
195  
196 =head2 process_SEQ
197  
198 Process a sequence of expressions. Usage:
199  
200 $runner->process_SEQ($entry, [$expr1, ...]);
201  
202 =cut
203  
204 0 ▶sub process_SEQ {
Subroutine process_SEQ: not called CC=2 SCAR=17.9
Pod documented
205 0 my ($self, $entry, $lrExprs) = @_;
206 0 my $retval;
207  
208 0 $retval = $self->_process($entry, $_) foreach @{$lrExprs};
209 0 return $retval;
210 } #process_SEQ()
211  
212 # }}}1
213  
214 2 1; # End of App::GitFind::FileProcessor
215 __END__
« support/cmdline.yp ↑ Summary lib/App/GitFind/Class.pm »