新增功能:启动程序和设置页面添加日志窗口

This commit is contained in:
li-xiaochen 2025-01-25 17:41:01 +08:00
parent 9295d9f2cf
commit cae312a272
5 changed files with 55 additions and 53 deletions

View file

@ -11,6 +11,8 @@ upgrade_script_name = "upgrade.bat"
# 获取最新版本发布信息
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"

View file

@ -1,7 +1,6 @@
import io
import os
import subprocess
import threading
from _winapi import CREATE_NO_WINDOW
from pathlib import Path
from shutil import rmtree
@ -11,7 +10,7 @@ import requests
from launcher import config
from launcher.constants import download_git_url, download_python_url, get_new_version_url, upgrade_script_name, \
mirror_list
mirror_list, file_name
from launcher.file.download import init_download, download_file
from launcher.file.extract import extract_7z_file
from launcher.file.utils import ensure_directory_exists, check_command_path
@ -30,7 +29,7 @@ command_list = {
"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": "start ..\\python\\pythonw webview_ui.py",
"webview": "..\\python\\pythonw webview_ui.py",
"manager": "start ..\\python\\pythonw manager.py",
}
@ -49,24 +48,17 @@ def parse_stderr(stderr_output):
return "未定义的错误"
def read_stream(stream, log_type, output_list=None):
def process_lines(text_io):
for line in iter(text_io.readline, ''):
text = line.rstrip('\n').strip()
custom_event(log_type, text)
if output_list is not None:
output_list.append(text)
detected_encoding = 'utf-8'
text_io = io.TextIOWrapper(stream, encoding=detected_encoding, errors='replace')
try:
process_lines(text_io)
except UnicodeDecodeError:
stream.seek(0) # 重新将流指针重置到开头
text_io = io.TextIOWrapper(stream, encoding='gbk', errors='replace')
process_lines(text_io)
finally:
text_io.close()
def check_command_end(command_key, output):
end_keywords = {
"webview": {"WebSocket客户端建立连接": "mower_ng已成功运行"}
}
if command_key in end_keywords:
keywords = end_keywords[command_key]
for keyword in keywords:
if keyword in output:
custom_event(LogType.info, keywords[keyword])
return True
return False
class Api:
@ -91,8 +83,6 @@ class Api:
# 更新启动器本身
def update_self(self, download_url):
logger.info(f"开始更新启动器 {download_url}")
file_name = os.path.basename(download_url)
file_name = "launcher.7z"
current_path = os.getcwd()
download_tmp_folder = os.path.join(current_path, "download_tmp")
# 确保 download_tmp 文件夹存在
@ -144,8 +134,8 @@ class Api:
except Exception as e:
return repr(e)
def run(self, command, cwd=None):
command = command_list[command]
def run(self, command_key, cwd=None):
command = command_list[command_key]
if callable(command):
command = command()
if callable(command):
@ -161,18 +151,28 @@ class Api:
try:
stdout_stderr = []
with subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=cwd, bufsize=0,
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=cwd, bufsize=0,
universal_newlines=False
) as p:
stdout_thread = threading.Thread(target=read_stream, args=(p.stdout, LogType.command_out))
stderr_thread = threading.Thread(target=read_stream,
args=(p.stderr, LogType.command_out, stdout_stderr))
def process_lines(text_io):
for line in iter(text_io.readline, ''):
text = line.rstrip('\n').strip()
custom_event(LogType.command_out, text)
if stdout_stderr is not None:
stdout_stderr.append(text)
if check_command_end(command_key, text):
break
stdout_thread.start()
stderr_thread.start()
stdout_thread.join()
stderr_thread.join()
detected_encoding = 'utf-8'
text_io = io.TextIOWrapper(p.stdout, encoding=detected_encoding, errors='replace')
try:
process_lines(text_io)
except UnicodeDecodeError:
p.stdout.seek(0) # 重新将流指针重置到开头
text_io = io.TextIOWrapper(p.stdout, encoding='gbk', errors='replace')
process_lines(text_io)
finally:
text_io.close()
if p.returncode == 0:
return "success"

View file

@ -1,4 +1,8 @@
<script setup>
import { useConfigStore } from '@/stores/config.js'
import { form_item_label_style } from '@/styles/styles.js'
const conf = useConfigStore().config
function webview() {
pywebview.api.run('webview', 'mower-ng')
}
@ -9,23 +13,16 @@ function manager() {
</script>
<template>
<n-flex
vertical
style="
gap: 16px;
height: 100%;
padding: 16px;
box-sizing: border-box;
justify-content: center;
align-items: center;
"
>
<n-button class="launch-btn" type="primary" secondary size="large" @click="webview">
单开运行
</n-button>
<n-button class="launch-btn" type="primary" secondary size="large" @click="manager">
多开器
</n-button>
<n-flex vertical style="gap: 16px; height: 100%; padding: 16px; box-sizing: border-box">
<n-form label-placement="left" :show-feedback="false" label-width="auto" label-align="left">
<n-form-item label="启动程序" :label-style="form_item_label_style">
<n-space size="large">
<n-button class="launch-btn" type="primary" secondary size="large" @click="webview"> 单开运行 </n-button>
<n-button class="launch-btn" type="primary" secondary size="large" @click="manager"> 多开器 </n-button>
</n-space>
</n-form-item>
</n-form>
<log-component />
</n-flex>
</template>
@ -34,4 +31,4 @@ function manager() {
width: 120px;
height: 48px;
}
</style>
</style>

View file

@ -91,5 +91,6 @@ async function check_update() {
</n-space>
</n-alert>
</n-form>
<log-component />
</n-flex>
</template>

View file

@ -3,10 +3,12 @@ import { defineStore } from 'pinia'
export const useConfigStore = defineStore('config', () => {
class Config {
constructor(conf) {
// 整体 Total
this.page = conf.page
this.is_already_show_doc = conf.is_already_show_doc
// 更新代码 UpdatePart
this.branch = conf.branch
this.mirror = conf.mirror
this.is_already_show_doc = conf.is_already_show_doc
}
}