forked from FooSoft/yomichan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bump_ver
executable file
·79 lines (59 loc) · 1.69 KB
/
bump_ver
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
#!/bin/python3
import dataclasses
import datetime
import functools
import json
import os.path
from typing import Optional
MANIFEST_VARIANTS = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'ext', 'manifest.json',
)
@dataclasses.dataclass
class Version:
year: str
month: str
day: str
revision: str
def as_str(self):
return '.'.join(dataclasses.astuple(self))
@classmethod
def from_str(cls, string: str):
return cls(*string.split('.'))
def __eq__(self, other):
return (
self.year == other.year
and self.month == other.month
and self.day == other.day
)
def bump_rev(self):
self.revision = str(int(self.revision) + 1)
return self
@functools.cache
def today_date():
return datetime.date.today()
def make_new_version() -> Version:
return Version(
str(today_date().year)[-2:],
str(today_date().month),
str(today_date().day),
'0'
)
def increase_ver_num(old_version: Version) -> Version:
new_version = make_new_version()
if old_version == new_version:
new_version = old_version.bump_rev()
return new_version
def find_old_version() -> Optional[Version]:
try:
with open(MANIFEST_VARIANTS, encoding='utf8') as f:
data = json.load(f)
return Version.from_str(data['version'])
except (FileNotFoundError, TypeError, KeyError, json.JSONDecodeError):
return None
def main():
old_version = find_old_version()
new_version = increase_ver_num(old_version) if old_version else make_new_version()
print(new_version.as_str())
if __name__ == '__main__':
main()