Skip to content
Merged
Show file tree
Hide file tree
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
66 changes: 45 additions & 21 deletions bakelite/generator/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=redefined-builtin
"""Command-line interface for bakelite code generation."""

import sys
from pathlib import Path

import click

Expand All @@ -9,50 +10,73 @@

@click.group()
def cli() -> None:
pass
"""Bakelite protocol code generator."""


@cli.command()
@click.option("--language", "-l", required=True)
@click.option("--input", "-i", required=True)
@click.option("--output", "-o", required=True)
def gen(language: str, input: str, output: str) -> None:
with open(input, "r", encoding="utf-8") as f:
@click.option("--language", "-l", required=True, help="Target language (python, cpptiny)")
@click.option("--input", "-i", "input_file", required=True, help="Input protocol file")
@click.option("--output", "-o", "output_file", required=True, help="Output file")
@click.option(
"--runtime-import",
"runtime_import",
is_flag=False,
flag_value="bakelite.proto",
default=None,
help="Import path for runtime (no value=bakelite.proto, omit=bakelite_runtime)",
)
Comment thread
emmapowers marked this conversation as resolved.
def gen(language: str, input_file: str, output_file: str, runtime_import: str | None) -> None:
"""Generate protocol code from a definition file."""
with open(input_file, encoding="utf-8") as f:
proto = f.read()

proto_def = parse(proto)

if language == "python":
generated_file = python.render(*proto_def)
# Default to "bakelite_runtime" (relative import) if not specified
import_path = runtime_import if runtime_import is not None else "bakelite_runtime"
generated_file = python.render(*proto_def, runtime_import=import_path)
elif language == "cpptiny":
generated_file = cpptiny.render(*proto_def)
else:
print(f"Unknown language: {language}")
sys.exit(1)

with open(output, "w", encoding="utf-8") as f:
with open(output_file, "w", encoding="utf-8") as f:
f.write(generated_file)


@cli.command()
@click.option("--language", "-l", required=True)
@click.option("--output", "-o", required=True)
def runtime(language: str, output: str) -> None:
runtime_func = None

@click.option("--language", "-l", required=True, help="Target language (python, cpptiny)")
@click.option(
"--output",
"-o",
"output_path",
default=None,
help="Output path (file for cpptiny, directory for python)",
)
@click.option("--name", default="bakelite_runtime", help="Runtime folder name (python only)")
def runtime(language: str, output_path: str | None, name: str) -> None:
"""Generate runtime support code."""
if language == "cpptiny":
runtime_func = cpptiny.runtime
output_path = output_path or "bakelite.h"
generated_file = cpptiny.runtime()
with open(output_path, "w", encoding="utf-8") as f:
f.write(generated_file)
Comment thread
emmapowers marked this conversation as resolved.
elif language == "python":
output_path = output_path or "."
runtime_dir = Path(output_path) / name
runtime_dir.mkdir(parents=True, exist_ok=True)
for filename, content in python.runtime().items():
(runtime_dir / filename).write_text(content, encoding="utf-8")
print(f"Generated Python runtime in {runtime_dir}")
else:
print(f"Unkown language: {language}")
print(f"Unknown language: {language}")
sys.exit(1)

generated_file = runtime_func()

with open(output, "w", encoding="utf-8") as f:
f.write(generated_file)


def main() -> None:
"""Main entry point."""
cli()


Expand Down
Loading
Loading