diff --git a/launcher/config/conf.py b/launcher/config/conf.py index a5df264..cfeca5c 100644 --- a/launcher/config/conf.py +++ b/launcher/config/conf.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Literal, get_args, get_origin from pydantic import BaseModel, model_validator from pydantic_core import PydanticUndefined @@ -15,6 +15,18 @@ class ConfModel(BaseModel): data[name] = expected_type else: data[name] = field.default + value = data[name] + + # 检查 Literal 类型并修正 + if get_origin(expected_type) is Literal: + valid_literals = get_args(expected_type) + if value not in valid_literals: + # 修正为默认值 + data[name] = ( + field.default + if field.default is not PydanticUndefined + else None + ) return data @@ -55,9 +67,17 @@ class LaunchPart(ConfModel): is_show_log: bool = False +class OtherPart(ConfModel): + """其他配置""" + + # xx.zhaozuohong.vip镜像 (访问xx.zhaozuohong.vip url时,0=原路径 1=在.zhaozuohong.vip前添加-cf前缀) + base_mirror: Literal["0", "1"] = "0" + + class Conf( Total, UpdatePart, LaunchPart, + OtherPart, ): pass diff --git a/launcher/constants.py b/launcher/constants.py index 3e519e2..10766e8 100644 --- a/launcher/constants.py +++ b/launcher/constants.py @@ -8,18 +8,21 @@ constants.py update_tmp_folder = "download_tmp" # 更新脚本名 upgrade_script_name = "upgrade.bat" +# 下载新版本压缩包名 +file_name = "launcher.7z" # 获取最新版本发布信息 get_new_version_url = ( "https://git.zhaozuohong.vip/api/v1/repos/mower-ng/launcher/releases/latest" ) -# 下载新版本压缩包名 -file_name = "launcher.7z" # 下载地址 download_git_url = "https://list.zhaozuohong.vip/mower-ng/git.7z" download_python_url = "https://list.zhaozuohong.vip/mower-ng/python.7z" +# mower-ng git链接 +mower_ng_git_url = "https://git.zhaozuohong.vip/mower-ng/mower-ng.git" + # pip镜像地址 mirror_list = { "pypi": "https://pypi.org/simple", @@ -30,3 +33,15 @@ mirror_list = { # 实例文件夹名 instances_folder_name = "instances" + +# cli命令 +cli_command = { + "status": "获取mower-ng实例状态", + "launch": "启动mower-ng进程", + "exit": "停止mower-ng进程", + "kill": "强制退出mower-ng进程", + "start": "开始运行调度器", + "stop": "停止运行调度器", + "webui": "在浏览器中打开网页面板", + "log": "通过WebSocket获取日志", +} diff --git a/launcher/sys_config/config_dist.json b/launcher/sys_config/config_dist.json index 9c9c5fd..499fad5 100644 --- a/launcher/sys_config/config_dist.json +++ b/launcher/sys_config/config_dist.json @@ -1,5 +1,5 @@ { - "version": "v0.6", + "version": "v0.7.1", "url": "ui/dist/index.html", "log_level": "INFO", "debug": false diff --git a/launcher/utils.py b/launcher/utils.py new file mode 100644 index 0000000..bb56ecc --- /dev/null +++ b/launcher/utils.py @@ -0,0 +1,12 @@ +from launcher import config + + +def build_base_url(url: str) -> str: + """ + 构建xx.zhaozuohong.vip,如果配置base_mirror是1,在.zhaozuohong.vip前添加-cf前缀 + :param url: 带有xx.zhaozuohong.vip的url字符串 + :return: 构建完成的url + """ + if config.conf.base_mirror == "1": + url = url.replace(".zhaozuohong.vip", "-cf.zhaozuohong.vip") + return url diff --git a/launcher/webview/api.py b/launcher/webview/api.py index 227f82e..8bb62a3 100644 --- a/launcher/webview/api.py +++ b/launcher/webview/api.py @@ -2,7 +2,7 @@ import io import os import shutil import subprocess -import threading +import time from _winapi import CREATE_NO_WINDOW from pathlib import Path from shutil import rmtree @@ -11,7 +11,6 @@ from subprocess import Popen import requests from launcher import config -from launcher.config.conf import LaunchPart from launcher.constants import ( download_git_url, download_python_url, @@ -20,6 +19,8 @@ from launcher.constants import ( mirror_list, file_name, instances_folder_name, + mower_ng_git_url, + cli_command, ) from launcher.file.download import init_download, download_file from launcher.file.extract import extract_7z_file @@ -27,22 +28,29 @@ from launcher.file.utils import ensure_directory_exists, check_command_path from launcher.instances import manager from launcher.log import logger from launcher.sys_config import sys_config +from launcher.utils import build_base_url from launcher.webview.events import custom_event, LogType command_list = { - "download_git": lambda: init_download("git", download_git_url, os.getcwd()), + "download_git": lambda: init_download( + "git", build_base_url(download_git_url), os.getcwd() + ), "download_python": lambda: init_download( - "python", download_python_url, os.getcwd() + "python", build_base_url(download_python_url), os.getcwd() ), "lfs": "git\\bin\\git lfs install", "ensurepip": "python\\python -m ensurepip --default-pip", - "clone": "git\\bin\\git -c lfs.concurrenttransfers=100 clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow", + "clone": lambda: f"git\\bin\\git -c lfs.concurrenttransfers=100 clone {build_base_url(mower_ng_git_url)} --branch slow", + "set_remote": lambda: f"..\\git\\bin\\git remote set-url origin {build_base_url(mower_ng_git_url)}", + "set_lfs": lambda: f"..\\git\\bin\\git config lfs.url {build_base_url(mower_ng_git_url)}/info/lfs", "fetch": lambda: f"..\\git\\bin\\git fetch origin {config.conf.branch} --progress", "switch": lambda: f"..\\git\\bin\\git -c lfs.concurrenttransfers=100 switch -f {config.conf.branch} --progress", "reset": lambda: f"..\\git\\bin\\git -c lfs.concurrenttransfers=200 reset --hard origin/{config.conf.branch}", "pip_tools_install": lambda: f"..\\python\\Scripts\\pip install --no-cache-dir -i {mirror_list[config.conf.mirror]} pip-tools --no-warn-script-location", "pip_sync": lambda: f"..\\python\\Scripts\\pip-sync -i {mirror_list[config.conf.mirror]} requirements.txt", "webview": lambda instance_path="": f'..\\python\\pythonw -X utf8 webview_ui.py "{instance_path}"', + "cli": lambda path, + command: f'..\\python\\pythonw -X utf8 cli.py -p "{path}" {command}', } @@ -86,11 +94,12 @@ class Api: def get_new_version(self): logger.info("获取最新版本号") - response = requests.get(get_new_version_url) + response = requests.get(build_base_url(get_new_version_url)) return response.json() # 更新启动器本身 def update_self(self, download_url): + download_url = build_base_url(download_url) logger.info(f"开始更新启动器 {download_url}") current_path = os.getcwd() download_tmp_folder = os.path.join(current_path, "download_tmp") @@ -221,28 +230,6 @@ class Api: ) == instances_dir and os.path.exists(abs_path): shutil.rmtree(abs_path) - def start_checked_instance(self): - checked_instances = [ - instance for instance in config.conf.instances if instance.checked - ] - if not checked_instances: - custom_event(LogType.warning, "没有选中的实例") - return [{"status": False, "message": "No checked instances"}] - - def _run_instance(instance: LaunchPart.Instance): - self.run( - "webview", - "mower-ng", - {"instance_path": instance.path}, - ) - - # 创建并启动线程 目前进程依然有阻塞,待优化 - for instance in checked_instances: - thread = threading.Thread( - target=_run_instance, args=(instance,), daemon=True - ) - thread.start() - def migrate_default_instance(self): """迁移默认实例文件到新实例""" source_path = os.path.join(os.getcwd(), "mower-ng") @@ -258,3 +245,63 @@ class Api: else: os.startfile(path) custom_event(LogType.info, f"成功打开文件夹:{path}") + + def test_base_url_connect(self): + url = build_base_url(get_new_version_url) + custom_event(LogType.info, f"开始测试URL连接:{url}") + try: + start_time = time.time() + response = requests.get(url) + end_time = time.time() + if response.status_code == 200: + elapsed_time_ms = (end_time - start_time) * 1000 + custom_event( + LogType.info, f"测试成功,响应时间为 {elapsed_time_ms:.2f} 毫秒" + ) + else: + custom_event( + LogType.error, f"测试失败: HTTP状态码 {response.status_code}" + ) + except requests.exceptions.RequestException as e: + custom_event(LogType.error, f"发生错误: {e}") + + def cli_control(self, command: str, path: str): + """ + 统一的CLI控制接口,用于执行指定命令并可选地指定工作目录。 + + :param command_str: 要执行的命令字符串或命令键(如 "status", "launch" 等) + :param path: 实例路径 + """ + if command not in cli_command: + custom_event( + LogType.error, + f"无效的命令字符串或命令键:{command},请检查输入。", + ) + return + try: + ret = self.run("cli", "mower-ng", {"command": command, "path": path}) + if command == "launch" and ret == "success": + self.run("cli", "mower-ng", {"command": "webui", "path": path}) + except Exception as e: + custom_event(LogType.error, f"{cli_command[command]} 失败 {repr(e)}") + + def batch_cli_control(self, command): + checked_instances = [ + instance for instance in config.conf.instances if instance.checked + ] + if not checked_instances: + custom_event(LogType.warning, "没有选中的实例") + return [{"status": False, "message": "No checked instances"}] + + for instance in checked_instances: + try: + custom_event(LogType.info, f"{instance.name} {cli_command[command]}") + self.cli_control(command, instance.path) + custom_event( + LogType.info, f"{instance.name} {cli_command[command]} 完成" + ) + except Exception as e: + custom_event( + LogType.error, + f"{instance.name} {cli_command[command]} 失败 {repr(e)}", + ) diff --git a/ui/src/components/BaseMirrorOption.vue b/ui/src/components/BaseMirrorOption.vue new file mode 100644 index 0000000..8d1037d --- /dev/null +++ b/ui/src/components/BaseMirrorOption.vue @@ -0,0 +1,30 @@ + + + + + diff --git a/ui/src/main.js b/ui/src/main.js index 132098e..647f8a5 100644 --- a/ui/src/main.js +++ b/ui/src/main.js @@ -1,4 +1,3 @@ -import 'vfonts/Lato.css' import 'vfonts/FiraCode.css' import './styles/global.css' diff --git a/ui/src/pages/Init.vue b/ui/src/pages/Init.vue index ac577ad..0120bfe 100644 --- a/ui/src/pages/Init.vue +++ b/ui/src/pages/Init.vue @@ -1,4 +1,6 @@