-
Notifications
You must be signed in to change notification settings - Fork 0
/
bumpversion.py
151 lines (112 loc) · 4.07 KB
/
bumpversion.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
142
143
144
145
146
147
148
149
150
151
import json
import sys
from datetime import date
import re
def throw_error(text):
print("❌ ERROR: " + text)
sys.exit(1)
def detect_version(argv):
if len(argv) > 1:
version_string = argv[1]
version_string = version_string.lower()
print('Version in argv: "{}"'.format(repr(version_string)))
if (
not (re.match("^[0-9]+\.[0-9]+\.[0-9]+$", version_string) is None)
or version_string == "nightly"
):
valid = True
else:
valid = False
if not (valid):
throw_error("Version doesn't match required format")
print('✅ Detected version: VERSION "{}"'.format(version_string))
if version_string == "nightly":
return "nightly.{}".format(str(date.today()))
else:
return_version = list(map(int, version_string.split(".")))
return "{}.{}.{}".format(
return_version[0], return_version[1], return_version[2]
)
else:
throw_error("No command line argument detected")
def new_version_greater(new_ver, old_ver):
if (
not re.match(
"^[0-9]+\.[0-9]+\.[0-9]+\+nightly\.[0-9]{4}-[0-9]{2}-[0-9]{2}$", old_ver
)
is None
):
old_ver = old_ver[: old_ver.find("+")]
new_ver = list(map(int, new_ver.split(".")))
old_ver = list(map(int, old_ver.split(".")))
for i in range(0, 3):
if new_ver[i] > old_ver[i]:
return True
return False
def get_new_version(new_ver, old_ver, nightly):
if nightly is True:
if (
re.match(
"^[0-9]+\.[0-9]+\.[0-9]+\+nightly\.[0-9]{4}-[0-9]{2}-[0-9]{2}$", old_ver
)
is None
):
return "{}+{}".format(old_ver, new_ver)
else:
return "{}+{}".format(old_ver[: old_ver.find("+")], new_ver)
else:
return new_ver
def bump_package_json(version, nightly):
print("> Loading package.json")
with open("./package.json", "r") as f:
packagejson_dict = json.load(f)
packagejson_ver = packagejson_dict["version"]
print("Old Version: " + packagejson_ver)
version = get_new_version(version, packagejson_ver, nightly)
print("New Version: " + version)
if nightly is False:
check_version = new_version_greater(version, packagejson_ver)
else:
check_version = True
if not (check_version):
throw_error("New version is <= old version")
packagejson_dict["version"] = version
print("> Writing to package.json")
with open("./package.json", "w") as f:
f.write(json.dumps(packagejson_dict, indent=2) + "\n")
print("✅ SUCCESS")
def bump_package_lock_json(version, nightly):
print("\n> Loading package-lock.json")
with open("./package-lock.json", "r") as f:
packagelockjson_dict = json.load(f)
packagelockjson_ver = packagelockjson_dict["version"]
print("Old Version: " + packagelockjson_ver)
version = get_new_version(version, packagelockjson_ver, nightly)
print("New Version: " + version)
if nightly is False:
check_version = new_version_greater(version, packagelockjson_ver)
else:
check_version = True
if not (check_version):
throw_error("New version is <= old version")
packagelockjson_dict["version"] = version
packagelockjson_dict["packages"][""]["version"] = version
print("> Writing to package-lock.json")
with open("./package-lock.json", "w") as f:
f.write(json.dumps(packagelockjson_dict, indent=2) + "\n")
print("✅ SUCCESS")
def bump_version(version, nightly):
bump_package_json(version, nightly)
bump_package_lock_json(version, nightly)
if __name__ == "__main__":
print("### Robot Coral CI ###\n")
print("---Loading new version number---")
version = detect_version(sys.argv)
print("\n---Bumping versions---")
print("{}\n".format(version))
if version[:7] == "nightly":
bump_version(version, True)
else:
bump_version(version, False)
print("\n----------")
print("\n✅ SUCCESS")