upgrade-dsh.sh 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # 升级脚本:将仓库中 dsh 上游版本号提升到指定版本
  3. # 更新范围:Dockerfile(ARG + 注释示例)、docker-compose.yml(默认值)、README.md / README.zh.md(示例与变量表)
  4. # DESIGN.md 为历史设计稿,不在自动升级范围(差异由 §10 实现差异记录人工维护)
  5. #
  6. # Upgrade script: bump the dsh upstream version in the repo to the given version
  7. # Updated files: Dockerfile (ARG + comment example), docker-compose.yml (default), README.md / README.zh.md (examples and variable tables)
  8. # DESIGN.md is a historical design doc, out of scope for auto-upgrade (differences are maintained manually in §10)
  9. # Usage: upgrade-dsh.sh <new-version> [repo-dir](默认当前目录)
  10. set -euo pipefail
  11. NEW="$1"
  12. DIR="${2:-.}"
  13. # 从 Dockerfile 提取当前版本(仅带值的 ARG 声明行)
  14. #
  15. # Extract the current version from the Dockerfile (only the ARG line with a value)
  16. OLD="$(grep -E '^ARG DSH_VERSION=' "$DIR/Dockerfile" | head -n1 | cut -d= -f2)"
  17. [ -n "$OLD" ] || { echo "错误:未能在 Dockerfile 找到 ARG DSH_VERSION" >&2; exit 1; }
  18. [ "$NEW" != "$OLD" ] || { echo "新版本与当前版本相同($NEW),无需升级" >&2; exit 0; }
  19. # npm 版本号字符集为 [0-9A-Za-z.-];先转义 OLD 为正则字面量,再以「版本边界」锚定替换:
  20. # 仅当 OLD 前后都不是版本字符时才替换,避免把 OLD 当作更长版本的前缀误改写
  21. # (如 0.1.0 不应把历史引用 0.1.0-rc.6 改成 0.1.1-rc.6)。NEW 为 npm 版本号
  22. # (不含 & 与 \),可直接作替换串。
  23. #
  24. # npm version chars are [0-9A-Za-z.-]; escape OLD to a regex literal, then anchor on
  25. # version boundaries: replace OLD only when neither neighbour is a version char, so OLD
  26. # is never rewritten as a prefix of a longer version (e.g. 0.1.0 must not turn the
  27. # historical reference 0.1.0-rc.6 into 0.1.1-rc.6). NEW is an npm version (no & or \),
  28. # safe as a replacement string.
  29. ESC="$(printf '%s' "$OLD" | sed 's/[][\\^$.|*+?()]/\\&/g')"
  30. # Dockerfile / README:版本边界替换(见上注释)
  31. #
  32. # Dockerfile / README: version-boundary replacement (see the note above)
  33. for f in Dockerfile README.md README.zh.md; do
  34. [ -f "$DIR/$f" ] || { echo "错误:缺少 $f" >&2; exit 1; }
  35. sed -i "s/\\(^\\|[^-0-9A-Za-z.]\\)${ESC}\\([^-0-9A-Za-z.]\\|$\\)/\\1${NEW}\\2/g" "$DIR/$f"
  36. done
  37. # docker-compose.yml:版本号嵌在 ${DSH_VERSION:-OLD} 中,前置 `-` 是 bash 默认值语法,
  38. # 与 prerelease 内部的 `-` 无法仅凭字符类区分,故用精确上下文替换(compose 中版本号只出现于该位置)
  39. #
  40. # docker-compose.yml: the version sits inside ${DSH_VERSION:-OLD}, whose leading `-` is bash
  41. # default-value syntax and is indistinguishable from a prerelease `-` by character class alone,
  42. # so replace with an exact context match (the version appears only at that spot in compose)
  43. [ -f "$DIR/docker-compose.yml" ] || { echo "错误:缺少 docker-compose.yml" >&2; exit 1; }
  44. sed -i "s/\${DSH_VERSION:-${ESC}}/\${DSH_VERSION:-${NEW}}/g" "$DIR/docker-compose.yml"
  45. # 原子验证:作为完整版本号的 OLD 必须全部消失,新版本号已写入
  46. # (验证同样按版本边界/精确上下文匹配,历史引用的更长版本号不视为残留)
  47. #
  48. # Atomic verification: OLD as a full version number must be fully gone and the new one written
  49. # (also matched at version boundaries / exact contexts; longer historical versions are not leftovers)
  50. LEFT="$(grep -lE "(^|[^-0-9A-Za-z.])${ESC}([^-0-9A-Za-z.]|$)" "$DIR"/Dockerfile "$DIR"/README.md "$DIR"/README.zh.md 2>/dev/null || true)"
  51. LEFT_COMPOSE="$(grep -lE "\$\{DSH_VERSION:-${ESC}\}" "$DIR"/docker-compose.yml 2>/dev/null || true)"
  52. [ -z "$LEFT$LEFT_COMPOSE" ] || { echo "错误:旧版本仍残留于 $LEFT $LEFT_COMPOSE" >&2; exit 1; }
  53. grep -q "$NEW" "$DIR/Dockerfile" || { echo "错误:新版本未写入" >&2; exit 1; }
  54. echo "已升级:$OLD → $NEW(Dockerfile / docker-compose.yml / README.md / README.zh.md)"