当我们在Xcode里面Build App的时候,可以在Bulid同时运行一些sh脚本。
进入Build Phases页面,点击加号选择“New Run Script Phases”创建Run Script
和一般的shell没有太大的区别,但是多了很多环境变量。
可以添加一个 Run Script,添加一条命令。
env
可以在脚本中直接使用这些变量。
// bundle id PRODUCT_BUNDLE_IDENTIFIER // info.plist 的位置 INFOPLIST_PATH // 工程根目录 SRCROOT
输出这些环境变量,结果如下(通过 Xcode -> View -> Navigators -> Show the Report navigator 查看)
常用的 Run Script
- Build Number 自增
if [ $CONFIGURATION == Release ]; then echo "Bumping build number..." plist=${PROJECT_DIR}/${INFOPLIST_FILE} #increment the build number (ie 115 to 116) buildnum=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${plist}") if [[ "${buildnum}" == "" ]]; then echo "No build number in $plist" exit 2 fi buildnum=$(expr $buildnum + 1) /usr/libexec/Plistbuddy -c "Set CFBundleVersion $buildnum" "${plist}" echo "Bumped build number to $buildnum" else echo $CONFIGURATION " build - Not bumping build number." fi
- 打包上传时移除第三方库中无用的部分
# Without further ado, here’s the script. Add a Run Script step to your build steps, put it after your step to embed frameworks, set it to use /bin/sh and enter the following script: APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}" # This script loops through the frameworks embedded in the application and # removes unused architectures. find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK do FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable) FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME" echo "Executable is $FRAMEWORK_EXECUTABLE_PATH" EXTRACTED_ARCHS=() for ARCH in $ARCHS do echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME" lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH" EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH") done echo "Merging extracted architectures: ${ARCHS}" lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}" rm "${EXTRACTED_ARCHS[@]}" echo "Replacing original executable with thinned version" rm "$FRAMEWORK_EXECUTABLE_PATH" mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH" done
- build时删除一个目录
# Type a script or drag a script file from your workspace to insert its path. APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}" echo "${TARGET_BUILD_DIR}/${WRAPPER_NAME}/UIDesign" # rm -rf "${TARGET_BUILD_DIR}/${WRAPPER_NAME}/UIDesign"