1#!/usr/bin/env python
2"""Increments the build version of a C++ projects.
3
4This script increments the build version after every build of
5a Visual Stuido project. It manipulates the DkVersion.h file in order
6to do so. The version number defined there is displayed in help and
7used for the VS_VERSION_INFO in the *.rc file
8"""
9import logging
10
11__author__ = "Markus Diem"
12__credits__ = ["Markus Diem"]
13__license__ = "GPLv3"
14__version__ = "0.3"
15__maintainer__ = "Markus Diem"
16__email__ = "markus@nomacs.org"
17__status__ = "Production"
18
19
20OUTPUT_NAME = "versionupdate"
21
22
23def update(filepath: str, copy: bool = False):
24    from shutil import move
25    from os import remove
26    from utils.fun import version
27
28    v = version()
29
30    dstpath, ext = os.path.splitext(filepath)
31    dstpath += "-versioned" + ext
32
33    with open(filepath, "r") as src:
34        with open(dstpath, "w") as dst:
35
36            for l in src.readlines():
37
38                l = update_version_string(v, l)
39                l = update_version_rc(v, l)
40
41                l = add_git_tag_string(l)
42
43                dst.write(l)
44
45    # replace current file?
46    if not copy:
47        remove(filepath)
48        move(dstpath, filepath)
49
50
51def update_version_string(version: str, line: str):
52
53    # searching (DkVersion.h): #define NOMACS_VERSION_STR "3.14.42\0"
54    # searching (msi installer): <?define ProductVersion = "3.14.42"?>
55    # searching (inno installer): define MyAppVersion "3.14.42"
56    if "NOMACS_VERSION_STR" in line or \
57        "<?define ProductVersion" in line or \
58        "define MyAppVersion" in line:
59
60        str_ver = line.split("\"")
61        line = str_ver[0] + "\"" + version + "\"" + str_ver[-1]
62
63    return line
64
65def update_version_rc(version: str, line: str):
66
67    # searching: #define NOMACS_VERSION_RC 3,14,42
68    if "NOMACS_VERSION_RC" in line:
69
70        str_ver = line.split(" ")
71        str_ver[-1] = version.replace(".", ",")
72
73        line = " ".join(str_ver) + "\n"
74
75    return line
76
77def add_git_tag_string(line: str):
78
79    # searching: #define NOMACS_GIT_TAG "4add4f1f6b6c731a9f4cf63596e087d4f68c2aed"
80    if "NOMACS_GIT_TAG" in line:
81
82        line = line.replace("\n", "")
83
84        v = line.split("\"")
85
86        if len(v) == 3:
87            v[-2] = git_tag()
88
89        line = "\"".join(v) + "\n"
90
91    return line
92
93def git_tag():
94    import subprocess
95
96    tag = subprocess.check_output(["git", "rev-parse", "HEAD"])
97    tag = tag.strip().decode("utf-8")
98
99    return tag
100
101if __name__ == "__main__":
102    import argparse
103    import os
104    from utils.fun import mypath
105
106    parser = argparse.ArgumentParser(
107       description='Increments the build version of a C++ project and adds the git rev as product version.')
108
109
110    parser.add_argument("inputfile", type=str,
111                        help="""full path to the file who's version should be updated""")
112    parser.add_argument("--copy", action='store_true',
113                        help="""if set, a _ver file will be created""")
114
115    args = parser.parse_args()
116
117    if not os.path.isfile(args.inputfile):
118        print("input file does not exist: " + args.inputfile)
119        exit()
120
121    update(args.inputfile, args.copy)
122