初步代替几个bat脚本的功能

This commit is contained in:
zhbaor 2024-09-23 16:12:34 +08:00
commit d0fbc89012
19 changed files with 4002 additions and 0 deletions

79
ui/src/App.vue Normal file
View file

@ -0,0 +1,79 @@
<script setup>
import Init from '@/pages/Init.vue'
import Launch from '@/pages/Launch.vue'
import Update from '@/pages/Update.vue'
import { darkTheme, dateZhCN, zhCN } from 'naive-ui'
const loading = ref(true)
const page = ref(null)
function load_config() {
console.log('load_config')
console.log(pywebview)
pywebview.api.get_page().then((value) => {
page.value = value
loading.value = false
})
}
const log = ref('')
provide('log', log)
const log_ele = ref(null)
provide('log_ele', log_ele)
watch(log, () => {
nextTick(() => {
log_ele.value?.scrollTo({ position: 'bottom' })
})
})
onMounted(() => {
if (window.pywebview && pywebview.api) {
load_config()
} else {
window.addEventListener('pywebviewready', load_config)
}
window.addEventListener('log', (e) => {
log.value += e.detail.log
})
})
function set_page(value) {
log.value = ''
pywebview.api.set_page(value)
}
const running = ref(false)
provide('running', running)
const steps = ref([])
provide('steps', steps)
</script>
<template>
<n-config-provider :theme="darkTheme" :locale="zhCN" :dateLocale="dateZhCN">
<n-spin v-if="loading" class="container">
<template #description>加载中</template>
</n-spin>
<n-tabs
v-else
type="card"
placement="left"
class="container"
:default-value="page"
@update:value="set_page"
:disabled="running"
>
<n-tab-pane name="init" tab="初始化"><init /></n-tab-pane>
<n-tab-pane name="update" tab="更新代码"><update /></n-tab-pane>
<n-tab-pane name="launch" tab="启动程序"><launch /></n-tab-pane>
</n-tabs>
<n-global-style />
</n-config-provider>
</template>
<style scoped>
.container {
width: 100vw;
height: 100vh;
}
</style>

View file

@ -0,0 +1,48 @@
<script setup>
import PlayIcon from '@vicons/ionicons5/Play'
import { inject } from 'vue'
const running = inject('running')
const log = inject('log')
const steps = inject('steps')
const current_step = inject('current_step')
const current_state = inject('current_state')
async function start() {
log.value = ''
running.value = true
for (const [i, step] of steps.value.entries()) {
current_step.value = i + 1
current_state.value = 'process'
for (const cmd of step.command) {
await pywebview.api.run(cmd, step.cwd)
}
}
current_state.value = 'finish'
running.value = false
}
</script>
<template>
<n-button class="float" type="primary" :loading="running" :disabled="running" @click="start">
<template #icon>
<n-icon>
<play-icon />
</n-icon>
</template>
{{ running ? '' : '运行' }}
</n-button>
</template>
<style scoped>
.float {
position: absolute;
right: 25px;
bottom: 25px;
opacity: 0.8;
}
.float:hover {
opacity: 1;
}
</style>

View file

@ -0,0 +1,8 @@
<script setup>
const log = inject('log')
const log_ele = inject('log_ele')
</script>
<template>
<n-log :log="log" style="flex-grow: 1" ref="log_ele" />
</template>

7
ui/src/main.js Normal file
View file

@ -0,0 +1,7 @@
import 'vfonts/Lato.css'
import 'vfonts/FiraCode.css'
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.mount('#app')

26
ui/src/pages/Init.vue Normal file
View file

@ -0,0 +1,26 @@
<script setup>
const steps = ref([
{ title: '设置Git LFS', command: ['git/bin/git lfs install'] },
{ title: '安装pip', command: ['../python/python -m ensurepip --default-pip'] },
{
title: '下载代码',
command: ['git/bin/git clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow']
}
])
provide('steps', steps)
const current_step = ref(1)
provide('current_step', current_step)
const current_state = ref('wait')
provide('current_state', current_state)
</script>
<template>
<n-flex vertical style="gap: 16px; height: 100%; padding: 16px; box-sizing: border-box">
<n-alert title="以下步骤仅需运行一次" type="warning" />
<n-steps :current="current_step" :status="current_state" size="small">
<n-step v-for="step in steps" :title="step.title" />
</n-steps>
<log-component />
</n-flex>
<float-button />
</template>

33
ui/src/pages/Launch.vue Normal file
View file

@ -0,0 +1,33 @@
<script setup>
function webview() {
pywebview.api.run('start ../python/pythonw webview_ui.py', './mower-ng')
}
function manager() {
pywebview.api.run('start ../python/pythonw manager.py', './mower-ng')
}
</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" @click="webview">单开运行</n-button>
<n-button class="launch-btn" @click="manager">多开器</n-button>
</n-flex>
</template>
<style scoped>
.launch-btn {
width: 160px;
height: 48px;
}
</style>

56
ui/src/pages/Update.vue Normal file
View file

@ -0,0 +1,56 @@
<script setup>
const branch = ref(null)
onMounted(() => {
pywebview.api.get_branch().then((value) => {
branch.value = value
})
})
watch(branch, () => {
pywebview.api.set_branch(branch.value)
})
const steps = ref([
{
title: '下载源码',
command: [
'../git/bin/git fetch',
`../git/bin/git switch -f ${branch.value}`,
`../git/bin/git reset --hard origin/${branch.value}`
],
cwd: './mower-ng'
},
{
title: '安装依赖',
command: [
'../python/Scripts/pip install -i https://mirror.sjtu.edu.cn/pypi/web/simple -r requirements.txt'
],
cwd: './mower-ng'
}
])
provide('steps', steps)
const current_step = ref(1)
provide('current_step', current_step)
const current_state = ref('wait')
provide('current_state', current_state)
</script>
<template>
<n-flex vertical style="gap: 16px; height: 100%; padding: 16px; box-sizing: border-box">
<n-flex>
<div>代码分支</div>
<n-radio-group v-model:value="branch">
<n-flex>
<n-radio value="fast">fast</n-radio>
<n-radio value="slow">slow</n-radio>
</n-flex>
</n-radio-group>
</n-flex>
<n-steps :current="current_step" :status="current_state" size="small">
<n-step v-for="step in steps" :title="step.title" />
</n-steps>
<log-component />
</n-flex>
<float-button />
</template>