mac Shell脚本打包iOS应用 (支持批量打包中级版)

龚玄天
2023-12-01
#!/bin/sh
 
# 使用方法:
# step1: 将该脚本放在工程的根目录下(跟.xcworkspace文件or .xcodeproj文件同目录)
# step2: 根据情况修改下面的参数
# step3: 打开终端,执行脚本。(输入sh ,然后将脚本文件拉到终端,会生成文件路径,然后enter就可)
 
# =============项目自定义部分(自定义好下列参数后再执行该脚本)=================== #
 
# 是否编译工作空间 (例:若是用Cocopods管理的.xcworkspace项目,赋值true;用Xcode默认创建的.xcodeproj,赋值false)
is_workspace="true"
 
# .xcworkspace的名字,如果is_workspace为true,则必须填。否则可不填
workspace_name=""
 
# .xcodeproj的名字,如果is_workspace为false,则必须填。否则可不填
project_name=""
 
# 需要打包的scheme名称  数组支持多个scheme 根据需要配置
arr_ProjectNmae=("" "" "") 
 
# 配置证书 用哪个填写哪个
mobileprovision_development=""  # 开发
mobileprovision_app-store=""    # 发布
mobileprovision_aenterprise=""  # 企业证书
 
# 项目的bundleID,手动管理Profile时必填 
bundle_identifier=""
 
# =============配置结束=================== #
 
