diff --git a/.gitignore b/.gitignore index 3eab2be..e32caef 100644 --- a/.gitignore +++ b/.gitignore @@ -162,4 +162,10 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ +launcher.iml + +# Mower-ng +git/ +python/ +mower-ng/ diff --git a/README.md b/README.md index a43125d..5f04d6a 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,11 @@ 前端运行 `npm run build` 生成 `ui/dist`,之后安装 PyInstaller,运行 ```bash -pyinstaller -w -F --add-data ui/dist:ui/dist launcher.py +pyinstaller -w --add-data ui/dist:ui/dist launcher.py +``` + +在dist文件夹生成launcher文件夹,切到dist文件夹下运行 + +```bash +tar -cf launcher.tar launcher ``` diff --git a/launcher.py b/launcher.py index b09ed77..700b7cc 100644 --- a/launcher.py +++ b/launcher.py @@ -3,13 +3,18 @@ import mimetypes from pathlib import Path from shutil import rmtree from subprocess import PIPE, STDOUT, Popen + +import requests import webview +from log import logger +from python.Lib import shutil, os, subprocess + mimetypes.add_type("text/html", ".html") mimetypes.add_type("text/css", ".css") mimetypes.add_type("application/javascript", ".js") -version = "2024-11-28" +version = "v0.2" config = { "page": "init", @@ -18,6 +23,10 @@ config = { } config_path = Path("launcher.json") +get_new_version_url = "https://git.zhaozuohong.vip/api/v1/repos/mower-ng/launcher/releases/latest" + +upgrade_script_name = "upgrade.bat" + try: with config_path.open("r") as f: user_config = json.load(f) @@ -39,7 +48,6 @@ mirror_list = { "sjtu": "https://mirror.sjtu.edu.cn/pypi/web/simple", } - command_list = { "lfs": "git\\bin\\git lfs install", "ensurepip": "python\\python -m ensurepip --default-pip", @@ -72,9 +80,45 @@ class Api: def set_mirror(self, mirror): config["mirror"] = mirror - def update_self(self): - # 更新启动器本身 - pass + def get_version(self): + return version + + def get_new_version(self): + logger.info("获取最新版本号") + response = requests.get(get_new_version_url) + return response.json() + + # 更新启动器本身 + def update_self(self, download_url): + # 下载压缩包的全路径 + download_path = os.path.join(os.getcwd(), os.path.basename(download_url)) + logger.info(f"下载新版本: {download_url}到{download_path}") + response = requests.get(download_url, stream=True) + if response.status_code == 200: + with open(download_path, 'wb') as file: + shutil.copyfileobj(response.raw, file) + logger.info("下载完成") + else: + logger.error(f"下载新版本失败: {response.status_code}") + return f"下载新版本失败: {response.status_code}" + + script_path = os.path.join(os.getcwd(), upgrade_script_name) + folder_path = os.path.join(os.getcwd(), "_internal") + exe_path = os.path.join(os.getcwd(), "launcher.exe") + with open(script_path, 'w') as b: + TempList = f"@echo off\n" + TempList += f"timeout /t 3 /nobreak\n" # 等待进程退出 + TempList += f"rmdir {folder_path}\n" # 删除_internal + TempList += f"del {exe_path}\n" # 删除exe + TempList += f"tar -xf {download_path} -C ..\n" # 解压压缩包 + TempList += f"timeout /t 1 /nobreak\n" # 等待解压 + TempList += f"start {exe_path}\n" # 启动新程序 + TempList += f"del {download_path}\n" # 删除压缩包 + TempList += f"exit" + b.write(TempList) + # 不显示cmd窗口 + subprocess.Popen([script_path], creationflags=subprocess.CREATE_NO_WINDOW) + os._exit(0) def rm_site_packages(self): site_packages_path = Path("./python/Lib/site-packages") @@ -97,7 +141,7 @@ class Api: custom_event(command + "\n") try: with Popen( - command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0 + command, stdout=PIPE, stderr=STDOUT, shell=True, cwd=cwd, bufsize=0 ) as p: for data in p.stdout: try: @@ -112,7 +156,12 @@ class Api: return "failed" +# 如果当前路径存在更新脚本,则删除 +if Path(upgrade_script_name).exists(): + os.remove(upgrade_script_name) + window = webview.create_window(f"mower-ng launcher {version}", "ui/dist/index.html", js_api=Api()) +# window = webview.create_window(f"mower-ng launcher {version}", "http://localhost:5173/", js_api=Api()) webview.start() with config_path.open("w") as f: diff --git a/ui/src/App.vue b/ui/src/App.vue index 5d09561..85435b9 100644 --- a/ui/src/App.vue +++ b/ui/src/App.vue @@ -4,6 +4,7 @@ import Launch from '@/pages/Launch.vue' import Update from '@/pages/Update.vue' import Fix from '@/pages/Fix.vue' import { dateZhCN, zhCN } from 'naive-ui' +import Settings from '@/pages/Settings.vue' const loading = ref(true) const page = ref(null) @@ -15,6 +16,14 @@ function load_config() { }) } +async function init_version() { + version.value = await pywebview.api.get_version() + new_version.value = await pywebview.api.get_new_version() + if (new_version.value.tag_name > version.value) { + update_able.value = true + } +} + const log = ref('') provide('log', log) const log_ele = ref(null) @@ -28,8 +37,12 @@ watch(log, () => { onMounted(() => { if (window.pywebview && pywebview.api) { load_config() + init_version() } else { - window.addEventListener('pywebviewready', load_config) + window.addEventListener('pywebviewready', () => { + load_config() + init_version() + }) } window.addEventListener('log', (e) => { log.value += e.detail.log @@ -46,6 +59,15 @@ provide('running', running) const steps = ref([]) provide('steps', steps) + +const update_able = ref(false) +provide('update_able', update_able) + +const version = ref('') +provide('version', version) + +const new_version = ref({}) +provide('new_version', new_version)