Skip to content

gaoguobin/codex-eide-rebuild / skills/eide-rebuild

eide-rebuild

Verified

EIDE rebuild and compile Agent Skill for Embedded IDE for VS Code workspaces. Runs a Python unify_builder runner and returns compact or full JSON build results.

scripts/eide_rebuild/tools.py

View on GitHub
from __future__ import annotations

import json
import os
import re
import shutil
import subprocess
import urllib.request
from pathlib import Path

from .eide_model import require_yaml_module
from .platform import current_platform, normalize_path


UNIFY_BUILDER_CHECK_TIMEOUT_SECONDS = 30
DOTNET_RUNTIMES_TIMEOUT_SECONDS = 10
EIDE_MCP_DEFAULT_PORT = 8940
EIDE_MCP_HEALTH_TIMEOUT_SECONDS = 0.5


class ToolchainMismatchError(FileNotFoundError):
    def __init__(self, message: str) -> None:
        super().__init__(message)
        self.error_code = "GCC_VERSION_MISMATCH"
        self.exit_code = 3


def _resolve_existing_path(path_value: str, expect_dir: bool = False) -> str:
    candidate = Path(path_value).expanduser()
    if expect_dir:
        if candidate.is_dir():
            return normalize_path(candidate.resolve())
    elif candidate.exists():
        return normalize_path(candidate.resolve())
    raise FileNotFoundError(str(candidate))


def _timeout_payload(value: object) -> str:
    if value is None:
        return ""
    if isinstance(value, bytes):
        return value.decode(errors="replace")
    return str(value)


def _error_type(error: BaseException) -> str:
    return type(error).__name__


def _tool_check_ok(path_value: str) -> dict[str, object]:
    return {
        "ok": True,
        "path": path_value,
        "message": "",
        "errorType": "",
    }


def _tool_check_error(error: BaseException) -> dict[str, object]:
    return {
        "ok": False,
        "path": "",
        "message": str(error),
        "errorType": _error_type(error),
    }


def _path_if_dir(path_value: Path) -> str | None:
    return normalize_path(path_value.resolve()) if path_value.is_dir() else None


def _resolve_non_strict_path(path_value: Path) -> Path:
    try:
        return path_value.expanduser().resolve()
    except OSError:
        return path_value.expanduser()


def _version_key(path_value: Path) -> tuple[int, ...]:
    match = re.search(r"(\d+(?:\.\d+)+)", path_value.name)
    if not match:
        return (0,)
    return tuple(int(part) for part in match.group(1).split("."))


def _iter_existing_dirs(paths: list[Path]) -> list[Path]:
    result: list[Path] = []
    seen: set[str] = set()
    for path in paths:
        try:
            resolved = path.expanduser().resolve()
        except OSError:
            continue
        normalized = normalize_path(resolved)
        if normalized in seen or not resolved.is_dir():
            continue
        seen.add(normalized)
        result.append(resolved)
    return result


def _preferred_candidate(candidates: list[Path]) -> Path:
    return sorted(
        candidates,
        key=lambda path: (_version_key(path), path.stat().st_mtime),
        reverse=True,
    )[0]


def _extension_roots() -> list[Path]:
    override = os.environ.get("EIDE_REBUILD_VSCODE_EXTENSIONS_ROOT")
    if override:
        return _iter_existing_dirs([Path(override)])
    home_override = os.environ.get("EIDE_REBUILD_HOME")
    if home_override:
        return _iter_existing_dirs([Path(home_override) / ".vscode" / "extensions"])
    return _iter_existing_dirs([Path.home() / ".vscode" / "extensions"])


def find_eide_extension_dir() -> str:
    override = os.environ.get("EIDE_REBUILD_EIDE_EXTENSION_DIR")
    if override:
        return _resolve_existing_path(override, expect_dir=True)

    candidates: list[Path] = []
    for root in _extension_roots():
        candidates.extend(path for path in root.glob("cl.eide-*") if path.is_dir())
    if not candidates:
        raise FileNotFoundError("EIDE extension directory")

    best = _preferred_candidate(candidates)
    return normalize_path(best.resolve())


