import json from enum import Enum def init_project(): # validate_values() version = Version('package.json') version.parse() version.increment(ReleaseLevel.MAJOR) version.write() 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, config_file_path): 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): 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_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): 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 write(self): match self.config_file_type: case 'json': self.__write_json() def __write_json(self): with open(self.config_file_path, 'r+') as json_file: json_data = json.load(json_file) json_data['version'] = self.get() json_file.seek(0) json.dump(json_data, json_file, indent=4) json_file.truncate() def get(self) -> str: version_string = ".".join([str(x) for x in self.version]) if self.is_snapshot: version_string += "-SNAPSHOT" return version_string