Files
Shuery e95ec522f2
CI / lint + test + build (push) Canceled after 0s
build: dist 产物入库,支持 Gitea 免构建直接下载
- .gitignore 移除 dist/ 忽略(仅忽略构建临时文件)
- dist/media_organizer 随仓库提交:Gitea raw 链接可直接下载单文件产物
- README 获取项目恢复 curl 下载引导(raw/branch/main/dist/media_organizer)
- build.sh 注释明确入库策略:CI --check 强制产物与源码同步
2026-08-14 23:41:55 +08:00

91 lines
2.9 KiB
Bash
Executable File
Raw Permalink 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
# ============================================================================
# 构建脚本:bashly generate → 单文件分发脚本 dist/media_organizer
#
# 用法:
# ./build.sh 构建(bashly generate + 语法/重复函数检查)
# ./build.sh --check 仅校验当前 dist/media_organizer 是否与 src/ 最新源码一致
#
# 版本单一来源:src/lib/main.sh 的 SCRIPT_VERSION 是唯一权威版本号,
# 构建时注入 bashly.yml 的 version 字段(产物 --version 与脚本内版本一致)。
#
# 产物入库策略:dist/media_organizer 随仓库提交(Gitea 可 raw 直接下载,
# 免构建即用);CI 的 `./build.sh --check` 强制产物与源码同步——源码变更
# 后必须重新构建并提交产物,否则 CI 失败。
#
# 依赖:bashly(gem install bashly;开发期工具,产物运行无需 Ruby)
# ============================================================================
set -euo pipefail
cd "$(dirname "$0")"
OUT="dist/media_organizer"
BASE="$(pwd)"
# 定位 bashly:环境变量 BASHLY 覆盖 > PATH > 本仓库 .rv-rubies(rv 管理的 Ruby)
find_bashly() {
if [[ -n "${BASHLY:-}" ]] && [[ -x "$BASHLY" ]]; then
echo "$BASHLY"
return 0
fi
if command -v bashly >/dev/null 2>&1; then
command -v bashly
return 0
fi
local d
for d in "$BASE"/.rv-rubies/*/bin/bashly; do
[[ -x "$d" ]] && {
echo "$d"
return 0
}
done
return 1
}
if ! BASHLY_CMD="$(find_bashly)"; then
echo "错误:未找到 bashly(gem install bashly,或设 BASHLY=/path/to/bashly)" >&2
exit 1
fi
# 版本单一来源:从 main.sh 提取 SCRIPT_VERSION
VERSION="$(sed -n 's/^readonly SCRIPT_VERSION="\([^"]*\)"/\1/p' src/lib/main.sh | head -1)"
if [[ -z "$VERSION" ]]; then
echo "错误:无法从 src/lib/main.sh 提取 SCRIPT_VERSION" >&2
exit 1
fi
# 在临时目录生成(注入版本号,不触碰工作区源码)
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
cp -r src "$work/src"
sed -i "s/^version: .*/version: $VERSION/" "$work/src/bashly.yml"
(cd "$work" && "$BASHLY_CMD" generate --quiet >/dev/null)
generated="$work/media_organizer"
# 语法检查
if ! bash -n "$generated"; then
echo "错误:构建产物语法检查未通过" >&2
exit 1
fi
# 重复函数定义检查
dups="$(grep -oE '^[A-Za-z_][A-Za-z0-9_]*\(\) \{' "$generated" | sort | uniq -d || true)"
if [[ -n "$dups" ]]; then
echo "错误:存在重复函数定义:$dups" >&2
exit 1
fi
if [[ "${1:-}" == "--check" ]]; then
if [[ -f "$OUT" ]] && cmp -s "$generated" "$OUT"; then
echo "OK:$OUT 与最新源码一致(v$VERSION)"
else
echo "DIFF:$OUT 已过期或不存在,请运行 ./build.sh 重新生成" >&2
exit 1
fi
else
mkdir -p "$(dirname "$OUT")"
chmod +x "$generated"
mv "$generated" "$OUT"
trap - EXIT
echo "构建完成:$OUT v$VERSION($(wc -l <"$OUT") 行,$(wc -c <"$OUT") 字节)"
fi