import subprocess
import sys
from codes.common import clibs
from pathlib import Path

UIC_CMD = "pyside6-uic"


def single_uic(ui_path: str, py_path: str):
    for file in Path(ui_path).rglob("*.ui"):
        file_name = file.stem
        ui_file = file
        py_file = Path(py_path) / f"{file_name}.py"
        cmd = [UIC_CMD, "-o", py_file, ui_file]

        print(f"Processing  {ui_file} -> {py_file}")
        try:
            subprocess.run(cmd, check=True, capture_output=True, text=True)
        except subprocess.CalledProcessError as e:
            print(f"转换失败: {ui_file}\n{e.stderr}", file=sys.stderr)

def main():
    ui_path = clibs.base_path / "assets" / "ui"
    py_path = clibs.base_path / "codes" / "ui"
    single_uic(str(ui_path), str(py_path))

if __name__ == "__main__":
    main()
