Re-write squasher

Add ability to skip over sections (ignore) vs. completely remove them
(remove).
This commit is contained in:
Dan Church 2023-06-29 12:57:15 -05:00
parent 1582920286
commit 9dc3e4578c
Signed by: h3xx
GPG key ID: EA2BF379CD2CDBD0
6 changed files with 38 additions and 16 deletions

View file

@ -18,17 +18,35 @@ for my $arg (@ARGV) {
warn "Reading $filename\n";
open my $fh, '<', $filename or die "Can't open $filename: $!";
my $in_pod = 0;
my $in_ignore_section = 0;
my $in_section = '';
my $ignore_lines = 0;
while (<$fh>) {
if (/#.*:squash-ignore-start:$/) {
$in_ignore_section = 1;
next;
$in_section = 'ignore';
$ignore_lines = 1;
} elsif (/#.*:squash-ignore-end:$/) {
$in_ignore_section = 0;
$in_section = '';
$ignore_lines = 1;
}
if ($ignore_lines > 0) {
$ignore_lines--;
next;
}
next if $in_ignore_section;
next if /#.*:squash-ignore-line:$/;
if ($in_section eq 'ignore') {
$code .= $_;
next;
}
if (/#.*:squash-remove-start:$/) {
$in_section = 'remove';
next;
} elsif (/#.*:squash-remove-end:$/) {
$in_section = '';
next;
}
next if $in_section eq 'remove';
next if /#.*:squash-remove-line:$/;
next if /^\s*1;$/;
@ -49,6 +67,10 @@ for my $arg (@ARGV) {
s{\s*##.+critic.*}{};
$code .= $_;
}
# Warn if there were unterminated :squash-*: sections
warn "$filename: Unterminated :squash-$in_section-start: section" if $in_section;
close $fh;
}