Add known working config profile for reverting changes
This commit is contained in:
@@ -6,10 +6,10 @@ from typing import Any
|
||||
|
||||
from .bootstrap import ensure_healthy_bootstrap
|
||||
from .config import repo_root
|
||||
from .installer import apply_plan
|
||||
from .installer import apply_plan, disable_plugin
|
||||
from .models import Lockfile, Registry, load_lockfile, load_registry
|
||||
from .planner import create_plan
|
||||
from .state import load_installed_state
|
||||
from .state import load_installed_state, load_known_good_state, save_known_good_state
|
||||
|
||||
|
||||
def _resolve_paths(
|
||||
@@ -161,3 +161,72 @@ def enable_disabled_plugins(
|
||||
enabled.append({"plugin": plugin_id, "planPath": str(batch_path), "applied": applied_by_plugin.get(plugin_id, 0)})
|
||||
|
||||
return {"enabled": enabled, "errors": errors}
|
||||
|
||||
|
||||
def save_known_good_set(*, instance: str, state_root: Path) -> dict[str, Any]:
|
||||
"""Record the currently enabled plugin ids as the known-good set for this instance."""
|
||||
installed_state = load_installed_state(state_root, instance)
|
||||
plugin_ids = sorted(installed_state.get("plugins", {}))
|
||||
known_good = {
|
||||
"instance": instance,
|
||||
"beatSaberVersion": installed_state.get("beatSaberVersion"),
|
||||
"pluginIds": plugin_ids,
|
||||
}
|
||||
save_known_good_state(state_root, instance, known_good)
|
||||
return known_good
|
||||
|
||||
|
||||
def restore_known_good_set(
|
||||
*,
|
||||
instance: str,
|
||||
instance_path: Path,
|
||||
state_root: Path,
|
||||
registry: str = "registry/plugins",
|
||||
lockfile: str | None = None,
|
||||
repo: Path | None = None,
|
||||
progress: Callable[[str], None] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Disable plugins outside the saved known-good set and re-enable the ones that are missing."""
|
||||
known_good = load_known_good_state(state_root, instance)
|
||||
known_good_ids = set(known_good.get("pluginIds", []))
|
||||
if not known_good:
|
||||
raise KeyError(f"no known-good set is saved for this instance: {instance}")
|
||||
|
||||
installed_state = load_installed_state(state_root, instance)
|
||||
currently_enabled = set(installed_state.get("plugins", {}))
|
||||
|
||||
to_disable = sorted(currently_enabled - known_good_ids)
|
||||
disabled: list[str] = []
|
||||
disable_errors: list[dict[str, str]] = []
|
||||
for plugin_id in to_disable:
|
||||
try:
|
||||
result = disable_plugin(instance, instance_path, state_root, plugin_id, False)
|
||||
if result["stateUpdated"]:
|
||||
disabled.append(plugin_id)
|
||||
else:
|
||||
skipped = "; ".join(f"{item['path']} {item['reason']}" for item in result["skipped"])
|
||||
disable_errors.append({"plugin": plugin_id, "error": skipped or "no files changed"})
|
||||
except Exception as exc:
|
||||
disable_errors.append({"plugin": plugin_id, "error": str(exc)})
|
||||
|
||||
installed_state = load_installed_state(state_root, instance)
|
||||
currently_enabled = set(installed_state.get("plugins", {}))
|
||||
to_enable = sorted(known_good_ids - currently_enabled)
|
||||
enable_result = enable_disabled_plugins(
|
||||
instance=instance,
|
||||
instance_path=instance_path,
|
||||
state_root=state_root,
|
||||
plugin_ids=to_enable,
|
||||
registry=registry,
|
||||
lockfile=lockfile,
|
||||
repo=repo,
|
||||
progress=progress,
|
||||
)
|
||||
|
||||
return {
|
||||
"knownGood": known_good,
|
||||
"disabled": disabled,
|
||||
"disableErrors": disable_errors,
|
||||
"enabled": [item["plugin"] for item in enable_result["enabled"]],
|
||||
"enableErrors": enable_result["errors"],
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user