File: fix-worker-imports-for-optimized-builds.pl

package info (click to toggle)
webkit2gtk 2.48.3-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 429,620 kB
  • sloc: cpp: 3,696,936; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 295
file content (70 lines) | stat: -rwxr-xr-x 1,767 bytes parent folder | download | duplicates (14)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Getopt::Long;
use File::Copy qw/move/;
use File::Temp qw/tempfile/;
use File::Spec;

sub fixWorkerImportsInFile($$);
sub fixWorkerImportsInDirectory($);

our $inputDirectory;

GetOptions('input-directory=s' => \$inputDirectory);

if (defined $inputDirectory) {
    fixWorkerImportsInDirectory($inputDirectory);
    exit;
}

print "Usage: $0 --input-directory <path>\n";
exit;

sub fixWorkerImportsInFile($$)
{
    my $inputScriptFilename = shift;
    my $outputScriptFilename = shift;

    open IN, $inputScriptFilename or die "Couldn't open $inputScriptFilename: $!";
    my ($out, $tempFilename) = tempfile(UNLINK => 0) or die;

    my $previousLine = "";
    while (<IN>) {
        s|/External/Esprima/esprima.js|/Esprima.js|;
        print $out $_;

        # Error if there is an "External/" path that we did not rewrite.
        if ($_ =~ /External\//) {
            my $sanitizedPath = $inputScriptFilename;
            $sanitizedPath =~ s/^.*?Workers/Workers/;
            print "ERROR: $sanitizedPath: Unhandled External importScript in Worker script on line $.: $_";
            exit 1;
        }
    }

    close $out;
    close IN;

    move $tempFilename, $outputScriptFilename or die "$!";
}

sub fixWorkerImportsInDirectory($)
{
    my $inputDirectory = shift;

    opendir(DIR, $inputDirectory) || die "$!";
    my @files = grep { !/^\.{1,2}$/ } readdir (DIR);
    closedir(DIR);

    foreach my $file (@files) {
        next if $file eq '.' or $file eq '..';
        my $path = File::Spec->catdir($inputDirectory, $file);
        if (-d $path) {
            fixWorkerImportsInDirectory($path);
        } elsif ($file =~ /\.js$/) {
            fixWorkerImportsInFile($path, $path);
        }
    }
}