use strict;
use warnings;
if (@ARGV && $ARGV[0] eq 'diff') {
my ($orig_file, $new_file) = @ARGV[1..2];
my $orig_api = grab_api($orig_file);
my $new_api = grab_api($new_file);
diff_apis($orig_api, $new_api);
exit 0;
}
my $path = "/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_Reference";
my $url_prefix = "https://developer.mozilla.org$path";
if (@ARGV && $ARGV[0] eq 'mdn') {
shift(@ARGV);
while(<>) {
if (/<li>([\w:]+)/) {
print STDERR "Checking $1...\n";
system("wget", "-q", "$url_prefix/$1");
if ($? == 0) {
s!<li>([\w:]+)!<li><a href="$path/$1">$1</a>!;
}
}
print;
}
exit 0;
}
sub grab_api {
my ($file) = @_;
open(my $fh, "<", $file) or die "open $file: $!";
my $grabbing;
my @api;
while(<$fh>) {
if ($grabbing && /^(\w+)/) {
push @api, $1;
}
$grabbing = /JS_PUBLIC_API/;
}
return \@api;
}
sub diff_apis {
my ($old, $new) = @_;
my %old;
@old{@$old} = ();
my %new;
@new{@$new} = ();
open(my $ofh, ">", "/tmp/r-c.diff.1");
print $ofh "$_\n" foreach (@$old);
close $ofh;
open(my $nfh, ">", "/tmp/r-c.diff.2");
print $nfh "$_\n" foreach (@$new);
close $nfh;
open(my $diff, "diff -u /tmp/r-c.diff.1 /tmp/r-c.diff.2 |");
while(<$diff>) {
if (/^-(\w+)/) {
next if exists $new{$1}; } elsif (/^\+(\w+)/) {
next if exists $old{$1}; }
print;
}
}
my @added;
my @renamed;
my @replaced;
my @deleted;
my @other;
my %N;
my $renaming;
my $replacing;
while (<>) {
my $name;
if (/^[ +-](\w+)/) {
$name = $1;
$N{$name} = $name =~ /^JS_/ ? $name : "JS::$name";
}
if (/^-/) {
die if ! $name;
if (/\[rename\]/) {
$renaming = $name;
} elsif (/\[replace\]/) {
$replacing = $name;
} elsif (/\[other\]/) {
push @other, $name;
} else {
push @deleted, $name;
}
} elsif (/^\+/) {
die if ! $name;
if ($renaming) {
push @renamed, [ $renaming, $name ];
undef $renaming;
} elsif ($replacing) {
push @replaced, [ $replacing, $name ];
undef $replacing;
} elsif (/\[other\]/) {
push @other, $name;
} else {
push @added, $name;
}
}
}
open(my $fh, "<", "jsapi.blame") or die "open jsapi.blame: $!";
my $grabbing;
my %changerev;
my %revs;
while(<$fh>) {
if ($grabbing && /^\s*(\d+): (\w+)/ ) {
$changerev{$2} = $1;
$revs{$1} = 1;
}
$grabbing = /JS_PUBLIC_API/;
}
my %bug;
for my $rev (keys %revs) {
open(my $fh, "hg log -r $rev -T '{desc}' |");
while(<$fh>) {
if (/[bB]ug (\d+)/) {
$bug{$rev} = $1;
}
}
}
sub get_bug_suffix {
my ($api) = @_;
$DB::single = 1 if ! $changerev{$api};
my $bug = $bug{$changerev{$api}};
return $bug ? " {{{bug($bug)}}}" : "";
}
print "(new apis)\n";
print "<ul>\n";
print " <li>$N{$_}" . get_bug_suffix($_) . "</li>\n" foreach @added;
print " <li>$N{$_->[0]} renamed to $N{$_->[1]}" . get_bug_suffix($_->[1]) . "</li>\n" foreach @renamed;
print " <li>$N{$_->[0]} replaced with $N{$_->[1]}" . get_bug_suffix($_->[1]) . "</li>\n" foreach @replaced;
print "</ul>\n";
print "\n";
print qq(<h2 id="Deleted_APIs">Deleted APIs</h2>\n);
print "<ul>\n";
print " <li>$N{$_}</li>\n" foreach @deleted;
print "</ul>\n";
print "\n";
print qq(<h2 id="Changed_APIs">Changed APIs</h2>\n);
print "<ul>\n";
print " <li>$N{$_}" . get_bug_suffix($_) . "</li>\n" foreach @other;
print "</ul>\n";
print "\n";