from __future__ import annotations import os import platform import subprocess from pathlib import Path from typing import Any class LocalSampleAppService: def __init__(self, sample_app_root: str) -> None: self.root = Path(sample_app_root).resolve() def deploy_order_service(self, version: str) -> dict[str, Any]: self._ensure_root() self._run_script("build") self._run_script("stop", check=False) self._run_script("start", extra_args=[f"-Version {version}"] if self._is_windows() else [version]) return self.status() def status(self) -> dict[str, Any]: self._ensure_root() result = self._run_script("status", check=False) return { "status_text": (result.stdout + "\n" + result.stderr).strip(), "return_code": result.returncode, "running": result.returncode == 0, } def _run_script(self, action: str, extra_args: list[str] | None = None, check: bool = True) -> subprocess.CompletedProcess[str]: extra_args = extra_args or [] if self._is_windows(): script_path = self.root / "scripts" / f"{action}.ps1" command = ["powershell", "-ExecutionPolicy", "Bypass", "-File", str(script_path), *extra_args] else: script_path = self.root / "scripts" / f"{action}.sh" command = ["bash", str(script_path), *extra_args] return subprocess.run(command, cwd=str(self.root), capture_output=True, text=True, check=check) def _ensure_root(self) -> None: if not self.root.exists(): raise FileNotFoundError(f"sample app root not found: {self.root}") def _is_windows(self) -> bool: return platform.system().upper().startswith("WIN")