distrolint.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 89 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk
5  
6 use v5.36;
7 use Object::Pad 0.800;
8  
9 package App::perl::distrolint 0.09;
10 class App::perl::distrolint;
11  
12 use List::Util qw( max );
13 use Module::Pluggable::Object;
14  
15 use String::Tagged::Terminal;
16  
17 use App::perl::distrolint::Config;
18  
19 =head1 NAME
20  
21 C<App::perl::distrolint> - distribution-wide linting tools for Perl distributions
22  
23 =head1 SYNOPSIS
24  
25 =for highlighter perl
26  
27 use App::perl::distrolint;
28  
29 exit App::perl::distrolint->new->run( @ARGV );
30  
31 =head1 DESCRIPTION
32  
33 This module implements an application for applying various code-linting tests
34 across the source code for an entire Perl distribution. The individual checks
35 performed are described more in the various C<App::perl::distrolint::Check::*>
36 modules.
37  
38 At present in this very early version, many of these checks are very
39 opinionated, doing things specific to the way I personally lay out my code.
40 This distribution currently exists largely to allow people to see the kinds of
41 things that are possible, and also acts as a demonstration of the use of
42 L<Text::Treesitter> and F<tree-sitter-perl> to be used as a static linting
43 tool for Perl source code.
44  
45 A limited amount of customisation and configuration can be performed on a
46 per-project or per-user basis, by writing settings into F<distrolint.ini>
47 files. This is described in more detail in C<App::perl::distrolint::Config>.
48  
49 =cut
50  
51 my $finder = Module::Pluggable::Object->new(
52 search_path => [qw( App::perl::distrolint::Check )],
53 require => 1,
54 );
55  
56 my %COL = (
57 red => 1,
58 green => 2,
59 yellow => 3,
60 );
61  
62 my %FORMAT = (
63 bullet => { bold => 1, fgindex => $COL{yellow} },
64 pass => { bold => 1, fgindex => $COL{green} },
65 fail => { bold => 1, fgindex => $COL{red} },
66 note => { fgindex => $COL{yellow} },
67 diag => {},
68  
69 file => { italic => 1 },
70 literal => { altfont => 1 },
71 );
72  
73 my $PASS = String::Tagged::Terminal->new_tagged( "PASS", $FORMAT{pass}->%* );
74 my $FAIL = String::Tagged::Terminal->new_tagged( "FAIL", $FORMAT{fail}->%* );
75  
76 my $notecount;
77  
78 method run ( @argv )
79 {
80 my @checks = $self->checks;
81  
82 my $namelen = max map { length $_->{name} } @checks;
83  
84 my $totalcount = 0;
85 my $passcount = 0;
86 $notecount = 0;
87  
88 foreach my $check ( @checks ) {
89 App::perl::distrolint::Config->is_check_enabled( $check->{name} ) or
90 next;
91  
92 my $name = $check->{name};
93 $name .= "." x ($namelen - length $name);
94  
95 String::Tagged::Terminal->new
96 ->append( " " )
97 ->append_tagged( "*", $FORMAT{bullet}->%* )
98 ->append( sprintf " %s (%s)", $name, $check->{desc} )
99 ->say_to_terminal;
100  
101 my $ok = $check->{obj}->run( $self );
102  
103 String::Tagged::Terminal->from_sprintf( " -- %s",
104 $ok ? $PASS : $FAIL
105 )->say_to_terminal;
106  
107 $totalcount++;
108 $passcount++ if $ok;
109 }
110  
111 print "\n";
112 printf "%d of %d checks passed", $passcount, $totalcount;
113 printf " (%d FAILed)", $totalcount - $passcount if $passcount < $totalcount;
114 printf " (and %d notes)", $notecount if $notecount;
115 print "\n";
116  
117 return 1 if $passcount < $totalcount;
118 return 2 if $notecount;
119 return 0;
120 }
121  
122 method checks ()
123 {
124 my @checks = map {
125 my $pkg = $_;
126 {
127 obj => $pkg->new,
128 name => $pkg =~ s/^App::perl::distrolint::Check:://r,
129 desc => $pkg->DESC,
130 sort => $pkg->SORT,
131 }
132 } $finder->plugins;
133  
134 return sort { $a->{sort} <=> $b->{sort} } @checks;
135 }
136  
137 # Formatted output support for checks
138 # Yes we're just shoving this in the toplevel `App::` namespace.
139  
140 sub App::format_file ( $app, $path, $line = undef )
141 {
142 my $str = String::Tagged::Terminal->new_tagged( $path, $FORMAT{file}->%* );
143 $str .= " line $line" if defined $line;
144 return $str;
145 }
146  
147 sub App::format_literal ( $app, $str )
148 {
149 $str =~ s/([\\`])/\\$1/g;
150 return "`" . String::Tagged::Terminal->new_tagged( $str, $FORMAT{literal}->%* ) . "`";
151 }
152  
153 sub App::note ( $app, @args )
154 {
155 $notecount++;
156 App->_print_formatted( note => String::Tagged::Terminal->join( "", @args ) );
157 }
158  
159 sub App::diag ( $app, @args )
160 {
161 App->_print_formatted( diag => String::Tagged::Terminal->join( "", @args ) );
162 }
163  
164 sub App::_print_formatted ( $app, $style, $str )
165 {
166 # TODO: wish for $st->apply_tags()
167 $str->apply_tag( 0, -1, $_, $FORMAT{$style}{$_} ) for keys $FORMAT{$style}->%*;
168  
169 foreach my $line ( $str->split( qr/\n/ ) ) {
170 ( " > " . $line )->say_to_terminal;
171 }
172 }
173  
174 =head1 AUTHOR
175  
176 Paul Evans <leonerd@leonerd.org.uk>
177  
178 =cut
179  
180 0x55AA;