from __future__ import annotations import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] GUI_PACKAGE = ROOT / "src" / "bedit_gui" FORMS_DIR = GUI_PACKAGE / "ui" / "forms" GENERATED_UI_DIR = GUI_PACKAGE / "ui" / "generated" QRC_FILE = GUI_PACKAGE / "resources" / "resources.qrc" GENERATED_RESOURCES = ( GUI_PACKAGE / "resources" / "generated" / "resources_rc.py" ) RESOURCE_IMPORT = "import bedit_gui.resources.resources_rc" GENERATED_RESOURCE_IMPORT = ( "from bedit_gui.resources.generated import resources_rc" ) def execute(*command: str) -> None: print("+", " ".join(command)) subprocess.run(command, check=True) def generate_ui() -> None: GENERATED_UI_DIR.mkdir(parents=True, exist_ok=True) for source in sorted(FORMS_DIR.glob("*.ui")): destination = GENERATED_UI_DIR / f"ui_{source.stem}.py" execute( "pyside6-uic", "--absolute-imports", "--python-paths", str(ROOT / "src"), str(source), "-o", str(destination), ) generated = destination.read_text(encoding="utf-8") if RESOURCE_IMPORT not in generated: raise RuntimeError( f"could not find the generated resource import in {destination}" ) destination.write_text( generated.replace( RESOURCE_IMPORT, GENERATED_RESOURCE_IMPORT, 1, ), encoding="utf-8", ) def generate_resources() -> None: GENERATED_RESOURCES.parent.mkdir( parents=True, exist_ok=True, ) execute( "pyside6-rcc", str(QRC_FILE), "-o", str(GENERATED_RESOURCES), ) def main() -> None: generate_ui() generate_resources() if __name__ == "__main__": main()