bump_version.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import json
  12. import os
  13. import re
  14. from dataclasses import asdict, dataclass
  15. from typing import Optional
  16. @dataclass
  17. class Version:
  18. major: str
  19. minor: str
  20. patch: str
  21. ext: Optional[str] = None
  22. def bump_ext_version(self) -> None:
  23. if not self.ext:
  24. return
  25. reg = re.compile(r"[0-9]+$")
  26. num = reg.findall(self.ext)[0]
  27. self.ext = self.ext.replace(num, str(int(num) + 1))
  28. def validate_suffix(self, suffix="dev"):
  29. if suffix not in self.ext:
  30. raise Exception(f"Version does not contain suffix {suffix}")
  31. @property
  32. def name(self) -> str:
  33. """returns a string representation of a version"""
  34. return f"{self.major}.{self.minor}.{self.patch}"
  35. @property
  36. def dev_name(self) -> str:
  37. """returns a string representation of a version"""
  38. return f"{self.name}.{self.ext}"
  39. def __str__(self) -> str:
  40. """returns a string representation of a version"""
  41. version_str = f"{self.major}.{self.minor}.{self.patch}"
  42. if self.ext:
  43. version_str = f"{version_str}.{self.ext}"
  44. return version_str
  45. def __load_version_from_path(base_path: str) -> Version:
  46. """Load version.json file from base path."""
  47. with open(os.path.join(base_path, "version.json")) as version_file:
  48. data = json.load(version_file)
  49. return Version(**data)
  50. def __write_version_to_path(base_path: str, version: Version) -> None:
  51. with open(os.path.join(base_path, "version.json"), "w") as version_file:
  52. json.dump(asdict(version), version_file)
  53. def extract_version(base_path: str) -> Version:
  54. """
  55. Load version.json file from base path and return the version string.
  56. """
  57. return __load_version_from_path(base_path)
  58. def bump_ext_version(version: Version, _base_path: str) -> None:
  59. version.bump_ext_version()
  60. __write_version_to_path(_base_path, version)
  61. if __name__ == "__main__":
  62. paths = (
  63. [
  64. f"taipy{os.sep}common",
  65. f"taipy{os.sep}core",
  66. f"taipy{os.sep}rest",
  67. f"taipy{os.sep}gui",
  68. f"taipy{os.sep}templates",
  69. "taipy",
  70. ]
  71. )
  72. for _path in paths:
  73. _version = extract_version(_path)
  74. bump_ext_version(_version, _path)
  75. print(f"NEW_VERSION={_version.dev_name}") # noqa T201 # type: ignore[reportPossiblyUnboundVariable]