app-starlock/scripts/bundle_install_and_auto_add.sh
2025-05-17 09:28:28 +08:00

86 lines
3.0 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.

#!/bin/bash
# 用法: bash scripts/bundle_install_and_auto_add.sh <gemfile_path> <bundle_path> <main_cmd>
# 例如: bash scripts/bundle_install_and_auto_add.sh ios/Gemfile vendor/bundle_ios "bundle exec fastlane -v"
GEMFILE_PATH="$1"
BUNDLE_PATH="$2"
MAIN_CMD="$3"
if [ -z "$GEMFILE_PATH" ] || [ -z "$BUNDLE_PATH" ] || [ -z "$MAIN_CMD" ]; then
echo "用法: $0 <gemfile_path> <bundle_path> <main_cmd>"
exit 1
fi
# 保证rubygems.org官方源始终存在开头
gem sources --add https://rubygems.org || true
max_auto_add=3
add_count=0
success=0
# 1. 检查并自动补全Gemfile缺失依赖
while [ $add_count -lt $max_auto_add ]; do
echo "[INFO] 第$((add_count+1))次尝试运行主命令: $MAIN_CMD (仅捕获缺失gem)"
$MAIN_CMD > bundle_missing_gem.log 2>&1 && success=1 && break
missing_gem=$(grep -o "Could not find [^ ]*" bundle_missing_gem.log | awk '{print $4}' | head -n1)
if [ -n "$missing_gem" ]; then
if ! grep -q "gem '$missing_gem'" "$GEMFILE_PATH"; then
echo "gem '$missing_gem'" >> "$GEMFILE_PATH"
echo "[AUTO] Gemfile已自动补全: $missing_gem"
else
echo "[WARN] Gemfile已包含 $missing_gem,但依然缺失,可能是平台或缓存问题"
fi
else
echo "[INFO] 未检测到缺失gem或主命令已成功。"
break
fi
add_count=$((add_count+1))
sleep 2
echo "[WARN] 第$add_count 次自动补全后重试..."
done
if [ $success -eq 1 ]; then
echo "[SUCCESS] 所有依赖已补全,主命令执行成功。"
# 再次保证rubygems.org官方源始终存在结尾
gem sources --add https://rubygems.org || true
exit 0
fi
# 2. 统一执行镜像切换和重试的bundle install
try_count=0
max_try=3
success=0
bundle config mirror.https://rubygems.org https://mirrors.aliyun.com/rubygems/
while [ $try_count -lt $max_try ]; do
echo "[INFO] 第$((try_count+1))次尝试使用阿里云镜像 bundle install..."
bundle config set --local path "$BUNDLE_PATH"
bundle install --gemfile "$GEMFILE_PATH"
if [ $? -eq 0 ]; then
echo "[SUCCESS] 使用阿里云镜像 bundle install 成功"
success=1
break
fi
try_count=$((try_count+1))
sleep 2
echo "[WARN] 阿里云镜像 bundle install 第$try_count 次失败,准备重试..."
done
if [ $success -eq 0 ]; then
echo "[ERROR] 阿里云镜像 bundle install 失败$max_try次,切换为官方源重试..."
bundle config mirror.https://rubygems.org https://rubygems.org
bundle config set --local path "$BUNDLE_PATH"
bundle install --gemfile "$GEMFILE_PATH"
if [ $? -eq 0 ]; then
echo "[SUCCESS] 官方源 bundle install 成功"
else
echo "[FATAL] 官方源 bundle install 依然失败请检查网络或Gemfile配置。"
exit 2
fi
fi
# 3. 最后再执行一次主命令校验
$MAIN_CMD || { echo "[FATAL] 主命令依然失败请人工检查Gemfile和依赖环境。"; exit 3; }
# 再次保证rubygems.org官方源始终存在结尾
gem sources --add https://rubygems.org || true
echo "[SUCCESS] 所有依赖已补全,主命令执行成功。"
exit 0