Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1519)

Unified Diff: third_party/closure_compiler/checker.py

Issue 369643002: Lay groudwork to Closure compile JavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: asdf Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/closure_compiler/checker.py
diff --git a/third_party/closure_compiler/checker.py b/third_party/closure_compiler/checker.py
new file mode 100755
index 0000000000000000000000000000000000000000..c504ad5f5749cf0b8d80b90296313c5dd4b298f5
--- /dev/null
+++ b/third_party/closure_compiler/checker.py
@@ -0,0 +1,135 @@
+#!/usr/bin/env python
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import argparse
+import os
+import subprocess
+import sys
+import tempfile
+
+
+class Checker(object):
+ _common_closure_args = [
+ "--accept_const_keyword",
+ "--language_in=ECMASCRIPT5",
+ "--summary_detail_level=3",
+ "--warning_level=VERBOSE",
+ "--jscomp_error=accessControls",
+ "--jscomp_error=ambiguousFunctionDecl",
+ "--jscomp_error=checkEventfulObjectDisposal",
+ "--jscomp_error=checkStructDictInheritance",
+ "--jscomp_error=checkTypes",
+ "--jscomp_error=checkVars",
+ "--jscomp_error=constantProperty",
+ "--jscomp_error=deprecated",
+ "--jscomp_error=duplicateMessage",
+ "--jscomp_error=es3",
+ "--jscomp_error=externsValidation",
+ "--jscomp_error=globalThis",
+ "--jscomp_error=invalidCasts",
+ "--jscomp_error=misplacedTypeAnnotation",
+ "--jscomp_error=missingProperties",
+ "--jscomp_error=missingReturn",
+ "--jscomp_error=nonStandardJsDocs",
+ "--jscomp_error=suspiciousCode",
+ "--jscomp_error=undefinedNames",
+ "--jscomp_error=undefinedVars",
+ "--jscomp_error=unknownDefines",
+ "--jscomp_error=uselessCode",
+ "--jscomp_error=visibility",
+ ]
+
+ _found_java = False
+
+ _jar_command = [
+ "java",
+ "-jar",
+ "-Xms1024m",
+ "-server",
+ "-XX:+TieredCompilation"
+ ]
+
+ def __init__(self, verbose=False):
+ self._current_dir = os.path.dirname(__file__)
+ self._compiler_jar = os.path.join(self._current_dir, "lib", "compiler.jar")
+ self._runner_jar = os.path.join(self._current_dir, "runner", "runner.jar")
+ self._verbose = verbose
+
+ def _clean_up(self):
+ if hasattr(self, '_args_file'):
+ self._debug("Deleting args file (" + self._args_file.name + ")")
+ os.remove(self._args_file.name)
+ del self._args_file
+
+ def _debug(self, msg, error=False):
+ if self._verbose:
+ if error:
+ print sys.stderr, "(ERROR) " + msg
+ else:
+ print "(INFO) " + msg
+
+ def _fatal(self, msg):
+ self._debug(msg, error=True)
+ self._clean_up()
+ sys.exit(1)
+
+ def _run_command(self, cmd):
+ cmd_str = " ".join(cmd)
+ self._debug("Running command: " + cmd_str)
+
+ devnull = open(os.devnull, "w")
+ return subprocess.Popen(
+ cmd_str, stdout=devnull, stderr=subprocess.PIPE, shell=True)
+
+ def _check_java_path(self):
+ if self._found_java:
+ return
+
+ proc = self._run_command(["which", "java"])
+ proc.communicate()
+ if proc.returncode == 0:
+ self._found_java = True
+ else:
+ self._fatal("Cannot find java (`which java` => %s)" % proc.returncode)
+
+ def _run_jar(self, jar, args=[]):
+ self._check_java_path()
+ return self._run_command(self._jar_command + [jar] + args)
+
+ def check(self, file, depends=[], externs=[]):
+ self._debug("FILE: " + file)
+
+ if file.endswith("_externs.js"):
+ self._debug("Skipping externs")
+ return
+
+ j = os.path.join
+ src_dir = j(self._current_dir, "..", "..")
+ # Omit blah_externs.js if we're compiling blah.js.
+ externs = [j(src_dir, e) for e in externs if file != e[:-11] + ".js"]
+
+ args = ["--js=" + file] + ["--externs=" + e for e in externs]
+ args_file_content = " " + " ".join(self._common_closure_args + args)
+ self._debug("Args: " + args_file_content.strip())
+
+ self._args_file = tempfile.NamedTemporaryFile(mode='wt', delete=False)
+ self._debug("Args file: " + self._args_file.name)
+ self._args_file.write(args_file_content)
+ self._args_file.close()
+
+ runner_args = ["--compiler-args-file=" + self._args_file.name]
+ runner_cmd = self._run_jar(self._runner_jar, args=runner_args)
+ (_, output) = runner_cmd.communicate()
+ output = output.strip()
+
+ if runner_cmd.returncode:
+ self._fatal(output)
+ else:
+ line_sep = os.linesep + "## "
+ self._debug("Output: " + line_sep + line_sep.join(output.splitlines()))
+
+ self._clean_up()
+
+ return runner_cmd.returncode == 0

Powered by Google App Engine
This is Rietveld 408576698