-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-versions.py
executable file
·141 lines (110 loc) · 4.12 KB
/
update-versions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
import re
import sys
import time
def progress_bar(toolbar_width: int):
sleep_time = 1 / toolbar_width
sys.stdout.write('[%s]' % (' ' * toolbar_width))
sys.stdout.flush()
sys.stdout.write('\b' * (toolbar_width + 1))
for i in range(toolbar_width):
time.sleep(sleep_time)
sys.stdout.write('-')
sys.stdout.flush()
sys.stdout.write(']\n')
filename = 'version.cmake'
project_name = 'project-name'
ver_major = 'ver-major'
ver_minor = 'ver-minor'
ver_patch = 'ver-patch'
key_regex = {(project_name, r'\Aset\((ROBOCIN_PROJECT_NAME\s)(.+)\)'),
(ver_major, r'\Aset\((ROBOCIN_PROJECT_VERSION_MAJOR\s)(.+)\)'),
(ver_minor, r'\Aset\((ROBOCIN_PROJECT_VERSION_MINOR\s)(.+)\)'),
(ver_patch, r'\Aset\((ROBOCIN_PROJECT_VERSION_PATCH\s)(.+)\)')}
value = dict()
with open(filename, 'r+') as file:
for line in file:
for (key, regex) in key_regex:
r = re.compile(regex)
if r.match(line):
value[key] = r.match(line).group(2)
assert (len(value) == 4)
original_message = 'RoboCIn\'s ' + value[project_name] + ' v' + value[ver_major] + \
'.' + value[ver_minor] + \
'.' + value[ver_patch]
colored_message = '\033[32m' + 'RoboCIn\'s ' + '\033[39m' + '\033[1m' + \
value[project_name] + '\033[0m' + ' v' + \
'\033[1m' + value[ver_major] + '\033[0m' + '.' + \
'\033[1m' + value[ver_minor] + '\033[0m' + '.' + \
'\033[1m' + value[ver_patch] + '\033[0m'
print(colored_message)
progress_bar(len(original_message) - 2)
version = None
if len(sys.argv) >= 2:
version = sys.argv[1].lower()
if version not in ['major', 'minor', 'patch']:
print('Leaving without changes.')
exit(0)
else:
try:
version = input('Which version do you want to update [major/minor/patch]?\n').lower()
except (EOFError, KeyboardInterrupt) as exception:
print('Leaving without changes.')
exit(0)
while version not in ['major', 'minor', 'patch']:
try:
version = input('Sorry, try again.\n').lower()
except (EOFError, KeyboardInterrupt) as exception:
print('Leaving without changes.')
exit(0)
print('The project version ' + '\033[1m' + version + '\033[0m' + ' will be updated:')
new_major = value[ver_major]
new_minor = value[ver_minor]
new_patch = value[ver_patch]
print('\033[1m' + 'v' + value[ver_major] + '.' + value[ver_minor] + '.' + value[ver_patch] + '\033[0m', end=' -> ')
if version == "major":
new_major = str(int(value[ver_major]) + 1)
new_minor = '0'
new_patch = '0'
elif version == "minor":
new_minor = str(int(value[ver_minor]) + 1)
new_patch = '0'
elif version == "patch":
new_patch = str(int(value[ver_patch]) + 1)
print('\033[1m' + 'v' + new_major + '.' + new_minor + '.' + new_patch + '\033[0m')
option = None
try:
option = input('Do you want to continue [Y/n]?\n').lower()
except (EOFError, KeyboardInterrupt) as exception:
print('Leaving without changes.')
exit(0)
while option not in ['y', 'n']:
try:
option = input('Sorry, try again.\n').lower()
except (EOFError, KeyboardInterrupt) as exception:
print('Leaving without changes.')
exit(0)
if option == 'n':
print('Leaving without changes.')
exit(0)
with open(filename, 'r+') as file:
lines = file.read().splitlines()
for (index, line) in enumerate(lines):
for (key, regex) in key_regex:
r = re.compile(regex)
if r.match(line):
new_value = None
if key == ver_major:
new_value = new_major
elif key == ver_minor:
new_value = new_minor
elif key == ver_patch:
new_value = new_patch
else:
continue
lines[index] = 'set(' + r.match(line).group(1) + new_value + ')'
file.seek(0)
file.write('\n'.join(lines) + '\n')
file.truncate()
progress_bar(len(original_message) - 2)
print('The project version has been updated successfully.')