File: squash

package info (click to toggle)
ack 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,676 kB
  • sloc: perl: 10,935; ansic: 21; fortran: 11; python: 6; makefile: 5; javascript: 3
file content (113 lines) | stat: -rwxr-xr-x 2,595 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!perl

=pod

Squashes together the parts of ack into the single ack-standalone
file.

The arguments to squash are either filenames from the ack distro
like Ack.pm, or module names like File::Next.  For modules, squash
loads them and then pushes them into ack-standalone.

POD gets stripped from all modules.

=cut

use warnings;
use strict;

use File::Next;

# Make clear that ack is not supposed to be edited.
my $NO_EDIT_COMMENT = <<'EOCOMMENT';
#
# This file, ack, is generated code.
# Please DO NOT EDIT or send patches for it.
#
# Please take a look at the source from
# https://github.com/beyondgrep/ack3
# and submit patches against the individual files
# that build ack.
#
EOCOMMENT

my @parts;
push @parts, <<"PERL";
#!/usr/bin/env perl
$NO_EDIT_COMMENT
\$App::Ack::STANDALONE = 1;
PERL

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";
    }
    my $is_ack = $filename eq 'ack';

    warn "Reading $filename\n";
    push @parts,
        "{\n",
        _get_code_from_file( $filename ),
        "}\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;

    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;
        }

        # See if we're in module POD blocks.
        if ( $filename ne 'ack' ) {
            $was_in_pod = $in_pod;

            if ( /^=(\w+)/ ) {
                $in_pod = ($1 ne 'cut');
                next;
            }
            elsif ( $in_pod ) {
                next;
            }
        }

        # Replace the shebang line and append 'no edit' comment
        if ( s{^#!.+}{package main;} ) {
            # Skip the shebang line and replace it with package statement.
        }

        # Remove Perl::Critic comments.
        # I'd like to remove all comments, but this is a start
        s{\s*##.+critic.*}{};
        push @lines, $_;
    }

    close $fh;

    return join( '', @lines );
}