def _candidate_model_dirs(base_dir: Path) -> list[Path]:
    return [
        base_dir,
        base_dir / "models",
        base_dir / "res" / "data" / "models",
        base_dir / "data" / "models",
    ]


def _resolve_model_dir(base_dir: Path) -> str | None:
    for candidate in _candidate_model_dirs(base_dir):
        if (candidate / "arm.gcc.model.json").exists():
            return normalize_path(candidate.resolve())
    return None


def find_dotnet() -> str:
    override = os.environ.get("EIDE_REBUILD_DOTNET") or os.environ.get("DOTNET_HOST_PATH")
    if override:
        return _resolve_existing_path(override)
    command_path = shutil.which("dotnet")
    if command_path:
        return normalize_path(Path(command_path).resolve())
    raise FileNotFoundError("dotnet")


def find_eide_tools_dir() -> str:
    override = os.environ.get("EIDE_REBUILD_EIDE_TOOLS_DIR") or os.environ.get("EIDE_TOOLS_DIR")
    if override:
        resolved = _resolve_model_dir(Path(override).expanduser())
        if resolved:
            return resolved
        raise FileNotFoundError(str(Path(override).expanduser()))

    extension_dir = Path(find_eide_extension_dir())
    resolved = _resolve_model_dir(extension_dir)
    if resolved:
        return resolved
    raise FileNotFoundError("EIDE tools directory")


def _platform_tool_dir(extension_dir: Path) -> Path:
    platform_name = current_platform()
    platform_folder = "win32" if platform_name == "windows" else platform_name
    return extension_dir / "res" / "tools" / platform_folder


def find_unify_builder() -> str:
    override = os.environ.get("EIDE_REBUILD_UNIFY_BUILDER")
    if override:
        return _resolve_existing_path(override)

    tools_override = os.environ.get("EIDE_REBUILD_EIDE_TOOLS_DIR") or os.environ.get("EIDE_TOOLS_DIR")
    if tools_override:
        direct_candidate = Path(tools_override).expanduser() / "unify_builder.dll"
        if direct_candidate.exists():
            return normalize_path(direct_candidate.resolve())

    unify_root = _platform_tool_dir(Path(find_eide_extension_dir())) / "unify_builder"
    candidate_names = ["unify_builder.exe", "unify_builder.dll"] if current_platform() == "windows" else ["unify_builder.dll"]
    for candidate_name in candidate_names:
        candidate = unify_root / candidate_name
        if candidate.exists():
            return normalize_path(candidate.resolve())
    raise FileNotFoundError("unify_builder.dll")


def resolve_unify_builder_dll(unify_builder_path: str) -> str:
    path_obj = Path(unify_builder_path)
    if path_obj.suffix.lower() == ".dll":
        return normalize_path(path_obj.resolve())

    sibling_dll = path_obj.with_suffix(".dll")
    if sibling_dll.exists():
        return normalize_path(sibling_dll.resolve())

    raise FileNotFoundError(str(sibling_dll))


def find_eide_utils_dir() -> str:
    override = os.environ.get("EIDE_REBUILD_EIDE_UTILS_DIR")
    if override:
        return _resolve_existing_path(override, expect_dir=True)

    utils_dir = _platform_tool_dir(Path(find_eide_extension_dir())) / "utils"
    resolved = _path_if_dir(utils_dir)
    if resolved:
        return resolved
    raise FileNotFoundError("EIDE utils directory")


def _toolchain_search_roots() -> list[Path]:
    override = os.environ.get("EIDE_REBUILD_TOOLS_ROOT")
    if override:
        return _iter_existing_dirs([Path(override)])

    roots: list[Path] = []
    home_override = os.environ.get("EIDE_REBUILD_HOME")
    if home_override:
        return _iter_existing_dirs([Path(home_override) / ".eide" / "tools"])
    roots.append(Path.home() / ".eide" / "tools")
    return _iter_existing_dirs(roots)


