mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
72 lines
3.0 KiB
Python
72 lines
3.0 KiB
Python
from importlib import resources
|
||
from pathlib import Path
|
||
|
||
import click
|
||
|
||
from .version_comparator import VersionComparator
|
||
|
||
|
||
class DashboardManager:
|
||
_bundled_dist = resources.files("astrbot") / "dashboard" / "dist"
|
||
|
||
async def ensure_installed(self, astrbot_root: Path) -> None:
|
||
"""Ensure the dashboard assets are installed and up to date."""
|
||
from astrbot.core.config.default import VERSION
|
||
from astrbot.core.utils.io import download_dashboard, get_dashboard_version
|
||
|
||
if self._bundled_dist.is_dir():
|
||
click.echo("Dashboard is bundled with the package – skipping download.")
|
||
return
|
||
|
||
try:
|
||
dashboard_version = await get_dashboard_version()
|
||
match dashboard_version:
|
||
case None:
|
||
click.echo("Dashboard is not installed")
|
||
if click.confirm(
|
||
"Install dashboard?",
|
||
default=True,
|
||
abort=True,
|
||
):
|
||
click.echo("Installing dashboard...")
|
||
try:
|
||
await download_dashboard(
|
||
path="data/dashboard.zip",
|
||
extract_path=str(astrbot_root / "data"),
|
||
version=f"v{VERSION}",
|
||
latest=False,
|
||
)
|
||
click.echo("Dashboard installed successfully")
|
||
except Exception as e:
|
||
click.echo(f"Failed to install dashboard: {e}")
|
||
|
||
case str():
|
||
if VersionComparator.compare_version(VERSION, dashboard_version) <= 0:
|
||
click.echo("Dashboard is already up to date")
|
||
return
|
||
try:
|
||
version = dashboard_version.split("v")[1]
|
||
click.echo(f"Dashboard version: {version}")
|
||
await download_dashboard(
|
||
path="data/dashboard.zip",
|
||
extract_path=str(astrbot_root / "data"),
|
||
version=f"v{VERSION}",
|
||
latest=False,
|
||
)
|
||
except Exception as e:
|
||
click.echo(f"Failed to download dashboard: {e}")
|
||
return
|
||
except FileNotFoundError:
|
||
click.echo("Initializing dashboard directory...")
|
||
try:
|
||
await download_dashboard(
|
||
path=str(astrbot_root / "data" / "dashboard.zip"),
|
||
extract_path=str(astrbot_root / "data"),
|
||
version=f"v{VERSION}",
|
||
latest=False,
|
||
)
|
||
click.echo("Dashboard initialized successfully")
|
||
except Exception as e:
|
||
click.echo(f"Failed to download dashboard: {e}")
|
||
return
|