新建一个名为xxxxxx .sh的文件,把下面代码复制进文件内,把文件内的xxxxxx替换成自己工程名字即可,使用方法参考文件内说明
#!/usr/bin/env bash
echo "how to use."
echo "===================="
echo "build for debug"
echo "./ipa.sh"
echo "===================="
echo "build for release"
echo "./ipa.sh --release"
echo "===================="
echo "build for app store"
echo "./ipa.sh --release --app-store"
echo "===================="
sleep 1
#
#
#
#
#
#
# 使用方法:
# step1: 将该脚本放在工程的根目录下(跟.xcworkspace文件or .xcodeproj文件同目录)
# step2: 根据情况修改下面的参数
# step3: 打开终端,执行脚本。(输入sh ,然后将脚本文件拉到终端,会生成文件路径,然后enter就可)
# =============项目自定义部分(自定义好下列参数后再执行该脚本)=================== #
# 是否编译工作空间 (例:若是用Cocopods管理的.xcworkspace项目,赋值true;用Xcode默认创建的.xcodeproj,赋值false)
echo $1 $2
configuration_type=$1
method_type=$2
if [ "$configuration_type" == "--release" ]; then
build_configuration="Release"
else
build_configuration="Debug"
fi
if [ "$method_type" == "--app-store" ]; then
method="app-store"
elif [ "$method_type" == "--ad-hoc" ]; then
method="ad-hoc"
else
method="development"
fi
is_workspace="true"
# .xcworkspace的名字,如果is_workspace为true,则必须填。否则可不填
workspace_name="xxxxxx"
# .xcodeproj的名字,如果is_workspace为false,则必须填。否则可不填
project_name="xxxxxx"
# 指定项目的scheme名称(也就是工程的target名称),必填
scheme_name="xxxxxx"
# 指定要打包编译的方式 : Release,Debug。一般用Release。必填
build_configuration="Release"
# method,打包的方式。方式分别为 development, ad-hoc, app-store, enterprise 。必填
method="ad-hoc"
#compileBitcode选项没有配置,默认compileBitcode为YES,要设置成NO就可以了
compileBitcode="NO"
# 下面两个参数只是在手动指定Pofile文件的时候用到,如果使用Xcode自动管理Profile,直接留空就好
# (跟method对应的)mobileprovision文件名,需要先双击安装.mobileprovision文件.手动管理Profile时必填
mobileprovision_name=""
# 项目的bundleID,手动管理Profile时必填
bundle_identifier=""
# 蒲公英app key
api_key=""
echo "--------------------脚本配置参数检查--------------------"
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}"
# =======================脚本的一些固定参数定义(无特殊情况不用修改)====================== #
# 获取当前脚本所在目录
script_dir="$( cd "$( dirname "$0" )" && pwd )"
# 工程根目录
project_dir=$script_dir
echo $project_dir
echo "工程根目录 :========= $project_dir =============="
echo "--------------------自动增加版本号---------------------"
cd ${project_dir}
#获取当前版本号
get_build_version=`xcodebuild -showBuildSettings -target xxxxxx | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION = '`
old_build_version=$get_build_version
echo "old_build_version = $old_build_version"
#版本号增加
agvtool new-version $(($old_build_version+1))
#输出新版本号
get_current_build_veresion=`xcodebuild -showBuildSettings -target xxxxxx | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION = '`
current_build_veresion=$get_current_build_veresion
echo "current_build_veresion = $current_build_veresion"
# 时间
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}"
echo " ipa_name=$ipa_name"
# 指定导出ipa包需要用到的plist配置文件的路径
export_options_plist_path="$project_dir/ExportOptions.plist"
echo "--------------------脚本固定参数检查--------------------"
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 "----------------------开始构建项目--------------------------------"
# 进入项目工程目录
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 "--------------- 项目构建成功 🚀 🚀 🚀 -----------------"
else
echo "--------------- 项目构建失败 😢 😢 😢 ----------------"
exit 1
fi
echo "------------------------开始导出ipa文件------------------------------"
# 检查是否有dSYM文件
dSYM_path="$export_archive_path/dSYMs/$scheme_name.app.dSYM"
export_dSYM_path="$export_path/$scheme_name.app.dSYM"
if [ -d $dSYM_path ]; then
cp -r $dSYM_path $export_dSYM_path
else
echo "no dSYM file"
echo "dSYM_path=${dSYM_path}"
fi
# 先删除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 :compileBitcode bool ${compileBitcode}" $export_options_plist_path
/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 "--------------- exportArchive ipa包成功,准备进行重命名 ---------------"
else
echo "exportArchive ipa包失败 😢 😢 😢 "
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 "--------------- 导出 ${ipa_name}.ipa 包成功 🎉 🎉 🎉 --------------- "
# open $export_path
else
echo "--------------- 导出 ${ipa_name}.ipa 包失败 😢 😢 😢 --------------- "
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 "使用AutoPackageScript打包总用时: ${SECONDS}s "
#上传ipa到蒲公英
#if [ -f "${export_ipa_path}/${ipa_name}.ipa" ]
#then
# echo '开始上传ipa/apk到蒲公英'
# curl -F "file=@${export_ipa_path}/${ipa_name}.ipa" -F "_api_key=${api_key}" 'http://www.pgyer.com/apiv2/app/upload'
#else
# echo "在目录:${export_ipa_path}/${ipa_name}.ipa 不存在"
#fi
mv "${export_ipa_path}/${ipa_name}.ipa" "${export_ipa_path}/${ipa_name}-${method}-${build_configuration}.ipa"
exit 0