diff --git a/launcher/constants.py b/launcher/constants.py index 507139e..10766e8 100644 --- a/launcher/constants.py +++ b/launcher/constants.py @@ -33,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/webview/api.py b/launcher/webview/api.py index f7769f2..859c14c 100644 --- a/launcher/webview/api.py +++ b/launcher/webview/api.py @@ -2,7 +2,6 @@ import io import os import shutil import subprocess -import threading import time from _winapi import CREATE_NO_WINDOW from pathlib import Path @@ -12,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, @@ -22,6 +20,7 @@ from launcher.constants import ( 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 @@ -50,6 +49,8 @@ command_list = { "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}', } @@ -229,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") @@ -285,3 +264,42 @@ class Api: ) 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: + self.run("cli", "mower-ng", {"command": command, "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/pages/Launch.vue b/ui/src/pages/Launch.vue index faaacff..7300528 100644 --- a/ui/src/pages/Launch.vue +++ b/ui/src/pages/Launch.vue @@ -1,7 +1,17 @@