原文 : tomczhen的部落格
執行環境
建議根據實際情況安裝:
- 如專案中沒有使用CocoaPods管理類庫則不用安裝CocoaPods;
- xctool是一個很好的構建工具, 有更加友好的log輸出和使用方法. 如使用Xcode自帶的xcodebuild構建則不需要安裝xctool.
安裝 CocoaPods
-
更新Ruby
gem update --system
-
修改Ruby安裝源為國內源
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
注:使用gem sources -l
命令檢視源列表 -
安裝cocoapods
sudo gem install cocoapods
安裝 xctool
-
安裝brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
-
安裝Xcode’s Command Line Tools
xcode-select install
-
使用brew安裝xctool
brew install xctool
使用說明
- 執行指令碼前需要在xcode中配置好證書及描述檔案。
- 如果不需要修改程式碼請註釋掉相關sed語句。
- 預設使用xctool進行構建,需要先安裝好xctool。
- 使用xcodebuild構建需要註釋xctool相關語句,並取消xcodebuild相關語句的註釋。
- 在專案目錄建立指令碼檔案,例如:iosbuild.sh(與.xcworkspace檔案同級目錄),使用命令chmod 777 iosbuild.sh為指令碼檔案增加可執行許可權,執行指令碼會根據配置在EXPORT_PATH中生成ipa檔案並上傳至蒲公英平臺。
指令碼程式碼
#!/bin/sh
set -e
####################
#Project Setting
####################
#project name
PROJECT_NAME=yourprojectname
#workspace name
PROJECT_WORKSPACE_NAME=yourworkspacename
#scheme name
PROJECT_SCHEME_NAME=yourschemename
#profile name
PROFILE_NAME="yourprofilename"
#project Path
PROJECT_PATH=$(pwd)
#build time
BUILD_TIME=$(date +%Y%m%d%H%M)
#project temp path
PROJECT_TEMP_PATH=${PROJECT_PATH}_TEMP
#archive path
ARCHIVE_PATH=${PROJECT_PATH}/build/release/${PROJECT_NAME}_r${BUILD_TIME}
#export path
EXPORT_PATH=${ARCHIVE_PATH}.ipa
#sdk version
SDK_VERSION=iphoneos
######################
#api url
######################
API_SERVER_URL='"http:\/\/api.xxxx.com"'
######################
#third-party key
######################
#thirdpartykey
THIRD_PARTY_KEY='"keyvalue"'
######################
#pgyer key
######################
#pgyer ukey
PGY_UKEY=yourpgyukey
#pgyer api key
PGY_API_KEY=yourpgyapikey
######################
#build
######################
#pod install
pod install --verbose --no-repo-update
#copy project to temp path
echo "copy project files to temp path."
cp -r -f ${PROJECT_PATH} ${PROJECT_TEMP_PATH}
cd ${PROJECT_TEMP_PATH}
#modify code
echo "config api server url."
sed -i "" "/^\/\//!s/.* APIServerUrl.*/NSString * const APIServerUrl= @${API_SERVER_URL};/" yourcodefilepath
echo "config third-party keys."
#rongcloud api key
sed -i "" "/^\/\//!s/.*THIRD_PARTY_KEY.*/#define THIRD_PARTY_KEY @${THIRD_PARTY_KEY}/g" yourcodefilepath
#clean project
echo "clean project."
#xcodebuild -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} clean
xctool -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} clean
#archive project
echo "archive project."
#xcodebuild -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} build archive -archivePath ${ARCHIVE_PATH}
xctool -workspace ${PROJECT_WORKSPACE_NAME}.xcworkspace -scheme ${PROJECT_SCHEME_NAME} build archive -archivePath ${ARCHIVE_PATH}
#delete project temp path
echo "delete project temp path."
cd ${PROJECT_PATH}
rm -r -f ${PROJECT_TEMP_PATH}
#export archive file to ipa
echo "export ipa file to export path."
xcodebuild -exportArchive -archivePath ${ARCHIVE_PATH}.xcarchive -exportPath ${EXPORT_PATH} -exportFormat ipa -exportProvisioningProfile ${PROFILE_NAME}
#upload to pgyer
echo "upload to pgyer."
curl -F "file=@${EXPORT_PATH}" -F "uKey=${PGY_UKEY}" -F "_api_key=${PGY_API_KEY}" http://www.pgyer.com/apiv1/app/upload
echo "upload finish."
複製程式碼