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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 content: A string of the file contens to write to a temporary file. | 182 content: A string of the file contens to write to a temporary file. |
183 | 183 |
184 Return: | 184 Return: |
185 The filepath of the newly created, written, and closed temporary file. | 185 The filepath of the newly created, written, and closed temporary file. |
186 """ | 186 """ |
187 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: | 187 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: |
188 self._temp_files.append(tmp_file.name) | 188 self._temp_files.append(tmp_file.name) |
189 tmp_file.write(contents) | 189 tmp_file.write(contents) |
190 return tmp_file.name | 190 return tmp_file.name |
191 | 191 |
192 def check(self, sources, out_file=None, closure_args=None, | 192 def check(self, sources, out_file=None, runner_args=None, closure_args=None, |
193 custom_sources=True): | 193 custom_sources=True): |
194 """Closure compile |sources| while checking for errors. | 194 """Closure compile |sources| while checking for errors. |
195 | 195 |
196 Args: | 196 Args: |
197 sources: Files to check. sources[0] is the typically the target file. | 197 sources: Files to check. sources[0] is the typically the target file. |
198 sources[1:] are externs and dependencies in topological order. Order | 198 sources[1:] are externs and dependencies in topological order. Order |
199 is not guaranteed if custom_sources is True. | 199 is not guaranteed if custom_sources is True. |
200 out_file: A file where the compiled output is written to. | 200 out_file: A file where the compiled output is written to. |
201 runner_args: Arguments passed to runner.jar. | |
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). |
204 | 205 |
205 Returns: | 206 Returns: |
206 (found_errors, stderr) A boolean indicating whether errors were found and | 207 (found_errors, stderr) A boolean indicating whether errors were found and |
207 the raw Closure compiler stderr (as a string). | 208 the raw Closure compiler stderr (as a string). |
208 """ | 209 """ |
209 is_extern = lambda f: 'extern' in f | 210 is_extern = lambda f: 'extern' in f |
210 externs_and_deps = [self._POLYMER_EXTERNS] | 211 externs_and_deps = [self._POLYMER_EXTERNS] |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 os.makedirs(out_dir) | 258 os.makedirs(out_dir) |
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 = ["--%s" % arg for arg in runner_args or []] |
aberent
2016/08/02 10:52:24
Using the same variable for both the processed and
Dan Beam
2016/08/02 19:36:33
Done.
| |
269 runner_args += ["--compiler-args-file=%s" % args_file] | |
268 _, stderr = self._run_jar(self._runner_jar, runner_args) | 270 _, stderr = self._run_jar(self._runner_jar, runner_args) |
269 | 271 |
270 errors = stderr.strip().split("\n\n") | 272 errors = stderr.strip().split("\n\n") |
271 maybe_summary = errors.pop() | 273 maybe_summary = errors.pop() |
272 | 274 |
273 if re.search(".*error.*warning.*typed", maybe_summary): | 275 if re.search(".*error.*warning.*typed", maybe_summary): |
274 self._log_debug("Summary: %s" % maybe_summary) | 276 self._log_debug("Summary: %s" % maybe_summary) |
275 else: | 277 else: |
276 # Not a summary. Running the jar failed. Bail. | 278 # Not a summary. Running the jar failed. Bail. |
277 self._log_error(stderr) | 279 self._log_error(stderr) |
(...skipping 23 matching lines...) Expand all Loading... | |
301 | 303 |
302 if __name__ == "__main__": | 304 if __name__ == "__main__": |
303 parser = argparse.ArgumentParser( | 305 parser = argparse.ArgumentParser( |
304 description="Typecheck JavaScript using Closure compiler") | 306 description="Typecheck JavaScript using Closure compiler") |
305 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, | 307 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, |
306 help="Path to a source file to typecheck") | 308 help="Path to a source file to typecheck") |
307 parser.add_argument("--custom_sources", action="store_true", | 309 parser.add_argument("--custom_sources", action="store_true", |
308 help="Whether this rules has custom sources.") | 310 help="Whether this rules has custom sources.") |
309 parser.add_argument("-o", "--out_file", | 311 parser.add_argument("-o", "--out_file", |
310 help="A file where the compiled output is written to") | 312 help="A file where the compiled output is written to") |
313 parser.add_argument("-r", "--runner_args", nargs=argparse.ZERO_OR_MORE, | |
314 help="Arguments passed to runner.jar") | |
311 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 315 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
312 help="Arguments passed directly to the Closure compiler") | 316 help="Arguments passed directly to the Closure compiler") |
313 parser.add_argument("-v", "--verbose", action="store_true", | 317 parser.add_argument("-v", "--verbose", action="store_true", |
314 help="Show more information as this script runs") | 318 help="Show more information as this script runs") |
315 opts = parser.parse_args() | 319 opts = parser.parse_args() |
316 | 320 |
317 checker = Checker(verbose=opts.verbose) | 321 checker = Checker(verbose=opts.verbose) |
318 | 322 |
319 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, | 323 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, |
320 closure_args=opts.closure_args, | 324 closure_args=opts.closure_args, |
325 runner_args=opts.runner_args, | |
321 custom_sources=opts.custom_sources) | 326 custom_sources=opts.custom_sources) |
322 | 327 |
323 if found_errors: | 328 if found_errors: |
324 if opts.custom_sources: | 329 if opts.custom_sources: |
325 print stderr | 330 print stderr |
326 sys.exit(1) | 331 sys.exit(1) |
OLD | NEW |