dda-devops-build/version.py
2023-02-16 14:21:27 +01:00

64 lines
No EOL
1.9 KiB
Python

from enum import Enum
from file_handlers import FileHandler
def init_project():
# validate_values()
version = Version.from_file('build.gradle')
version.increment(ReleaseLevel.SNAPSHOT)
version.to_file()
print(version.get())
def prepare_release():
pass
def release_in_git():
pass
class ReleaseLevel(Enum):
MAJOR = 0
MINOR = 1
PATCH = 2
SNAPSHOT = 3
class Version():
def __init__(self, version, is_snapshot):
self.version = version
self.is_snapshot = is_snapshot
self.file_handler = None
@classmethod
def from_file(cls, config_file_path):
file_handler = FileHandler.from_file_path(config_file_path)
version, is_snapshot = file_handler.parse()
inst = cls(version, is_snapshot)
inst.file_handler = file_handler
return inst
def to_file(self):
if self.file_handler is None:
raise Exception('Version was not created by from_file method.')
else:
self.file_handler.write(self.get())
def increment(self, level: ReleaseLevel):
self.is_snapshot = False
match level:
case ReleaseLevel.SNAPSHOT:
self.is_snapshot = True
case ReleaseLevel.PATCH:
self.version[ReleaseLevel.PATCH.value] += 1
case ReleaseLevel.MINOR:
self.version[ReleaseLevel.PATCH.value] = 0
self.version[ReleaseLevel.MINOR.value] += 1
case ReleaseLevel.MAJOR:
self.version[ReleaseLevel.PATCH.value] = 0
self.version[ReleaseLevel.MINOR.value] = 0
self.version[ReleaseLevel.MAJOR.value] += 1
def get(self) -> str:
version_string = ".".join([str(x) for x in self.version])
if self.is_snapshot:
version_string += "-SNAPSHOT"
return version_string