This means the betterproto plugin no longer needs to depend durectly on protobuf. This requires a small runtime hack to monkey patch some google types to get around the fact that the compiler uses proto2, but betterproto expects proto3. Also: - regenerate google.protobuf package - fix a regex bug in the logic for determining whether to use a google wrapper type. - fix a bug causing comments to get mixed up when multiple proto files generate code into a single python module
57 lines
1.5 KiB
Python
Executable File
57 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import os
|
|
import sys
|
|
|
|
from betterproto.lib.google.protobuf.compiler import (
|
|
CodeGeneratorRequest,
|
|
CodeGeneratorResponse,
|
|
)
|
|
|
|
from betterproto.plugin.parser import generate_code
|
|
from betterproto.plugin.models import monkey_patch_oneof_index
|
|
|
|
|
|
def main() -> None:
|
|
"""The plugin's main entry point."""
|
|
# Read request message from stdin
|
|
data = sys.stdin.buffer.read()
|
|
|
|
# Apply Work around for proto2/3 difference in protoc messages
|
|
monkey_patch_oneof_index()
|
|
|
|
# Parse request
|
|
request = CodeGeneratorRequest()
|
|
request.parse(data)
|
|
|
|
dump_file = os.getenv("BETTERPROTO_DUMP")
|
|
if dump_file:
|
|
dump_request(dump_file, request)
|
|
|
|
# Create response
|
|
response = CodeGeneratorResponse()
|
|
|
|
# Generate code
|
|
generate_code(request, response)
|
|
|
|
# Serialise response message
|
|
output = response.SerializeToString()
|
|
|
|
# Write to stdout
|
|
sys.stdout.buffer.write(output)
|
|
|
|
|
|
def dump_request(dump_file: str, request: CodeGeneratorRequest) -> None:
|
|
"""
|
|
For developers: Supports running plugin.py standalone so its possible to debug it.
|
|
Run protoc (or generate.py) with BETTERPROTO_DUMP="yourfile.bin" to write the request to a file.
|
|
Then run plugin.py from your IDE in debugging mode, and redirect stdin to the file.
|
|
"""
|
|
with open(str(dump_file), "wb") as fh:
|
|
sys.stderr.write(f"\033[31mWriting input from protoc to: {dump_file}\033[0m\n")
|
|
fh.write(request.SerializeToString())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|