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 from argparse import Action |
5 | 6 |
6 """Runs Closure compiler on JavaScript files to check for errors and produce | 7 """Runs Closure compiler on JavaScript files to check for errors and produce |
7 minified output.""" | 8 minified output.""" |
8 | 9 |
9 import argparse | 10 import argparse |
10 import os | 11 import os |
11 import re | 12 import re |
12 import subprocess | 13 import subprocess |
13 import sys | 14 import sys |
14 import tempfile | 15 import tempfile |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 184 |
184 Return: | 185 Return: |
185 The filepath of the newly created, written, and closed temporary file. | 186 The filepath of the newly created, written, and closed temporary file. |
186 """ | 187 """ |
187 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: | 188 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: |
188 self._temp_files.append(tmp_file.name) | 189 self._temp_files.append(tmp_file.name) |
189 tmp_file.write(contents) | 190 tmp_file.write(contents) |
190 return tmp_file.name | 191 return tmp_file.name |
191 | 192 |
192 def check(self, sources, out_file=None, closure_args=None, | 193 def check(self, sources, out_file=None, closure_args=None, |
193 custom_sources=True): | 194 custom_sources=True, enable_chrome_pass=True): |
194 """Closure compile |sources| while checking for errors. | 195 """Closure compile |sources| while checking for errors. |
195 | 196 |
196 Args: | 197 Args: |
197 sources: Files to check. sources[0] is the typically the target file. | 198 sources: Files to check. sources[0] is the typically the target file. |
198 sources[1:] are externs and dependencies in topological order. Order | 199 sources[1:] are externs and dependencies in topological order. Order |
199 is not guaranteed if custom_sources is True. | 200 is not guaranteed if custom_sources is True. |
200 out_file: A file where the compiled output is written to. | 201 out_file: A file where the compiled output is written to. |
201 closure_args: Arguments passed directly to the Closure compiler. | 202 closure_args: Arguments passed directly to the Closure compiler. |
202 custom_sources: Whether |sources| was customized by the target (e.g. not | 203 custom_sources: Whether |sources| was customized by the target (e.g. not |
203 in GYP dependency order). | 204 in GYP dependency order). |
(...skipping 13 matching lines...) Expand all Loading... |
217 | 218 |
218 externs = filter(is_extern, externs_and_deps) | 219 externs = filter(is_extern, externs_and_deps) |
219 deps = filter(lambda f: not is_extern(f), externs_and_deps) | 220 deps = filter(lambda f: not is_extern(f), externs_and_deps) |
220 | 221 |
221 assert externs or deps or self._target | 222 assert externs or deps or self._target |
222 | 223 |
223 self._log_debug("Externs: %s" % externs) | 224 self._log_debug("Externs: %s" % externs) |
224 self._log_debug("Dependencies: %s" % deps) | 225 self._log_debug("Dependencies: %s" % deps) |
225 self._log_debug("Target: %s" % self._target) | 226 self._log_debug("Target: %s" % self._target) |
226 | 227 |
227 js_args = deps + [self._target] if self._target else [] | 228 js_args = deps + ([self._target] if self._target else []) |
228 | 229 |
229 if not custom_sources: | 230 if not custom_sources: |
230 # TODO(dbeam): compiler.jar automatically detects "@externs" in a --js arg | 231 # TODO(dbeam): compiler.jar automatically detects "@externs" in a --js arg |
231 # and moves these files to a different AST tree. However, because we use | 232 # and moves these files to a different AST tree. However, because we use |
232 # one big funky <include> meta-file, it thinks all the code is one big | 233 # one big funky <include> meta-file, it thinks all the code is one big |
233 # externs. Just use --js when <include> dies. | 234 # externs. Just use --js when <include> dies. |
234 | 235 |
235 cwd, tmp_dir = os.getcwd(), tempfile.gettempdir() | 236 cwd, tmp_dir = os.getcwd(), tempfile.gettempdir() |
236 rel_path = lambda f: os.path.join(os.path.relpath(cwd, tmp_dir), f) | 237 rel_path = lambda f: os.path.join(os.path.relpath(cwd, tmp_dir), f) |
237 contents = ['<include src="%s">' % rel_path(f) for f in js_args] | 238 contents = ['<include src="%s">' % rel_path(f) for f in js_args] |
(...skipping 20 matching lines...) Expand all Loading... |
258 args += ["--js_output_file=%s" % out_file] | 259 args += ["--js_output_file=%s" % out_file] |
259 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] | 260 args += ["--create_source_map=%s" % (self._MAP_FILE_FORMAT % out_file)] |
260 | 261 |
261 args_file_content = " %s" % " ".join(args) | 262 args_file_content = " %s" % " ".join(args) |
262 self._log_debug("Args: %s" % args_file_content.strip()) | 263 self._log_debug("Args: %s" % args_file_content.strip()) |
263 | 264 |
264 args_file = self._create_temp_file(args_file_content) | 265 args_file = self._create_temp_file(args_file_content) |
265 self._log_debug("Args file: %s" % args_file) | 266 self._log_debug("Args file: %s" % args_file) |
266 | 267 |
267 runner_args = ["--compiler-args-file=%s" % args_file] | 268 runner_args = ["--compiler-args-file=%s" % args_file] |
| 269 if enable_chrome_pass: |
| 270 runner_args += ["--enable_chrome_pass"] |
| 271 |
268 _, stderr = self._run_jar(self._runner_jar, runner_args) | 272 _, stderr = self._run_jar(self._runner_jar, runner_args) |
269 | 273 |
270 errors = stderr.strip().split("\n\n") | 274 errors = stderr.strip().split("\n\n") |
271 maybe_summary = errors.pop() | 275 maybe_summary = errors.pop() |
272 | 276 |
273 if re.search(".*error.*warning.*typed", maybe_summary): | 277 summary = re.search("(?P<error_count>\d+).*error.*warning", maybe_summary) |
| 278 if summary: |
274 self._log_debug("Summary: %s" % maybe_summary) | 279 self._log_debug("Summary: %s" % maybe_summary) |
275 else: | 280 else: |
276 # Not a summary. Running the jar failed. Bail. | 281 # Not a summary. Running the jar failed. Bail. |
277 self._log_error(stderr) | 282 self._log_error(stderr) |
278 self._nuke_temp_files() | 283 self._nuke_temp_files() |
279 sys.exit(1) | 284 sys.exit(1) |
280 | 285 |
281 if errors and out_file: | 286 if summary.group('error_count') != "0" and out_file: |
282 if os.path.exists(out_file): | 287 if os.path.exists(out_file): |
283 os.remove(out_file) | 288 os.remove(out_file) |
284 if os.path.exists(self._MAP_FILE_FORMAT % out_file): | 289 if os.path.exists(self._MAP_FILE_FORMAT % out_file): |
285 os.remove(self._MAP_FILE_FORMAT % out_file) | 290 os.remove(self._MAP_FILE_FORMAT % out_file) |
286 | 291 |
287 if not custom_sources: | 292 if not custom_sources: |
288 filtered_errors = self._filter_errors(errors) | 293 filtered_errors = self._filter_errors(errors) |
289 errors = map(self._clean_up_error, filtered_errors) | 294 errors = map(self._clean_up_error, filtered_errors) |
290 output = self._format_errors(errors) | 295 output = self._format_errors(errors) |
291 | 296 |
(...skipping 11 matching lines...) Expand all Loading... |
303 parser = argparse.ArgumentParser( | 308 parser = argparse.ArgumentParser( |
304 description="Typecheck JavaScript using Closure compiler") | 309 description="Typecheck JavaScript using Closure compiler") |
305 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, | 310 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, |
306 help="Path to a source file to typecheck") | 311 help="Path to a source file to typecheck") |
307 parser.add_argument("--custom_sources", action="store_true", | 312 parser.add_argument("--custom_sources", action="store_true", |
308 help="Whether this rules has custom sources.") | 313 help="Whether this rules has custom sources.") |
309 parser.add_argument("-o", "--out_file", | 314 parser.add_argument("-o", "--out_file", |
310 help="A file where the compiled output is written to") | 315 help="A file where the compiled output is written to") |
311 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 316 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
312 help="Arguments passed directly to the Closure compiler") | 317 help="Arguments passed directly to the Closure compiler") |
| 318 parser.add_argument("--disable_chrome_pass", action="store_true", |
| 319 help="Disables the extra Chrome specific processing") |
313 parser.add_argument("-v", "--verbose", action="store_true", | 320 parser.add_argument("-v", "--verbose", action="store_true", |
314 help="Show more information as this script runs") | 321 help="Show more information as this script runs") |
315 opts = parser.parse_args() | 322 opts = parser.parse_args() |
316 | 323 |
317 checker = Checker(verbose=opts.verbose) | 324 checker = Checker(verbose=opts.verbose) |
318 | 325 |
319 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, | 326 found_errors, stderr = checker.check( |
320 closure_args=opts.closure_args, | 327 opts.sources, |
321 custom_sources=opts.custom_sources) | 328 out_file=opts.out_file, |
| 329 closure_args=opts.closure_args, |
| 330 custom_sources=opts.custom_sources, |
| 331 enable_chrome_pass=not opts.disable_chrome_pass) |
322 | 332 |
323 if found_errors: | 333 if found_errors: |
324 if opts.custom_sources: | 334 if opts.custom_sources: |
325 print stderr | 335 print stderr |
326 sys.exit(1) | 336 sys.exit(1) |
OLD | NEW |