Its too hard to bring maven to accept protobuf, bash is easier. But protobuf files generate warning. Seems not possible to ignore the warning everywhere. SuppressWarnings in packe-info.java on package level is not possible. This script adds a SuppressWarnings to all generated protobuf files and can be used as step after protoc invocation. Inegrates well with bash-based workflows.
from pathlib import Path
def main():
proto_dir = Path("src/main/proto")
java_dir = Path("src/main/java")
assert proto_dir.is_dir(), f"{proto_dir} is not a directory"
# Step 1: Collect packages
package_paths = set()
for proto_file_path in proto_dir.glob("**/*.proto"):
package_path = proto_file_path.parent
java_package_path = java_dir / package_path.relative_to(proto_dir)
package_paths.add(java_package_path)
# Step 2: Get all java files
java_file_paths = set()
for package in package_paths:
for java_file_path in package.glob("**/*.java"):
java_file_paths.add(java_file_path)
# Step 3: Add SuppressWarnings annotation
for java_file_path in java_file_paths:
with open(java_file_path, "r") as file:
lines = file.readlines()
if not is_proto_generated_file(lines):
continue
insert_supress_warning(lines)
with open(java_file_path, "w") as file:
for line in lines:
file.write(line)
def is_proto_generated_file(lines: list[str]):
for line in lines:
if not line.strip():
continue
if "Generated by the protocol buffer compiler" in line:
return True
return False
def insert_supress_warning(lines: list[str]):
for i, line in enumerate(lines):
if '@SuppressWarnings({"all"})' in line:
return
if "public final class" in line:
lines.insert(i, '@SuppressWarnings({"all"})\n')
return
main()
No comments:
Post a Comment