for(( i=0;i<${#arr_ProjectNmae[@]};i++)) do
#${#array[@]}获取数组长度用于循环
dengyu=" = "
logValue=${i}${dengyu}${arr_ProjectNmae[i]}
echo "\033[32;1m$logValue\033[0m"
done;
read readNumber
if [[ -z $readNumber ]];
then
readNumber=0
fi
scheme_name=${arr_ProjectNmae[readNumber]}
echo "ProjectNmae = $scheme_name"
#------------------------------------------------------------------------------
# DebugNmae=(Debug Release)
# for(( i=0;i<${#DebugNmae[@]};i++)) do
# #${#array[@]}获取数组长度用于循环
# dengyu=" = "
# logValue=${i}${dengyu}${DebugNmae[i]}
# echo "\033[32;1m$logValue\033[0m"
# done;
# read readNumber
# if [[ -z $readNumber ]];
# then
# readNumber=0
# fi
# build_configuration=${DebugNmae[readNumber]}
# echo "build_configuration = $build_configuration"
#------------------------------------------------------------------------------
methodNmae=(development app-store ad-hoc aenterprise)
for(( i=0;i<${#methodNmae[@]};i++)) do
#${#array[@]}获取数组长度用于循环
dengyu=" = "
logValue=${i}${dengyu}${methodNmae[i]};
echo "\033[32;1m$logValue\033[0m"
done;
read readNumber
if [[ -z $readNumber ]];
then
readNumber=0
fi
method=${methodNmae[readNumber]}
echo "method = $method"
#------------------------------------------------------------------------------
if [ "$method" = "${methodNmae[0]}" ];then
	mobileprovision_name=${mobileprovision_development}
  build_configuration="Debug"
	text="开发证书"
elif [ "$method" = "${methodNmae[1]}" ];then
	mobileprovision_name=${mobileprovision_app-store}
  build_configuration="Release"
	text="发布证书"
elif [ "$method" = "${methodNmae[2]}" ];then
	mobileprovision_name=${mobileprovision_development}
  build_configuration="Debug"
	text="内侧证书"
elif [ "$method" = "${methodNmae[3]}" ];then
	mobileprovision_name=${mobileprovision_aenterprise}
  build_configuration="Release"
	text="企业证书"
else
  mobileprovision_name=${mobileprovision_development}
  build_configuration="Debug"
	text="默认开发证书"
fi
 
echo "\033[32;1m$text\033[0m"
echo "--------------------脚本配置参数检查--------------------"
echo "mobileprovision_name=${mobileprovision_name} \033[0m"
echo "\033[33;1mis_workspace=${is_workspace} "
echo "workspace_name=${workspace_name}"
echo "project_name=${project_name}"
echo "scheme_name=${scheme_name}"
echo "build_configuration=${build_configuration}"
echo "bundle_identifier=${bundle_identifier}"
echo "method=${method}"
echo "mobileprovision_name=${mobileprovision_name} \033[0m"
 
# =======================脚本的一些固定参数定义(无特殊情况不用修改)====================== #
 
# 获取当前脚本所在目录
script_dir="$( cd "$( dirname "$0"  )" && pwd  )"
# 工程根目录
project_dir=$script_dir
 
# 时间
DATE=`date '+%Y%m%d_%H%M%S'`
# 指定输出导出文件夹路径
export_path="$project_dir/Package/$scheme_name-$DATE"
# 指定输出归档文件路径
export_archive_path="$export_path/$scheme_name.xcarchive"
# 指定输出ipa文件夹路径
export_ipa_path="$export_path"
# 指定输出ipa名称
ipa_name="${scheme_name}_${DATE}"
# 指定导出ipa包需要用到的plist配置文件的路径
export_options_plist_path="$project_dir/ExportOptions.plist"
 
 
echo "--------------------脚本固定参数检查--------------------"
echo "\033[33;1mproject_dir=${project_dir}"
echo "DATE=${DATE}"
echo "export_path=${export_path}"
echo "export_archive_path=${export_archive_path}"
echo "export_ipa_path=${export_ipa_path}"
echo "export_options_plist_path=${export_options_plist_path}"
echo "ipa_name=${ipa_name} \033[0m"
 
# =======================自动打包部分(无特殊情况不用修改)====================== #
 
echo "------------------------------------------------------"
echo "\033[32m开始构建项目  \033[0m"
# 进入项目工程目录
cd ${project_dir}
 
# 指定输出文件目录不存在则创建
if [ -d "$export_path" ] ; then
    echo $export_path
else
    mkdir -pv $export_path
fi
 
# 判断编译的项目类型是workspace还是project
if $is_workspace ; then
# 编译前清理工程
xcodebuild clean -workspace ${workspace_name}.xcworkspace \
                 -scheme ${scheme_name} \
                 -configuration ${build_configuration}
 
xcodebuild archive -workspace ${workspace_name}.xcworkspace \
                   -scheme ${scheme_name} \
                   -configuration ${build_configuration} \
                   -archivePath ${export_archive_path}
else
# 编译前清理工程
xcodebuild clean -project ${project_name}.xcodeproj \
                 -scheme ${scheme_name} \
                 -configuration ${build_configuration}
 
xcodebuild archive -project ${project_name}.xcodeproj \
                   -scheme ${scheme_name} \
                   -configuration ${build_configuration} \
                   -archivePath ${export_archive_path}
fi
 
#  检查是否构建成功
#  xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if [ -d "$export_archive_path" ] ; then
    echo "\033[32;1m项目构建成功 ✅  ✅  ✅  \033[0m"
else
    echo "\033[31;1m项目构建失败 ❌  ❌  ❌ \033[0m"
    exit 1
fi
echo "------------------------------------------------------"
 
echo "\033[32m开始导出ipa文件 \033[0m"
 
 
# 先删除export_options_plist文件
if [ -f "$export_options_plist_path" ] ; then
    #echo "${export_options_plist_path}文件存在,进行删除"
    rm -f $export_options_plist_path
fi
# 根据参数生成export_options_plist文件
/usr/libexec/PlistBuddy -c  "Add :method String ${method}"  $export_options_plist_path
/usr/libexec/PlistBuddy -c  "Add :provisioningProfiles:"  $export_options_plist_path
/usr/libexec/PlistBuddy -c  "Add :provisioningProfiles:${bundle_identifier} String ${mobileprovision_name}"  $export_options_plist_path
 
 
xcodebuild  -exportArchive \
            -archivePath ${export_archive_path} \
            -exportPath ${export_ipa_path} \
            -exportOptionsPlist ${export_options_plist_path} \
            -allowProvisioningUpdates
 
# 检查ipa文件是否存在
if [ -f "$export_ipa_path/$scheme_name.ipa" ] ; then
    echo "\033[32;1mexportArchive ipa包成功,准备进行重命名\033[0m"
else
    echo "\033[31;1mexportArchive ipa包失败 ❌  ❌  ❌  \033[0m"
    exit 1
fi
 
# 修改ipa文件名称
mv $export_ipa_path/$scheme_name.ipa $export_ipa_path/$ipa_name.ipa
 
# 检查文件是否存在
if [ -f "$export_ipa_path/$ipa_name.ipa" ] ; then
    echo "\033[32;1m导出 ${ipa_name}.ipa 包成功 ✅  ✅  ✅  \033[0m"
    open $export_path
else
    echo "\033[31;1m导出 ${ipa_name}.ipa 包失败 ❌  ❌  ❌  \033[0m"
    exit 1
fi
 
# 删除export_options_plist文件(中间文件)
if [ -f "$export_options_plist_path" ] ; then
    #echo "${export_options_plist_path}文件存在,准备删除"
    rm -f $export_options_plist_path
fi
 
# 输出打包总用时
echo "\033[36;1m使用AutoPackageScript打包总用时: ${SECONDS}s \033[0m"
 
exit 0

 

 类似资料: