| 1 |
|
| 2 |
|
| 3 |
from subprocess import check_output |
| 4 |
|
| 5 |
import os |
| 6 |
|
| 7 |
from abc import ABC, abstractmethod |
| 8 |
|
| 9 |
workspace = check_output(["bazel", "info", "workspace"]).decode().strip() |
| 10 |
|
| 11 |
|
| 12 |
class LanguageConfig(ABC): |
| 13 |
"""Abstract base class for language-specific configurations.""" |
| 14 |
|
| 15 |
@property |
| 16 |
@abstractmethod |
| 17 |
def language(self) -> str: |
| 18 |
"""Returns the language name.""" |
| 19 |
pass |
| 20 |
|
| 21 |
@property |
| 22 |
@abstractmethod |
| 23 |
def bazel_query_kind(self) -> str: |
| 24 |
"""Returns the Bazel query kind for this language.""" |
| 25 |
pass |
| 26 |
|
| 27 |
@property |
| 28 |
@abstractmethod |
| 29 |
def generated_file_pattern(self) -> str: |
| 30 |
"""Returns the file pattern for generated files.""" |
| 31 |
pass |
| 32 |
|
| 33 |
@abstractmethod |
| 34 |
def get_input_dir(self, bazel_bin: str, rule_dir: str) -> str: |
| 35 |
"""Constructs the input directory path for generated files.""" |
| 36 |
pass |
| 37 |
|
| 38 |
|
| 39 |
class GoConfig(LanguageConfig): |
| 40 |
"""Configuration for Go.""" |
| 41 |
|
| 42 |
@property |
| 43 |
def language(self) -> str: |
| 44 |
return "go" |
| 45 |
|
| 46 |
@property |
| 47 |
def bazel_query_kind(self) -> str: |
| 48 |
return 'kind("go_proto_library", ...)' |
| 49 |
|
| 50 |
@property |
| 51 |
def generated_file_pattern(self) -> str: |
| 52 |
return "*.go" |
| 53 |
|
| 54 |
def get_input_dir(self, bazel_bin: str, rule_dir: str) -> str: |
| 55 |
return os.path.join( |
| 56 |
bazel_bin, rule_dir, "pkg_go_proto_", "github.com/cncf/xds/go", rule_dir |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
class PythonConfig(LanguageConfig): |
| 61 |
"""Configuration for Python.""" |
| 62 |
|
| 63 |
@property |
| 64 |
def language(self) -> str: |
| 65 |
return "python" |
| 66 |
|
| 67 |
@property |
| 68 |
def bazel_query_kind(self) -> str: |
| 69 |
return 'kind("py_proto_library", ...)' |
| 70 |
|
| 71 |
@property |
| 72 |
def generated_file_pattern(self) -> str: |
| 73 |
return "*_pb2.py" |
| 74 |
|
| 75 |
def get_input_dir(self, bazel_bin: str, rule_dir: str) -> str: |
| 76 |
return os.path.join(bazel_bin, rule_dir) |
| 77 |
|
| 78 |
|