Skip to content

Commit

Permalink
Put each file in ack-standalone in its own scope. GH #376
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Feb 25, 2024
1 parent 50e0d91 commit ed82082
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ History file for ack 3. https://beyondgrep.com/

NEXT
========================================
[FEATURES]
Add support for Starlark and Pytest filetypes.

[FIXES]
The ack-standalone that gets built, and is what's available for download at
https://beyondgrep.com, now puts each of the modules it pulls in into its
own lexical scope. This should fix any future conflicts of pragmata that
might come up in the future. Thanks, mauke. (GH #376)


v3.7.0 Sat Feb 25 14:00:57 CST 2023
========================================
Expand Down
47 changes: 31 additions & 16 deletions squash
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ my $NO_EDIT_COMMENT = <<'EOCOMMENT';
#
EOCOMMENT

my $debug_mode = grep { $_ eq '--debug' } @ARGV;
@ARGV = grep { $_ ne '--debug' } @ARGV;

my $code = <<"PERL";
my @parts;
push @parts, <<"PERL";
#!/usr/bin/env perl
$NO_EDIT_COMMENT
\$App::Ack::STANDALONE = 1;
Expand All @@ -47,22 +45,44 @@ for my $arg ( @ARGV ) {
$key =~ s{::}{/}g;
$filename = $INC{$key} or die "Can't find the file for $arg";
}
my $is_ack = $filename eq 'ack';

warn "Reading $filename\n";
if ( !$is_ack ) {
push @parts, "{\n";
}
push @parts, _get_code_from_file( $filename );
if ( !$is_ack ) {
push @parts, "}\n";
}
}

my $code = join( '', @parts );

# We only use two of the four constructors in File::Next, so delete the other two.
for my $unused_func ( qw( dirs everything ) ) {
$code =~ s/^sub $unused_func\b.*?^}//sm or die qq{Unable to find the sub "$unused_func" to remove from ack-standalone};
}
print $code;

exit 0;


sub _get_code_from_file {
my $filename = shift;

my @lines;
open( my $fh, '<', $filename ) or die "Can't open $filename: $!";
my $was_in_pod = 0;
my $in_pod = 0;
$code .= "#line 1 '$filename'\n" if $debug_mode && $filename ne 'ack';

while ( <$fh> ) {
next if /^use (?:File::Next|App::Ack)/;

s/^use parent (.+)/BEGIN {\n our \@ISA = $1\n}/;

if ( $was_in_pod && !$in_pod ) {
$was_in_pod = 0;
if ($debug_mode) {
$code .= "#line $. '$filename'\n";
}
}

# See if we're in module POD blocks.
Expand All @@ -86,15 +106,10 @@ for my $arg ( @ARGV ) {
# Remove Perl::Critic comments.
# I'd like to remove all comments, but this is a start
s{\s*##.+critic.*}{};
$code .= $_;
push @lines, $_;
}
close $fh;
}
# We only use two of the four constructors in File::Next, so delete the other two.
for my $unused_func ( qw( dirs everything ) ) {
$code =~ s/^sub $unused_func\b.*?^}//sm or die qq{Unable to find the sub "$unused_func" to remove from ack-standalone};
return join( '', @lines );
}
print $code;
exit 0;

0 comments on commit ed82082

Please sign in to comment.