试图自动增加内部版本号xCode(Unix脚本)

时间:2014-07-31 13:52:32

标签: ios xcode bash unix

我正在尝试编写一个脚本,以便在每次运行时自动增加xCode内部版本号。但是下面的代码不起作用 - 内部版本号很快变得很大,目前是-9077426947807241788(!)。

有人可以告诉我哪里出错吗?

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber))
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%04d" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

## Update the build date
buildDate=$(date -u +"%T %d %b %Y")
/usr/libexec/PlistBuddy -c "Set :CFBuildDate $buildDate" "${SRCROOT}/${INFOPLIST_FILE}"

5 个答案:

答案 0 :(得分:0)

你可以使用这个,它的工作

if [ $# -ne 1 ]; then
echo usage: $0 plist-file
exit 1
fi

plist=$1
dir=$(dirname "$plist")

# Only increment the build number if source files have changed
if [ -n "$(find "$dir" \! -path "*xcuserdata*" \! -path "*.hg" \! -path "*.hgcheck" \! -path "*.hgignore" \! -path "*.hglf" \! -path "Docs/html" -newer "$plist")" ]; then
buildnum=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$plist")
if [ -z "$buildnum" ]; then
echo "No build number in $plist"
exit 2
fi
buildnum=$(expr $buildnum + 1)
/usr/libexec/Plistbuddy -c "Set CFBundleVersion $buildnum" "$plist"
CFBuildDate=$(date "+%m-%d-%Y %H:%M:%S o\'clock")
/usr/libexec/PlistBuddy -c "Set :CFBuildDate $CFBuildDate" "$plist"
echo "Incremented build number to $buildnum with build date: CFBuildDate"
else
echo "Not incrementing build number as source files have not changed"
fi

答案 1 :(得分:0)

下面的脚本会自动从包含项目的Git存储库中设置Xcode项目的版本短版字符串。

要在Xcode中使用它,请将内容添加到应用程序目标的Run Script构建阶段。

脚本

#!/bin/bash

if [ "${CONFIGURATION}" != "Release" ]; then

set -o errexit
set -o nounset

# Get info.plist path
INFO_PLIST="${BUILT_PRODUCTS_DIR}/${WRAPPER_NAME}/Info"
APP_SHORT_VERSION_PLIST=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFOPLIST_FILE}"`
DOT="."

# Get git commit number from HEAD's branch
VERSION=$(git --git-dir="${PROJECT_DIR}/.git" --work-tree="${PROJECT_DIR}/" rev-list HEAD | wc -l)

# Trim spaces
VERSION=$(echo "${VERSION}" | sed -e 's/^[ \t]*//')

# Concatenate short version and commit count
VERSION=$APP_SHORT_VERSION_PLIST$DOT$VERSION

# DEBUG
echo "VERSION: ${VERSION}"

# Write to plist
defaults write $INFO_PLIST CFBundleVersion $VERSION

fi

答案 2 :(得分:0)

您必须验证plist是否具有CFBundleVersion。否则,这将在您的问题中给出错误的数字。这是我的实施:

############
#!/bin/bash
plist="$INFOPLIST_FILE"
dir="$(dirname "$plist")"

buildnum=$(/usr/libexec/Plistbuddy -c "Print CFBundleVersion" "$plist")
if [ -z "$buildnum" ]; then
echo "No build number in $plist, set to 1"
/usr/libexec/PlistBuddy -c "Add :CFBundleVersion string 1" ${plist}
else
buildnum=$(expr $buildnum + 1)
/usr/libexec/Plistbuddy -c "Set CFBundleVersion $buildnum" "$plist"
echo "Incremented build number to $buildnum"

fi

答案 3 :(得分:0)

你有很多“深刻”和“权威”的答案。如果我可以天真地加我的两分钱:

buildNumber=$(printf "%04d" $buildNumber)
#                     ^^^^
#                 output as a *decimal* number with 4 digits left-padded with 0

根据我上面读到的两行,构建号用十六进制表示。正确的格式应为%04X

缺少转换可能会解释观察到的行为:例如,如果当前版本号为0010 16 。您下一个号码目前是0017 16 而不是0011 16 。等等(事情变得非常快!)

答案 4 :(得分:0)

查看脚本,您只需删除以下行

buildNumber = $((0X $ buildNumber))