def _workspace_settings(workspace_path: str) -> dict[str, object]:
    if not workspace_path:
        return {}
    path_obj = Path(workspace_path).expanduser()
    if not path_obj.is_file():
        return {}
    try:
        payload = json.loads(path_obj.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError):
        return {}
    settings = payload.get("settings") or {}
    return settings if isinstance(settings, dict) else {}


def _expand_workspace_path(path_value: str) -> str:
    expanded = str(path_value or "").strip()
    if not expanded:
        return ""
    home_tokens = ("${userHome}", "${userRoot}")
    if any(token in expanded for token in home_tokens):
        home_dir = str(Path.home())
        for token in home_tokens:
            expanded = expanded.replace(token, home_dir)
    expanded = os.path.expandvars(expanded)
    return normalize_path(_resolve_non_strict_path(Path(expanded)))


def _extract_gcc_version(path_value: str | Path) -> str:
    match = re.search(r"gcc[-_]?(\d+\.\d+\.\d+)", str(path_value), re.IGNORECASE)
    if match:
        return match.group(1)
    return ""


def _workspace_gcc_requirement(workspace_path: str) -> dict[str, str]:
    settings = _workspace_settings(workspace_path)
    install_dir = ""
    setting_key = ""
    for key in sorted(settings):
        if not re.fullmatch(r"EIDE\..+\.GCC\.InstallDirectory", str(key), re.IGNORECASE):
            continue
        value = settings.get(key)
        if isinstance(value, str) and value.strip():
            setting_key = str(key)
            install_dir = _expand_workspace_path(value)
            break
    version = _extract_gcc_version(install_dir)
    return {
        "settingKey": setting_key,
        "installDir": install_dir,
        "version": version,
    }


def _is_gcc_toolchain_root(path_value: str | Path) -> bool:
    root = Path(path_value)
    return any(
        (root / "bin" / executable_name).is_file()
        for executable_name in ("arm-none-eabi-gcc.exe", "arm-none-eabi-gcc")
    )


def _toolchain_candidates() -> list[Path]:
    candidates: list[Path] = []
    seen: set[str] = set()
    patterns = ("**/bin/arm-none-eabi-gcc.exe", "**/bin/arm-none-eabi-gcc")
    for root in _toolchain_search_roots():
        for pattern in patterns:
            for gcc_path in root.glob(pattern):
                candidate = _resolve_non_strict_path(gcc_path.parent.parent)
                normalized = normalize_path(candidate)
                if normalized in seen or not candidate.is_dir():
                    continue
                seen.add(normalized)
                candidates.append(candidate)
    return candidates


def _format_toolchain_candidates(candidates: list[Path]) -> str:
    formatted: list[str] = []
    for candidate in sorted(candidates, key=lambda path: normalize_path(path)):
        version = _extract_gcc_version(candidate.name) or "unknown"
        formatted.append(f"{version} @ {normalize_path(candidate)}")
    return ", ".join(formatted)


def find_toolchain_root(workspace_path: str = "") -> str:
    override = os.environ.get("EIDE_REBUILD_TOOLCHAIN_ROOT") or os.environ.get("COMPILER_DIR")
    if override:
        return _resolve_existing_path(override, expect_dir=True)

    requirement = _workspace_gcc_requirement(workspace_path)
    configured_install_dir = requirement.get("installDir", "")
    configured_version = requirement.get("version", "")
    if configured_install_dir and _is_gcc_toolchain_root(configured_install_dir):
        return normalize_path(Path(configured_install_dir).resolve())

    candidates = _toolchain_candidates()
    if not candidates:
        if configured_version:
            raise ToolchainMismatchError(f"GCC {configured_version} from workspace was requested; discovered []")
        raise FileNotFoundError("toolchain root")

    if configured_version:
        matches = [candidate for candidate in candidates if _extract_gcc_version(candidate.name) == configured_version]
        if matches:
            return normalize_path(_preferred_candidate(matches))
        discovered = _format_toolchain_candidates(candidates)
        raise ToolchainMismatchError(
            f"GCC {configured_version} from workspace was requested; discovered [{discovered}]"
        )

    return normalize_path(_preferred_candidate(candidates))


