app-starlock/scripts/bundle_missing_gem_auto_add.sh

49 lines
1.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.

#!/bin/bash
# 用法: bash scripts/bundle_missing_gem_auto_add.sh <gemfile_path> <bundle_path> <main_cmd>
# 例如: bash scripts/bundle_missing_gem_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
max_try=3
try_count=0
success=0
bundle check --path "$BUNDLE_PATH" || bundle install --gemfile "$GEMFILE_PATH" --path "$BUNDLE_PATH"
while [ $try_count -lt $max_try ]; do
echo "[INFO] 第$((try_count+1))次尝试运行主命令: $MAIN_CMD"
$MAIN_CMD > bundle_missing_gem.log 2>&1 && success=1 && break
# 捕获缺失gem
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
bundle install --gemfile "$GEMFILE_PATH" --path "$BUNDLE_PATH"
else
echo "[FATAL] 未检测到缺失gem需人工介入。日志如下"
cat bundle_missing_gem.log
exit 2
fi
try_count=$((try_count+1))
sleep 2
echo "[WARN] 第$try_count 次自动补全后重试..."
done
if [ $success -eq 1 ]; then
echo "[SUCCESS] 所有依赖已补全,主命令执行成功。"
exit 0
else
echo "[FATAL] 自动补全$max_try次后依然失败请人工检查Gemfile和依赖环境。"
exit 3
fi