-
Notifications
You must be signed in to change notification settings - Fork 0
/
upver.py
executable file
·155 lines (116 loc) · 3.72 KB
/
upver.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
152
153
154
155
#!/bin/python3.10
import subprocess
import argparse
from typing import List, cast
from functools import total_ordering
@total_ordering
class Version:
def __init__(self, major: int, minor: int, patch: int) -> None:
self.major = major
self.minor = minor
self.patch = patch
def __str__(self) -> str:
return f"v{self.major}.{self.minor}.{self.patch}"
def __eq__(self, other: object) -> bool: # type: ignore
if other is Version:
other = cast(Version, other)
return (
self.major == other.major
and self.minor == other.minor
and self.patch == other.patch
)
return False
def __lt__(self, other: "Version") -> bool:
if self.major < other.major:
return True
elif self.major > other.major:
return False
if self.minor < other.minor:
return True
elif self.minor > other.minor:
return False
if self.patch < other.patch:
return True
elif self.patch > other.patch:
return False
return False
@staticmethod
def fromString(tag: str):
assert tag[0] == "v"
tag = tag.strip("v")
splitted = tag.split(".")
if len(splitted) != 3:
raise Exception(
f"version is not setted correctly, should be something like this v1.3.12 and is {tag}"
)
return Version(
int(splitted[0]),
int(splitted[1]),
int(splitted[2]),
)
def up_version(v: Version):
with open("./share/version", "w") as f:
f.write(v.__str__())
p = subprocess.run(["git", "add", "share/version"], check=True)
assert p.returncode == 0
p = subprocess.run(["git", "commit", "-m", f"bump version to {v}"], check=True)
assert p.returncode == 0
p = subprocess.run(["git", "tag", f"{v}"], check=True)
assert p.returncode == 0
p = subprocess.run(["git", "push"], check=True)
assert p.returncode == 0
p = subprocess.run(["git", "push", "origin", f"{v}"], check=True)
assert p.returncode == 0
def majorFunc(v: Version):
v.major += 1
v.minor = 0
v.patch = 0
print("Major", v.__str__(), "to", v)
up_version(v)
def minorFunc(v: Version):
v.minor += 1
v.patch = 0
print("Minor", v.__str__(), "to", v)
up_version(v)
def patchFunc(v: Version):
v.patch += 1
print("Patch", v.__str__(), "to", v)
up_version(v)
def parse_arguments(lastTag: Version):
parser = argparse.ArgumentParser(
prog="./upver.py",
description="bump version",
)
subparsers = parser.add_subparsers(required=True)
major = subparsers.add_parser("major", description="up major version")
major.set_defaults(func=majorFunc)
minor = subparsers.add_parser("minor", description="up minor version")
minor.set_defaults(func=minorFunc)
patch = subparsers.add_parser("patch", description="up patch version")
patch.set_defaults(func=patchFunc)
args = parser.parse_args()
args.func(lastTag)
def getTags() -> List[Version]:
p = subprocess.run(
["git", "tag", "--list"],
stdout=subprocess.PIPE,
check=True,
text=True,
)
lines = p.stdout.split("\n")
return parseTages(lines)
def parseTages(lines: list[str]) -> List[Version]:
result: list[Version] = []
for s in lines:
if s == "":
continue
result.append(Version.fromString(s))
result.sort()
return result
if __name__ == "__main__":
tags = getTags()
if len(tags) == 0:
lastVersion = Version.fromString("v0.0.0")
else:
lastVersion = tags[-1]
parse_arguments(lastVersion)