Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 Args: | 169 Args: |
| 170 sources: Files to check. sources[0] is the typically the target file. | 170 sources: Files to check. sources[0] is the typically the target file. |
| 171 sources[1:] are externs and dependencies in topological order. Order | 171 sources[1:] are externs and dependencies in topological order. Order |
| 172 is not guaranteed if custom_sources is True. | 172 is not guaranteed if custom_sources is True. |
| 173 out_file: A file where the compiled output is written to. | 173 out_file: A file where the compiled output is written to. |
| 174 closure_args: Arguments passed directly to the Closure compiler. | 174 closure_args: Arguments passed directly to the Closure compiler. |
| 175 custom_sources: Whether |sources| was customized by the target (e.g. not | 175 custom_sources: Whether |sources| was customized by the target (e.g. not |
| 176 in GYP dependency order). | 176 in GYP dependency order). |
| 177 | 177 |
| 178 Returns: | 178 Returns: |
| 179 (found_errors, stderr) A boolean indicating whether errors were found and | 179 (return_code, found_errors, stderr) The return code from the jar command, |
| 180 the raw Closure compiler stderr (as a string). | 180 a boolean indicating whether type checking related errors were found |
| 181 and the raw Closure compiler stderr (as a string). Note that it is | |
| 182 possible for |found_errors| to be false, and |return_code| to be | |
| 183 non-zero, for example when wrong flags are passed to the compiler | |
| 184 (which yields an InvalidOptionsException) | |
| 181 """ | 185 """ |
| 182 is_extern = lambda f: 'externs' in f | 186 is_extern = lambda f: 'externs' in f |
| 183 externs_and_deps = [self._POLYMER_EXTERNS] | 187 externs_and_deps = [self._POLYMER_EXTERNS] |
| 184 | 188 |
| 185 if custom_sources: | 189 if custom_sources: |
| 186 if custom_includes: | 190 if custom_includes: |
| 187 # TODO(dbeam): this is fairly hacky. Can we just remove custom_sources | 191 # TODO(dbeam): this is fairly hacky. Can we just remove custom_sources |
| 188 # soon when all the things kept on life support using it die? | 192 # soon when all the things kept on life support using it die? |
| 189 self._target = sources.pop() | 193 self._target = sources.pop() |
| 190 externs_and_deps += sources | 194 externs_and_deps += sources |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 os.makedirs(out_dir) | 240 os.makedirs(out_dir) |
| 237 | 241 |
| 238 checks_only = 'checks_only' in closure_args | 242 checks_only = 'checks_only' in closure_args |
| 239 | 243 |
| 240 if not checks_only: | 244 if not checks_only: |
| 241 args += ["--js_output_file=%s" % out_file] | 245 args += ["--js_output_file=%s" % out_file] |
| 242 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] | 246 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] |
| 243 | 247 |
| 244 self._log_debug("Args: %s" % " ".join(args)) | 248 self._log_debug("Args: %s" % " ".join(args)) |
| 245 | 249 |
| 246 _, stderr = self.run_jar(self._compiler_jar, args) | 250 return_code, stderr = self.run_jar(self._compiler_jar, args) |
| 247 | 251 |
| 248 errors = stderr.strip().split("\n\n") | 252 errors = stderr.strip().split("\n\n") |
| 249 maybe_summary = errors.pop() | 253 maybe_summary = errors.pop() |
| 250 | 254 |
| 251 summary = re.search("(?P<error_count>\d+).*error.*warning", maybe_summary) | 255 summary = re.search("(?P<error_count>\d+).*error.*warning", maybe_summary) |
| 252 if summary: | 256 if summary: |
| 253 self._log_debug("Summary: %s" % maybe_summary) | 257 self._log_debug("Summary: %s" % maybe_summary) |
| 254 else: | 258 else: |
| 255 # Not a summary. Running the jar failed. Bail. | 259 # Not a summary. Running the jar failed. Bail. |
| 256 self._log_error(stderr) | 260 self._log_error(stderr) |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 273 errors = map(self._clean_up_error, errors) | 277 errors = map(self._clean_up_error, errors) |
| 274 output = self._format_errors(errors) | 278 output = self._format_errors(errors) |
| 275 | 279 |
| 276 if errors: | 280 if errors: |
| 277 prefix = "\n" if output else "" | 281 prefix = "\n" if output else "" |
| 278 self._log_error("Error in: %s%s%s" % (self._target, prefix, output)) | 282 self._log_error("Error in: %s%s%s" % (self._target, prefix, output)) |
| 279 elif output: | 283 elif output: |
| 280 self._log_debug("Output: %s" % output) | 284 self._log_debug("Output: %s" % output) |
| 281 | 285 |
| 282 self._nuke_temp_files() | 286 self._nuke_temp_files() |
| 283 return bool(errors), stderr | 287 return return_code, bool(errors), stderr |
|
dpapad
2017/06/08 22:26:05
An alternative to adding a new return variable, is
| |
| 284 | 288 |
| 285 | 289 |
| 286 if __name__ == "__main__": | 290 if __name__ == "__main__": |
| 287 parser = argparse.ArgumentParser( | 291 parser = argparse.ArgumentParser( |
| 288 description="Typecheck JavaScript using Closure compiler") | 292 description="Typecheck JavaScript using Closure compiler") |
| 289 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, | 293 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, |
| 290 help="Path to a source file to typecheck") | 294 help="Path to a source file to typecheck") |
| 291 parser.add_argument("--custom_sources", action="store_true", | 295 parser.add_argument("--custom_sources", action="store_true", |
| 292 help="Whether this rules has custom sources.") | 296 help="Whether this rules has custom sources.") |
| 293 parser.add_argument("--custom_includes", action="store_true", | 297 parser.add_argument("--custom_includes", action="store_true", |
| 294 help="If present, <include>s are processed when" | 298 help="If present, <include>s are processed when" |
| 295 "using --custom_files.") | 299 "using --custom_files.") |
| 296 parser.add_argument("-o", "--out_file", required=True, | 300 parser.add_argument("-o", "--out_file", required=True, |
| 297 help="A file where the compiled output is written to") | 301 help="A file where the compiled output is written to") |
| 298 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 302 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
| 299 help="Arguments passed directly to the Closure compiler") | 303 help="Arguments passed directly to the Closure compiler") |
| 300 parser.add_argument("-v", "--verbose", action="store_true", | 304 parser.add_argument("-v", "--verbose", action="store_true", |
| 301 help="Show more information as this script runs") | 305 help="Show more information as this script runs") |
| 302 opts = parser.parse_args() | 306 opts = parser.parse_args() |
| 303 | 307 |
| 304 checker = Checker(verbose=opts.verbose) | 308 checker = Checker(verbose=opts.verbose) |
| 305 | 309 |
| 306 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, | 310 return_code, found_errors, stderr = checker.check( |
| 307 closure_args=opts.closure_args, | 311 opts.sources, out_file=opts.out_file, |
| 308 custom_sources=opts.custom_sources, | 312 closure_args=opts.closure_args, |
| 309 custom_includes=opts.custom_includes) | 313 custom_sources=opts.custom_sources, |
| 314 custom_includes=opts.custom_includes) | |
| 310 | 315 |
| 311 if found_errors: | 316 if found_errors or return_code > 0 : |
| 312 if opts.custom_sources: | 317 if opts.custom_sources: |
| 313 print stderr | 318 print stderr |
| 314 sys.exit(1) | 319 sys.exit(1) |
| OLD | NEW |