lib/App/Greple/select.pm

filter: Statementstmt 44.8% 35 / 78 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 31.8% 7 / 22 Podpod 0.0% 0 / 8 totaltotal 31.8% 42 / 132 CC 15 SCAR 27.0
CC
15
Cov
34%
select
42.0
check
30.0
prologue
20.0
CRAP 14.8
« lib/App/Greple/pgp.pm ↑ Summary lib/App/Greple/Filter.pm »
1 =head1 NAME
2  
3 select - Greple module to select files
4  
5 =head1 SYNOPSIS
6  
7 greple -Mdig -Mselect ... --dig .
8  
9 FILENAME
10 --suffix file suffixes
11 --select-name regex match for file name
12 --select-path regex match for file path
13 --x-suffix exclusive version of --suffix
14 --x-select-name exclusive version of --select-name
15 --x-select-path exclusive version of --select-path
16  
17 DATA
18 --shebang included in #! line
19 --select-data regex match for file data
20 --select-longer=# contain lines longer than #
21 --x-shebang exclusive version of --shebang
22 --x-select-data exclusive version of --select-data
23 --x-select-longer exclusive version of --select-longer
24  
25 =head1 DESCRIPTION
26  
27 Greple's B<-Mselect> module allows to filter files using their name
28 and content data. It is usually supposed to be used along with
29 B<-Mfind> or B<-Mdig> module.
30  
31 For example, next command scan files end with C<.pl> and C<.pm> under
32 current directory.
33  
34 greple -Mdig -Mselect --suffix=pl,pm foobar --dig .
35  
36 This is almost equivalent to the next command using B<--dig> option
37 with extra conditional expression for B<find> command.
38  
39 greple -Mdig foobar --dig . -name '*.p[lm]'
40  
41 The problems is that the above command does not search perl command
42 script without suffixes. Next command looks for both files looking at
43 C<#!> (shebang) line.
44  
45 greple -Mdig -Mselect --suffix=pl,pm --shebang perl foobar --dig .
46  
47 Generic option B<--select-name>, B<--select-path> and B<--select-data>
48 take regular expression and works for arbitrary use.
49  
50 =head2 ORDER and DEFAULT
51  
52 Besides normal inclusive rules, there is exclusive rules which start
53 with B<--x-> option name.
54  
55 As for the order of rules, all exclusive rules are checked first, then
56 inclusive rules are applied.
57  
58 When no rules are matched, default action is taken. If no inclusive
59 rule exists, it is selected. Otherwise discarded.
60  
61 =head1 OPTIONS
62  
63 =head2 FILENAME
64  
65 =over 7
66  
67 =item B<--suffix>=I<xx,yy,zz ...>
68  
69 Specify one or more file name suffixes connecting by comma (C<,>).
70 They will be converted to C</\.(xx|yy|zz)$/> expression and compared
71 to the file name.
72  
73 =item B<--select-name>=I<regex>
74  
75 Specify regular expression and it is compared to the file name. Next
76 command search Makefiles under any directory.
77  
78 greple -Mselect --select-name '^Makefile.*'
79  
80 =item B<--select-path>=I<regex>
81  
82 Specify regular expression and it is compared to the file path.
83  
84 =item B<--x-suffix>=I<xx,yy,zz ...>
85  
86 =item B<--x-select-name>=I<regex>
87  
88 =item B<--x-select-path>=I<regex>
89  
90 These are reverse version of corresponding options. File is not
91 selected when matched.
92  
93 =back
94  
95 =head2 DATA
96  
97 =over 7
98  
99 =item B<--shebang>=I<aa,bb,cc>
100  
101 This option test if a given string is included in the first line of
102 the file start with C<#!> (aka shebang) mark. Multiple names can be
103 specified connecting by command (C<,>). Given string is converted to
104 the next regular expression:
105  
106 /\A #! .*\b (aa|bb|cc)/x
107  
108 =item B<--select-data>=I<regex>
109  
110 Specify regular expression and it is compared to the file content
111 data. Multi-line modifier is enabled by default.
112  
113 =item B<--x-shebang>=I<aa,bb,cc>
114  
115 =item B<--x-select-data>=I<regex>
116  
117 These are reverse version of corresponding options. File is not
118 selected when matched.
119  
120 =item B<--select-longer>=I<number>
121  
122 =item B<--x-select-longer>=I<number>
123  
124 Search or ignore files which contain lines longer than I<number>.
125  
126 =back
127  
128 =head2 SEE ALSO
129  
130 L<App::Greple::type>
131  
132 =cut
133  
134 package App::Greple::select;
135  
136 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 2 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
139 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 2 ▶use App::Greple::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 2 our @select_shebang;
143 2 our @select_suffix;
144 2 our @select_name;
145 2 our @select_path;
146 2 our @select_data;
147  
148 2 our @discard_shebang;
149 2 our @discard_suffix;
150 2 our @discard_name;
151 2 our @discard_path;
152 2 our @discard_data;
153  
154 2 my %opt;
155 2 my $select = __PACKAGE__->new();
156  
157 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
158 2 my $class = shift;
159 2 my $obj = bless { include => [],
160 exclude => [] }, $class;
161 2 lock_keys %$obj;
162 2 $obj;
163 }
164  
165 0 ▶sub include { @{shift->{include}} }
Subroutine include: not called CC=1 SCAR=6.9
Pod undocumented
166  
167 0 ▶sub exclude { @{shift->{exclude}} }
Subroutine exclude: not called CC=1 SCAR=6.9
Pod undocumented
168  
169 package #
170 FilterEnt
171 2 {
172 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
173 0 my $class = shift;
174 0 bless [ @_ ], $class;
175 }
176 0 ▶ sub type { shift->[0] }
Subroutine type: not called CC=1 SCAR=6.9
177 0 ▶ sub regex { shift->[1] }
Subroutine regex: not called CC=1 SCAR=6.9
178 0 ▶ sub action { shift->[2] }
Subroutine action: not called CC=1 SCAR=6.9
179 }
180  
181 0 ▶sub add {
Subroutine add: not called CC=2 SCAR=17.9
Pod undocumented
182 0 my $obj = shift;
183 0 my($type, $regex, $action) = @_;
184 0 ▶ my $list = $action ? $obj->{include} : $obj->{exclude};
Branch: 1 branch
BranchTrueFalseTotal
$action ? : 0 0 0
185 0 push @{$list}, FilterEnt->new($type, $regex, $action);
186 }
187  
188 0 ▶sub check {
Subroutine check: not called CC=5 SCAR=34.0
Pod undocumented
189 0 my $obj = shift;
190 0 my($path, $data) = @_;
191 0 my $name = $path =~ s{\A.*/}{}r;
192 0 for my $f ($obj->exclude, $obj->include) {
193 0 my $type = $f->type;
194 my $compare = { name => \$name ,
195 path => \$path ,
196 0 ▶ data => $data }->{$type} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $compare = {"name", \$name, "path", \$path, "data", $data}->{$type} or die 0 0 0
197 0 ▶ ${$compare} =~ $f->regex and return $f->action;
Branch: 1 branch
BranchTrueFalseTotal
${$compare;} =~ $f->regex and return $f->action 0 0 0
198 }
199 0 ▶ return $obj->include ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$obj->include ? : 0 0 0
200 }
201  
202 ############################################################
203  
204 0 ▶sub opt {
Subroutine opt: not called CC=2 SCAR=17.9
Pod undocumented
205 0 ▶ while (my($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
206 0 $opt{$k} = $v;
207 }
208 }
209  
210 2 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
211  
212 0 ▶sub prologue {
Subroutine prologue: not called CC=4 SCAR=30.0
Pod undocumented
213 0 for (
214 0 ▶ [ \@select_shebang, q/,/, data => 1, sub { qr/\A\#!.*\b\Q$_[0]\E/ } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 ▶ [ \@discard_shebang, q/,/, data => 0, sub { qr/\A\#!.*\b\Q$_[0]\E/ } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 ▶ [ \@select_suffix, q/,/, name => 1, sub { qr/\.\Q$_[0]\E$/ } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 ▶ [ \@discard_suffix, q/,/, name => 0, sub { qr/\.\Q$_[0]\E$/ } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 [ \@select_data, q//, data => 1 ],
219 [ \@discard_data, q//, data => 0 ],
220 [ \@select_name, q//, name => 1 ],
221 [ \@discard_name, q//, name => 0 ],
222 [ \@select_path, q//, path => 1 ],
223 [ \@discard_path, q//, path => 0 ],
224 ) {
225 0 my($list, $split, $type, $action, $re) = @$_;
226 0 do {
227 0 map { $select->add($type, $_, $action) }
228 0 ▶ map { $re ? $re->($_) : qr/$_/m }
Branch: 1 branch
BranchTrueFalseTotal
$re ? : 0 0 0
229 0 ▶ map { $split ? split($split, $_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$split ? : 0 0 0
230 @$list;
231 };
232 }
233 }
234  
235 0 ▶sub select {
Subroutine select: not called CC=6 SCAR=37.4
Pod undocumented
236 0 my %arg = @_;
237 0 ▶ my $name = delete $arg{&FILELABEL} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $name = delete $arg{&FILELABEL} or die 0 0 0
238 0 ▶ if ($select->check($name, *_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->check($name, *_)) { } 0 0 0
239 0 ▶ say $name if $opt{yes};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"yes"} 0 0 0
240 0 ▶ $opt{die} and die "SKIP $name\n";
Branch: 1 branch
BranchTrueFalseTotal
$opt{"die"} and die "SKIP $name\n" 0 0 0
241 } else {
242 0 ▶ say $name if $opt{no};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"no"} 0 0 0
243 0 die "SKIP $name\n";
244 }
245 }
246  
247 2 1;
248  
249 __DATA__
« lib/App/Greple/pgp.pm ↑ Summary lib/App/Greple/Filter.pm »