5
5
6
6
from pants .backend .codegen .protobuf .protoc import Protoc
7
7
from pants .backend .codegen .protobuf .python .additional_fields import PythonSourceRootField
8
+ from pants .backend .codegen .protobuf .python .grpc_python_plugin import GrpcPythonPlugin
8
9
from pants .backend .codegen .protobuf .python .python_protobuf_subsystem import PythonProtobufSubsystem
9
- from pants .backend .codegen .protobuf .target_types import ProtobufSources
10
+ from pants .backend .codegen .protobuf .target_types import ProtobufGrcpToggle , ProtobufSources
10
11
from pants .backend .python .target_types import PythonSources
11
12
from pants .backend .python .util_rules import extract_pex , pex
12
13
from pants .backend .python .util_rules .extract_pex import ExtractedPexDistributions
@@ -52,6 +53,7 @@ class GeneratePythonFromProtobufRequest(GenerateSourcesRequest):
52
53
async def generate_python_from_protobuf (
53
54
request : GeneratePythonFromProtobufRequest ,
54
55
protoc : Protoc ,
56
+ grpc_python_plugin : GrpcPythonPlugin ,
55
57
python_protobuf_subsystem : PythonProtobufSubsystem ,
56
58
) -> GeneratedSources :
57
59
download_protoc_request = Get (
@@ -112,13 +114,25 @@ async def generate_python_from_protobuf(
112
114
)
113
115
extracted_mypy_wheels = await Get (ExtractedPexDistributions , Pex , mypy_pex )
114
116
117
+ downloaded_grpc_plugin = (
118
+ await Get (
119
+ DownloadedExternalTool ,
120
+ ExternalToolRequest ,
121
+ grpc_python_plugin .get_request (Platform .current ),
122
+ )
123
+ if request .protocol_target .get (ProtobufGrcpToggle ).value
124
+ else None
125
+ )
126
+
115
127
unmerged_digests = [
116
128
all_sources_stripped .snapshot .digest ,
117
129
downloaded_protoc_binary .digest ,
118
130
empty_output_dir ,
119
131
]
120
132
if extracted_mypy_wheels :
121
133
unmerged_digests .append (extracted_mypy_wheels .digest )
134
+ if downloaded_grpc_plugin :
135
+ unmerged_digests .append (downloaded_grpc_plugin .digest )
122
136
input_digest = await Get (Digest , MergeDigests (unmerged_digests ))
123
137
124
138
argv = [downloaded_protoc_binary .exe , "--python_out" , output_dir ]
@@ -135,6 +149,10 @@ async def generate_python_from_protobuf(
135
149
output_dir ,
136
150
]
137
151
)
152
+ if downloaded_grpc_plugin :
153
+ argv .extend (
154
+ [f"--plugin=protoc-gen-grpc={ downloaded_grpc_plugin .exe } " , "--grpc_out" , output_dir ]
155
+ )
138
156
argv .extend (target_sources_stripped .snapshot .files )
139
157
140
158
env = {}
0 commit comments