Add write_json logic

This commit is contained in:
bom 2023-02-08 12:37:00 +01:00
parent 1fe186cc4a
commit eb6f5eb568

View file

@ -24,53 +24,52 @@ class ReleaseLevel(Enum):
class Version(): class Version():
def __init__(self, config_file_path): 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_path = config_file_path
self.config_file_type = config_file_path.split('.')[-1]
def parse(self): def parse(self):
if self.config_file_path.split('.')[-1] == 'json': match self.config_file_type:
case 'json':
self.__parse_json() self.__parse_json()
def __parse_json(self): def __parse_json(self):
with open(self.config_file_path, 'r') as json_file: with open(self.config_file_path, 'r') as json_file:
json_data = json.load(json_file) json_version = json.load(json_file)['version']
self.version = json_data['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): def increment(self, level: ReleaseLevel):
self.is_snapshot = False
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: match level:
case ReleaseLevel.SNAPSHOT:
self.is_snapshot = True
case ReleaseLevel.PATCH: case ReleaseLevel.PATCH:
self.version = self.version.replace("-SNAPSHOT", "") self.version[ReleaseLevel.PATCH.value] += 1
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)
case ReleaseLevel.MINOR: case ReleaseLevel.MINOR:
self.version = self.version.replace("-SNAPSHOT", "") self.version[ReleaseLevel.PATCH.value] = 0
split_version = self.version.split('.') self.version[ReleaseLevel.MINOR.value] += 1
minor_version = int(split_version[ReleaseLevel.MINOR.value])
self.version = ".".join(split_version[:ReleaseLevel.MINOR.value]) + "." + str(minor_version + 1) + ".0"
case ReleaseLevel.MAJOR: case ReleaseLevel.MAJOR:
self.version = self.version.replace("-SNAPSHOT", "") self.version[ReleaseLevel.PATCH.value] = 0
split_version = self.version.split('.') self.version[ReleaseLevel.MINOR.value] = 0
major_version = int(split_version[ReleaseLevel.MAJOR.value]) self.version[ReleaseLevel.MAJOR.value] += 1
self.version = "".join(split_version[:ReleaseLevel.MAJOR.value]) + str(major_version + 1) + ".0" + ".0"
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: def get(self) -> str:
return self.version version_string = ".".join([str(x) for x in self.version])
if self.is_snapshot:
version_string += "-SNAPSHOT"
return version_string