dda-devops-build/devops_test.py

76 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 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):
self.version = "0.0.0"
self.config_file_path = config_file_path
def parse(self):
2023-02-08 10:22:55 +00:00
if self.config_file_path.split('.')[-1] == 'json':
2023-02-08 09:57:40 +00:00
self.__parse_json()
def __parse_json(self):
with open(self.config_file_path, 'r') as json_file:
json_data = json.load(json_file)
2023-02-08 10:22:55 +00:00
self.version = json_data['version']
def increment(self, level: ReleaseLevel):
2023-02-08 11:14:53 +00:00
if level is ReleaseLevel.SNAPSHOT:
if "-SNAPSHOT" not in self.version:
2023-02-08 10:22:55 +00:00
self.version = self.version + "-SNAPSHOT"
2023-02-08 11:14:53 +00:00
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", "")
2023-02-08 10:52:15 +00:00
split_version = self.version.split('.')
patch_version = int(split_version[ReleaseLevel.PATCH.value])
2023-02-08 11:14:53 +00:00
self.version = ".".join(split_version[:ReleaseLevel.PATCH.value]) + "." + str(patch_version + 1)
2023-02-08 10:22:55 +00:00
case ReleaseLevel.MINOR:
2023-02-08 11:14:53 +00:00
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"
2023-02-08 10:22:55 +00:00
case ReleaseLevel.MAJOR:
2023-02-08 11:14:53 +00:00
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"
2023-02-08 09:57:40 +00:00
2023-02-08 10:37:41 +00:00
def get(self) -> str:
return self.version