|
30 | 30 | use Term::ANSIColor; |
31 | 31 | use Getopt::Long qw(:config pass_through); |
32 | 32 |
|
33 | | -# 0. Parse options |
| 33 | +# Parse options |
34 | 34 | my $no_fast_fail = 0; |
35 | 35 | GetOptions("no-fast-fail" => \$no_fast_fail); |
36 | 36 |
|
|
42 | 42 | } |
43 | 43 | my $check_perl_excludes_file = $ENV{CHECK_PERL_EXCLUDES} || '.check_perl_excludes'; |
44 | 44 |
|
45 | | -# Load excludes and check for stale entries and format |
46 | | -my %excludes; |
47 | | -my $stale_excludes = 0; |
48 | | -if (-f $check_perl_excludes_file) { |
49 | | - print colored("Checking entries in $check_perl_excludes_file\n", "bold white"); |
50 | | - open my $fh, '<', $check_perl_excludes_file or die "Cannot open $check_perl_excludes_file: $!"; |
51 | | - while (<$fh>) { |
52 | | - chomp; |
53 | | - # Skip blank lines and comments |
54 | | - next if /^\s*$/ || /^\s*#/; |
55 | | - |
56 | | - if (!-e $_) { |
57 | | - print colored("error: ", "bold red") . "Stale exclude (file not found): $_\n"; |
58 | | - $stale_excludes = 1; |
| 45 | +sub load_excludes { |
| 46 | + my ($file) = @_; |
| 47 | + my %excludes; |
| 48 | + if (-f $file) { |
| 49 | + open my $fh, '<', $file or die "Cannot open $file: $!"; |
| 50 | + while (<$fh>) { |
| 51 | + chomp; |
| 52 | + # Skip blank lines and comments |
| 53 | + next if /^\s*$/ || /^\s*#/; |
| 54 | + $excludes{$_} = 1; |
59 | 55 | } |
60 | | - $excludes{$_} = 1; |
| 56 | + close $fh; |
61 | 57 | } |
62 | | - close $fh; |
| 58 | + return %excludes; |
63 | 59 | } |
64 | | -if ($stale_excludes) { |
65 | | - exit 1; |
| 60 | + |
| 61 | +my %excludes = load_excludes($check_perl_excludes_file); |
| 62 | + |
| 63 | +# Warn if any excludes don't exist |
| 64 | +for my $exclude (keys %excludes) { |
| 65 | + if (!-e $exclude) { |
| 66 | + print colored("warning: ", "bold yellow") . "Excluded file not found: $exclude\n"; |
| 67 | + } |
66 | 68 | } |
67 | 69 |
|
68 | | -# 2. Find files to check using native File::Find |
| 70 | +# Find files to check using native File::Find |
69 | 71 | my @files; |
70 | 72 | my @roots = @ARGV; |
71 | 73 | if (!@roots) { |
72 | 74 | @roots = ('.'); |
73 | 75 | } |
74 | 76 |
|
75 | | -# Identify explicitly passed files and warn if they are in the excludes list |
| 77 | +# Normalize "./path" to "path" |
| 78 | +sub normalize_path { |
| 79 | + my ($path) = @_; |
| 80 | + $path =~ s/^\.\///; |
| 81 | + return $path; |
| 82 | +} |
| 83 | + |
| 84 | +# Warn if explicitly specified files are in the excludes list |
76 | 85 | my %explicit_files; |
77 | 86 | foreach my $arg (@roots) { |
78 | | - if (-f $arg) { |
79 | | - my $path = $arg; |
80 | | - $path =~ s/^\.\///; # Normalize |
81 | | - if ($excludes{$path}) { |
82 | | - print colored("warning: ", "bold yellow") |
83 | | - . "File '$path' is explicitly specified but is in the excludes list. Skipping.\n"; |
84 | | - } |
85 | | - else { |
86 | | - $explicit_files{$path} = 1; |
87 | | - } |
| 87 | + if (!-f $arg) { |
| 88 | + next; |
| 89 | + } |
| 90 | + my $path = $arg; |
| 91 | + $path = normalize_path($path); |
| 92 | + if ($excludes{$path}) { |
| 93 | + print colored("warning: ", "bold yellow") |
| 94 | + . "File '$path' is explicitly specified but is in the excludes list. Skipping.\n"; |
| 95 | + } |
| 96 | + else { |
| 97 | + $explicit_files{$path} = 1; |
88 | 98 | } |
| 99 | + |
89 | 100 | } |
90 | 101 |
|
91 | | -find( |
92 | | - { |
93 | | - wanted => sub { |
94 | | - my $path = $File::Find::name; |
95 | | - $path =~ s/^\.\///; # Normalize "./path" to "path" |
| 102 | +# Collect files to check, applying excludes and pruning directories |
| 103 | +sub on_wanted { |
| 104 | + my $path = $File::Find::name; |
| 105 | + $path = normalize_path($path); |
96 | 106 |
|
97 | | - # If the file was explicitly passed on the command line and not excluded, we always check it |
98 | | - if ($explicit_files{$path}) { |
99 | | - push @files, $path if $path =~ /\.(pl|pm|t)$/; |
100 | | - return; |
101 | | - } |
| 107 | + # If the file was explicitly passed on the command line and not excluded, we always check it |
| 108 | + if ($explicit_files{$path}) { |
| 109 | + push @files, $path if $path =~ /\.(pl|pm|t)$/; |
| 110 | + return; |
| 111 | + } |
102 | 112 |
|
103 | | - # Prune hidden directories (except .) and obsolete directories |
104 | | - if (-d $_) { |
105 | | - if (($path ne '.' && $path =~ m{(^|/)\.}) || $path =~ m{(^|/)obsolete($|/)}) { |
106 | | - $File::Find::prune = 1; |
107 | | - return; |
108 | | - } |
109 | | - } |
110 | | - # Filter for Perl files |
111 | | - return unless -f $_; |
112 | | - return unless $path =~ /\.(pl|pm|t)$/; |
| 113 | + # Prune hidden directories (except .) and obsolete directories |
| 114 | + if (-d $_) { |
| 115 | + if (($path ne '.' && $path =~ m{(^|/)\.}) || $path =~ m{(^|/)obsolete($|/)}) { |
| 116 | + $File::Find::prune = 1; |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + # Filter for Perl files |
| 121 | + return unless -f $_; |
| 122 | + return unless $path =~ /\.(pl|pm|t)$/; |
113 | 123 |
|
114 | | - # Skip explicitly excluded files |
115 | | - return if $excludes{$path}; |
| 124 | + # Skip explicitly excluded files |
| 125 | + return if $excludes{$path}; |
116 | 126 |
|
117 | | - push @files, $path; |
118 | | - }, |
119 | | - no_chdir => 1, |
120 | | - }, |
121 | | - @roots |
122 | | -); |
| 127 | + push @files, $path; |
| 128 | + return; |
| 129 | +} |
| 130 | + |
| 131 | +# Run the find with our custom wanted function |
| 132 | +find({wanted => \&on_wanted, no_chdir => 1,}, @roots); |
123 | 133 |
|
124 | 134 | if (!@files) { |
125 | 135 | print colored("No Perl files to check.\n", "bold white"); |
126 | 136 | exit 0; |
127 | 137 | } |
128 | 138 |
|
129 | 139 | my $total = scalar @files; |
130 | | -print colored("Checking Perl syntax of $total files using $max_workers workers ...\n", "bold white"); |
| 140 | +print colored("Checking $total files using $max_workers workers ...\n", "bold white"); |
131 | 141 |
|
132 | 142 | my %running; # pid => { file => ..., pipe => ... } |
133 | 143 | my @failed; |
|
136 | 146 | my $fast_failing = 0; |
137 | 147 |
|
138 | 148 | sub wait_for_worker { |
139 | | - # We use a blocking wait to get any finished child |
140 | 149 | my $pid = wait(); |
141 | | - if ($pid > 0) { |
142 | | - my $job = delete $running{$pid}; |
143 | | - my $file = $job->{file}; |
144 | | - my $fh = $job->{pipe}; |
| 150 | + if ($pid <= 0) { |
| 151 | + return; # No children |
| 152 | + } |
145 | 153 |
|
146 | | - # Read any output from the pipe |
147 | | - my $output = do {local $/; <$fh>}; |
148 | | - close $fh; |
| 154 | + my $job = delete $running{$pid}; |
| 155 | + my $file = $job->{file}; |
| 156 | + my $fh = $job->{pipe}; |
| 157 | + |
| 158 | + # Read any output from the pipe |
| 159 | + my $output = do {local $/; <$fh>}; |
| 160 | + close $fh; |
149 | 161 |
|
150 | | - if ($? != 0 && !$fast_failing) { |
151 | | - # Check if it was a real failure or if we killed it |
152 | | - # (If we killed it, $? will indicate a signal) |
153 | | - my $was_signaled = $? & 127; |
154 | | - |
155 | | - if (!$was_signaled) { |
156 | | - print "\n" . colored("error: ", "bold red") . "Syntax error in $file:\n"; |
157 | | - # Indent each line of the output with a tab and color it red |
158 | | - my $indented_output = $output; |
159 | | - $indented_output =~ s/^/\t/mg; |
160 | | - print colored($indented_output, "red") . "\n"; |
161 | | - push @failed, $file; |
162 | | - |
163 | | - if (!$no_fast_fail) { |
164 | | - $fast_failing = 1; |
165 | | - print colored("warning: ", "bold yellow") |
166 | | - . "Fast-fail enabled, terminating remaining workers... (pass --no-fast-fail to disable)\n"; |
167 | | - kill 'KILL', keys %running; |
168 | | - } |
| 162 | + if ($? != 0 && !$fast_failing) { |
| 163 | + # Check if it was a real failure or if we killed it |
| 164 | + # (If we killed it, $? will indicate a signal) |
| 165 | + my $was_signaled = $? & 127; |
| 166 | + |
| 167 | + if (!$was_signaled) { |
| 168 | + print "\n" . colored("error: ", "bold red") . "Syntax error in $file:\n"; |
| 169 | + # Indent each line of the output with a tab and color it red |
| 170 | + my $indented_output = $output; |
| 171 | + $indented_output =~ s/^/\t/mg; |
| 172 | + print colored($indented_output, "red") . "\n"; |
| 173 | + push @failed, $file; |
| 174 | + |
| 175 | + if (!$no_fast_fail) { |
| 176 | + $fast_failing = 1; |
| 177 | + print colored("warning: ", "bold yellow") |
| 178 | + . "Fast-fail enabled, terminating remaining workers... (pass --no-fast-fail to disable)\n"; |
| 179 | + kill 'KILL', keys %running; |
169 | 180 | } |
170 | 181 | } |
171 | | - $finished++; |
172 | 182 | } |
| 183 | + $finished++; |
| 184 | + return; |
173 | 185 | } |
174 | 186 |
|
175 | 187 | # Set up signal handling for graceful termination |
@@ -237,7 +249,7 @@ sub wait_for_worker { |
237 | 249 | wait_for_worker(); |
238 | 250 | } |
239 | 251 |
|
240 | | -print "\n"; |
| 252 | +print "\r" . (" " x 80) . "\r"; |
241 | 253 |
|
242 | 254 | if (@failed) { |
243 | 255 | print colored("error: ", "bold red") . "Check failed. See above for details.\n"; |
|
0 commit comments