參考 PPAutoPackageScript 基于 AutoPacking-iOS 修改
使用方法AutoPackageDemo
#!/bin/sh
# 該腳本使用方法
# 源碼地址:https://github.com/stackhou
# step 1. 在工程根目錄新建Shell文件夾,在該文件夾中新建文件autopacking.sh,將該腳本復(fù)制到autopacking.sh文件并保存(或者直接復(fù)制該文件);
# step 2. 配置該腳本;
# step 2. cd 該腳本目錄鳍征,運(yùn)行chmod +x autopacking.sh;
# step 3. 終端運(yùn)行 sh autopacking.sh;
# step 4. 選擇不同選項....
# step 5. Success ?? ?? ??!
# ************************* 配置 Start ********************************
# 上傳到蒲公英
__PGYER_U_KEY="44a10b*******************************4e"
__PGYER_API_KEY="b465*******************************1ed"
# 上傳到 Fir
#__FIR_API_TOKEN="xKKdjdldlodeikK626266skdkkddK"
# 指定項目的scheme名稱
__SCHEME_NAME="AutoPackageDemo"
# 指定打包編譯的模式,如:Release, Debug...
__BUILD_CONFIGURATION="Release"
# 工程類型(.xcworkspace項目,賦值true; .xcodeproj項目, 賦值false)
__IS_WORKSPACE="true"
# AdHoc, AppStore, Enterprise, Development
echo "\033[36;1m請選擇打包方式(輸入序號, 按回車即可) \033[0m"
echo "\033[33;1m1. AdHoc \033[0m"
echo "\033[33;1m2. AppStore \033[0m"
echo "\033[33;1m3. Enterprise \033[0m"
echo "\033[33;1m4. Development \033[0m\n"
# 讀取用戶輸入并存到變量里
read parameter
sleep ${__SLEEP_TIME}
__BUILD_METHOD="${parameter}"
# 判讀用戶是否有輸入
if [[ "${__BUILD_METHOD}" == "1" ]]; then
ExportOptionsPlistPath="./AutoPackage/AdHocExportOptionsPlist.plist"
elif [[ "${__BUILD_METHOD}" == "2" ]]; then
ExportOptionsPlistPath="./AutoPackage/AppStoreExportOptionsPlist.plist"
elif [[ "${__BUILD_METHOD}" == "3" ]]; then
ExportOptionsPlistPath="./AutoPackage/EnterpriseExportOptionsPlist.plist"
elif [[ "${__BUILD_METHOD}" == "4" ]]; then
ExportOptionsPlistPath="./AutoPackage/DevelopmentExportOptionsPlist.plist"
else
echo "${__LINE_BREAK_LEFT} 您輸入的打包方式參數(shù)無效!!! ${__LINE_BREAK_RIGHT}"
exit 1
fi
# 選擇內(nèi)測網(wǎng)站 用Fir或者pgyer
echo "\033[36;1m請選擇ipa內(nèi)測發(fā)布平臺 (輸入序號, 按回車即可) \033[0m"
echo "\033[33;1m1. None \033[0m"
echo "\033[33;1m2. Pgyer \033[0m"
echo "\033[33;1m3. Fir \033[0m"
echo "\033[33;1m4. Pgyer and Fir \033[0m\n"
# 讀取用戶輸入并存到變量里
read parameter
sleep ${__SLEEP_TIME}
__UPLOAD_TYPE_SELECTED="${parameter}"
# 成功出包后是否自動打開文件夾
__IS_AUTO_OPENT_FILE="true"
# 配置完畢開始打包
echo "\033[36;1m配置完畢開始打包 \033[0m"
# ===============================自動打包部分=============================
echo "${__LINE_BREAK_LEFT} 使用打包配置文件路徑=${ExportOptionsPlistPath} ${__LINE_BREAK_RIGHT}"
# 打包計時
__CONSUME_TIME=0
# 回退到工程目錄
cd ../
__PROGECT_PATH=`pwd`
echo "${__LINE_BREAK_LEFT} 進(jìn)入工程目錄=${__PROGECT_PATH} ${__LINE_BREAK_RIGHT}"
# 獲取項目名稱
__PROJECT_NAME=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'`
# 已經(jīng)指定Target的Info.plist文件路徑
__CURRENT_INFO_PLIST_NAME="Info.plist"
# 獲取 Info.plist 路徑
__CURRENT_INFO_PLIST_PATH="${__PROJECT_NAME}/${__CURRENT_INFO_PLIST_NAME}"
# 當(dāng)前的plist文件路徑
echo "${__LINE_BREAK_LEFT} 當(dāng)前Info.plist路徑= ${__CURRENT_INFO_PLIST_PATH} ${__LINE_BREAK_RIGHT}"
# 獲取版本號
__BUNDLE_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${__CURRENT_INFO_PLIST_PATH}`
# 獲取編譯版本號
__BUNDLE_BUILD_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${__CURRENT_INFO_PLIST_PATH}`
# 打印版本信息
echo "${__LINE_BREAK_LEFT} 打包版本=${__BUNDLE_VERSION} 編譯版本=${__BUNDLE_BUILD_VERSION} ${__LINE_BREAK_RIGHT}"
# 編譯生成文件目錄
__EXPORT_PATH="./build"
# 指定輸出文件目錄不存在則創(chuàng)建
if test -d "${__EXPORT_PATH}" ; then
echo "${__LINE_BREAK_LEFT} 保存歸檔文件和ipa的路徑=${__EXPORT_PATH} ${__LINE_BREAK_RIGHT}"
rm -rf ${__EXPORT_PATH}
else
mkdir -pv ${__EXPORT_PATH}
fi
# 歸檔文件路徑
__EXPORT_ARCHIVE_PATH="${__EXPORT_PATH}/${__SCHEME_NAME}.xcarchive"
# ipa 導(dǎo)出路徑
__EXPORT_IPA_PATH="${__EXPORT_PATH}"
# 獲取時間 如:201706011145
__CURRENT_DATE="$(date +%Y%m%d_%H%M%S)"
# ipa 名字
__IPA_NAME="${__SCHEME_NAME}_V${__BUNDLE_BUILD_VERSION}_${__CURRENT_DATE}"
echo "${__LINE_BREAK_LEFT} 打包APP名字=${__IPA_NAME} ${__LINE_BREAK_RIGHT}"
# 修改編輯版本
#__SET_BUNDLE_BUILD_VERSION="${__BUNDLE_BUILD_VERSION}.${__CURRENT_DATE}"
#/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${__SET_BUNDLE_BUILD_VERSION}" "${__CURRENT_INFO_PLIST_PATH}"
echo "${__LINE_BREAK_LEFT} 開始構(gòu)建項目 ${__LINE_BREAK_RIGHT}"
if ${__IS_WORKSPACE} ; then
#echo "${__LINE_BREAK_LEFT} 開始pod ${__LINE_BREAK_RIGHT}"
#pod install --verbose --no-repo-update
#echo "${__LINE_BREAK_LEFT} pod完成 ${__LINE_BREAK_RIGHT}"
if [[ ${__BUILD_CONFIGURATION} == "Debug" ]]; then
echo "${__LINE_BREAK_LEFT} 您選擇了以 xcworkspace-Debug 模式打包 ${__LINE_BREAK_RIGHT}"
# step 1. Clean
xcodebuild clean -workspace ${__PROJECT_NAME}.xcworkspace \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION}
# step 2. Archive
xcodebuild archive -workspace ${__PROJECT_NAME}.xcworkspace \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-archivePath ${__EXPORT_ARCHIVE_PATH} \
CFBundleVersion=${__BUNDLE_BUILD_VERSION} \
-destination generic/platform=ios
elif [[ ${__BUILD_CONFIGURATION} == "Release" ]]; then
echo "${__LINE_BREAK_LEFT} 您選擇了以 xcworkspace-Release 模式打包 ${__LINE_BREAK_RIGHT}"
# step 1. Clean
xcodebuild clean -workspace ${__PROJECT_NAME}.xcworkspace \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION}
# step 2. Archive
xcodebuild archive -workspace ${__PROJECT_NAME}.xcworkspace \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-archivePath ${__EXPORT_ARCHIVE_PATH} \
CFBundleVersion=${__BUNDLE_BUILD_VERSION} \
-destination generic/platform=ios
else
echo "${__LINE_BREAK_LEFT} 您輸入的參數(shù)不對 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
echo "Usage:\n"
echo "sh autopacking.sh"
echo "sh autopacking.sh"
exit 1
fi
else
if [[ ${__BUILD_CONFIGURATION} == "Debug" ]] ; then
echo "${__LINE_BREAK_LEFT}您選擇了以 xcodeproj-Debug 模式打包 ${__LINE_BREAK_RIGHT}"
# step 1. Clean
xcodebuild clean -project ${__PROJECT_NAME}.xcodeproj \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-alltargets
# step 2. Archive
xcodebuild archive -project ${__PROJECT_NAME}.xcodeproj \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-archivePath ${__EXPORT_ARCHIVE_PATH} \
CFBundleVersion=${__BUNDLE_BUILD_VERSION} \
-destination generic/platform=ios
elif [[ ${__BUILD_CONFIGURATION} == "Release" ]]; then
echo "${__LINE_BREAK_LEFT} 您選擇了以 xcodeproj-Release 模式打包 ${__LINE_BREAK_RIGHT}"
# step 1. Clean
xcodebuild clean -project ${__PROJECT_NAME}.xcodeproj \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-alltargets
# step 2. Archive
xcodebuild archive -project ${__PROJECT_NAME}.xcodeproj \
-scheme ${__SCHEME_NAME} \
-configuration ${__BUILD_CONFIGURATION} \
-archivePath ${__EXPORT_ARCHIVE_PATH} \
CFBundleVersion=${__BUNDLE_BUILD_VERSION} \
-destination generic/platform=ios
else
echo "${__LINE_BREAK_LEFT} 您輸入的參數(shù)不對 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
echo "Usage:\n"
echo "sh autopacking.sh"
echo "sh autopacking.sh"
exit 1
fi
fi
# 檢查是否構(gòu)建成功
# xcarchive 實(shí)際是一個文件夾不是一個文件所以使用 -d 判斷
if test -d "${__EXPORT_ARCHIVE_PATH}" ; then
echo "${__LINE_BREAK_LEFT} 項目構(gòu)建成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
else
echo "${__LINE_BREAK_LEFT} 項目構(gòu)建失敗 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
exit 1
fi
echo "${__LINE_BREAK_LEFT} 開始導(dǎo)出ipa文件 ${__LINE_BREAK_RIGHT}"
xcodebuild -exportArchive -archivePath ${__EXPORT_ARCHIVE_PATH} \
-exportPath ${__EXPORT_IPA_PATH} \
-destination generic/platform=ios \
-exportOptionsPlist ${ExportOptionsPlistPath} \
-allowProvisioningUpdates
# 修改ipa文件名稱
mv ${__EXPORT_IPA_PATH}/${__SCHEME_NAME}.ipa ${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa
# 檢查文件是否存在
if test -f "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa" ; then
echo "${__LINE_BREAK_LEFT} 導(dǎo)出 ${__IPA_NAME}.ipa 包成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
if test -n "${__UPLOAD_TYPE_SELECTED}"
then
if [[ "${__UPLOAD_TYPE_SELECTED}" == "1" ]] ; then
echo "${__LINE_BREAK_LEFT} 您選擇了不上傳到內(nèi)測網(wǎng)站 ${__LINE_BREAK_RIGHT}"
elif [[ "${__UPLOAD_TYPE_SELECTED}" == "2" ]]; then
curl -F "file=@${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa" \
-F "uKey=$__PGYER_U_KEY" \
-F "_api_key=$__PGYER_API_KEY" \
"http://www.pgyer.com/apiv1/app/upload"
echo "${__LINE_BREAK_LEFT} 上傳 ${__IPA_NAME}.ipa 包 到 pgyer 成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
elif [[ "${__UPLOAD_TYPE_SELECTED}" == "3" ]]; then
fir login -T ${__FIR_API_TOKEN}
fir publish "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa"
echo "${__LINE_BREAK_LEFT} 上傳 ${__IPA_NAME}.ipa 包 到 fir 成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
elif [[ "${__UPLOAD_TYPE_SELECTED}" == "4" ]]; then
fir login -T ${__FIR_API_TOKEN}
fir publish "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa"
echo "${__LINE_BREAK_LEFT} 上傳 ${__IPA_NAME}.ipa 包 到 fir 成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
curl -F "file=@{${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa}" \
-F "uKey=$__PGYER_U_KEY" \
-F "_api_key=$__PGYER_API_KEY" \
"http://www.pgyer.com/apiv1/app/upload"
echo "${__LINE_BREAK_LEFT} 上傳 ${__IPA_NAME}.ipa 包 到 pgyer 成功 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
else
echo "${__LINE_BREAK_LEFT} 您輸入 上傳內(nèi)測網(wǎng)站 參數(shù)無效!!! ${__LINE_BREAK_RIGHT}"
exit 1
fi
# 自動打開文件夾
if ${__IS_AUTO_OPENT_FILE} ; then
open ${__EXPORT_IPA_PATH}
fi
fi
else
echo "${__LINE_BREAK_LEFT} 導(dǎo)出 ${__IPA_NAME}.ipa 包失敗 ?? ?? ?? ${__LINE_BREAK_RIGHT}"
exit 1
fi
# 輸出打包總用時
echo "${__LINE_BREAK_LEFT} 使用YJShell腳本打包總耗時: ${SECONDS}s ${__LINE_BREAK_RIGHT}"