Files
worklog/package.sh
T
2026-10-29 16:27:34 +08:00

69 lines
2.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 打包脚本:停掉本地正在运行的 WorkLog 服务,然后打成 zip(不含数据库/日志/运行产物)。
# 用法:bash package.sh (产物:worklog-<日期>.zip,放在项目同级目录)
set -euo pipefail
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APP_NAME="WorkLog"
PORT="${WORKLOG_PORT:-37777}"
PYTHON="${PYTHON:-python}"
# ---------- 1. 关闭本地服务 ----------
# 优先按端口找进程(Windows 用 netstat,Linux/mac 用 lsof/ss)
stopped=0
if command -v netstat.exe >/dev/null 2>&1 && command -v taskkill.exe >/dev/null 2>&1; then
pids=$(netstat.exe -ano | grep -E "TCP.*:${PORT}\s.*LISTENING" | awk '{print $5}' | sort -u || true)
for pid in $pids; do
if [ -n "$pid" ] && [ "$pid" != "0" ]; then
echo "停止进程 PID=$pid (端口 $PORT)"
taskkill.exe //F //PID "$pid" || true
stopped=1
fi
done
else
if command -v lsof >/dev/null 2>&1; then
pids=$(lsof -ti tcp:"$PORT" 2>/dev/null || true)
elif command -v ss >/dev/null 2>&1; then
pids=$(ss -lptn "sport = :$PORT" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | sort -u)
else
pids=""
fi
for pid in $pids; do
echo "停止进程 PID=$pid (端口 $PORT)"
kill -9 "$pid" 2>/dev/null || true
stopped=1
done
fi
[ "$stopped" = "1" ] && echo "本地服务已停止" || echo "端口 $PORT 上没有运行中的服务"
sleep 1
# ---------- 2. 打包 ----------
STAMP="$(date +%Y%m%d-%H%M%S)"
ZIP_NAME="worklog-${STAMP}.zip"
# 用 Python zipfile 打包(Windows Git Bash 无 zip 命令),排除数据库/日志/缓存
"$PYTHON" - "$PROJECT_DIR" "$ZIP_NAME" << 'PYEOF'
import fnmatch, os, sys, zipfile
project, zip_name = os.path.abspath(sys.argv[1]), sys.argv[2]
out_path = os.path.join(os.path.dirname(project), zip_name)
skip_dirs = {"__pycache__", ".venv", ".git", "data"}
skip_files = {".env", ".run-dev.log"}
skip_ext = (".pyc", ".zip", ".log")
with zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zf:
for root, dirs, files in os.walk(project):
dirs[:] = [d for d in dirs if d not in skip_dirs]
for f in files:
if (f in skip_files or f.endswith(skip_ext)
or (f.startswith('.env.') and f != '.env.example')
or any(fnmatch.fnmatch(f, pattern) for pattern in ('*.db', '*.db-*', '*.sqlite', '*.sqlite-*', '*.sqlite3', '*.sqlite3-*', '*.bak*'))):
continue
full = os.path.join(root, f)
arc = os.path.relpath(full, project).replace(os.sep, "/")
zf.write(full, arc)
print("packed ->", out_path)
PYEOF
echo "打包完成:$(dirname "$PROJECT_DIR")/$ZIP_NAME"