Enhance script for handling updates
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tomllib
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import dataclass, field, replace
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
@@ -53,6 +53,47 @@ class Lockfile:
|
||||
plugins: tuple[LockedPlugin, ...]
|
||||
|
||||
|
||||
def _quote_toml_string(value: str) -> str:
|
||||
return '"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"'
|
||||
|
||||
|
||||
def lockfile_to_toml(lockfile: Lockfile) -> str:
|
||||
lines = [
|
||||
f"beat_saber_version = {_quote_toml_string(lockfile.beat_saber_version)}",
|
||||
f"instance = {_quote_toml_string(lockfile.instance)}",
|
||||
"",
|
||||
]
|
||||
for plugin in lockfile.plugins:
|
||||
lines.append("[[plugins]]")
|
||||
lines.append(f"id = {_quote_toml_string(plugin.id)}")
|
||||
if plugin.repo is not None:
|
||||
lines.append(f"repo = {_quote_toml_string(plugin.repo)}")
|
||||
if plugin.tag is not None:
|
||||
lines.append(f"tag = {_quote_toml_string(plugin.tag)}")
|
||||
if plugin.asset is not None:
|
||||
lines.append(f"asset = {_quote_toml_string(plugin.asset)}")
|
||||
if plugin.sha256 is not None:
|
||||
lines.append(f"sha256 = {_quote_toml_string(plugin.sha256)}")
|
||||
if plugin.install_strategy is not None:
|
||||
lines.append(f"install_strategy = {_quote_toml_string(plugin.install_strategy)}")
|
||||
if plugin.reason is not None:
|
||||
lines.append(f"reason = {_quote_toml_string(plugin.reason)}")
|
||||
lines.append("")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def write_lockfile(path: Path, lockfile: Lockfile) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(lockfile_to_toml(lockfile), encoding="utf-8")
|
||||
|
||||
|
||||
def replace_locked_plugins(lockfile: Lockfile, replacements: dict[str, LockedPlugin]) -> Lockfile:
|
||||
return replace(
|
||||
lockfile,
|
||||
plugins=tuple(replacements.get(plugin.id, plugin) for plugin in lockfile.plugins),
|
||||
)
|
||||
|
||||
|
||||
def _load_toml(path: Path) -> dict[str, Any]:
|
||||
with path.open("rb") as handle:
|
||||
return tomllib.load(handle)
|
||||
|
||||
Reference in New Issue
Block a user