如何通过使用git commit number作为xcode构建号来避免无限循环

时间:2016-02-15 10:44:49

标签: ios xcode git

我最近使用脚本来增加与git提交相关的项目构建号,然后我遇到了无限循环。

我构建,然后更改构建号,然后我提交因为info.plist已更改,然后构建号将在下次构建时更改,所以我必须再次提交它,因为info.plist已更改,那么你知道会发生什么。

有什么方法可以避免这种情况吗?或者有更好的方法来自动增加内部版本号吗?

PS。因为我和其他人一起工作,所以每次修改时手动更换数字或者碰撞它都很难保持这个数字。

3 个答案:

答案 0 :(得分:1)

是的,这是一个经典问题。我使用类似的技术,但我没有将构建号与git提交相关联,而是检测到源树中文件的变化与版本文件的修改时间相关(一个简单的文件)保存版本和内部版本号)。如果有变化,那么我会破坏内部版本号。

这是我使用的python脚本。您应该在Xcode项目中将其作为单独的目标,然后使其他目标依赖于此目标。它将根据需要使用新版本/内部版本号更新您的.plist文件。它假定源树低于版本文件的位置

#!/usr/bin/env python
#
# Bump build number in Info.plist files if a source file have changed.
#
# usage: bump_buildnum.py buildnum.ver Info.plist [ ... Info.plist ]
#
# andy@trojanfoe.com, 2014.
#

import sys, os, subprocess, re

def read_verfile(name):
    version = None
    build = None
    verfile = open(name, "r")
    for line in verfile:
        match = re.match(r"^version\s+(\S+)", line)
        if match:
            version = match.group(1).rstrip()
        match = re.match(r"^build\s+(\S+)", line)
        if match:
            build = int(match.group(1).rstrip())
    verfile.close()
    return (version, build)

def write_verfile(name, version, build):
    verfile = open(name, "w")
    verfile.write("version {0}\n".format(version))
    verfile.write("build {0}\n".format(build))
    verfile.close()
    return True

def set_plist_version(plistname, version, build):
    if not os.path.exists(plistname):
        print("{0} does not exist".format(plistname))
        return False

    plistbuddy = '/usr/libexec/Plistbuddy'
    if not os.path.exists(plistbuddy):
        print("{0} does not exist".format(plistbuddy))
        return False

    cmdline = [plistbuddy,
        "-c", "Set CFBundleShortVersionString {0}".format(version),
        "-c", "Set CFBundleVersion {0}".format(build),
        plistname]
    if subprocess.call(cmdline) != 0:
        print("Failed to update {0}".format(plistname))
        return False

    print("Updated {0} with v{1} ({2})".format(plistname, version, build))
    return True

def should_bump(vername, dirname):
    verstat = os.stat(vername)
    allnames = []
    for dirname, dirnames, filenames in os.walk(dirname):
        for filename in filenames:
            allnames.append(os.path.join(dirname, filename))

    for filename in allnames:
        filestat = os.stat(filename)
        if filestat.st_mtime > verstat.st_mtime:
            print("{0} is newer than {1}".format(filename, vername))
            return True

    return False

def upver(vername):
    (version, build) = read_verfile(vername)
    if version == None or build == None:
        print("Failed to read version/build from {0}".format(vername))
        return False

    # Bump the version number if any files in the same directory as the version file
    # have changed, including sub-directories.
    srcdir = os.path.dirname(vername)
    bump = should_bump(vername, srcdir)

    if bump:
        build += 1
        print("Incremented to build {0}".format(build))
        write_verfile(vername, version, build)
        print("Written {0}".format(vername))
    else:
        print("Staying at build {0}".format(build))

    return (version, build)

if __name__ == "__main__":
    if os.environ.has_key('ACTION') and os.environ['ACTION'] == 'clean':
        print("{0}: Not running while cleaning".format(sys.argv[0]))
        sys.exit(0)

    if len(sys.argv) < 3:
        print("Usage: {0} buildnum.ver Info.plist [... Info.plist]".format(sys.argv[0]))
        sys.exit(1)
    vername = sys.argv[1]

    (version, build) = upver(vername)
    if version == None or build == None:
        sys.exit(2)

    for i in range(2, len(sys.argv)):
        plistname = sys.argv[i]
        set_plist_version(plistname, version, build)        

    sys.exit(0)

答案 1 :(得分:0)

我使用的方法是在文件中包含一个包含版本号的占位符,然后让构建服务器在创建可分发包之前用正确的版本号本地替换该占位符。存储库中的info.plist版本不会更改;它总是有占位符。

答案 2 :(得分:0)

我用纯粹的bash制作了@ trojanfoe的答案。这假设一个名为versions的文件放在根项目目录中。

#!/bin/bash

beginswith() { case $2 in "$1"*) true;; *) false;; esac; }

shopt -s dotglob

versionModifiedAt=$(date -r versions +%s)
needsUpdate=false

processEntry() {
  if [[ "$1" == "versions" ]]; then return; fi
  if [[ "$1" == ".git" ]];     then return; fi
  if beginswith ".git/" "$1";  then return; fi

  if [ $(date -r "$1" +%s) -gt "$versionModifiedAt" ]; then
    needsUpdate=true
  fi
}

# Top level
for entry in *; do
  processEntry "$entry"
  if [[ $needsUpdate == true ]]; then
    break;
  fi
done

# Nested
if [[ $needsUpdate != true ]]; then
  for entry in **/*; do
    processEntry "$entry"
    if [[ $needsUpdate == true ]]; then
      break;
    fi
  done
fi

if [[ $needsUpdate != true ]]; then
  echo "No update needed"
  exit 0
fi
echo "Updating version and build info"


buildPlist=${PROJECT_DIR}/${INFOPLIST_FILE}

# Increment build number
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$buildPlist")
buildNumber=$((0x$buildNumber))
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%04X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$buildPlist"

echo -e "${git_release_version#*v}\n$buildNumber" > versions