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