新增功能:使用cli控制实例启停、在浏览器中打开网页面板
This commit is contained in:
parent
de2c2af2bd
commit
5576f8de39
3 changed files with 98 additions and 33 deletions
|
@ -33,3 +33,15 @@ mirror_list = {
|
||||||
|
|
||||||
# 实例文件夹名
|
# 实例文件夹名
|
||||||
instances_folder_name = "instances"
|
instances_folder_name = "instances"
|
||||||
|
|
||||||
|
# cli命令
|
||||||
|
cli_command = {
|
||||||
|
"status": "获取mower-ng实例状态",
|
||||||
|
"launch": "启动mower-ng进程",
|
||||||
|
"exit": "停止mower-ng进程",
|
||||||
|
"kill": "强制退出mower-ng进程",
|
||||||
|
"start": "开始运行调度器",
|
||||||
|
"stop": "停止运行调度器",
|
||||||
|
"webui": "在浏览器中打开网页面板",
|
||||||
|
"log": "通过WebSocket获取日志",
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ import io
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
from _winapi import CREATE_NO_WINDOW
|
from _winapi import CREATE_NO_WINDOW
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -12,7 +11,6 @@ from subprocess import Popen
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from launcher import config
|
from launcher import config
|
||||||
from launcher.config.conf import LaunchPart
|
|
||||||
from launcher.constants import (
|
from launcher.constants import (
|
||||||
download_git_url,
|
download_git_url,
|
||||||
download_python_url,
|
download_python_url,
|
||||||
|
@ -22,6 +20,7 @@ from launcher.constants import (
|
||||||
file_name,
|
file_name,
|
||||||
instances_folder_name,
|
instances_folder_name,
|
||||||
mower_ng_git_url,
|
mower_ng_git_url,
|
||||||
|
cli_command,
|
||||||
)
|
)
|
||||||
from launcher.file.download import init_download, download_file
|
from launcher.file.download import init_download, download_file
|
||||||
from launcher.file.extract import extract_7z_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_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",
|
"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}"',
|
"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):
|
) == instances_dir and os.path.exists(abs_path):
|
||||||
shutil.rmtree(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):
|
def migrate_default_instance(self):
|
||||||
"""迁移默认实例文件到新实例"""
|
"""迁移默认实例文件到新实例"""
|
||||||
source_path = os.path.join(os.getcwd(), "mower-ng")
|
source_path = os.path.join(os.getcwd(), "mower-ng")
|
||||||
|
@ -285,3 +264,42 @@ class Api:
|
||||||
)
|
)
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
custom_event(LogType.error, f"发生错误: {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)}",
|
||||||
|
)
|
||||||
|
|
|
@ -1,7 +1,17 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { useConfigStore } from '@/stores/config.js'
|
import { useConfigStore } from '@/stores/config.js'
|
||||||
import { NButton } from 'naive-ui'
|
import { NButton } from 'naive-ui'
|
||||||
import { Add, Pencil, Play, Folder, TrashOutline, Archive } from '@vicons/ionicons5'
|
import {
|
||||||
|
Add,
|
||||||
|
Pencil,
|
||||||
|
Play,
|
||||||
|
Folder,
|
||||||
|
TrashOutline,
|
||||||
|
Archive,
|
||||||
|
Browsers,
|
||||||
|
Stop,
|
||||||
|
Search
|
||||||
|
} from '@vicons/ionicons5'
|
||||||
|
|
||||||
const notification = useNotification()
|
const notification = useNotification()
|
||||||
|
|
||||||
|
@ -64,13 +74,11 @@ function end_update_instance_name() {
|
||||||
function open_folder(path) {
|
function open_folder(path) {
|
||||||
pywebview.api.open_folder(path)
|
pywebview.api.open_folder(path)
|
||||||
}
|
}
|
||||||
function start_instance(instance) {
|
function cli(command, instance) {
|
||||||
pywebview.api.run('webview', 'mower-ng', {
|
pywebview.api.cli_control(command, instance.path)
|
||||||
instance_path: instance.path
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
function start_checked_instance() {
|
function batch_cli_control(command) {
|
||||||
pywebview.api.start_checked_instance()
|
pywebview.api.batch_cli_control(command)
|
||||||
}
|
}
|
||||||
async function handle_migrate(key) {
|
async function handle_migrate(key) {
|
||||||
if (key == 'default') {
|
if (key == 'default') {
|
||||||
|
@ -132,7 +140,7 @@ async function handle_migrate(key) {
|
||||||
class="launch-btn"
|
class="launch-btn"
|
||||||
type="primary"
|
type="primary"
|
||||||
secondary
|
secondary
|
||||||
@click="start_checked_instance"
|
@click="batch_cli_control('launch')"
|
||||||
:disabled="!check_all && !check_part"
|
:disabled="!check_all && !check_part"
|
||||||
>
|
>
|
||||||
<template #icon>
|
<template #icon>
|
||||||
|
@ -140,6 +148,18 @@ async function handle_migrate(key) {
|
||||||
</template>
|
</template>
|
||||||
启动所选实例
|
启动所选实例
|
||||||
</n-button>
|
</n-button>
|
||||||
|
<n-button
|
||||||
|
class="launch-btn"
|
||||||
|
type="error"
|
||||||
|
secondary
|
||||||
|
@click="batch_cli_control('exit')"
|
||||||
|
:disabled="!check_all && !check_part"
|
||||||
|
>
|
||||||
|
<template #icon>
|
||||||
|
<n-icon :component="Stop"></n-icon>
|
||||||
|
</template>
|
||||||
|
停止所选实例
|
||||||
|
</n-button>
|
||||||
<n-dropdown trigger="click" :options="migrate_options" @select="handle_migrate">
|
<n-dropdown trigger="click" :options="migrate_options" @select="handle_migrate">
|
||||||
<n-button class="launch-btn" type="primary" secondary>
|
<n-button class="launch-btn" type="primary" secondary>
|
||||||
<template #icon>
|
<template #icon>
|
||||||
|
@ -194,11 +214,26 @@ async function handle_migrate(key) {
|
||||||
</n-space>
|
</n-space>
|
||||||
<template #suffix>
|
<template #suffix>
|
||||||
<n-space :wrap="false">
|
<n-space :wrap="false">
|
||||||
<n-button type="primary" size="small" @click="start_instance(item)">
|
<n-button type="primary" ghost size="small" @click="cli('status', item)">
|
||||||
|
<template #icon>
|
||||||
|
<n-icon :component="Search"></n-icon>
|
||||||
|
</template>
|
||||||
|
</n-button>
|
||||||
|
<n-button type="primary" size="small" @click="cli('launch', item)">
|
||||||
<template #icon>
|
<template #icon>
|
||||||
<n-icon :component="Play"></n-icon>
|
<n-icon :component="Play"></n-icon>
|
||||||
</template>
|
</template>
|
||||||
</n-button>
|
</n-button>
|
||||||
|
<n-button type="primary" ghost size="small" @click="cli('webui', item)">
|
||||||
|
<template #icon>
|
||||||
|
<n-icon :component="Browsers"></n-icon>
|
||||||
|
</template>
|
||||||
|
</n-button>
|
||||||
|
<n-button type="error" ghost size="small" @click="cli('exit', item)">
|
||||||
|
<template #icon>
|
||||||
|
<n-icon :component="Stop"></n-icon>
|
||||||
|
</template>
|
||||||
|
</n-button>
|
||||||
<n-popconfirm @positive-click="delete_instance(index, item.path)">
|
<n-popconfirm @positive-click="delete_instance(index, item.path)">
|
||||||
<template #trigger>
|
<template #trigger>
|
||||||
<n-button type="error" ghost size="small">
|
<n-button type="error" ghost size="small">
|
||||||
|
|
Loading…
Add table
Reference in a new issue