iOS自动更新版本号脚本

由于经常在公司代码持续集成平台构建代码时候忘记更新版本号,经常浪费一次宝贵的(长长的)构建时间,现在Mac用的工具是 Cornerstone,可以支持 commit 前后执行脚本。所以写了这个脚本,在commit之后执行,用于自动更新版本号到svn。
Usage:

1,保证在 terminal 下面可以正常使用 svn 命令提交目标项目代码

2,修改配置常量 TARGET_SRC_ROOT,TARGET_PROJECT_NAMES,TARGET_PLIST_FILES

3,根据自己项目需要修改 generate_version_code 逻辑

4,在Cornerstone 的 commit 界面将本脚本设置为提交后执行


# -*- coding: utf-8 -*-

import os
import xml.etree.cElementTree as etree
import datetime

TARGET_SRC_ROOT = "../src"
TARGET_PROJECT_NAMES = ["Demo"]
TARGET_PLIST_FILES = ["Info1.plist", "Info2.plist"]


def get_version_value_node(root):
    dict_node = root.find("dict")
    if not dict_node:
        return False

    version_key_node = None
    version_value_node = None
    for key_value_node in dict_node:
        text = key_value_node.text
        if text == "CFBundleVersion":
            version_key_node = key_value_node
            continue
        if version_key_node is not None:
            version_value_node = key_value_node
            break

    if version_value_node is None:
        return

    return version_value_node


def replace_version_code(file_path, old_version_code, new_version_code):
    file_content = None
    with open(file_path, "rb") as file:
        file_content = file.read()

    if not isinstance(file_content, str):
        return False

    file_content = file_content.replace(old_version_code, new_version_code)
    with open(file_path, "wb") as file:
        file.write(file_content)

    return True


# 根据旧的版本号生成新的版本号
# 项目要求格式是 YYMMDD + Build,如 16010102
# 表示 2016 年 1 月 1 日 第二次构建的版本
def generate_version_code(old_version_code):
    if not isinstance(old_version_code, str):
        return None

    if len(old_version_code) != 8:
        return None

    date_code = old_version_code[0:6]
    build_code = old_version_code[6:]
    int_build_code = int(build_code)

    today = datetime.date.today()
    new_date_code = today.strftime("%Y%m%d")
    new_date_code = new_date_code[2:]
    if new_date_code == date_code:
        int_build_code += 1
    else:
        int_build_code = 1

    new_build_code = str(int_build_code).zfill(2)
    new_version_code = new_date_code + new_build_code

    return new_version_code


def update_version(plist_file_path):
    if not isinstance(plist_file_path, str):
        return False

    xml_tree = etree.ElementTree(file=plist_file_path)
    if not xml_tree:
        return False

    root = xml_tree.getroot()
    version_value_node = get_version_value_node(root)
    if version_value_node is None:
        return False

    version_code = version_value_node.text
    if not isinstance(version_code, str):
        return False

    new_version_code = generate_version_code(version_code)
    result = replace_version_code(plist_file_path, version_code, new_version_code)

    return result


def file_log(message):
    current_dir = os.path.realpath(os.path.dirname(__file__))
    path = os.path.join(current_dir, "log.txt")
    with open(path, "a+") as f:
        f.write(message)
        f.write("\r\n")


def commit_files(plist_files):
    commit_command = """svn ci -m "* auto update version script" """
    for plist_file in plist_files:
        commit_command += '"' + plist_file + '" '
    os.system(commit_command)
    file_log("exec command " + commit_command)


def main():
    current_dir = os.path.realpath(os.path.dirname(__file__))
    plist_files = []
    for project_name in TARGET_PROJECT_NAMES:
        path = os.path.join(TARGET_SRC_ROOT, project_name)
        path = os.path.join(current_dir, path)
        for plist_file in TARGET_PLIST_FILES:
            plist_path = os.path.join(path, plist_file)
            plist_path = os.path.abspath(plist_path)
            update_version(plist_path)
            plist_files.append(plist_path)
    commit_files(plist_files)


if __name__ == '__main__':
    main()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值