def build_process_env(extra_env: dict[str, str] | None, toolchain_root: str) -> dict[str, str]:
    env = os.environ.copy()
    if extra_env:
        env.update({str(key): str(value) for key, value in extra_env.items()})

    path_parts: list[str] = []
    try:
        path_parts.append(str(Path(find_eide_utils_dir()).resolve()))
    except FileNotFoundError:
        pass

    toolchain_bin = Path(toolchain_root) / "bin"
    if toolchain_bin.is_dir():
        path_parts.append(str(toolchain_bin.resolve()))

    existing_path = env.get("PATH", "")
    if existing_path:
        path_parts.append(existing_path)
    env["PATH"] = os.pathsep.join(path_parts)
    return env


def check_unify_builder_runtime(dotnet_path: str, unify_builder_path: str) -> dict[str, object]:
    unify_builder_dll = resolve_unify_builder_dll(unify_builder_path)
    launch_command = [dotnet_path, "exec", "--roll-forward", "Major", unify_builder_dll, "-v"]
    runtime_config = Path(unify_builder_dll).with_suffix(".runtimeconfig.json")
    framework_name = ""
    framework_version = ""
    if runtime_config.exists():
        try:
            payload = json.loads(runtime_config.read_text(encoding="utf-8"))
        except (OSError, json.JSONDecodeError) as error:
            return {
                "ok": False,
                "requiredFramework": "",
                "requiredVersion": "",
                "installedVersions": [],
                "message": f"Failed to read runtimeconfig {normalize_path(runtime_config)}: {error}",
                "errorType": _error_type(error),
                "stdout": "",
                "stderr": "",
                "launchCommand": launch_command,
            }
        runtime_options = payload.get("runtimeOptions") or {}
        framework = runtime_options.get("framework") or {}
        framework_name = str(framework.get("name") or "")
        framework_version = str(framework.get("version") or "")

    try:
        completed = subprocess.run(
            launch_command,
            capture_output=True,
            text=True,
            check=False,
            timeout=UNIFY_BUILDER_CHECK_TIMEOUT_SECONDS,
        )
    except subprocess.TimeoutExpired as error:
        return {
            "ok": False,
            "requiredFramework": framework_name,
            "requiredVersion": framework_version,
            "installedVersions": [],
            "message": f"unify_builder runtime check timed out after {UNIFY_BUILDER_CHECK_TIMEOUT_SECONDS} seconds.",
            "stdout": _timeout_payload(error.output),
            "stderr": _timeout_payload(error.stderr),
            "launchCommand": launch_command,
        }
    ok = completed.returncode == 0
    message = ""
    if not ok:
        message = completed.stderr.strip() or completed.stdout.strip() or "Failed to start unify_builder."

    installed_versions: list[str] = []
    try:
        runtimes = subprocess.run(
            [dotnet_path, "--list-runtimes"],
            capture_output=True,
            text=True,
            check=False,
            timeout=DOTNET_RUNTIMES_TIMEOUT_SECONDS,
        )
    except subprocess.TimeoutExpired:
        runtimes = subprocess.CompletedProcess(args=[dotnet_path, "--list-runtimes"], returncode=124, stdout="", stderr="")
    if runtimes.returncode == 0 and framework_name:
        for raw_line in runtimes.stdout.splitlines():
            match = re.match(r"^(?P<name>\S+)\s+(?P<version>\d+\.\d+\.\d+)", raw_line.strip())
            if not match or match.group("name") != framework_name:
                continue
            installed_versions.append(match.group("version"))

    return {
        "ok": ok,
        "requiredFramework": framework_name,
        "requiredVersion": framework_version,
        "installedVersions": installed_versions,
        "message": message,
        "stdout": completed.stdout or "",
        "stderr": completed.stderr or "",
        "launchCommand": launch_command,
    }


