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
10import os
11
12__author__ = "Markus Diem"
13__credits__ = ["Markus Diem"]
14__license__ = "GPLv3"
15__version__ = "0.2"
16__maintainer__ = "Markus Diem"
17__email__ = "markus@nomacs.org"
18__status__ = "Production"
19
20
21OUTPUT_NAME = "versionincrement"
22
23def increment(line: str, newversion: str):
24
25    str_ver = line.split(":")
26
27    # searching version.cache: version:0.3.5
28    if "version" in line and len(str_ver) == 2:
29
30        old_v = str_ver[1].split(".")
31
32        if len(old_v) == 3:
33            # increment
34            build = str(int(old_v[-1])+1)
35
36        line = str_ver[0] + ":" + newversion + "." + build
37
38        # send status message only once
39        print("[Version Incrementer] " + line.replace("version:", "version updated: "))
40
41    return line
42
43
44def update_version(version: str):
45    from shutil import move
46    from os import remove
47
48    from utils.fun import version_cache
49
50    filepath = version_cache()
51    tmppath = filepath + "tmp"
52
53    if os.path.exists(filepath):
54        with open(filepath, "r") as src:
55            with open(tmppath, "w") as dst:
56                for l in src.readlines():
57
58                    l = increment(l, version)
59                    dst.write(l)
60
61        # swap
62        remove(filepath)
63        move(tmppath, filepath)
64
65    else:
66        with open(filepath, "w") as dst:
67            dst.write("version:" + version)
68
69
70if __name__ == "__main__":
71    import argparse
72
73    parser = argparse.ArgumentParser(
74       description='Increments the version in a version cache file.')
75
76    parser.add_argument("version", type=str,
77                        help="""current nomacs version - typically optained from cmake""")
78
79    args = parser.parse_args()
80
81    update_version(args.version)
82