Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Runs Closure compiler on JavaScript files to check for errors and produce | 6 """Runs Closure compiler on JavaScript files to check for errors and produce |
| 7 minified output.""" | 7 minified output.""" |
| 8 | 8 |
| 9 import argparse | 9 import argparse |
| 10 import os | 10 import os |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 "--jscomp_error=undefinedVars", | 42 "--jscomp_error=undefinedVars", |
| 43 "--jscomp_error=unknownDefines", | 43 "--jscomp_error=unknownDefines", |
| 44 "--jscomp_error=uselessCode", | 44 "--jscomp_error=uselessCode", |
| 45 "--jscomp_error=visibility", | 45 "--jscomp_error=visibility", |
| 46 "--language_in=ECMASCRIPT5_STRICT", | 46 "--language_in=ECMASCRIPT5_STRICT", |
| 47 "--summary_detail_level=3", | 47 "--summary_detail_level=3", |
| 48 "--compilation_level=SIMPLE_OPTIMIZATIONS", | 48 "--compilation_level=SIMPLE_OPTIMIZATIONS", |
| 49 "--source_map_format=V3", | 49 "--source_map_format=V3", |
| 50 ] | 50 ] |
| 51 | 51 |
| 52 # Extra flags used when compiling polymer code. | |
| 53 _POLYMER_ARGS = [ | |
| 54 "--polymer_pass", | |
| 55 "--extra_annotation_name=attribute", | |
| 56 "--extra_annotation_name=status", | |
| 57 "--extra_annotation_name=element", | |
| 58 "--extra_annotation_name=homepage", | |
| 59 "--extra_annotation_name=submodule", | |
| 60 "--extra_annotation_name=group", | |
|
Dan Beam
2015/05/12 01:09:06
arguable that we should be doing:
_POLYMER_ARGS
Jeremy Klein
2015/05/12 03:58:23
Done.
| |
| 61 ] | |
| 62 | |
| 52 # These are the extra flags used when compiling in strict mode. | 63 # These are the extra flags used when compiling in strict mode. |
| 53 # Flags that are normally disabled are turned on for strict mode. | 64 # Flags that are normally disabled are turned on for strict mode. |
| 54 _STRICT_CLOSURE_ARGS = [ | 65 _STRICT_CLOSURE_ARGS = [ |
| 55 "--jscomp_error=reportUnknownTypes", | 66 "--jscomp_error=reportUnknownTypes", |
| 56 "--jscomp_error=duplicate", | 67 "--jscomp_error=duplicate", |
| 57 "--jscomp_error=misplacedTypeAnnotation", | 68 "--jscomp_error=misplacedTypeAnnotation", |
| 58 ] | 69 ] |
| 59 | 70 |
| 60 _DISABLED_CLOSURE_ARGS = [ | 71 _DISABLED_CLOSURE_ARGS = [ |
| 61 # TODO(dbeam): happens when the same file is <include>d multiple times. | 72 # TODO(dbeam): happens when the same file is <include>d multiple times. |
| 62 "--jscomp_off=duplicate", | 73 "--jscomp_off=duplicate", |
| 63 # TODO(fukino): happens when cr.defineProperty() has a type annotation. | 74 # TODO(fukino): happens when cr.defineProperty() has a type annotation. |
| 64 # Avoiding parse-time warnings needs 2 pass compiling. crbug.com/421562. | 75 # Avoiding parse-time warnings needs 2 pass compiling. crbug.com/421562. |
| 65 "--jscomp_off=misplacedTypeAnnotation", | 76 "--jscomp_off=misplacedTypeAnnotation", |
| 66 ] | 77 ] |
| 67 | 78 |
| 68 _JAR_COMMAND = [ | 79 _JAR_COMMAND = [ |
| 69 "java", | 80 "java", |
| 70 "-jar", | 81 "-jar", |
| 71 "-Xms1024m", | 82 "-Xms1024m", |
| 72 "-client", | 83 "-client", |
| 73 "-XX:+TieredCompilation" | 84 "-XX:+TieredCompilation" |
| 74 ] | 85 ] |
| 75 | 86 |
| 76 _MAP_FILE_FORMAT = "%s.map" | 87 _MAP_FILE_FORMAT = "%s.map" |
| 77 | 88 |
| 78 def __init__(self, verbose=False, strict=False): | 89 def __init__(self, verbose=False, strict=False, polymer=False): |
| 79 """ | 90 """ |
| 80 Args: | 91 Args: |
| 81 verbose: Whether this class should output diagnostic messages. | 92 verbose: Whether this class should output diagnostic messages. |
| 82 strict: Whether the Closure Compiler should be invoked more strictly. | 93 strict: Whether the Closure Compiler should be invoked more strictly. |
| 94 polymer: Whether the Polymer pass is being run. | |
| 83 """ | 95 """ |
| 84 current_dir = os.path.join(os.path.dirname(__file__)) | 96 current_dir = os.path.join(os.path.dirname(__file__)) |
|
Dan Beam
2015/05/12 01:09:06
can you move this to a global and re-use?
Jeremy Klein
2015/05/12 03:58:23
Done.
| |
| 85 self._runner_jar = os.path.join(current_dir, "runner", "runner.jar") | 97 self._runner_jar = os.path.join(current_dir, "runner", "runner.jar") |
| 86 self._temp_files = [] | 98 self._temp_files = [] |
| 87 self._verbose = verbose | 99 self._verbose = verbose |
| 88 self._strict = strict | 100 self._strict = strict |
| 101 self._polymer = polymer | |
| 89 self._error_filter = error_filter.PromiseErrorFilter() | 102 self._error_filter = error_filter.PromiseErrorFilter() |
| 90 | 103 |
| 91 def _nuke_temp_files(self): | 104 def _nuke_temp_files(self): |
| 92 """Deletes any temp files this class knows about.""" | 105 """Deletes any temp files this class knows about.""" |
| 93 if not self._temp_files: | 106 if not self._temp_files: |
| 94 return | 107 return |
| 95 | 108 |
| 96 self._log_debug("Deleting temp files: %s" % ", ".join(self._temp_files)) | 109 self._log_debug("Deleting temp files: %s" % ", ".join(self._temp_files)) |
| 97 for f in self._temp_files: | 110 for f in self._temp_files: |
| 98 os.remove(f) | 111 os.remove(f) |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 if out_file: | 265 if out_file: |
| 253 args += ["--js_output_file=%s" % out_file] | 266 args += ["--js_output_file=%s" % out_file] |
| 254 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] | 267 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] |
| 255 | 268 |
| 256 if externs: | 269 if externs: |
| 257 args += ["--externs=%s" % e for e in externs] | 270 args += ["--externs=%s" % e for e in externs] |
| 258 | 271 |
| 259 if output_wrapper: | 272 if output_wrapper: |
| 260 args += ['--output_wrapper="%s"' % output_wrapper] | 273 args += ['--output_wrapper="%s"' % output_wrapper] |
| 261 | 274 |
| 275 if self._polymer: | |
| 276 args += self._POLYMER_ARGS | |
| 277 | |
| 262 args_file_content = " %s" % " ".join(self._common_args() + args) | 278 args_file_content = " %s" % " ".join(self._common_args() + args) |
| 263 self._log_debug("Args: %s" % args_file_content.strip()) | 279 self._log_debug("Args: %s" % args_file_content.strip()) |
| 264 | 280 |
| 265 args_file = self._create_temp_file(args_file_content) | 281 args_file = self._create_temp_file(args_file_content) |
| 266 self._log_debug("Args file: %s" % args_file) | 282 self._log_debug("Args file: %s" % args_file) |
| 267 | 283 |
| 268 runner_args = ["--compiler-args-file=%s" % args_file] | 284 runner_args = ["--compiler-args-file=%s" % args_file] |
| 269 _, stderr = self._run_jar(self._runner_jar, runner_args) | 285 _, stderr = self._run_jar(self._runner_jar, runner_args) |
| 270 | 286 |
| 271 errors = stderr.strip().split("\n\n") | 287 errors = stderr.strip().split("\n\n") |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 parser.add_argument("-e", "--externs", nargs=argparse.ZERO_OR_MORE) | 391 parser.add_argument("-e", "--externs", nargs=argparse.ZERO_OR_MORE) |
| 376 parser.add_argument("-o", "--out_file", | 392 parser.add_argument("-o", "--out_file", |
| 377 help="A file where the compiled output is written to") | 393 help="A file where the compiled output is written to") |
| 378 parser.add_argument("-w", "--output_wrapper", | 394 parser.add_argument("-w", "--output_wrapper", |
| 379 help="Wraps output into this string at the place" | 395 help="Wraps output into this string at the place" |
| 380 + " denoted by the marker token %output%") | 396 + " denoted by the marker token %output%") |
| 381 parser.add_argument("-v", "--verbose", action="store_true", | 397 parser.add_argument("-v", "--verbose", action="store_true", |
| 382 help="Show more information as this script runs") | 398 help="Show more information as this script runs") |
| 383 parser.add_argument("--strict", action="store_true", | 399 parser.add_argument("--strict", action="store_true", |
| 384 help="Enable strict type checking") | 400 help="Enable strict type checking") |
| 401 parser.add_argument("-p", "--polymer", type=int, | |
| 402 help="'1' to run polymer-specific checks.") | |
| 385 parser.add_argument("--success-stamp", | 403 parser.add_argument("--success-stamp", |
| 386 help="Timestamp file to update upon success") | 404 help="Timestamp file to update upon success") |
| 387 | 405 |
| 388 parser.set_defaults(single_file=True, strict=False) | 406 parser.set_defaults(single_file=True, strict=False) |
| 389 opts = parser.parse_args() | 407 opts = parser.parse_args() |
| 390 | 408 |
| 391 depends = opts.depends or [] | 409 depends = opts.depends or [] |
| 392 externs = opts.externs or set() | 410 externs = set(opts.externs) or set() |
| 411 | |
| 412 if opts.polymer: | |
| 413 current_dir = os.path.join(os.path.dirname(__file__)) | |
| 414 polymer_externs = os.path.join( | |
| 415 current_dir, | |
| 416 "../polymer/v0_8/components-chromium/polymer-externs/polymer.externs.js" ) | |
|
Dan Beam
2015/05/12 01:10:57
80 col wrap
Dan Beam
2015/05/12 01:12:25
i think you might also want to use os.path.join()
Jeremy Klein
2015/05/12 03:58:23
Done.
Jeremy Klein
2015/05/12 03:58:23
Done.
| |
| 417 externs.add(os.path.abspath(polymer_externs)) | |
| 393 | 418 |
| 394 if opts.out_file: | 419 if opts.out_file: |
| 395 out_dir = os.path.dirname(opts.out_file) | 420 out_dir = os.path.dirname(opts.out_file) |
| 396 if not os.path.exists(out_dir): | 421 if not os.path.exists(out_dir): |
| 397 os.makedirs(out_dir) | 422 os.makedirs(out_dir) |
| 398 | 423 |
| 399 checker = Checker(verbose=opts.verbose, strict=opts.strict) | 424 checker = Checker(verbose=opts.verbose, strict=opts.strict, |
| 425 polymer=opts.polymer) | |
| 400 if opts.single_file: | 426 if opts.single_file: |
| 401 for source in opts.sources: | 427 for source in opts.sources: |
| 402 depends, externs = build.inputs.resolve_recursive_dependencies( | 428 depends, externs = build.inputs.resolve_recursive_dependencies( |
| 403 source, depends, externs) | 429 source, depends, externs) |
| 404 found_errors, _ = checker.check(source, out_file=opts.out_file, | 430 found_errors, _ = checker.check(source, out_file=opts.out_file, |
| 405 depends=depends, externs=externs, | 431 depends=depends, externs=externs, |
| 406 output_wrapper=opts.output_wrapper) | 432 output_wrapper=opts.output_wrapper) |
| 407 if found_errors: | 433 if found_errors: |
| 408 sys.exit(1) | 434 sys.exit(1) |
| 409 else: | 435 else: |
| 410 found_errors, stderr = checker.check_multiple( | 436 found_errors, stderr = checker.check_multiple( |
| 411 opts.sources, | 437 opts.sources, |
| 412 out_file=opts.out_file, | 438 out_file=opts.out_file, |
| 413 output_wrapper=opts.output_wrapper) | 439 output_wrapper=opts.output_wrapper) |
| 414 if found_errors: | 440 if found_errors: |
| 415 print stderr | 441 print stderr |
| 416 sys.exit(1) | 442 sys.exit(1) |
| 417 | 443 |
| 418 if opts.success_stamp: | 444 if opts.success_stamp: |
| 419 with open(opts.success_stamp, "w"): | 445 with open(opts.success_stamp, "w"): |
| 420 os.utime(opts.success_stamp, None) | 446 os.utime(opts.success_stamp, None) |
| OLD | NEW |