1#!/usr/bin/perl -w
2
3# Copyright (C) 2011 Apple Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24# THE POSSIBILITY OF SUCH DAMAGE.
25
26use strict;
27use FindBin;
28use File::Temp qw(tempfile);
29use lib $FindBin::Bin;
30use webkitdirs;
31
32my $inputPath = "";
33if ($ARGV[0]) {
34    $inputPath = $ARGV[0]
35} else {
36    # Create a temporary file for STDIN.
37    # FIXME: We can probably avoid putting this on the disk by directly piping
38    # to prettify.rb via IPC::Open2.
39    my $inputTempFileHandle;
40    ($inputTempFileHandle, $inputPath) = tempfile(
41        "inputtemp-XXXXXXXX",
42        DIR => File::Spec->tmpdir(),
43        SUFFIX => ".diff",
44        UNLINK => 0,
45    );
46
47    while (<STDIN>) {
48        print $inputTempFileHandle $_;
49    }
50
51    close($inputTempFileHandle);
52}
53
54# Create a temporary file for prettified patch.
55my ($prettydiffFileHandle, $prettydiffPath) = tempfile(
56    "prettydiff-XXXXXXXX",
57    DIR => File::Spec->tmpdir(),
58    SUFFIX => ".html",
59    UNLINK => 0,
60);
61close($prettydiffFileHandle);
62
63my $prettyPatchDir = sourceDir() . "/Tools/Scripts/webkitruby/PrettyPatch/";
64my $prettyPatchTool = sourceDir() . "/Tools/Scripts/webkitruby/PrettyPatch/prettify.rb";
65
66my $pathToPrettify = "ruby -I " . sourceDir() . "/Tools/Scripts/webkitruby/PrettyPatch/ " . sourceDir() . "/Tools/Scripts/webkitruby/PrettyPatch/prettify.rb";
67system "$pathToPrettify " . quotemeta($inputPath) . " > $prettydiffPath";
68
69if (isAppleMacWebKit()) {
70    system "open", $prettydiffPath;
71} elsif (isCygwin()) {
72    system "cygstart",$prettydiffPath;
73} elsif (isWindows()) {
74    system "start", $prettydiffPath;
75} elsif (isLinux() && `which xdg-open`) {
76    system "xdg-open", $prettydiffPath;
77} else {
78    print "Created prettified diff at " . $prettydiffPath . ".";
79}
80