111 lines
3.3 KiB
Python
111 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the shared drosync status contract from OpenAPI 3.1."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import pathlib
|
|
import re
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
OPENAPI = ROOT / "docs" / "drosync" / "openapi.yaml"
|
|
GO_OUTPUT = ROOT / "internal" / "domain" / "object_sync_contract_generated.go"
|
|
RUST_OUTPUT = ROOT / "tools" / "drosync" / "crates" / "protocol" / "src" / "generated.rs"
|
|
|
|
|
|
def pascal_case(value: str) -> str:
|
|
return "".join(part.capitalize() for part in re.split(r"[_-]+", value))
|
|
|
|
|
|
def load_contract() -> tuple[list[str], list[str], str]:
|
|
contract = yaml.safe_load(OPENAPI.read_text(encoding="utf-8"))
|
|
schemas = contract["components"]["schemas"]
|
|
session = list(schemas["SessionStatus"]["enum"])
|
|
item = list(schemas["ItemStatus"]["enum"])
|
|
protocol = schemas["SessionCreate"]["properties"]["protocolVersion"]["const"]
|
|
return session, item, protocol
|
|
|
|
|
|
def generate_go(session: list[str], item: list[str], protocol: str) -> str:
|
|
session_constants = "\n".join(
|
|
f'\tObjectSyncSession{pascal_case(value)} ObjectSyncSessionStatus = "{value}"' for value in session
|
|
)
|
|
item_constants = "\n".join(
|
|
f'\tObjectSyncItem{pascal_case(value)} ObjectSyncItemStatus = "{value}"' for value in item
|
|
)
|
|
return f"""// Code generated by scripts/generate_drosync_contract.py; DO NOT EDIT.
|
|
|
|
package domain
|
|
|
|
const ObjectSyncProtocolVersion = "{protocol}"
|
|
|
|
type ObjectSyncSessionStatus string
|
|
|
|
const (
|
|
{session_constants}
|
|
)
|
|
|
|
type ObjectSyncItemStatus string
|
|
|
|
const (
|
|
{item_constants}
|
|
)
|
|
"""
|
|
|
|
|
|
def generate_rust(session: list[str], item: list[str], protocol: str) -> str:
|
|
session_variants = "\n".join(f" {pascal_case(value)}," for value in session)
|
|
item_variants = "\n".join(f" {pascal_case(value)}," for value in item)
|
|
return f"""// Code generated by scripts/generate_drosync_contract.py; DO NOT EDIT.
|
|
|
|
use serde::{{Deserialize, Serialize}};
|
|
|
|
pub const PROTOCOL_VERSION: &str = "{protocol}";
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum SessionStatus {{
|
|
{session_variants}
|
|
}}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum ItemStatus {{
|
|
{item_variants}
|
|
}}
|
|
"""
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--check", action="store_true", help="fail when generated files differ")
|
|
args = parser.parse_args()
|
|
session, item, protocol = load_contract()
|
|
go_output = subprocess.run(
|
|
["gofmt"],
|
|
input=generate_go(session, item, protocol),
|
|
text=True,
|
|
capture_output=True,
|
|
check=True,
|
|
).stdout
|
|
rust_output = generate_rust(session, item, protocol)
|
|
if args.check:
|
|
stale = [
|
|
str(path.relative_to(ROOT))
|
|
for path, expected in ((GO_OUTPUT, go_output), (RUST_OUTPUT, rust_output))
|
|
if not path.exists() or path.read_text(encoding="utf-8") != expected
|
|
]
|
|
if stale:
|
|
raise SystemExit("generated drosync contract is stale: " + ", ".join(stale))
|
|
return
|
|
GO_OUTPUT.write_text(go_output, encoding="utf-8")
|
|
RUST_OUTPUT.write_text(rust_output, encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|