OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generates java source files from a mojom.Module.""" | 5 """Generates java source files from a mojom.Module.""" |
6 | 6 |
7 import argparse | 7 import argparse |
8 import ast | 8 import ast |
| 9 import contextlib |
9 import os | 10 import os |
10 import re | 11 import re |
| 12 import shutil |
| 13 import tempfile |
| 14 import zipfile |
11 | 15 |
12 from jinja2 import contextfilter | 16 from jinja2 import contextfilter |
13 | 17 |
14 import mojom.generate.generator as generator | 18 import mojom.generate.generator as generator |
15 import mojom.generate.module as mojom | 19 import mojom.generate.module as mojom |
16 from mojom.generate.template_expander import UseJinja | 20 from mojom.generate.template_expander import UseJinja |
17 | 21 |
18 | 22 |
19 GENERATOR_PREFIX = 'java' | 23 GENERATOR_PREFIX = 'java' |
20 | 24 |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 if method.response_parameters: | 347 if method.response_parameters: |
344 return True | 348 return True |
345 return False | 349 return False |
346 | 350 |
347 def HasMethodWithoutResponse(interface): | 351 def HasMethodWithoutResponse(interface): |
348 for method in interface.methods: | 352 for method in interface.methods: |
349 if not method.response_parameters: | 353 if not method.response_parameters: |
350 return True | 354 return True |
351 return False | 355 return False |
352 | 356 |
| 357 @contextlib.contextmanager |
| 358 def TempDir(): |
| 359 dirname = tempfile.mkdtemp() |
| 360 try: |
| 361 yield dirname |
| 362 finally: |
| 363 shutil.rmtree(dirname) |
| 364 |
| 365 def ZipContentInto(root, zip_filename): |
| 366 with zipfile.ZipFile(zip_filename, 'w') as zip_file: |
| 367 for dirname, _, files in os.walk(root): |
| 368 for filename in files: |
| 369 path = os.path.join(dirname, filename) |
| 370 path_in_archive = os.path.relpath(path, root) |
| 371 zip_file.write(path, path_in_archive) |
| 372 |
353 class Generator(generator.Generator): | 373 class Generator(generator.Generator): |
354 | 374 |
355 java_filters = { | 375 java_filters = { |
356 'interface_response_name': GetInterfaceResponseName, | 376 'interface_response_name': GetInterfaceResponseName, |
357 'constant_value': ConstantValue, | 377 'constant_value': ConstantValue, |
358 'default_value': DefaultValue, | 378 'default_value': DefaultValue, |
359 'decode_method': DecodeMethod, | 379 'decode_method': DecodeMethod, |
360 'expression_to_text': ExpressionToText, | 380 'expression_to_text': ExpressionToText, |
361 'encode_method': EncodeMethod, | 381 'encode_method': EncodeMethod, |
362 'has_method_with_response': HasMethodWithResponse, | 382 'has_method_with_response': HasMethodWithResponse, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 def GenerateInterfaceInternalSource(self, interface): | 430 def GenerateInterfaceInternalSource(self, interface): |
411 return self.GetJinjaExportsForInterface(interface) | 431 return self.GetJinjaExportsForInterface(interface) |
412 | 432 |
413 @UseJinja('java_templates/constants.java.tmpl', filters=java_filters) | 433 @UseJinja('java_templates/constants.java.tmpl', filters=java_filters) |
414 def GenerateConstantsSource(self, module): | 434 def GenerateConstantsSource(self, module): |
415 exports = self.GetJinjaExports() | 435 exports = self.GetJinjaExports() |
416 exports.update({'main_entity': GetConstantsMainEntityName(module), | 436 exports.update({'main_entity': GetConstantsMainEntityName(module), |
417 'constants': module.constants}) | 437 'constants': module.constants}) |
418 return exports | 438 return exports |
419 | 439 |
420 def GenerateFiles(self, unparsed_args): | 440 def DoGenerateFiles(self): |
421 parser = argparse.ArgumentParser() | 441 if not os.path.exists(self.output_dir): |
422 parser.add_argument('--java_output_directory', dest='java_output_directory') | 442 try: |
423 args = parser.parse_args(unparsed_args) | 443 os.makedirs(self.output_dir) |
424 if self.output_dir and args.java_output_directory: | 444 except: |
425 self.output_dir = os.path.join(args.java_output_directory, | 445 # Ignore errors on directory creation. |
426 GetPackage(self.module).replace('.', '/')) | 446 pass |
427 if not os.path.exists(self.output_dir): | |
428 try: | |
429 os.makedirs(self.output_dir) | |
430 except: | |
431 # Ignore errors on directory creation. | |
432 pass | |
433 | 447 |
434 # Keep this above the others as .GetStructs() changes the state of the | 448 # Keep this above the others as .GetStructs() changes the state of the |
435 # module, annotating structs with required information. | 449 # module, annotating structs with required information. |
436 for struct in self.GetStructs(): | 450 for struct in self.GetStructs(): |
437 self.Write(self.GenerateStructSource(struct), | 451 self.Write(self.GenerateStructSource(struct), |
438 '%s.java' % GetNameForElement(struct)) | 452 '%s.java' % GetNameForElement(struct)) |
439 | 453 |
440 for enum in self.module.enums: | 454 for enum in self.module.enums: |
441 self.Write(self.GenerateEnumSource(enum), | 455 self.Write(self.GenerateEnumSource(enum), |
442 '%s.java' % GetNameForElement(enum)) | 456 '%s.java' % GetNameForElement(enum)) |
443 | 457 |
444 for interface in self.module.interfaces: | 458 for interface in self.module.interfaces: |
445 self.Write(self.GenerateInterfaceSource(interface), | 459 self.Write(self.GenerateInterfaceSource(interface), |
446 '%s.java' % GetNameForElement(interface)) | 460 '%s.java' % GetNameForElement(interface)) |
447 self.Write(self.GenerateInterfaceInternalSource(interface), | 461 self.Write(self.GenerateInterfaceInternalSource(interface), |
448 '%s_Internal.java' % GetNameForElement(interface)) | 462 '%s_Internal.java' % GetNameForElement(interface)) |
449 | 463 |
450 if self.module.constants: | 464 if self.module.constants: |
451 self.Write(self.GenerateConstantsSource(self.module), | 465 self.Write(self.GenerateConstantsSource(self.module), |
452 '%s.java' % GetConstantsMainEntityName(self.module)) | 466 '%s.java' % GetConstantsMainEntityName(self.module)) |
453 | 467 |
| 468 def GenerateFiles(self, unparsed_args): |
| 469 parser = argparse.ArgumentParser() |
| 470 parser.add_argument('--java_output_directory', dest='java_output_directory') |
| 471 args = parser.parse_args(unparsed_args) |
| 472 package_path = GetPackage(self.module).replace('.', '/') |
| 473 |
| 474 # Generate the java files in a temporary directory and place a single |
| 475 # srcjar in the output directory. |
| 476 zip_filename = os.path.join(self.output_dir, |
| 477 "%s.srcjar" % self.module.name) |
| 478 with TempDir() as temp_java_root: |
| 479 self.output_dir = os.path.join(temp_java_root, package_path) |
| 480 self.DoGenerateFiles(); |
| 481 ZipContentInto(temp_java_root, zip_filename) |
| 482 |
| 483 if args.java_output_directory: |
| 484 # If requested, generate the java files directly into indicated directory. |
| 485 self.output_dir = os.path.join(args.java_output_directory, package_path) |
| 486 self.DoGenerateFiles(); |
| 487 |
454 def GetJinjaParameters(self): | 488 def GetJinjaParameters(self): |
455 return { | 489 return { |
456 'lstrip_blocks': True, | 490 'lstrip_blocks': True, |
457 'trim_blocks': True, | 491 'trim_blocks': True, |
458 } | 492 } |
459 | 493 |
460 def GetGlobals(self): | 494 def GetGlobals(self): |
461 return { | 495 return { |
462 'namespace': self.module.namespace, | 496 'namespace': self.module.namespace, |
463 'module': self.module, | 497 'module': self.module, |
464 } | 498 } |
OLD | NEW |