美文网首页
软件版本生成办法

软件版本生成办法

作者: 小辛_43ae | 来源:发表于2021-01-11 12:18 被阅读0次

    1.基于编译时间

    #define VERSION_MAJOR 1
    #define VERSION_MINOR 10
    #define VERSION_C 0
    
    std::string VGetVersion()
    {
        char version[32];
        sprintf(version, "V%dR%dC%02d", VERSION_MAJOR, VERSION_MINOR, VERSION_C);
        return std::move(std::string(version));
    }
    

    2.基于宏定义的办法

    直接代码

    std::string VGetDate()
    {
        std::string monthes[] =
        {
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec",
        };
    
        std::string dateStr = __DATE__;
    
        int year = atoi(dateStr.substr(dateStr.length() - 4).c_str());
    
        int month = 0;
        for (int i = 0; i < 12; i++)
        {
            if (dateStr.find(monthes[i]) != string::npos)
            {
                month = i + 1;
                break;
            }
        }
    
        std::string dayStr = dateStr.substr(4, 2);
        int day = atoi(dayStr.c_str());
    
        std::string timeStr = __TIME__;
    
        std::string hourStr = timeStr.substr(0, 2);
        int hour = atoi(hourStr.c_str());
        std::string minStr = timeStr.substr(3, 2);
        int min = atoi(minStr.c_str());
    
        char version[32];
        sprintf(version, "%04d-%02d-%02d %02d:%02d", year, month, day, hour, min);
        //sprintf(version, "V%dR%dC%02dB%04d%02d%02d %02d%02d", VERSION_MAJOR, VERSION_MINOR, VERSION_C, year, month, day, hour, min);
        return std::move(std::string(version));;
    }
    
    
    

    3.基于git版本信息

    对于源代码一直在迭代更新的项目,希望在每次生成项目的时候,都能自动地更新代码中的版本信息,以便测试发现问题的时候,能跟踪到对应版本的源代码。

    思路

    通过脚本调取git命令方式获取git信息,将该信息以宏定义的方式存到.h头文件,项目工程引用该文件。每次编译的时候自动调用该脚本即可完成。

    步骤

    git的安装,Python的安装这里不做介绍。

    1.GitPython 库的安装

    这里利用 python3 来实现,当然也可以用批处理器脚本来做。
    此处需要用到 GitPython 库,打开cmd窗口,使用pip安装:

    pip install GitPython
    

    2.getver.py 文件

    将如下代码保存为getver.py 文件放到所在工程目录中。
    git详细使用方法参考https://git-scm.com/book/zh/v2/
    本章涉及的内容在 2.3 Git 基础 - 查看提交历史

    # -*- coding: utf-8 -*-
    #2.3 Git 基础 - 查看提交历史
    #https://git-scm.com/book/zh/v2/
    from git import Repo
    import tkinter
    import tkinter.messagebox
    # 有未提交更改时提示
    repo = Repo('.')
    commitData = repo.git.log('-1','--pretty=format:%ci')
    commitHash = repo.git.log('-1','--pretty=format:(%h)')
    author = repo.git.log('-1','--pretty=format:%an')
    info = repo.git.log('-1','--pretty=format:%s')
    if repo.is_dirty():
        #a=tkinter.messagebox.askokcancel('提示', '有未提交修改, 确定更新版本信息?')
        #if not a:
        #    print("取消更新")
        #    raise
        commitData += '' 
    fi = open('./gitver.h', 'w', encoding='utf-8')
    fstr = '#ifndef _GITVER_H\n#define _GITVER_H\n'
    
    fstr += '#define GIT_COMMITDATA "' + commitData + '"\n'
    fstr += '#define GIT_COMMITHASH "' + commitHash + '"\n'
    fstr += '#define GIT_AUTHOR "' + author + '"\n'
    fstr += '#define GIT_INFO "' + info + '"\n'
    fstr += '\n#endif'
    fi.write(fstr)
    fi.close()
    print('成功更新git信息')
    print(commitData)
    

    存好文件打开控制台,运行上面python程序,则会在同级目录生成如下的 gitver.h 文件:

    #ifndef _GITVER_H
    #define _GITVER_H
    #define GIT_COMMITDATA "2021-01-08 11:50:01 +0800"
    #define GIT_COMMITHASH "(a0babba)"
    #define GIT_AUTHOR "jhx"
    #define GIT_INFO "Merge branch 'V1R10C00B010' of 192.168.5.4:software/TySrvDevelop into V1R10C00B010"
    
    #endif
    

    3.在项目中包含 gitver.h 头文件,引用版本信息

    #include "gitver.h"
    char* getVersion()
    {
        static char verstr[] = GIT_COMMITDATA;
        return verstr;
    }
    

    4.项目生成前自动运行脚本

    这是实现自动化的关键一步。VS 2015 允许设置在生成项目前要运行的命令,在“项目属性→生成事件→预先生成事件”中:
    注意:
    1.这是两条命令而非一行。
    2.路径的匹配,避免工程找不到py文件。
    3.避免项目引用找不到生成的gitver.h 文件。

    cd ..\..\
    getver.py
    
    image.png
    image.png

    相关文章

      网友评论

          本文标题:软件版本生成办法

          本文链接:https://www.haomeiwen.com/subject/bvyavktx.html