Refactor version class

This commit is contained in:
erik 2023-02-20 14:24:55 +01:00
parent 61629e9d97
commit 91f3d7d972

View file

@ -4,7 +4,7 @@ from file_handlers import FileHandler
def init_project(): def init_project():
# validate_values() # validate_values()
version = Version.from_file('build.gradle') version = Version.from_file('build.gradle')
version.increment(ReleaseLevel.SNAPSHOT) version.increment(ReleaseType.SNAPSHOT)
version.to_file() version.to_file()
print(version.get()) print(version.get())
@ -14,7 +14,7 @@ def prepare_release():
def release_in_git(): def release_in_git():
pass pass
class ReleaseLevel(Enum): class ReleaseType(Enum):
MAJOR = 0 MAJOR = 0
MINOR = 1 MINOR = 1
PATCH = 2 PATCH = 2
@ -22,27 +22,39 @@ class ReleaseLevel(Enum):
class Version(): class Version():
def __init__(self, version_list, is_snapshot): def __init__(self, version_list, release_type):
self.version_list = version_list self.version_list = None
self.is_snapshot = is_snapshot self.is_snapshot = None
self.version_string = None
def increment(self, level: ReleaseLevel): def increment(self, release_type: ReleaseType):
self.is_snapshot = False self.is_snapshot = False
match level: match release_type:
case ReleaseLevel.SNAPSHOT: case ReleaseType.SNAPSHOT:
self.is_snapshot = True self.is_snapshot = True
case ReleaseLevel.PATCH: case ReleaseType.PATCH:
self.version[ReleaseLevel.PATCH.value] += 1 self.version_list[ReleaseType.PATCH.value] += 1
case ReleaseLevel.MINOR: case ReleaseType.MINOR:
self.version[ReleaseLevel.PATCH.value] = 0 self.version_list[ReleaseType.PATCH.value] = 0
self.version[ReleaseLevel.MINOR.value] += 1 self.version_list[ReleaseType.MINOR.value] += 1
case ReleaseLevel.MAJOR: case ReleaseType.MAJOR:
self.version[ReleaseLevel.PATCH.value] = 0 self.version_list[ReleaseType.PATCH.value] = 0
self.version[ReleaseLevel.MINOR.value] = 0 self.version_list[ReleaseType.MINOR.value] = 0
self.version[ReleaseLevel.MAJOR.value] += 1 self.version_list[ReleaseType.MAJOR.value] += 1
def get_version_string(self) -> str: def get_version_string(self) -> str:
version_string = ".".join([str(x) for x in self.version_list]) self.version_string = ".".join([str(x) for x in self.version_list])
if self.is_snapshot: if self.is_snapshot:
version_string += "-SNAPSHOT" self.version_string += "-SNAPSHOT"
return version_string
@classmethod
def create_release_version(cls, version_list, release_type):
inst = cls(version_list, release_type)
inst.increment(release_type)
return inst
@classmethod
def create_bump_version(cls, version_list, release_type):
pass