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