72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
#!/usr/bin/env python3
|
|
"""SFCraft 文档开发服务器入口: 先同步物品贴图, 再启动 hugo server。
|
|
|
|
贴图同步失败(有物品找不到)时终止启动。
|
|
|
|
用法:
|
|
python scripts/dev.py [--no-sync] [hugo server 参数...]
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
SCRIPTS = Path(__file__).resolve().parent
|
|
|
|
|
|
def find_hugo():
|
|
"""按 HUGO 环境变量、PATH、winget 安装目录依次查找 hugo。"""
|
|
env = os.environ.get("HUGO")
|
|
if env and Path(env).is_file():
|
|
return env
|
|
|
|
name = "hugo.exe" if os.name == "nt" else "hugo"
|
|
for d in os.environ.get("PATH", "").split(os.pathsep):
|
|
if d:
|
|
cand = Path(d) / name
|
|
if cand.is_file():
|
|
return str(cand)
|
|
|
|
local = os.environ.get("LOCALAPPDATA")
|
|
if local:
|
|
base = Path(local) / "Microsoft" / "WinGet" / "Packages"
|
|
if base.is_dir():
|
|
for p in sorted(base.glob("*/hugo.exe")):
|
|
return str(p)
|
|
return None
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="SFCraft 开发服务器")
|
|
parser.add_argument(
|
|
"--no-sync", "-NoSync", action="store_true",
|
|
help="跳过贴图同步",
|
|
)
|
|
args, rest = parser.parse_known_args()
|
|
|
|
if not args.no_sync:
|
|
print("\n[1/2] 同步物品贴图")
|
|
r = subprocess.run([sys.executable, str(SCRIPTS / "sync-items.py")])
|
|
if r.returncode != 0:
|
|
print("[启动终止] 物品贴图同步失败, 未启动 hugo server。", file=sys.stderr)
|
|
sys.exit(r.returncode)
|
|
else:
|
|
print("[跳过] 已指定 --no-sync, 不执行贴图同步")
|
|
|
|
hugo = find_hugo()
|
|
if not hugo:
|
|
print(
|
|
"[错误] 未找到 hugo, 请安装 Hugo、将其加入 PATH, 或设置环境变量 HUGO。",
|
|
file=sys.stderr,
|
|
)
|
|
sys.exit(1)
|
|
|
|
print("\n[2/2] 启动 hugo server")
|
|
sys.exit(subprocess.run([hugo, "server", *rest]).returncode)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|