diff --git a/release_pod.sh b/release_pod.sh new file mode 100755 index 0000000..b87049c --- /dev/null +++ b/release_pod.sh @@ -0,0 +1,170 @@ +#!/usr/bin/env bash +set -euo pipefail + +# 简化 openim_sdk_core_ios 私有 Pod 发版流程: +# 1. 可选:替换 OpenIMCore.xcframework +# 2. 更新 openim_sdk_core_ios.podspec 中的 s.version +# 3. 可选:执行 pod lib lint +# 4. git commit / tag / push +# 5. 可选:推送到私有 Specs 仓库(pod repo push) +# +# 用法: +# ./release_pod.sh +# ./release_pod.sh 0.18.0 +# ./release_pod.sh 0.18.0 /path/to/OpenIMCore.xcframework +# +# 默认会从 openim_sdk_core_ios.podspec 读取当前版本并自动 +1 minor 作为新 tag, +# 例如 0.17.0 -> 0.18.0。 +# 默认会从 ../open-im-sdk-core/build/OpenIMCore.xcframework 拷贝 framework。 +# +# 常用环境变量: +# SKIP_LINT=1 跳过 pod lib lint +# LINT_ARGS="--allow-warnings" 自定义 lint 参数,默认 --allow-warnings +# SKIP_GIT=1 只更新文件,不 commit/tag/push +# COMMIT_MSG="release 0.18.0" 自定义 commit message +# PRIVATE_SPEC_REPO="your-spec-repo" 执行 pod repo push your-spec-repo openim_sdk_core_ios.podspec +# POD_REPO_PUSH_ARGS="--allow-warnings" 自定义 pod repo push 参数 + +PODSPEC="openim_sdk_core_ios.podspec" +FRAMEWORK_DIR="openim_sdk_core_ios/frameworks" +FRAMEWORK_NAME="OpenIMCore.xcframework" +# 默认 xcframework 来源:当前 pod 仓库上级目录的同级 open-im-sdk-core/build +DEFAULT_XCFRAMEWORK_SRC="../open-im-sdk-core/build/${FRAMEWORK_NAME}" + +usage() { + cat <&2 + exit 1 +} + +run() { + log "$*" + "$@" +} + +VERSION="${1:-}" +XCFRAMEWORK_SRC="${2:-$DEFAULT_XCFRAMEWORK_SRC}" + +[[ -f "$PODSPEC" ]] || fail "未找到 $PODSPEC,请在仓库根目录执行脚本" + +if [[ -z "$VERSION" ]]; then + VERSION="$(python3 - "$PODSPEC" <<'PY' +import re +import sys +from pathlib import Path + +podspec = Path(sys.argv[1]) +text = podspec.read_text() +match = re.search(r"s\.version\s*=\s*['\"]([^'\"]+)['\"]", text) +if not match: + raise SystemExit(f"未能在 {podspec} 中找到 s.version") +current = match.group(1) +parts = current.split(".") +if len(parts) != 3 or not all(part.isdigit() for part in parts): + raise SystemExit(f"当前版本号不支持自动递增:{current},请手动传入版本号,例如 0.18.0") +major, minor, patch = map(int, parts) +print(f"{major}.{minor + 1}.0") +PY +)" + log "未传版本号,自动从 $PODSPEC 递增得到:$VERSION" +fi + +if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]]; then + fail "版本号格式不正确:${VERSION},例如 0.18.0" +fi + +[[ -d "$XCFRAMEWORK_SRC" ]] || fail "xcframework 路径不存在:${XCFRAMEWORK_SRC}" +[[ "$(basename "$XCFRAMEWORK_SRC")" == "$FRAMEWORK_NAME" ]] || fail "请传入 ${FRAMEWORK_NAME} 目录" + +log "替换 ${FRAMEWORK_DIR}/${FRAMEWORK_NAME},来源:${XCFRAMEWORK_SRC}" +rm -rf "$FRAMEWORK_DIR/$FRAMEWORK_NAME" +mkdir -p "$FRAMEWORK_DIR" +cp -R "$XCFRAMEWORK_SRC" "$FRAMEWORK_DIR/" + +log "更新 ${PODSPEC} 版本号为 ${VERSION}" +python3 - "$PODSPEC" "$VERSION" <<'PY' +import re +import sys +from pathlib import Path + +podspec = Path(sys.argv[1]) +version = sys.argv[2] +text = podspec.read_text() +new_text, count = re.subn( + r"(s\.version\s*=\s*)['\"][^'\"]+['\"]", + rf"\1'{version}'", + text, + count=1, +) +if count != 1: + raise SystemExit(f"未能在 {podspec} 中找到唯一的 s.version") +podspec.write_text(new_text) +PY + +if [[ "${SKIP_LINT:-0}" != "1" ]]; then + if ! command -v pod >/dev/null 2>&1; then + fail "未安装 CocoaPods,或设置 SKIP_LINT=1 跳过 lint" + fi + # shellcheck disable=SC2206 + LINT_ARGS_ARRAY=(${LINT_ARGS:---allow-warnings}) + run pod lib lint "$PODSPEC" "${LINT_ARGS_ARRAY[@]}" +else + log "已跳过 pod lib lint" +fi + +if [[ "${SKIP_GIT:-0}" != "1" ]]; then + git diff --quiet -- "$PODSPEC" "$FRAMEWORK_DIR" && fail "没有检测到 podspec/framework 变更,无需发版" + + if git rev-parse "$VERSION" >/dev/null 2>&1; then + fail "tag 已存在:$VERSION" + fi + + run git add "$PODSPEC" "$FRAMEWORK_DIR" + run git commit -m "${COMMIT_MSG:-release $VERSION}" + run git tag "$VERSION" + run git push origin HEAD + run git push origin "$VERSION" +else + log "已跳过 git commit/tag/push" +fi + +if [[ -n "${PRIVATE_SPEC_REPO:-}" ]]; then + if ! command -v pod >/dev/null 2>&1; then + fail "未安装 CocoaPods,无法 pod repo push" + fi + # shellcheck disable=SC2206 + POD_REPO_PUSH_ARGS_ARRAY=(${POD_REPO_PUSH_ARGS:---allow-warnings}) + run pod repo push "$PRIVATE_SPEC_REPO" "$PODSPEC" "${POD_REPO_PUSH_ARGS_ARRAY[@]}" +fi + +log "完成:$VERSION"