参考mower的conf配置

This commit is contained in:
li-xiaochen 2024-12-02 22:34:54 +08:00
parent 01c4bee95e
commit 3aad1fe8ff
11 changed files with 199 additions and 67 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
/launcher.json
/conf.yml
/dist
# Byte-compiled / optimized / DLL files

View file

@ -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.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)

0
launcher/__init__.py Normal file
View file

View file

@ -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()

36
launcher/config/conf.py Normal file
View file

@ -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

56
ui/package-lock.json generated
View file

@ -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",

View file

@ -11,6 +11,7 @@
"format": "prettier --write src/"
},
"dependencies": {
"pinia": "^2.2.8",
"vue": "^3.5.11"
},
"devDependencies": {

View file

@ -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"
>
<n-tab-pane :disabled="running" name="init" tab="初始化"><init /></n-tab-pane>

View file

@ -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')

View file

@ -1,23 +1,10 @@
<script setup>
import { useConfigStore } from '@/stores/config.js'
const conf = useConfigStore().config
const branch = ref(null)
const mirror = ref(null)
onMounted(() => {
pywebview.api.get_branch().then((value) => {
branch.value = value
})
pywebview.api.get_mirror().then((value) => {
mirror.value = value
})
})
watch(branch, () => {
pywebview.api.set_branch(branch.value)
})
watch(mirror, () => {
pywebview.api.set_mirror(mirror.value)
})
const steps = computed(() => [
{
title: '更新源码',
@ -41,7 +28,7 @@ provide('current_state', current_state)
<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="mower-ng 代码分支">
<n-radio-group v-model:value="branch">
<n-radio-group v-model:value="conf.branch">
<n-flex>
<n-radio value="fast">测试版</n-radio>
<n-radio value="slow">稳定版</n-radio>
@ -49,7 +36,7 @@ provide('current_state', current_state)
</n-radio-group>
</n-form-item>
<n-form-item label="PyPI 仓库镜像">
<n-radio-group v-model:value="mirror">
<n-radio-group v-model:value="conf.mirror">
<n-flex>
<n-radio value="pypi">PyPI</n-radio>
<n-radio value="aliyun">阿里云镜像站</n-radio>

32
ui/src/stores/config.js Normal file
View file

@ -0,0 +1,32 @@
import { defineStore } from 'pinia'
export const useConfigStore = defineStore('config', () => {
class Config {
constructor(conf) {
this.page = conf.page
this.branch = conf.branch
this.mirror = conf.mirror
}
}
const config = ref({})
async function load_config() {
const conf = await pywebview.api.load_config()
config.value = new Config(conf)
console.log('config.value', config.value)
}
watch(
config,
() => {
pywebview.api.save_config(config.value)
},
{ deep: true }
)
return {
load_config,
config
}
})