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