using_zap.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2022 Project CHIP Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import os
  16. from enum import Enum, auto
  17. from .types import IdlFileType, InputIdlFile
  18. ZAP_GENERATE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'tools', 'zap', 'generate.py'))
  19. class ZapGeneratorType(Enum):
  20. APPLICATION_TEMPLATES = auto()
  21. @property
  22. def generation_template(self):
  23. if self == ZapGeneratorType.APPLICATION_TEMPLATES:
  24. return 'src/app/zap-templates/app-templates.json'
  25. else:
  26. raise Exception("Missing ZAP Generation type implementation")
  27. @property
  28. def subdir(self):
  29. if self == ZapGeneratorType.APPLICATION_TEMPLATES:
  30. return 'app-templates/zap-generated'
  31. else:
  32. raise Exception("Missing ZAP Generation type implementation")
  33. class ZapTarget:
  34. def __init__(self, idl: InputIdlFile, generation_type: ZapGeneratorType, sdk_root: str, runner):
  35. self.idl = idl
  36. self.sdk_root = sdk_root
  37. self.generation_type = generation_type
  38. self.runner = runner
  39. if idl.file_type != IdlFileType.ZAP:
  40. raise Exception(f"Can only code generate for `*.zap` input files, not for {idl}")
  41. def Generate(self, output_root: str):
  42. '''Runs generate.py to generate in the specified directory root'''
  43. output_dir = os.path.join(output_root, self.idl.pregen_subdir, self.generation_type.subdir)
  44. logging.info(f"Generating: {self.generation_type}:{self.idl.full_path} into {output_dir}")
  45. self.runner.ensure_directory_exists(output_dir)
  46. if self.idl.full_path.startswith(self.sdk_root):
  47. idl_path = self.idl.relative_path
  48. else:
  49. idl_path = self.idl.full_path
  50. cmd = [
  51. ZAP_GENERATE_PATH,
  52. '--templates', self.generation_type.generation_template,
  53. '--output-dir', output_dir,
  54. '--parallel',
  55. idl_path
  56. ]
  57. logging.debug(f"Executing {cmd}")
  58. self.runner.run(cmd, cwd=self.sdk_root)
  59. class ZapApplicationPregenerator:
  60. """Pregeneration logic for `src/app/zap-templates/app-templates.json` """
  61. def __init__(self, sdk_root):
  62. self.sdk_root = sdk_root
  63. def Accept(self, idl: InputIdlFile):
  64. if idl.file_type != IdlFileType.ZAP:
  65. return False
  66. # FIXME: implement a proper check
  67. if 'test_files' in idl.relative_path:
  68. return False
  69. return True
  70. def CreateTarget(self, idl: InputIdlFile, runner):
  71. # TODO: add additional arguments: tell how to invoke zap/codegen
  72. return ZapTarget(sdk_root=self.sdk_root, generation_type=ZapGeneratorType.APPLICATION_TEMPLATES, idl=idl, runner=runner)