def check_pyyaml_dependency() -> dict[str, object]:
    try:
        yaml_module = require_yaml_module()
    except RuntimeError as error:
        return {
            "ok": False,
            "package": "PyYAML",
            "module": "yaml",
            "version": "",
            "message": str(error),
        }

    return {
        "ok": True,
        "package": "PyYAML",
        "module": "yaml",
        "version": str(getattr(yaml_module, "__version__", "") or ""),
        "message": "",
    }


def check_eide_mcp_health(port: int | None = None) -> dict[str, object]:
    raw_port = os.environ.get("EIDE_REBUILD_MCP_PORT") or port or EIDE_MCP_DEFAULT_PORT
    try:
        port_value = int(raw_port)
    except (TypeError, ValueError) as error:
        return {
            "ok": False,
            "url": "",
            "port": raw_port,
            "message": f"Invalid EIDE MCP port: {raw_port}",
            "errorType": _error_type(error),
        }
    url = f"http://127.0.0.1:{port_value}/health"
    try:
        with urllib.request.urlopen(url, timeout=EIDE_MCP_HEALTH_TIMEOUT_SECONDS) as response:
            payload = json.loads(response.read().decode("utf-8"))
    except Exception as error:
        return {
            "ok": False,
            "url": url,
            "port": port_value,
            "message": str(error),
            "errorType": _error_type(error),
        }

    if not isinstance(payload, dict):
        return {
            "ok": False,
            "url": url,
            "port": port_value,
            "message": "Unexpected EIDE MCP health response.",
            "errorType": "UnexpectedResponse",
        }

    return {
        "ok": bool(payload.get("ok")),
        "url": url,
        "port": port_value,
        "message": "" if payload.get("ok") else "EIDE MCP health endpoint returned not ok.",
        "errorType": "",
        "response": payload,
    }


def run_doctor() -> dict[str, object]:
    tools: dict[str, str] = {}
    tool_checks: dict[str, dict[str, object]] = {}
    errors: list[str] = []
    runtime_info: dict[str, object] = {"ok": True}
    dependencies = {
        "pyyaml": check_pyyaml_dependency(),
    }
    capabilities = {
        "eideMcp": check_eide_mcp_health(),
    }

    checks = {
        "dotnet": find_dotnet,
        "eideExtensionDir": find_eide_extension_dir,
        "eideToolsDir": find_eide_tools_dir,
        "eideUtilsDir": find_eide_utils_dir,
        "unifyBuilder": find_unify_builder,
        "toolchainRoot": find_toolchain_root,
    }

    for name, getter in checks.items():
        try:
            path_value = getter()
            tools[name] = path_value
            tool_checks[name] = _tool_check_ok(path_value)
        except Exception as error:
            tool_checks[name] = _tool_check_error(error)
            errors.append(f"{name}: {error}")

    if "dotnet" in tools and "unifyBuilder" in tools:
        try:
            runtime_info = check_unify_builder_runtime(tools["dotnet"], tools["unifyBuilder"])
        except Exception as error:
            runtime_info = {
                "ok": False,
                "message": str(error),
                "errorType": _error_type(error),
            }
        if not runtime_info.get("ok", False):
            errors.append(str(runtime_info.get("message") or "Unify builder runtime check failed."))

    for dependency_info in dependencies.values():
        if not dependency_info.get("ok", False):
            errors.append(str(dependency_info.get("message") or "Python dependency check failed."))

    ok = not errors
    return {
        "ok": ok,
        "exitCode": 0 if ok else 3,
        "errorCode": "OK" if ok else "TOOL_NOT_FOUND",
        "message": "" if ok else "; ".join(errors),
        "platform": current_platform(),
        "tools": tools,
        "toolChecks": tool_checks,
        "dependencies": dependencies,
        "capabilities": capabilities,
        "runtime": runtime_info,
    }
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall