From eb6f5eb5684d8829e39af19ba882f567b88a19b4 Mon Sep 17 00:00:00 2001 From: bom Date: Wed, 8 Feb 2023 12:37:00 +0100 Subject: [PATCH] Add write_json logic --- devops_test.py | 75 +++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/devops_test.py b/devops_test.py index 2958295..4224191 100644 --- a/devops_test.py +++ b/devops_test.py @@ -24,53 +24,52 @@ class ReleaseLevel(Enum): class Version(): def __init__(self, config_file_path): - self.version = "0.0.0" + self.version = [] + self.is_snapshot = False self.config_file_path = config_file_path + self.config_file_type = config_file_path.split('.')[-1] def parse(self): - if self.config_file_path.split('.')[-1] == 'json': - self.__parse_json() + match self.config_file_type: + case 'json': + self.__parse_json() def __parse_json(self): with open(self.config_file_path, 'r') as json_file: - json_data = json.load(json_file) - self.version = json_data['version'] + json_version = json.load(json_file)['version'] + if '-SNAPSHOT' in json_version: + self.is_snapshot = True + json_version = json_version.replace('-SNAPSHOT', '') + self.version = [int(x) for x in json_version.split('.')] def increment(self, level: ReleaseLevel): - - if level is ReleaseLevel.SNAPSHOT: - if "-SNAPSHOT" not in self.version: - self.version = self.version + "-SNAPSHOT" - else: - # convert array to int - # e.g. patch index +1 - # convert back to str - # join - split_versiont = split_version. - - - match level: - - - case ReleaseLevel.PATCH: - self.version = self.version.replace("-SNAPSHOT", "") - split_version = self.version.split('.') - patch_version = int(split_version[ReleaseLevel.PATCH.value]) - self.version = ".".join(split_version[:ReleaseLevel.PATCH.value]) + "." + str(patch_version + 1) - + 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 = self.version.replace("-SNAPSHOT", "") - split_version = self.version.split('.') - minor_version = int(split_version[ReleaseLevel.MINOR.value]) - self.version = ".".join(split_version[:ReleaseLevel.MINOR.value]) + "." + str(minor_version + 1) + ".0" - + self.version[ReleaseLevel.PATCH.value] = 0 + self.version[ReleaseLevel.MINOR.value] += 1 case ReleaseLevel.MAJOR: - self.version = self.version.replace("-SNAPSHOT", "") - split_version = self.version.split('.') - major_version = int(split_version[ReleaseLevel.MAJOR.value]) - self.version = "".join(split_version[:ReleaseLevel.MAJOR.value]) + str(major_version + 1) + ".0" + ".0" - - + self.version[ReleaseLevel.PATCH.value] = 0 + self.version[ReleaseLevel.MINOR.value] = 0 + self.version[ReleaseLevel.MAJOR.value] += 1 + + def write(self): + match self.config_file_type: + case 'json': + self.__write_json() + + def __write_json(self): + with open(self.config_file_path, 'wr') as json_file: + json_data = json.load(json_file) + json_data['version'] = self.get() + json.dump(json_data, json_file) def get(self) -> str: - return self.version \ No newline at end of file + version_string = ".".join([str(x) for x in self.version]) + if self.is_snapshot: + version_string += "-SNAPSHOT" + return version_string \ No newline at end of file