Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/betterproto2_compiler/plugin/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,23 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
code = body_template.render(output_file=output_file)
code = header_template.render(output_file=output_file, version=version) + "\n" + code

# Sort imports, delete unused ones
code = subprocess.check_output(
["ruff", "check", "--select", "I,F401,TCH005", "--fix", "--silent", "-"],
input=code,
encoding="utf-8",
)
try:
# Sort imports, delete unused ones
code = subprocess.check_output(
["ruff", "check", "--select", "I,F401,TCH005", "--fix", "--silent", "-"],
input=code,
encoding="utf-8",
)

# Format the code
code = subprocess.check_output(["ruff", "format", "-"], input=code, encoding="utf-8")
except subprocess.CalledProcessError:
with open("invalid-generated-code.py", "w") as f:
f.write(code)

# Format the code
code = subprocess.check_output(["ruff", "format", "-"], input=code, encoding="utf-8")
raise SyntaxError(
f"Can't format the source code:\nThe invalid generated code has been written in `invalid-generated-code.py`"
)

# Validate the generated code.
validator = ModuleValidator(iter(code.splitlines()))
Expand Down
Loading