simplify_static_dir/util/squash

61 lines
1.5 KiB
Perl
Executable File

#!/usr/bin/perl
# vi: et sts=4 sw=4 ts=4
# Squashes together the parts of the app into the single script.
# (Adapted from the script that squashes App::Ack - see https://github.com/beyondgrep/ack3)
use warnings;
use strict;
my $code;
for my $arg (@ARGV) {
my $filename = $arg;
if ($arg =~ /::/) {
my $key = "$arg.pm";
$key =~ s{::}{/}g;
$filename = $INC{$key} or die "Can't find the file for $arg";
}
warn "Reading $filename\n";
open my $fh, '<', $filename or die "Can't open $filename: $!";
my $in_pod = 0;
my $in_ignore_section = 0;
while (<$fh>) {
if (/#.*:squash-ignore-start:$/) {
$in_ignore_section = 1;
next;
} elsif (/#.*:squash-ignore-end:$/) {
$in_ignore_section = 0;
next;
}
next if $in_ignore_section;
next if /#.*:squash-ignore-line:$/;
next if /^\s*1;$/;
if ($filename =~ /\.pm$/) {
# See if we're in module POD blocks
if (/^=(\w+)/) {
$in_pod = ($1 ne 'cut');
next;
}
elsif ($in_pod) {
next;
}
next if /^# vi:/;
}
# Remove Perl::Critic comments.
# I'd like to remove all comments, but this is a start
s{\s*##.+critic.*}{};
$code .= $_;
}
close $fh;
}
# Remove repeated newlines between paragraphs
$code =~ s/\n\n+/\n\n/gs;
print $code;
exit 0;