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 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, runner_args=None, closure_args=None, | 192 def check(self, |
| 193 sources, | |
| 194 gn_source_list_file=None, | |
| 195 out_file=None, | |
| 196 runner_args=None, | |
| 197 closure_args=None, | |
| 193 custom_sources=True): | 198 custom_sources=True): |
| 194 """Closure compile |sources| while checking for errors. | 199 """Closure compile |sources| while checking for errors. |
| 195 | 200 |
| 196 Args: | 201 Args: |
| 197 sources: Files to check. sources[0] is the typically the target file. | 202 sources: Files to check. sources[0] is the typically the target file. |
| 198 sources[1:] are externs and dependencies in topological order. Order | 203 sources[1:] are externs and dependencies in topological order. Order |
| 199 is not guaranteed if custom_sources is True. | 204 is not guaranteed if custom_sources is True. |
| 200 out_file: A file where the compiled output is written to. | 205 out_file: A file where the compiled output is written to. |
| 201 runner_args: Arguments passed to runner.jar. | 206 runner_args: Arguments passed to runner.jar. |
| 202 closure_args: Arguments passed directly to the Closure compiler. | 207 closure_args: Arguments passed directly to the Closure compiler. |
| 203 custom_sources: Whether |sources| was customized by the target (e.g. not | 208 custom_sources: Whether |sources| was customized by the target (e.g. not |
| 204 in GYP dependency order). | 209 in GYP dependency order). |
| 205 | 210 |
| 206 Returns: | 211 Returns: |
| 207 (found_errors, stderr) A boolean indicating whether errors were found and | 212 (found_errors, stderr) A boolean indicating whether errors were found and |
| 208 the raw Closure compiler stderr (as a string). | 213 the raw Closure compiler stderr (as a string). |
| 209 """ | 214 """ |
| 210 is_extern = lambda f: 'extern' in f | 215 is_extern = lambda f: 'extern' in f |
| 211 externs_and_deps = [self._POLYMER_EXTERNS] | 216 externs_and_deps = [self._POLYMER_EXTERNS] |
| 217 | |
| 218 # Convert the sources to absolute paths to make comparisons easy. | |
| 219 sources = map(os.path.abspath, sources) | |
| 220 # Process the source list. | |
| 221 if gn_source_list_file: | |
| 222 with open(gn_source_list_file) as f: | |
| 223 listed_sources = f.read().splitlines() | |
| 224 # Ignore the first line, GN puts the output file name here | |
| 225 listed_sources = listed_sources[1:] | |
| 226 listed_sources = map(os.path.abspath, listed_sources) | |
| 227 # Concatenate onto sources, removing duplicates | |
| 228 sources += [x for x in listed_sources if x not in sources] | |
| 212 | 229 |
| 213 if custom_sources: | 230 if custom_sources: |
| 214 externs_and_deps += sources | 231 externs_and_deps += sources |
| 215 else: | 232 else: |
| 216 self._target = sources[0] | 233 self._target = sources[0] |
| 217 externs_and_deps += sources[1:] | 234 externs_and_deps += sources[1:] |
| 218 | 235 |
| 219 externs = filter(is_extern, externs_and_deps) | 236 externs = filter(is_extern, externs_and_deps) |
| 220 deps = filter(lambda f: not is_extern(f), externs_and_deps) | 237 deps = filter(lambda f: not is_extern(f), externs_and_deps) |
| 221 | 238 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 | 317 |
| 301 self._nuke_temp_files() | 318 self._nuke_temp_files() |
| 302 return bool(errors), stderr | 319 return bool(errors), stderr |
| 303 | 320 |
| 304 | 321 |
| 305 if __name__ == "__main__": | 322 if __name__ == "__main__": |
| 306 parser = argparse.ArgumentParser( | 323 parser = argparse.ArgumentParser( |
| 307 description="Typecheck JavaScript using Closure compiler") | 324 description="Typecheck JavaScript using Closure compiler") |
| 308 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, | 325 parser.add_argument("sources", nargs=argparse.ONE_OR_MORE, |
| 309 help="Path to a source file to typecheck") | 326 help="Path to a source file to typecheck") |
| 327 parser.add_argument( | |
| 328 "--gn_source_list_file", | |
| 329 help="""A file generated by GN containing a list of | |
| 330 additional sources. Due to an oddity in how this is | |
| 331 generated the first line is ignored""") | |
| 310 parser.add_argument("--custom_sources", action="store_true", | 332 parser.add_argument("--custom_sources", action="store_true", |
| 311 help="Whether this rules has custom sources.") | 333 help="Whether this rules has custom sources.") |
| 312 parser.add_argument("-o", "--out_file", | 334 parser.add_argument("-o", "--out_file", |
| 313 help="A file where the compiled output is written to") | 335 help="A file where the compiled output is written to") |
| 314 parser.add_argument("-r", "--runner_args", nargs=argparse.ZERO_OR_MORE, | 336 parser.add_argument("-r", "--runner_args", nargs=argparse.ZERO_OR_MORE, |
| 315 help="Arguments passed to runner.jar") | 337 help="Arguments passed to runner.jar") |
| 316 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, | 338 parser.add_argument("-c", "--closure_args", nargs=argparse.ZERO_OR_MORE, |
| 317 help="Arguments passed directly to the Closure compiler") | 339 help="Arguments passed directly to the Closure compiler") |
| 318 parser.add_argument("-v", "--verbose", action="store_true", | 340 parser.add_argument("-v", "--verbose", action="store_true", |
| 319 help="Show more information as this script runs") | 341 help="Show more information as this script runs") |
| 320 opts = parser.parse_args() | 342 opts = parser.parse_args() |
| 321 | 343 |
| 322 checker = Checker(verbose=opts.verbose) | 344 checker = Checker(verbose=opts.verbose) |
| 323 | 345 |
| 324 found_errors, stderr = checker.check(opts.sources, out_file=opts.out_file, | 346 found_errors, stderr = checker.check(opts.sources, |
| 347 gn_source_list_file=opts.gn_source_list_f ile, | |
|
Dan Beam
2016/09/07 18:18:05
nit: wrap at 80 cols
| |
| 348 out_file=opts.out_file, | |
| 325 closure_args=opts.closure_args, | 349 closure_args=opts.closure_args, |
| 326 runner_args=opts.runner_args, | 350 runner_args=opts.runner_args, |
| 327 custom_sources=opts.custom_sources) | 351 custom_sources=opts.custom_sources) |
| 328 | 352 |
| 329 if found_errors: | 353 if found_errors: |
| 330 if opts.custom_sources: | 354 if opts.custom_sources: |
| 331 print stderr | 355 print stderr |
| 332 sys.exit(1) | 356 sys.exit(1) |
| OLD | NEW |