在xocde中选中对应targets ,然后选择 build phases
点击 + ,选择 “add new run script phases”
在展开新加的item,然后在其代码编辑区域输入以下代码:
#!/bin/bash
git=$(sh /etc/profile; which git)
git_release_version=$("$git" describe --tags --always --abbrev=0)
number_of_commits=$("$git" rev-list $git_release_version..HEAD --count)
target_plist="$TARGET_BUILD_DIR/$INFOPLIST_PATH"
dsym_plist="$DWARF_DSYM_FOLDER_PATH/$DWARF_DSYM_FILE_NAME/Contents/Info.plist"
src_plist="./$TARGET_NAME/Info.plist"
for plist in "$src_plist"; do
if [ -f "$plist" ]; then
echo "Start to update build number"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $number_of_commits" "$plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${git_release_version}" "$plist"
fi
done
完成上面的添加后,每次编译时,都会把git里面tag版本号和git的提交次数,分别赋值给App的版本号(CFBundleVersion)和构建本号(git_release_version)