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 | 182 |
183 Return: | 183 Return: |
184 The filepath of the newly created, written, and closed temporary file. | 184 The filepath of the newly created, written, and closed temporary file. |
185 """ | 185 """ |
186 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: | 186 with tempfile.NamedTemporaryFile(mode="wt", delete=False) as tmp_file: |
187 self._temp_files.append(tmp_file.name) | 187 self._temp_files.append(tmp_file.name) |
188 tmp_file.write(contents) | 188 tmp_file.write(contents) |
189 return tmp_file.name | 189 return tmp_file.name |
190 | 190 |
191 def check(self, sources, out_file, closure_args=None, | 191 def check(self, sources, out_file, closure_args=None, |
192 custom_sources=True): | 192 custom_sources=True, custom_includes=False): |
193 """Closure compile |sources| while checking for errors. | 193 """Closure compile |sources| while checking for errors. |
194 | 194 |
195 Args: | 195 Args: |
196 sources: Files to check. sources[0] is the typically the target file. | 196 sources: Files to check. sources[0] is the typically the target file. |
197 sources[1:] are externs and dependencies in topological order. Order | 197 sources[1:] are externs and dependencies in topological order. Order |
198 is not guaranteed if custom_sources is True. | 198 is not guaranteed if custom_sources is True. |
199 out_file: A file where the compiled output is written to. | 199 out_file: A file where the compiled output is written to. |
200 closure_args: Arguments passed directly to the Closure compiler. | 200 closure_args: Arguments passed directly to the Closure compiler. |
201 custom_sources: Whether |sources| was customized by the target (e.g. not | 201 custom_sources: Whether |sources| was customized by the target (e.g. not |
202 in GYP dependency order). | 202 in GYP dependency order). |
203 | 203 |
204 Returns: | 204 Returns: |
205 (found_errors, stderr) A boolean indicating whether errors were found and | 205 (found_errors, stderr) A boolean indicating whether errors were found and |
206 the raw Closure compiler stderr (as a string). | 206 the raw Closure compiler stderr (as a string). |
207 """ | 207 """ |
208 is_extern = lambda f: 'externs' in f | 208 is_extern = lambda f: 'externs' in f |
209 externs_and_deps = [self._POLYMER_EXTERNS] | 209 externs_and_deps = [self._POLYMER_EXTERNS] |
210 | 210 |
211 if custom_sources: | 211 if custom_sources: |
| 212 if custom_includes: |
| 213 # TODO(dbeam): this is fairly hacky. Can we just remove custom_sources |
| 214 # soon when all the things kept on life support using it die? |
| 215 self._target = sources.pop() |
212 externs_and_deps += sources | 216 externs_and_deps += sources |
213 else: | 217 else: |
214 self._target = sources[0] | 218 self._target = sources[0] |
215 externs_and_deps += sources[1:] | 219 externs_and_deps += sources[1:] |
216 | 220 |
217 externs = filter(is_extern, externs_and_deps) | 221 externs = filter(is_extern, externs_and_deps) |
218 deps = filter(lambda f: not is_extern(f), externs_and_deps) | 222 deps = filter(lambda f: not is_extern(f), externs_and_deps) |
219 | 223 |
220 assert externs or deps or self._target | 224 assert externs or deps or self._target |
221 | 225 |
222 self._log_debug("Externs: %s" % externs) | 226 self._log_debug("Externs: %s" % externs) |
223 self._log_debug("Dependencies: %s" % deps) | 227 self._log_debug("Dependencies: %s" % deps) |
224 self._log_debug("Target: %s" % self._target) | 228 self._log_debug("Target: %s" % self._target) |
225 | 229 |
226 js_args = deps + ([self._target] if self._target else []) | 230 js_args = deps + ([self._target] if self._target else []) |
227 | 231 |
228 if not custom_sources: | 232 process_includes = custom_includes or not custom_sources |
| 233 if process_includes: |
229 # TODO(dbeam): compiler.jar automatically detects "@externs" in a --js arg | 234 # TODO(dbeam): compiler.jar automatically detects "@externs" in a --js arg |
230 # and moves these files to a different AST tree. However, because we use | 235 # and moves these files to a different AST tree. However, because we use |
231 # one big funky <include> meta-file, it thinks all the code is one big | 236 # one big funky <include> meta-file, it thinks all the code is one big |
232 # externs. Just use --js when <include> dies. | 237 # externs. Just use --js when <include> dies. |
233 | 238 |
234 cwd, tmp_dir = os.getcwd(), tempfile.gettempdir() | 239 cwd, tmp_dir = os.getcwd(), tempfile.gettempdir() |
235 rel_path = lambda f: os.path.join(os.path.relpath(cwd, tmp_dir), f) | 240 rel_path = lambda f: os.path.join(os.path.relpath(cwd, tmp_dir), f) |
236 contents = ['<include src="%s">' % rel_path(f) for f in js_args] | 241 contents = ['<include src="%s">' % rel_path(f) for f in js_args] |
237 meta_file = self._create_temp_file("\n".join(contents)) | 242 meta_file = self._create_temp_file("\n".join(contents)) |
238 self._log_debug("Meta file: %s" % meta_file) | 243 self._log_debug("Meta file: %s" % meta_file) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 elif checks_only: | 291 elif checks_only: |
287 # Compile succeeded but --checks_only disables --js_output_file from | 292 # Compile succeeded but --checks_only disables --js_output_file from |
288 # actually writing a file. Write a file ourselves so incremental builds | 293 # actually writing a file. Write a file ourselves so incremental builds |
289 # still work. | 294 # still work. |
290 with open(out_file, 'w') as f: | 295 with open(out_file, 'w') as f: |
291 f.write('') | 296 f.write('') |
292 | 297 |
293 if not custom_sources: | 298 if process_includes: |
294 filtered_errors = self._filter_errors(errors) | 299 filtered_errors = self._filter_errors(errors) |
295 errors = map(self._clean_up_error, filtered_errors) | 300 errors = map(self._clean_up_error, filtered_errors) |
296 output = self._format_errors(errors) | 301 output = self._format_errors(errors) |
297 | 302 |
298 if errors: | 303 if errors: |
299 prefix = "\n" if output else "" | 304 prefix = "\n" if output else "" |
300 self._log_error("Error in: %s%s%s" % (self._target, prefix, output)) | 305 self._log_error("Error in: %s%s%s" % (self._target, prefix, output)) |
301 elif output: | 306 elif output: |
302 self._log_debug("Output: %s" % output) | 307 self._log_debug("Output: %s" % output) |
303 | 308 |
304 self._nuke_temp_files() | 309 self._nuke_temp_files() |
305 return bool(errors), stderr | 310 return bool(errors), stderr |
306 | 311 |
307 | 312 |
308 if __name__ == "__main__": | 313 if __name__ == "__main__": |
309 parser = argparse.ArgumentParser( | 314 parser = argparse.ArgumentParser( |
310 description="Typecheck JavaScript using Closure compiler") | 315 description="Typecheck JavaScript using Closure compiler") |
311 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, | 316 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, |
312 help="Path to a source file to typecheck") | 317 help="Path to a source file to typecheck") |
313 parser.add_argument("--custom_sources", action="store_true", | 318 parser.add_argument("--custom_sources", action="store_true", |
314 help="Whether this rules has custom sources.") | 319 help="Whether this rules has custom sources.") |
| 320 parser.add_argument("--custom_includes", action="store_true", |
| 321 help="If present, <include>s are processed when" |
| 322 "using --custom_files.") |
315 parser.add_argument("-o", "--out_file", required=True, | 323 parser.add_argument("-o", "--out_file", required=True, |
316 help="A file where the compiled output is written to") | 324 help="A file where the compiled output is written to") |
317 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 325 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
318 help="Arguments passed directly to the Closure compiler") | 326 help="Arguments passed directly to the Closure compiler") |
319 parser.add_argument("-v", "--verbose", action="store_true", | 327 parser.add_argument("-v", "--verbose", action="store_true", |
320 help="Show more information as this script runs") | 328 help="Show more information as this script runs") |
321 opts = parser.parse_args() | 329 opts = parser.parse_args() |
322 | 330 |
323 checker = Checker(verbose=opts.verbose) | 331 checker = Checker(verbose=opts.verbose) |
324 | 332 |
325 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, | 333 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, |
326 closure_args=opts.closure_args, | 334 closure_args=opts.closure_args, |
327 custom_sources=opts.custom_sources) | 335 custom_sources=opts.custom_sources, |
| 336 custom_includes=opts.custom_includes) |
328 | 337 |
329 if found_errors: | 338 if found_errors: |
330 if opts.custom_sources: | 339 if opts.custom_sources: |
331 print stderr | 340 print stderr |
332 sys.exit(1) | 341 sys.exit(1) |
OLD | NEW |