terraformDummyRepo/devops_test.py

77 lines
2.3 KiB
Python
Raw Normal View History

2023-02-08 09:57:40 +00:00
import json
2023-02-08 10:22:55 +00:00
from enum import Enum
2023-02-08 09:57:40 +00:00
def init_project():
# validate_values()
version = Version('package.json')
version.parse()
2023-02-08 11:14:53 +00:00
version.increment(ReleaseLevel.MAJOR)
2023-02-08 11:57:15 +00:00
version.write()
2023-02-08 10:37:41 +00:00
print(version.get())
2023-02-08 09:57:40 +00:00
def prepare_release():
pass
def release_in_git():
pass
2023-02-08 10:37:41 +00:00
class ReleaseLevel(Enum):
2023-02-08 10:52:15 +00:00
MAJOR = 0
MINOR = 1
PATCH = 2
SNAPSHOT = 3
2023-02-08 09:57:40 +00:00
2023-02-08 10:37:41 +00:00
class Version():
2023-02-08 10:22:55 +00:00
2023-02-08 09:57:40 +00:00
def __init__(self, config_file_path):
2023-02-08 11:37:00 +00:00
self.version = []
self.is_snapshot = False
2023-02-08 09:57:40 +00:00
self.config_file_path = config_file_path
2023-02-08 11:37:00 +00:00
self.config_file_type = config_file_path.split('.')[-1]
2023-02-08 09:57:40 +00:00
def parse(self):
2023-02-08 11:37:00 +00:00
match self.config_file_type:
case 'json':
self.__parse_json()
2023-02-08 09:57:40 +00:00
def __parse_json(self):
with open(self.config_file_path, 'r') as json_file:
2023-02-08 11:37:00 +00:00
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('.')]
2023-02-08 10:22:55 +00:00
def increment(self, level: ReleaseLevel):
2023-02-08 11:37:00 +00:00
self.is_snapshot = False
match level:
case ReleaseLevel.SNAPSHOT:
self.is_snapshot = True
case ReleaseLevel.PATCH:
self.version[ReleaseLevel.PATCH.value] += 1
2023-02-08 10:22:55 +00:00
case ReleaseLevel.MINOR:
2023-02-08 11:37:00 +00:00
self.version[ReleaseLevel.PATCH.value] = 0
self.version[ReleaseLevel.MINOR.value] += 1
2023-02-08 10:22:55 +00:00
case ReleaseLevel.MAJOR:
2023-02-08 11:37:00 +00:00
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):
2023-02-08 11:57:15 +00:00
with open(self.config_file_path, 'r+') as json_file:
2023-02-08 11:37:00 +00:00
json_data = json.load(json_file)
json_data['version'] = self.get()
2023-02-08 11:57:15 +00:00
json_file.seek(0)
json.dump(json_data, json_file, indent=4)
json_file.truncate()
2023-02-08 09:57:40 +00:00
2023-02-08 10:37:41 +00:00
def get(self) -> str:
2023-02-08 11:37:00 +00:00
version_string = ".".join([str(x) for x in self.version])
if self.is_snapshot:
version_string += "-SNAPSHOT"
return version_string