« CPAN/Maker/Bootstrapper/Role/ResolveVars.pm
↑ Summary
CPAN/Maker/Bootstrapper/Role/DepsFilter.pm »
| 1 | package CPAN::Maker::Bootstrapper::Role::Critic; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(slurp); | ||
| 8 | use Data::Dumper; | ||
| 9 | use English qw(-no_match_vars); | ||
| 10 | use File::Find; | ||
| 11 | use List::Util qw(none); | ||
| 12 | |||
| 13 | use Role::Tiny; | ||
| 14 | |||
| 15 | use Readonly; | ||
| 16 | |||
| 17 | Readonly::Scalar our $DEFAULT_THEME => 'pbp'; | ||
| 18 | Readonly::Scalar our $DEFAULT_SEVERITY => 5; | ||
| 19 | |||
| 20 | ######################################################################## | ||
| 21 | sub cmd_critique { | ||
| 22 | ######################################################################## | ||
| 23 | my ($self) = @_; | ||
| 24 | |||
| 25 | my $file_list = eval { | ||
| 26 | if ( my $manifest = $self->get_file_list ) { | ||
| 27 | my $list = slurp($manifest); | ||
| 28 | return [ split /\n/xsm, $list ]; | ||
| 29 | } | ||
| 30 | |||
| 31 | return [ ( $self->get_args ) ]; | ||
| 32 | }; | ||
| 33 | |||
| 34 | die "ERROR: no files to critique\n$OS_ERROR" | ||
| 35 | if !$file_list || !@{$file_list}; | ||
| 36 | |||
| 37 | eval { require Perl::Critic; 1; }; | ||
| 38 | |||
| 39 | die "ERROR: Perl::Critic is not installed!\n" | ||
| 40 | if $EVAL_ERROR; | ||
| 41 | |||
| 42 | my $theme = $ENV{PERLCRITIC_THEME} || $DEFAULT_THEME; | ||
| 43 | my $severity = $ENV{PERLCRITIC_SEVERITY} || $DEFAULT_SEVERITY; | ||
| 44 | |||
| 45 | my %options = ( | ||
| 46 | $ENV{PERLCRITICRC} ? ( '-profile' => $ENV{PERLCRITICRC} ) : (), | ||
| 47 | '-theme' => $theme, | ||
| 48 | '-severity' => $severity, | ||
| 49 | ); | ||
| 50 | |||
| 51 | my $critic = Perl::Critic->new(%options); | ||
| 52 | |||
| 53 | open my $old_stderr, '>&', \*STDERR | ||
| 54 | or die $OS_ERROR; | ||
| 55 | |||
| 56 | my %has_violations; | ||
| 57 | |||
| 58 | foreach my $file ( @{$file_list} ) { | ||
| 59 | my ($base_file) = $file =~ /^(.*?)[.]p[ml](?:[.]in)?$/xsm; | ||
| 60 | |||
| 61 | die "ERROR: $base_file not found.\n" | ||
| 62 | if !-e "$base_file.pm"; | ||
| 63 | |||
| 64 | open STDERR, '|-', 'tee', $base_file . '.crit' | ||
| 65 | or die "ERROR: could not open pipe to tee for $base_file.crit: $OS_ERROR\n"; | ||
| 66 | |||
| 67 | my @violations = $critic->critique( $base_file . '.pm' ); | ||
| 68 | |||
| 69 | foreach my $v (@violations) { | ||
| 70 | my $severity = $v->severity; | ||
| 71 | $has_violations{$severity}++; | ||
| 72 | |||
| 73 | my $level = { | ||
| 74 | 1 => 'info', | ||
| 75 | 2 => 'info', | ||
| 76 | 3 => 'warn', | ||
| 77 | 4 => 'warn', | ||
| 78 | 5 => 'error', | ||
| 79 | }->{$severity}; | ||
| 80 | |||
| 81 | my $violation_msg = $v->to_string; | ||
| 82 | chomp $violation_msg; | ||
| 83 | $violation_msg = "$violation_msg (Severity $severity)"; | ||
| 84 | $self->get_logger->$level($violation_msg); # one write -> tee -> terminal AND .crit | ||
| 85 | } | ||
| 86 | |||
| 87 | close STDERR | ||
| 88 | or die "ERROR: could not close pipe for $base_file.crit: $OS_ERROR\n"; | ||
| 89 | } | ||
| 90 | |||
| 91 | open STDERR, '>&', $old_stderr | ||
| 92 | or die $OS_ERROR; | ||
| 93 | |||
| 94 | open STDERR, '>&', $old_stderr | ||
| 95 | or die $OS_ERROR; | ||
| 96 | |||
| 97 | if ( keys %has_violations ) { | ||
| 98 | $self->get_logger->error( sprintf 'Total Violations: %s', | ||
| 99 | join q{, }, map { sprintf "%s: [%s]", $_, ( $has_violations{$_} // '0' ) } ( 1 .. 5 ) ); | ||
| 100 | } | ||
| 101 | |||
| 102 | return keys %has_violations ? $FAILURE : $SUCCESS; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |