launcher/main.py

76 lines
2.1 KiB
Python
Raw Normal View History

2024-09-23 16:12:34 +08:00
import json
from pathlib import Path
from subprocess import PIPE, STDOUT, Popen
import webview
config_path = Path("launcher.json")
try:
with config_path.open("r") as f:
config = json.load(f)
except Exception:
config = {
"page": "init",
"branch": "slow",
}
2024-09-23 19:42:56 +08:00
def custom_event(data):
data = json.dumps({"log": data})
2024-09-23 16:12:34 +08:00
js = f"var event = new CustomEvent('log', {{detail: {data}}}); window.dispatchEvent(event);"
window.evaluate_js(js)
2024-09-23 19:14:57 +08:00
command_list = {
2024-09-23 19:42:56 +08:00
"lfs": "git\\bin\\git lfs install",
2024-09-23 20:03:52 +08:00
"ensurepip": "python\\python -m ensurepip --default-pip",
2024-09-23 19:14:57 +08:00
"clone": "git\\bin\\git clone https://git.zhaozuohong.vip/mower-ng/mower-ng.git --branch slow",
2024-09-23 20:34:26 +08:00
"fetch_fast": "..\\git\\bin\\git fetch origin fast",
"fetch_slow": "..\\git\\bin\\git fetch origin slow",
2024-09-23 19:14:57 +08:00
"switch_fast": "..\\git\\bin\\git switch -f fast",
"switch_slow": "..\\git\\bin\\git switch -f slow",
"reset_fast": "..\\git\\bin\\git reset --hard origin/fast",
"reset_slow": "..\\git\\bin\\git reset --hard origin/slow",
2024-09-23 19:42:56 +08:00
"pip_install": "..\\python\\Scripts\\pip install -i https://mirror.sjtu.edu.cn/pypi/web/simple -r requirements.txt --no-warn-script-location",
2024-09-23 19:14:57 +08:00
"webview": "start ..\\python\\pythonw webview_ui.py",
"manager": "start ..\\python\\pythonw manager.py",
}
2024-09-23 16:12:34 +08:00
class Api:
def get_branch(self):
return config["branch"]
def set_branch(self, branch):
config["branch"] = branch
def get_page(self):
return config["page"]
def set_page(self, page):
config["page"] = page
def run(self, command, cwd=None):
2024-09-23 19:42:56 +08:00
command = command_list[command]
custom_event(command + "\n")
2024-09-23 16:12:34 +08:00
with Popen(
2024-09-23 19:42:56 +08:00
command,
2024-09-23 16:12:34 +08:00
stdout=PIPE,
stderr=STDOUT,
shell=True,
bufsize=1,
text=True,
cwd=cwd,
2024-09-23 20:30:32 +08:00
encoding="utf-8",
2024-09-23 16:12:34 +08:00
) as p:
for line in p.stdout:
2024-09-23 19:42:56 +08:00
custom_event(line)
2024-09-23 16:12:34 +08:00
2024-09-23 19:49:34 +08:00
window = webview.create_window("mower-ng launcher", "dist/index.html", js_api=Api())
2024-09-23 20:35:05 +08:00
webview.start()
2024-09-23 16:12:34 +08:00
with config_path.open("w") as f:
json.dump(config, f)