bin/generate_wrap_config.pl

filter: Statementstmt 100% 121 / 121 Branchbran 77.7% 28 / 36 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 93.8% 183 / 195 CC 28 SCAR 15.2
CC
28
Cov
94%
examine_files
9.0
run
7.2
list_subs
6.0
CRAP 4.6
« blib/lib/OpenTracing/WrapScope.pm ↑ Summary blib/lib/OpenTracing/AutoScope.pm »
1 #!/usr/bin/env perl
2 package OpenTracing::WrapScope::ConfigGenerator;
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature qw/say state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Getopt::Long qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::MoreUtils qw/notall/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl::Critic::Utils::McCabe qw/calculate_mccabe_of_sub/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Pod::Usage qw/pod2usage/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶exit run(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless &CORE::GLOBAL::caller() 0 2 2
18  
19 sub run {
20 8 ▶ GetOptionsFromArray(\@_,
Subroutine run: called CC=7 SCAR=19.7
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@_, "spec=s", \my $spec_file, "file=s", \my @files, "ignore=s", \my @ignore, "include=s", \my @include, "exclude=s", \my @exclude, "filter=s", \my @filters, "out=s", \my $output_file, "help", \my $help) or pod2usage("-verbose", 1, "-noperldoc", 1, "-msg", "Invalid options") 0 8 8
21 'spec=s' => \my $spec_file,
22 'file=s' => \my @files,
23 'ignore=s' => \my @ignore,
24 'include=s' => \my @include,
25 'exclude=s' => \my @exclude,
26 'filter=s' => \my @filters,
27 'out=s' => \my $output_file,
28 'help' => \my $help,
29 ) or pod2usage (
30 -verbose => 1,
31 -noperldoc => 1,
32 -msg => 'Invalid options',
33 );
34 8 ▶ pod2usage -verbose => 1, -noperldoc => 1 if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 8 8
35  
36 8 my %args;
37 8 ▶ %args = %{ YAML::XS::LoadFile($spec_file) } if defined $spec_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec_file 8 0 8
38 8 push @{ $args{files} }, @files;
39 8 push @{ $args{ignore} }, @ignore;
40 8 push @{ $args{include} }, @include;
41 8 push @{ $args{exclude} }, @exclude;
42 8 push @{ $args{filters} }, @filters;
43  
44 8 my @subs = OpenTracing::WrapScope::ConfigGenerator::examine_files(%args);
45  
46 8 ▶ open my $fh_out, '>', $output_file if $output_file;
Branch: 1 branch
BranchTrueFalseTotal
if $output_file 8 0 8
47 8 ▶ $fh_out //= \*STDOUT;
Condition50%1/2
$fh_out //= \*STDOUT
l!l
80
MC/DC0%0/1
$fh_out //= \*STDOUT
$fh_out
Truth table50%1/2
A
A= $fh_out
Aresult
11
00
48  
49 8 say {$fh_out} $_ foreach @subs;
50  
51 8 return 0;
52 }
53  
54 sub _generate_filters {
55 16 ▶ my ($filter_specs) = @_;
Subroutine _generate_filters: called CC=4 SCAR=13.9
56 16 ▶ return if not $filter_specs;
Branch: 1 branch
BranchTrueFalseTotal
unless $filter_specs 6 10 16
57  
58 state $GENERATORS = {
59 exclude_private => sub {
60 return sub {
61 18 ▶ my ($sub) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
62 18 return $sub->name !~ /(?:\A|::)_\w+\z/;
63 2 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
64 },
65 complexity => sub {
66 2 ▶ my ($threshold) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
67 2 ▶ croak 'No arguments for complexity filter' if not $threshold;
Branch: 1 branch
BranchTrueFalseTotal
unless $threshold 0 2 2
68 return sub {
69 12 ▶ my ($sub) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
70 12 return calculate_mccabe_of_sub($sub) >= $threshold;
71 2 };
72 },
73 10 };
74  
75 10 my @filters;
76 10 foreach (@$filter_specs) {
77 4 my ($filter, $arg) = split /=/;
78 4 ▶ my $generator = $GENERATORS->{$filter} or croak "No such filter: $_";
Branch: 1 branch
BranchTrueFalseTotal
my $generator = $GENERATORS->{$filter} or croak("No such filter: $_") 0 4 4
79 4 push @filters, $generator->($arg);
80 }
81 10 return \@filters;
82 }
83  
84 sub examine_files {
85 16 ▶ my %args = @_;
Subroutine examine_files: called CC=9 SCAR=22.0
86 16 ▶ my $files_base = $args{files} // [];
Condition50%1/2
$args{"files"} // []
l!l
160
MC/DC0%0/1
$args{"files"} // []
$args{"files"}
Truth table50%1/2
A
A= $args{"files"}
Aresult
11
00
87 16 ▶ my $files_ignore = $args{ignore} // [];
Condition100%2/2
$args{"ignore"} // []
l!l
106
MC/DC100%1/1
$args{"ignore"} // []
$args{"ignore"}
Truth table100%2/2
A
A= $args{"ignore"}
Aresult
11
00
88 16 ▶ my $subs_include = $args{include} // [];
Condition100%2/2
$args{"include"} // []
l!l
97
MC/DC100%1/1
$args{"include"} // []
$args{"include"}
Truth table100%2/2
A
A= $args{"include"}
Aresult
11
00
89 16 ▶ my $subs_exclude = $args{exclude} // [];
Condition100%2/2
$args{"exclude"} // []
l!l
97
MC/DC100%1/1
$args{"exclude"} // []
$args{"exclude"}
Truth table100%2/2
A
A= $args{"exclude"}
Aresult
11
00
90 16 my $filters = _generate_filters($args{filters});
91  
92 16 my @files = grep { -f } map { glob } @$files_base;
93 16 my %file_ignored = map { $_ => undef } map { glob } @$files_ignore;
94 16 my %sub_excluded = map { $_ => undef } @$subs_exclude;
95  
96 16 my @subs = @$subs_include;
97 16 foreach my $file (@files) {
98 66 ▶ next if exists $file_ignored{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $file_ignored{$file} 8 58 66
99  
100 58 foreach my $sub (list_subs($file, $filters)) {
101 126 ▶ next if exists $sub_excluded{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $sub_excluded{$sub} 4 122 126
102 122 push @subs, $sub;
103 }
104 }
105 16 return uniq @subs;
106 }
107  
108 sub list_subs {
109 63 ▶ my ($filename, $filters) = @_;
Subroutine list_subs: called CC=6 SCAR=17.9
110  
111 63 my $doc = PPI::Document->new($filename);
112 63 my $subs = $doc->find('PPI::Statement::Sub');
113 63 ▶ return if not $subs;
Branch: 1 branch
BranchTrueFalseTotal
unless $subs 0 63 63
114  
115 63 my @subs;
116 63 foreach my $sub (@$subs) {
117 162 ▶ next if notall { $_->($sub) } @$filters;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &notall(sub { &$_($sub); } , @$filters) 12 150 162
118  
119 150 ▶ if ($sub->name =~ /'|::/) { # qualified
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->name =~ /'|::/) { } 2 148 150
120 2 push @subs, $sub->name;
121 }
122 else {
123 148 my $pkg = _detect_package($sub);
124 148 ▶ $pkg = $pkg ? $pkg->namespace : 'main';
Branch: 1 branch
BranchTrueFalseTotal
$pkg ? : 140 8 148
125 148 push @subs, $pkg . '::' . $sub->name;
126 }
127 }
128 63 return @subs;
129 }
130  
131 sub _detect_package {
132 176 ▶ my ($elem) = @_;
Subroutine _detect_package: called CC=5 SCAR=16.1
133 176 ▶ return unless $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 8 168 176
134 168 ▶ return $elem if $elem->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Package") 6 162 168
135  
136 162 my $prev = $elem;
137 162 ▶ while ($prev = $prev->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev = $prev->sprevious_sibling) 258 28 286
138 258 ▶ return $prev if $prev->isa('PPI::Statement::Package');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Statement::Package") 134 124 258
139 }
140 28 return _detect_package($elem->parent);
141 }
142  
143 2 1;
144 __END__
145 =pod
146  
147 =head1 NAME
148  
149 generate_wrap_config.pl - generate input for OpenTracing::WrapScope
150  
151 =head1 SYNOPSIS
152  
153 generate_wrap_config.pl --out wrapscope.conf --file 'lib/*.pm'
154 generate_wrap_config.pl --file 'bin/*.pl' --filter complexity=5
155  
156 =head1 OPTIONS
157  
158 =head2 --file $file_pattern
159  
160 Shell file pattern for files to include in the search.
161 Can be specified multiple times.
162  
163 =head2 --ignore $file_pattern
164  
165 Shell file pattern for files to ignore.
166 Can be specified multiple times.
167  
168 =head2 --include $subroutine
169  
170 A subroutine name to unconditionally include in the results.
171 Overrides all other settings.
172 Can be specified multiple times.
173  
174 =head2 --exclude $subroutine
175  
176 A subroutine name to remove from the results.
177 Can be specified multiple times.
178  
179 =head2 --filter $filter_name[=$filter_argument]
180  
181 A filter to apply. Currently supported filters:
182  
183 =over 4
184  
185 =item * exclude_private
186  
187 Removes all subroutines starting with an underscore.
188  
189 =item * complexity=INT
190  
191 Narrows down the results to subroutines with a
192 L<cyclomatic complexity score|https://en.wikipedia.org/wiki/Cyclomatic_complexity>
193 greater than or equal than the argument. The argument is required.
194  
195 =back
196  
197 Can be specified multiple times.
198  
199 =head2 --out $filename
200  
201 The filename where the resulting config file should be written.
202 If this argument is not specified,
203 results will be printed to standard output.
204  
205 =head2 --spec $filename
206  
207 The filename of a YAML file which contains options for this program in hash form.
208 The keys correspond directly to options, most options can be specified multiple
209 times and their values should be arrays in the YAML file.
210  
211 For example, given this file:
212  
213 wrapscope_gen_config.yaml
214  
215 file: [ 'bin/*.pl', 'lib/*.pm' ]
216 filter: [ 'exclude_private' ]
217 ignore: [ 'Private*' ]
218 out: wrapscope_config.txt
219  
220 calling:
221  
222 generate_wrap_config.pl --spec wrapscope_gen_config.yaml
223  
224 is equivalent to:
225  
226 generate_wrap_config.pl \
227 --file 'bin/*.pl' \
228 --file 'lib/*.pm' \
229 --filter exclude_private \
230 --out wrapscope_config.txt \
231 --ignore 'Private*'
232  
233 if other options are specified alongside a spec file, they will be merged.
234  
235 =head2 --help
236  
237 Show this help.
238  
239 =cut
« blib/lib/OpenTracing/WrapScope.pm ↑ Summary blib/lib/OpenTracing/AutoScope.pm »