#!/bin/sh
# 使用方法:
# 脚本部署: 将该脚本放在工程的根目录下后缀名为".sh"(跟.xcworkspace文件or .xcodeproj文件同目录)
# 配置脚本: 根据情况修改下面的参数
# 执行脚本: 打开终端——-->sh 文件名.sh->回车
# =============项目自定义部分(自定义好下列参数后再执行该脚本)=================== #
# 是否编译工作空间
# Cocopods管理的.xcworkspace项目,赋值true
# Xcode默认创建的.xcodeproj,赋值false
is_workspace="true"
# .xcworkspace的名字,如果is_workspace为true,则必须填。否则可不填
workspace_name="XXXX"
# .xcodeproj的名字,如果is_workspace为false,则必须填。否则可不填
project_name="XXXX"
# 需要打包的scheme名称
# 数组支持多个scheme可重复 (可重复目的是在同一个项目打出来不同用途的包 比如:测试包与商店包同时打出来)
ProjectNmae_Arr=(
XXXX1
XXXX2
XXXX3
)
# 项目的bundleID,手动管理Profile时必填
# 需要打包的bundleIdentifie 至少添加一个 若配置多个与ProjectNmae_Arr一一对应即可
bundleIdentifier_Arr=(
"com.XXX.XXX"
)
# ================= 手动打包配置证书 使用哪个配置哪个,配置时至少添加一个,多个项目可用同一个证书打包 =================
# 开发证书配置
# "xxxx-xxxx-xxxx-xxxx-xxxx"
mobileprovision_development_Arr=(
"xxxx-xxxx-xxxx-xxxx-xxxx"
)
# 商店证书配置
# "xxxx-xxxx-xxxx-xxxx-xxxx"
mobileprovision_app_store_Arr=(
"xxxx-xxxx-xxxx-xxxx-xxxx"
)
# 企业证书配置
# "xxxx-xxxx-xxxx-xxxx-xxxx"
mobileprovision_aenterprise_Arr=(
"xxxx-xxxx-xxxx-xxxx-xxxx"
)
# 内测证书配置
# "xxxx-xxxx-xxxx-xxxx-xxxx"
mobileprovision_ad_hoc_Arr=(
"xxxx-xxxx-xxxx-xxxx-xxxx"
)
#============自动打包配置================
# 打包类型自动配置
# 至少添加一个数据作为通用配置
# "xxxx-xxxx-xxxx-xxxx-xxxx"
mobileprovision_auto_Arr=(
"xxxx-xxxx-xxxx-xxxx-xxxx"
)
# 打包类型自动配置
# 至少添加一个数据作为通用配置
# development app-store ad-hoc aenterprise
method_Arr=(
development
)
# 打包类型自动配置
# 至少添加一个数据作为通用配置
# Debug Release
build_configuration_Arr=(
Debug
)
#============================================
# 输入内容验证机制
# $1 输入的内容
# $2 与之匹配的最大值
funcReadResultManager(){
# echo "所有参数 = $*"
# echo "参数个数 = $#"
# echo "查看方法返回值 = $?"
readNumber=$1
maxNumber=$2
# 判断输入内容是否为数字 返回0 或者 1
expr $readNumber "+" 10 &> /dev/null
if [ $? -ne 0 ];then
echo "$readNumber 输入内容非数字 退出"
exit 1
fi
if [ $readNumber -eq $maxNumber ];then
echo "退出"
exit 1
fi
if [ $readNumber -gt $maxNumber ];then
echo "数字超过范围 退出"
exit 1
fi
return $readNumber
}
# 初始化项目
initProject(){
for(( i=0;i<${#ProjectNmae_Arr[@]};i++)) do
dengYu=" = "
logValue=${i}${dengYu}${ProjectNmae_Arr[i]}
echo "\033[32;1m$logValue\033[0m"
done;
arrLength=${#ProjectNmae_Arr[@]}
# echo "arrLength = $arrLength"
AutoPackConfig=$((arrLength))
ExitConfig=$((arrLength+1))
echo "\033[32;1m$AutoPackConfig = 自动跟随配置文件打包所有项目 \033[0m"
echo "\033[32;1m$ExitConfig = 退出 \033[0m"
read readNumber
if [[ -z $readNumber ]];then
readNumber=0
fi
funcReadResultManager $readNumber $ExitConfig
# 自动打包
if [ $readNumber -eq $AutoPackConfig ];then
echo "\033[32;1m自动打包\033[0m"
fi
# 手动选证书打包
if [ $readNumber -ne $AutoPackConfig ];then
ProjectNmae_count=${#ProjectNmae_Arr[@]}
bundle_identifier=${bundleIdentifier_Arr[readNumber]}
if [ $ProjectNmae_count -ge ${#bundleIdentifier_Arr[@]} ];then
echo "\033[33;1m项目数量与bundleIdentifier数量不一致但至少有一个 就用它吧\033[0m"
bundle_identifier=${bundleIdentifier_Arr[0]}
fi
scheme_name=${ProjectNmae_Arr[readNumber]}
echo "ProjectNmae = $scheme_name"
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;
arrLengthNew=${#methodNmae[@]}
readNumberNewPlusNew=$((arrLengthNew+0))
echo "\033[32;1m$readNumberNewPlusNew = 退出\033[0m"
read readNumberNew
if [[ -z $readNumberNew ]];then
readNumberNew=0
fi
funcReadResultManager $readNumberNew $readNumberNewPlusNew
readNumberNew=$?
method=${methodNmae[readNumberNew]}
echo "method = $method"
if [ "$method" = "${methodNmae[0]}" ];then
mobileprovision_New=$mobileprovision_development_Arr
build_configuration="Debug"
text="开发证书"
elif [ "$method" = "${methodNmae[1]}" ];then
mobileprovision_New=$mobileprovision_app_store_Arr
build_configuration="Release"
text="发布证书"
elif [ "$method" = "${methodNmae[2]}" ];then
mobileprovision_New=$mobileprovision_ad_hoc_Arr
build_configuration="Debug"
text="内侧证书"
else
mobileprovision_New=$mobileprovision_aenterprise_Arr
build_configuration="Release"
text="企业证书"
fi
mobileprovision_name=${mobileprovision_New[readNumberNew]}
if [ $ProjectNmae_count -ge ${#mobileprovision_New[@]} ];then
echo "\033[33;1m项目数量与证书数量不一致但至少有一个 就用它吧\033[0m"
mobileprovision_name=${mobileprovision_New[0]}
fi
fi
}
# $1 ProjectNmae
# $2 bundleID
# $3 mobileprovision
# $4 build_configuration Debug Release
# $5 method development app-store ad-hoc aenterprise
funPack(){
# echo "所有参数 = $*"
# echo "参数个数 = $#"
# echo "查看方法返回值 = $?"
scheme_name=$1
bundle_identifier=$2
mobileprovision_name=$3
build_configuration=$4
method=$5
echo "\033[32;1m$text\033[0m"
echo "--------------------脚本配置参数检查--------------------"
echo "\033[33;1m"
echo "is_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}"
echo "\033[0m"
# =======================脚本的一些固定参数定义(无特殊情况不用修改)====================== #
# 获取当前脚本所在目录
script_dir="$( cd "$( dirname "$0" )" && pwd )"
# 工程根目录
project_dir=$script_dir
# 下划线
pace="_"
# 年月日
yearStr=`date '+%Y%m%d'`
# 时分秒
timeStr=`date '+%H%M%S'`
# 时间组合体
DATE=$yearStr$pace$timeStr
# 创建的打包文件夹的名字
creatFileNmae="Package"
# 指定输出导出文件夹路径
export_path="$project_dir/$creatFileNmae/$scheme_name$pace$DATE"
# 指定输出归档文件路径
export_archive_path="$export_path/$scheme_name.xcarchive"
# 指定输出ipa文件夹路径
export_ipa_path="$export_path"
# 指定输出ipa名称
ipa_name="${scheme_name}$pace${DATE}"
# 指定导出ipa包需要用到的plist配置文件的路径
export_options_plist_path="$project_dir/ExportOptions.plist"
echo "--------------------脚本固定参数检查--------------------"
echo "\033[33;1m"
echo "project_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}"
echo "\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}秒\033[0m"
}
# # 计算数组元素个数
functionArrCount(){
arr = $1
return ${#arr[@]}
}
# 自动打包
autoPacage(){
echo "-----开始自动配置打包-----"
ProjectNmae_count=${#ProjectNmae_Arr[@]}
for(( i=0;i<${#ProjectNmae_Arr[@]};i++)) do
scheme_name=${ProjectNmae_Arr[i]}
bundle_identifier=${bundleIdentifier_Arr[i]}
val=`expr $i + 1`
echo "\033[33;1m"
if [ $val -gt ${#bundleIdentifier_Arr[@]} ];then
echo "mbundle_identifier 超出取值范围 取第一个"
bundle_identifier=${bundleIdentifier_Arr[0]}
fi
mobileprovision_name=${mobileprovision_auto_Arr[i]}
if [ $val -gt ${#mobileprovision_auto_Arr[@]} ];then
echo "mobileprovision_name 超出取值范围 取第一个"
mobileprovision_name=${mobileprovision_auto_Arr[0]}
fi
build_configuration=${build_configuration_Arr[i]}
if [ $val -gt ${#build_configuration_Arr[@]} ];then
echo "build_configuration 超出取值范围 取第一个"
build_configuration=${build_configuration_Arr[0]}
fi
method=${method_Arr[i]}
if [ $val -gt ${#method_Arr[@]} ];then
echo "method 超出取值范围 取第一个"
method=${method_Arr[0]}
fi
echo "\033[0m"
text="使用文件自动配置"
funPack $scheme_name $bundle_identifier $mobileprovision_name $build_configuration $method
echo "-----自动打包完毕-----"
done;
exit 0
}
# 手动打包
handPacage(){
echo "-----开始手动打包-----"
text="使用手动选择配置"
funPack $scheme_name $bundle_identifier $mobileprovision_name $build_configuration $method
echo "-----手动打包完毕-----"
exit 0
}
# 调用
initProject
if [ $readNumber -eq $AutoPackConfig ];then
autoPacage
fi
if [ $readNumber -ne $AutoPackConfig ];then
handPacage
fi
必须配置
手动证书管理Profile时必填配置
mobileprovision_name查看方式
1.打开终端
2.终端输入cat后面要有一个空格
cat
3.把mobileprovision证书拖进终端
4.回车
5.在里面找吧找到就粘贴上,没找到的继续找直到找打为止
配置好后脚本保存,文件名项目名字就可以,后缀名为.sh
放到xcodeproj同级目录下
配置完成见证奇迹
1.打开终端
2.终端输入sh空格
3.把保存的.sh文件直接拖进终端
4.回车
5.打包开始 完成后会自动打开文件保存ipa文件夹,
如果中途报错检查配置,
如果最后是检出失败,打包已经完成,可以手动检出, 在.sh同级目录下有个 Package 文件夹,里面有个文件夹带有包含项目名的文件夹,打开文件文,打开.xcarchive接下来手动检出就可以了。