remove `#!/usr/bin/env sh`
[liba.git] / script / version.py
blob0b27d98eb809fc122b3be90e8dbe79219426b989
1 #!/usr/bin/env python
2 import time, os, re
5 class version(object):
6 def __init__(self, release="0.0.0") -> None:
7 self.major, self.minor, self.patch = re.findall(r"(\d+)\.(\d+)\.(\d+)", release)[0]
8 self.version = self.major + "." + self.minor + "." + self.patch
9 self.tweak = time.strftime("%Y%m%d")
10 self.release = release
12 @staticmethod
13 def load(file):
14 if not os.path.exists(file):
15 print("missing {}".format(file))
16 return ""
17 with open(file, "r", encoding="UTF-8") as f:
18 return f.read()
20 @staticmethod
21 def save(file, text):
22 with open(file, "wb") as f:
23 f.write(text.encode("UTF-8"))
25 def update_version(self, file, feild="version"):
26 text = self.load(file)
27 res = re.findall(
28 r"{}([^0-9A-Za-z]+)([0-9]+)\.([0-9]+)\.([0-9]+)([.\w-]*)".format(feild), text
30 if res:
31 release = "{}.{}.{}{}".format(res[0][1], res[0][2], res[0][3], res[0][4])
32 if self.release != release:
33 line0 = "{}{}{}".format(feild, res[0][0], release)
34 line1 = "{}{}{}".format(feild, res[0][0], self.release)
35 self.save(file, text.replace(line0, line1))
37 def update_version_h(self, file):
38 text = text_ = self.load(file)
39 res = re.findall(r"#define A_VERSION_MAJOR (\d+)", text)
40 if res and self.major != res[0]:
41 line0 = "#define A_VERSION_MAJOR {}".format(res[0])
42 line1 = "#define A_VERSION_MAJOR {}".format(self.major)
43 text = text.replace(line0, line1)
44 res = re.findall(r"#define A_VERSION_MINOR (\d+)", text)
45 if res and self.minor != res[0]:
46 line0 = "#define A_VERSION_MINOR {}".format(res[0])
47 line1 = "#define A_VERSION_MINOR {}".format(self.minor)
48 text = text.replace(line0, line1)
49 res = re.findall(r"#define A_VERSION_PATCH (\d+)", text)
50 if res and self.patch != res[0]:
51 line0 = "#define A_VERSION_PATCH {}".format(res[0])
52 line1 = "#define A_VERSION_PATCH {}".format(self.patch)
53 text = text.replace(line0, line1)
54 res = re.findall(r"#define A_VERSION_TWEAK (\d+)", text)
55 if res and self.tweak != res[0]:
56 line0 = "#define A_VERSION_TWEAK {}".format(res[0])
57 line1 = "#define A_VERSION_TWEAK {}".format(self.tweak)
58 text = text.replace(line0, line1)
59 if text != text_:
60 self.save(file, text)
63 if __name__ == "__main__":
64 import argparse
66 parser = argparse.ArgumentParser()
67 parser.add_argument("version")
68 args = parser.parse_known_args()
69 v = version(args[0].version)
70 v.update_version("doc/Doxyfile", "PROJECT_NUMBER")
71 v.update_version("cmake/Project.cmake", "VERSION")
72 v.update_version("java/src/liba.java", "VERSION")
73 v.update_version("lua/liba.lua", "VERSION")
74 v.update_version_h("include/a/version.h")
75 v.update_version("javascript/package.json")
76 v.update_version("Cargo.toml")
77 v.update_version("conanfile.py")
78 v.update_version("meson.build")
79 v.update_version("setup.cfg")
80 v.update_version("xmake.lua")