| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # cpan-health: health analyser for CPAN distributions. | ||
| 4 | # Accepts a local path, a CPAN dist name (Foo-Bar), or a module name (Foo::Bar). | ||
| 5 | # Runs a battery of scored checks and prints a weighted score out of 100. | ||
| 6 | |||
| 7 | 12 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 12 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 12 | ▶ | use autodie qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | # Unicode glyphs in the terminal reporter require UTF-8 output. | ||
| 12 | use open OUT => ':encoding(UTF-8)'; | ||
| 13 | binmode STDOUT, ':encoding(UTF-8)'; | ||
| 14 | binmode STDERR, ':encoding(UTF-8)'; | ||
| 15 | |||
| 16 | # croak points errors at this script, not at internals. | ||
| 17 | use Carp qw(croak); | ||
| 18 | # File::Spec formats the absolute path shown in the "Checking ..." banner. | ||
| 19 | use File::Spec; | ||
| 20 | # Getopt::Long parses --format, --no-network, --min-score, etc. from @ARGV. | ||
| 21 | use Getopt::Long qw(:config no_auto_abbrev bundling); | ||
| 22 | # pod2usage prints the embedded POD as help text and exits. | ||
| 23 | use Pod::Usage qw(pod2usage); | ||
| 24 | |||
| 25 | our $VERSION = '0.1.0'; | ||
| 26 | |||
| 27 | # --------------------------------------------------------------------------- | ||
| 28 | # Parse CLI arguments | ||
| 29 | # --------------------------------------------------------------------------- | ||
| 30 | |||
| 31 | # Defaults for options that can be omitted. | ||
| 32 | my %opts = ( | ||
| 33 | format => 'terminal', # terminal (default), json, html, tap | ||
| 34 | severity => 3, # Perl::Critic severity level 1-5 | ||
| 35 | min_score => 0, # exit 1 if overall score falls below this | ||
| 36 | ); | ||
| 37 | |||
| 38 | GetOptions( | ||
| 39 | 'format|f=s' => \$opts{format}, | ||
| 40 | 'check=s' => \$opts{check}, | ||
| 41 | 'skip=s' => \$opts{skip}, | ||
| 42 | 'no-network|no_network' => \$opts{no_network}, | ||
| 43 | 'no-cover|no_cover' => \$opts{no_cover}, | ||
| 44 | 'severity=i' => \$opts{severity}, | ||
| 45 | 'min-score|min_score=i' => \$opts{min_score}, | ||
| 46 | 'cache-dir|cache_dir=s' => \$opts{cache_dir}, | ||
| 47 | 'output|o=s' => \$opts{output}, | ||
| 48 | 'quiet|q' => \$opts{quiet}, | ||
| 49 | 'badge' => \$opts{badge}, | ||
| 50 | 'github-annotations' => \$opts{github_annotations}, | ||
| 51 | 'list-checks' => \$opts{list_checks}, | ||
| 52 | 'history' => \$opts{history}, | ||
| 53 | 'ignore-abandoned|ignore_abandoned=s' => \$opts{ignore_abandoned}, | ||
| 54 | 'help|h' => sub { pod2usage(-verbose => 1, -exitval => 0) }, | ||
| 55 | 'man' => sub { pod2usage(-verbose => 2, -exitval => 0) }, | ||
| 56 | 'version|V' => sub { print "cpan-health $VERSION\n"; exit 0 }, | ||
| 57 | ) or pod2usage(-verbose => 0, -exitval => 2); | ||
| 58 | |||
| 59 | # Remaining positional argument is the target; default to the current directory. | ||
| 60 | my $target = shift @ARGV // '.'; | ||
| 61 | |||
| 62 | # Reject unknown format values before doing any real work. | ||
| 63 | my %VALID_FORMATS = map { $_ => 1 } qw(terminal json html tap markdown); | ||
| 64 | unless ($VALID_FORMATS{ lc $opts{format} }) { | ||
| 65 | croak "Unknown --format '$opts{format}'. Valid formats: " | ||
| 66 | . join(', ', sort keys %VALID_FORMATS) . "\n"; | ||
| 67 | } | ||
| 68 | |||
| 69 | # --------------------------------------------------------------------------- | ||
| 70 | # Load the main module lazily so --help and --version are instantaneous. | ||
| 71 | # --------------------------------------------------------------------------- | ||
| 72 | require Test::CPAN::Health; | ||
| 73 | |||
| 74 | # --------------------------------------------------------------------------- | ||
| 75 | # --list-checks: print all available check metadata and exit. | ||
| 76 | # --------------------------------------------------------------------------- | ||
| 77 | if ($opts{list_checks}) { | ||
| 78 | my $checks = Test::CPAN::Health->list_checks; | ||
| 79 | printf "%-22s %-6s %-12s %s\n", 'ID', 'WEIGHT', 'CATEGORY', 'NAME'; | ||
| 80 | printf "%s\n", '-' x 70; | ||
| 81 | for my $c (@{$checks}) { | ||
| 82 | printf "%-22s %-6s %-12s %s\n", | ||
| 83 | $c->{id}, $c->{weight}, $c->{category}, $c->{name}; | ||
| 84 | } | ||
| 85 | exit 0; | ||
| 86 | } | ||
| 87 | |||
| 88 | # --------------------------------------------------------------------------- | ||
| 89 | # --history: show score trend for the target distribution and exit. | ||
| 90 | # --------------------------------------------------------------------------- | ||
| 91 | if ($opts{history}) { | ||
| 92 | require Test::CPAN::Health::Cache; | ||
| 93 | my $cache = Test::CPAN::Health::Cache->new( | ||
| 94 | $opts{cache_dir} ? (cache_dir => $opts{cache_dir}) : (), | ||
| 95 | ); | ||
| 96 | my $rows = $cache->score_history($target, 20); | ||
| 97 | if (!@{$rows}) { | ||
| 98 | print "No score history found for '$target'.\n"; | ||
| 99 | exit 0; | ||
| 100 | } | ||
| 101 | printf "Score history for %s:\n", $target; | ||
| 102 | for my $row (@{$rows}) { | ||
| 103 | printf " %s v%-10s %3d/100\n", | ||
| 104 | scalar localtime($row->{recorded}), | ||
| 105 | $row->{version} // '?', | ||
| 106 | $row->{score}; | ||
| 107 | } | ||
| 108 | exit 0; | ||
| 109 | } | ||
| 110 | |||
| 111 | # --------------------------------------------------------------------------- | ||
| 112 | # Classify the target and run the health analyser | ||
| 113 | # --------------------------------------------------------------------------- | ||
| 114 | |||
| 115 | _print_banner(\%opts, $target); | ||
| 116 | |||
| 117 | my $health = _build_health(\%opts, $target); | ||
| 118 | my $report = $health->analyse; | ||
| 119 | my $rendered = $health->report_to($report); | ||
| 120 | |||
| 121 | _write_output(\%opts, $rendered); | ||
| 122 | _emit_badge(\%opts, $report) if $opts{badge}; | ||
| 123 | _emit_annotations(\%opts, $report) if $opts{github_annotations}; | ||
| 124 | _record_history(\%opts, $health, $report, $target); | ||
| 125 | |||
| 126 | exit($report->overall_score < $opts{min_score} ? 1 : 0); | ||
| 127 | |||
| 128 | # --------------------------------------------------------------------------- | ||
| 129 | # Helper subroutines | ||
| 130 | # --------------------------------------------------------------------------- | ||
| 131 | |||
| 132 | sub _print_banner { | ||
| 133 | my ($opts_ref, $tgt) = @_; | ||
| 134 | return if lc($opts_ref->{format}) ne 'terminal'; | ||
| 135 | return if $opts_ref->{quiet}; | ||
| 136 | printf "Checking %s ...\n\n", (-e $tgt ? File::Spec->rel2abs($tgt) : $tgt); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _build_health { | ||
| 141 | my ($opts_ref, $tgt) = @_; | ||
| 142 | my %tgt_arg; | ||
| 143 | if (-e $tgt) { | ||
| 144 | $tgt_arg{path} = $tgt; | ||
| 145 | } elsif ($tgt =~ / :: /x) { | ||
| 146 | $tgt_arg{module} = $tgt; | ||
| 147 | } else { | ||
| 148 | $tgt_arg{dist} = $tgt; | ||
| 149 | } | ||
| 150 | return Test::CPAN::Health->new( | ||
| 151 | %tgt_arg, | ||
| 152 | format => lc $opts_ref->{format}, | ||
| 153 | no_network => $opts_ref->{no_network} ? 1 : 0, | ||
| 154 | no_cover => $opts_ref->{no_cover} ? 1 : 0, | ||
| 155 | severity => $opts_ref->{severity}, | ||
| 156 | min_score => $opts_ref->{min_score}, | ||
| 157 | $opts_ref->{cache_dir} ? (cache_dir => $opts_ref->{cache_dir}) : (), | ||
| 158 | $opts_ref->{check} ? (checks => [split / \s* , \s* /x, $opts_ref->{check}]) : (), | ||
| 159 | $opts_ref->{skip} ? (skip => [split / \s* , \s* /x, $opts_ref->{skip}]) : (), | ||
| 160 | $opts_ref->{ignore_abandoned} ? (ignore_abandoned => [split / \s* , \s* /x, $opts_ref->{ignore_abandoned}]) : (), | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | sub _write_output { | ||
| 165 | my ($opts_ref, $output_text) = @_; | ||
| 166 | if ($opts_ref->{output}) { | ||
| 167 | open my $fh, '>', $opts_ref->{output}; | ||
| 168 | print {$fh} $output_text; | ||
| 169 | close $fh; | ||
| 170 | if (lc($opts_ref->{format}) eq 'terminal' && !$opts_ref->{quiet}) { | ||
| 171 | printf "Report written to %s\n", $opts_ref->{output}; | ||
| 172 | } | ||
| 173 | } else { | ||
| 174 | print $output_text; | ||
| 175 | print "\n" unless $output_text =~ / \n \z /x; | ||
| 176 | } | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub _emit_badge { | ||
| 181 | my ($opts_ref, $rpt) = @_; | ||
| 182 | my $score = $rpt->overall_score; | ||
| 183 | my $color = $score >= 90 ? 'brightgreen' : $score >= 70 ? 'yellow' : 'red'; | ||
| 184 | printf "Badge: https://img.shields.io/badge/cpan--health-%d%%2F100-%s?style=flat-square\n", | ||
| 185 | $score, $color; | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _emit_annotations { | ||
| 190 | my ($opts_ref, $rpt) = @_; | ||
| 191 | for my $result (@{ $rpt->results }) { | ||
| 192 | my $status = $result->status; | ||
| 193 | next if $status eq 'pass' || $status eq 'skip'; | ||
| 194 | my $level = ($status eq 'fail' || $status eq 'error') ? 'error' : 'warning'; | ||
| 195 | my $name = $result->data->{name} // $result->check_id; | ||
| 196 | my $summary = $result->summary; | ||
| 197 | $summary =~ s/ :: / /gx; | ||
| 198 | printf "::%s title=cpan-health %s::%s\n", $level, $name, $summary; | ||
| 199 | } | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _record_history { | ||
| 204 | my ($opts_ref, $h, $rpt, $tgt) = @_; | ||
| 205 | require Test::CPAN::Health::Cache; | ||
| 206 | my $cache = Test::CPAN::Health::Cache->new( | ||
| 207 | $opts_ref->{cache_dir} ? (cache_dir => $opts_ref->{cache_dir}) : (), | ||
| 208 | ); | ||
| 209 | my $dist_obj = $h->distribution; | ||
| 210 | if ($dist_obj) { | ||
| 211 | my $dist_name = $dist_obj->name // $tgt; | ||
| 212 | my $version = $dist_obj->version // '?'; | ||
| 213 | $cache->record_history($dist_name, $version, $rpt->overall_score); | ||
| 214 | } | ||
| 215 | return; | ||
| 216 | } | ||
| 217 | |||
| 218 | __END__ | ||