-
Notifications
You must be signed in to change notification settings - Fork 0
/
version
executable file
·111 lines (86 loc) · 3.27 KB
/
version
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
#!/usr/bin/env python
import re
import sys
import subprocess
# Define the file-regex pairs
# For example, if the version is defined as `version = "1.2.3"` in each file
VERSIONED_FILES = {
"setup.py": r'(?P<prefix>version\s*=\s*["\'])(?P<version>\d+\.\d+\.\d+)(?P<suffix>["\'])',
"chroma/__init__.py": r'(?P<prefix>__version__\s*=\s*["\'])(?P<version>\d+\.\d+\.\d+)(?P<suffix>["\'])',
"flake.nix": r'(?P<prefix>version\s*=\s*["\'])(?P<version>\d+\.\d+\.\d+)(?P<suffix>["\'])',
}
VERSION_REGEX = r"^\d+\.\d+\.\d+$"
def commit(version):
"""
Make a Git commit with the new version and create a tag.
"""
commit_message = f"chore: bump version to {version}"
subprocess.run(["git", "add", *VERSIONED_FILES.keys()], check=True)
subprocess.run(["git", "commit", "-m", commit_message], check=True)
subprocess.run(["git", "tag", f"v{version}"], check=True)
print(f"Committed and tagged the version as v{version}")
def is_valid_version(version):
"""
Check if the version string is a valid semantic version.
"""
return re.match(VERSION_REGEX, version) is not None
def increment_version(version, part):
"""
Increment the version number based on the part: major, minor, or patch.
"""
major, minor, patch = map(int, version.split("."))
if part == "major":
major += 1
minor = 0
patch = 0
elif part == "minor":
minor += 1
patch = 0
elif part == "patch":
patch += 1
return f"{major}.{minor}.{patch}"
def bump_version(file, regex, part, version=str()):
"""
Perform version bump on the first matching file and regex.
"""
with open(file, "r") as f:
content = f.read()
match = re.search(regex, content)
if not match:
raise ValueError(f"No version found in {file} using regex: {regex}")
# Extract the current version and increment it
current_version = match.group("version")
if not version:
new_version = increment_version(current_version, part)
else:
new_version = version
# Replace the old version with the new version
updated_content = re.sub(regex, rf"\g<prefix>{new_version}\g<suffix>", content)
# Write the changes back to the file
with open(file, "w") as f:
f.write(updated_content)
print(f"Bumped version in {file}: {current_version} -> {new_version}")
return new_version
def main():
if len(sys.argv) == 2 and sys.argv[1] in ["major", "minor", "patch"]:
part = sys.argv[1]
new_version = None
for file, regex in VERSIONED_FILES.items():
if new_version is None:
new_version = bump_version(file, regex, part)
else:
bump_version(file, regex, part, new_version)
commit(new_version)
elif len(sys.argv) == 3 and sys.argv[1] == "set":
new_version = sys.argv[2]
if not is_valid_version(new_version):
print(f"Error: '{new_version}' is not a valid version format.")
sys.exit(1)
for file, regex in VERSIONED_FILES.items():
bump_version(file, regex, None, new_version)
commit(new_version)
else:
print("Usage: python bump_version.py [major|minor|patch] or [set <version>]")
sys.exit(1)
if __name__ == "__main__":
main()