-
Notifications
You must be signed in to change notification settings - Fork 56
/
bump-version.py
85 lines (71 loc) · 3.58 KB
/
bump-version.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
#!/usr/bin/env python3
# Copyright (c) 2023 Sebastian Thomschke and others.
# SPDX-License-Identifier: EPL-2.0
import re, os, sys
MIN_PYTHON_VERSION = (3, 10)
if sys.version_info < MIN_PYTHON_VERSION:
print(f'ERROR: This script requires at least Python {".".join(map(str, MIN_PYTHON_VERSION))} but found Python {".".join(map(str, sys.version_info[0:2]))}.')
sys.exit(1)
if not len(sys.argv) == 2:
print("Recursively bumps the project version in all pom.xml, feature.xml and META-INF/MANIFEST.MF files")
print()
print("USAGE: python bump-version.py (major|minor|micro)")
sys.exit(1)
def increment_version(version:str, level:str):
match = re.match(r'^(\d+)\.(\d+)\.(\d+)$', version)
assert match, f'Unparseable version: {version}'
major = int(match.group(1))
minor = int(match.group(2))
patch = int(match.group(3))
match level.casefold():
case 'major':
major += 1
minor = 0
patch = 0
case 'minor':
minor += 1
patch = 0
case 'micro' | 'patch':
patch += 1
case _:
raise RuntimeError(f'Unknown version upgrade level: {level}. Available are: major, minor, patch')
return f'{major}.{minor}.{patch}'
THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
print(f"Determine current version...")
with open(os.path.join(THIS_FILE_DIR, "pom.xml"), "rt", encoding = 'utf-8') as fh:
content = fh.read()
old_version = re.search(r"<version>(\d+\.\d+\.\d+)-SNAPSHOT</version", content).group(1)
assert old_version, "Cannot determine version in root pom.xml"
print(f"Current version: {old_version}")
new_version = increment_version(old_version, sys.argv[1])
print(f"New version: {new_version}")
for root, dirs, files in os.walk(THIS_FILE_DIR):
dirs[:] = [d for d in dirs if not d.startswith('.') and not d == "target" ] # exclude hidden dirs and "target" dirs
for file in files:
match file:
case 'pom.xml':
filepath = os.path.join(root, file)
print(f"Updating [{filepath}]...")
with open(filepath, "rt", encoding = 'utf-8') as fh:
content = fh.read()
content = content.replace(f"<version>{old_version}-SNAPSHOT</version>", f"<version>{new_version}-SNAPSHOT</version>")
with open(filepath, "wt", encoding = 'utf-8') as fh:
fh.write(content)
case 'MANIFEST.MF':
if root.endswith("META-INF"):
filepath = os.path.join(root, file)
print(f"Updating [{filepath}]...")
with open(filepath, "rt", encoding = 'utf-8') as fh:
content = fh.read()
content = content.replace(f"Bundle-Version: {old_version}.qualifier", f"Bundle-Version: {new_version}.qualifier", 1)
content = re.sub(r'(org[.]eclipse[.]tm4e[.].+;bundle-version=")([^"]+)(")', rf'\g<1>{new_version}\g<3>', content)
with open(filepath, "wt", encoding = 'utf-8') as fh:
fh.write(content)
case 'feature.xml':
filepath = os.path.join(root, file)
print(f"Updating [{filepath}]...")
with open(filepath, "rt", encoding = 'utf-8') as fh:
content = fh.read()
content = re.sub(r'(version=")([0-9.]+)(.qualifier")', rf'\g<1>{new_version}\g<3>', content)
with open(filepath, "wt", encoding = 'utf-8') as fh:
fh.write(content)