From 3aad1fe8ffc41bb5c50521f05e3eefd3f5a15ba7 Mon Sep 17 00:00:00 2001
From: li-xiaochen <397721316@qq.com>
Date: Mon, 2 Dec 2024 22:34:54 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E8=80=83mower=E7=9A=84conf=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 2 +-
launcher.py | 47 ++++++++-----------------------
launcher/__init__.py | 0
launcher/config/__init__.py | 40 ++++++++++++++++++++++++++
launcher/config/conf.py | 36 ++++++++++++++++++++++++
ui/package-lock.json | 56 +++++++++++++++++++++++++++++++++++++
ui/package.json | 1 +
ui/src/App.vue | 25 ++++++++---------
ui/src/main.js | 4 +++
ui/src/pages/Update.vue | 23 ++++-----------
ui/src/stores/config.js | 32 +++++++++++++++++++++
11 files changed, 199 insertions(+), 67 deletions(-)
create mode 100644 launcher/__init__.py
create mode 100644 launcher/config/__init__.py
create mode 100644 launcher/config/conf.py
create mode 100644 ui/src/stores/config.js
diff --git a/.gitignore b/.gitignore
index e32caef..cec6701 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-/launcher.json
+/conf.yml
/dist
# Byte-compiled / optimized / DLL files
diff --git a/launcher.py b/launcher.py
index 27c42a5..d4cbeea 100644
--- a/launcher.py
+++ b/launcher.py
@@ -7,6 +7,7 @@ from subprocess import CREATE_NO_WINDOW, PIPE, STDOUT, Popen
import requests
import webview
+from launcher import config
from log import logger
import shutil
import os
@@ -17,26 +18,12 @@ mimetypes.add_type("application/javascript", ".js")
version = "v0.2"
-config = {
- "page": "init",
- "branch": "slow",
- "mirror": "aliyun",
-}
-config_path = Path("launcher.json")
-
get_new_version_url = (
"https://git-cf.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)
- config.update(user_config)
-except Exception:
- pass
-
def custom_event(data):
data = json.dumps({"log": data})
@@ -65,23 +52,15 @@ command_list = {
class Api:
- def get_branch(self):
- return config["branch"]
- def set_branch(self, branch):
- config["branch"] = branch
+ def load_config(self):
+ logger.info("读取配置文件")
+ return config.conf.model_dump()
- def get_page(self):
- return config["page"]
-
- def set_page(self, page):
- config["page"] = page
-
- def get_mirror(self):
- return config["mirror"]
-
- def set_mirror(self, mirror):
- config["mirror"] = mirror
+ def save_config(self, conf):
+ logger.info(f"更新配置文件{conf}")
+ config.conf = config.Conf(**conf)
+ config.save_conf()
def get_version(self):
return version
@@ -144,7 +123,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:
@@ -163,11 +142,9 @@ class Api:
if Path(upgrade_script_name).exists():
os.remove(upgrade_script_name)
+# url = "ui/dist/index.html"
+url = "http://localhost:5173/"
window = webview.create_window(
- f"mower-ng launcher {version}", "ui/dist/index.html", js_api=Api()
+ f"mower-ng launcher {version}", url, 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:
- json.dump(config, f)
diff --git a/launcher/__init__.py b/launcher/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/launcher/config/__init__.py b/launcher/config/__init__.py
new file mode 100644
index 0000000..c2dbc6e
--- /dev/null
+++ b/launcher/config/__init__.py
@@ -0,0 +1,40 @@
+import os
+
+import yaml
+from yamlcore import CoreDumper, CoreLoader
+
+from launcher.config.conf import Conf
+from python.Lib.pathlib import Path
+
+conf_path = Path(os.path.join(os.getcwd(), "conf.yml"))
+
+
+def save_conf():
+ with conf_path.open("w", encoding="utf8") as f:
+ # json.dump(conf.model_dump(), f, ensure_ascii=False, indent=4) # Use json.dump
+ yaml.dump(
+ conf.model_dump(),
+ f,
+ Dumper=CoreDumper,
+ encoding="utf-8",
+ default_flow_style=False,
+ allow_unicode=True,
+ )
+
+
+def load_conf():
+ global conf
+ if not conf_path.is_file():
+ conf_path.parent.mkdir(exist_ok=True)
+ conf = Conf()
+ save_conf()
+ return
+ with conf_path.open("r", encoding="utf-8") as f:
+ data = yaml.load(f, Loader=CoreLoader)
+ if data is None:
+ data = {}
+ conf = Conf(**data)
+
+
+conf: Conf
+load_conf()
diff --git a/launcher/config/conf.py b/launcher/config/conf.py
new file mode 100644
index 0000000..0f60eec
--- /dev/null
+++ b/launcher/config/conf.py
@@ -0,0 +1,36 @@
+from pydantic import BaseModel, model_validator
+from pydantic_core import PydanticUndefined
+
+
+class ConfModel(BaseModel):
+ @model_validator(mode="before")
+ @classmethod
+ def nested_defaults(cls, data):
+ for name, field in cls.model_fields.items():
+ if name not in data:
+ if field.default is PydanticUndefined:
+ data[name] = field.annotation()
+ else:
+ data[name] = field.default
+ return data
+
+
+class Total(ConfModel):
+ """整体"""
+ # 所在页面
+ page: str = "init"
+
+
+class UpdatePart(ConfModel):
+ """更新代码"""
+ # mower-ng 代码分支
+ branch: str = "slow"
+ # PyPI 仓库镜像
+ mirror: str = "aliyun"
+
+
+class Conf(
+ Total,
+ UpdatePart,
+):
+ pass
diff --git a/ui/package-lock.json b/ui/package-lock.json
index bc18944..3e30e87 100644
--- a/ui/package-lock.json
+++ b/ui/package-lock.json
@@ -8,6 +8,7 @@
"name": "ui",
"version": "0.0.0",
"dependencies": {
+ "pinia": "^2.2.8",
"vue": "^3.5.11"
},
"devDependencies": {
@@ -1131,6 +1132,11 @@
"@vue/shared": "3.5.11"
}
},
+ "node_modules/@vue/devtools-api": {
+ "version": "6.6.4",
+ "resolved": "https://registry.npmmirror.com/@vue/devtools-api/-/devtools-api-6.6.4.tgz",
+ "integrity": "sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g=="
+ },
"node_modules/@vue/eslint-config-prettier": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/@vue/eslint-config-prettier/-/eslint-config-prettier-10.0.0.tgz",
@@ -2534,6 +2540,56 @@
"url": "https://github.com/sponsors/jonschlinkert"
}
},
+ "node_modules/pinia": {
+ "version": "2.2.8",
+ "resolved": "https://registry.npmmirror.com/pinia/-/pinia-2.2.8.tgz",
+ "integrity": "sha512-NRTYy2g+kju5tBRe0oNlriZIbMNvma8ZJrpHsp3qudyiMEA8jMmPPKQ2QMHg0Oc4BkUyQYWagACabrwriCK9HQ==",
+ "dependencies": {
+ "@vue/devtools-api": "^6.6.3",
+ "vue-demi": "^0.14.10"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/posva"
+ },
+ "peerDependencies": {
+ "@vue/composition-api": "^1.4.0",
+ "typescript": ">=4.4.4",
+ "vue": "^2.6.14 || ^3.5.11"
+ },
+ "peerDependenciesMeta": {
+ "@vue/composition-api": {
+ "optional": true
+ },
+ "typescript": {
+ "optional": true
+ }
+ }
+ },
+ "node_modules/pinia/node_modules/vue-demi": {
+ "version": "0.14.10",
+ "resolved": "https://registry.npmmirror.com/vue-demi/-/vue-demi-0.14.10.tgz",
+ "integrity": "sha512-nMZBOwuzabUO0nLgIcc6rycZEebF6eeUfaiQx9+WSk8e29IbLvPU9feI6tqW4kTo3hvoYAJkMh8n8D0fuISphg==",
+ "hasInstallScript": true,
+ "bin": {
+ "vue-demi-fix": "bin/vue-demi-fix.js",
+ "vue-demi-switch": "bin/vue-demi-switch.js"
+ },
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://github.com/sponsors/antfu"
+ },
+ "peerDependencies": {
+ "@vue/composition-api": "^1.0.0-rc.1",
+ "vue": "^3.0.0-0 || ^2.6.0"
+ },
+ "peerDependenciesMeta": {
+ "@vue/composition-api": {
+ "optional": true
+ }
+ }
+ },
"node_modules/pkg-types": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/pkg-types/-/pkg-types-1.2.0.tgz",
diff --git a/ui/package.json b/ui/package.json
index cec47a3..4e707b9 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -11,6 +11,7 @@
"format": "prettier --write src/"
},
"dependencies": {
+ "pinia": "^2.2.8",
"vue": "^3.5.11"
},
"devDependencies": {
diff --git a/ui/src/App.vue b/ui/src/App.vue
index 85435b9..be241cb 100644
--- a/ui/src/App.vue
+++ b/ui/src/App.vue
@@ -5,16 +5,12 @@ import Update from '@/pages/Update.vue'
import Fix from '@/pages/Fix.vue'
import { dateZhCN, zhCN } from 'naive-ui'
import Settings from '@/pages/Settings.vue'
+import { useConfigStore } from '@/stores/config.js'
+const configStore = useConfigStore()
const loading = ref(true)
const page = ref(null)
-
-function load_config() {
- pywebview.api.get_page().then((value) => {
- page.value = value
- loading.value = false
- })
-}
+let conf
async function init_version() {
version.value = await pywebview.api.get_version()
@@ -23,6 +19,12 @@ async function init_version() {
update_able.value = true
}
}
+async function initialize_config() {
+ await configStore.load_config()
+ conf = configStore.config
+ await init_version()
+ loading.value = false
+}
const log = ref('')
provide('log', log)
@@ -36,12 +38,10 @@ watch(log, () => {
onMounted(() => {
if (window.pywebview && pywebview.api) {
- load_config()
- init_version()
+ initialize_config()
} else {
window.addEventListener('pywebviewready', () => {
- load_config()
- init_version()
+ initialize_config()
})
}
window.addEventListener('log', (e) => {
@@ -51,7 +51,6 @@ onMounted(() => {
function set_page(value) {
log.value = ''
- pywebview.api.set_page(value)
}
const running = ref(false)
@@ -80,7 +79,7 @@ provide('new_version', new_version)
type="card"
placement="left"
class="container"
- :default-value="page"
+ v-model:value="conf.page"
@update:value="set_page"
>
diff --git a/ui/src/main.js b/ui/src/main.js
index 1877952..e8343a7 100644
--- a/ui/src/main.js
+++ b/ui/src/main.js
@@ -2,6 +2,10 @@ import 'vfonts/Lato.css'
import 'vfonts/FiraCode.css'
import { createApp } from 'vue'
+import { createPinia } from 'pinia'
import App from './App.vue'
+
const app = createApp(App)
+const pinia = createPinia()
+app.use(pinia)
app.mount('#app')
diff --git a/ui/src/pages/Update.vue b/ui/src/pages/Update.vue
index 448108a..1d7019a 100644
--- a/ui/src/pages/Update.vue
+++ b/ui/src/pages/Update.vue
@@ -1,23 +1,10 @@