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():
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
version_string = ".".join([str(x) for x in self.version])
if self.is_snapshot:
version_string += "-SNAPSHOT"
return version_string