WIP Create version repo in services with classmethod

The goal is to create a single version_repo object for each
file loaded. In that way we may avoid loading the file for loading
and writing.
This commit is contained in:
erik 2023-02-20 13:33:28 +01:00
parent dac901769d
commit 88c3acb73c
3 changed files with 34 additions and 28 deletions

View file

@ -3,6 +3,7 @@ class InitReleaseService():
def __init__(self, commit_id, file): def __init__(self, commit_id, file):
self.commit_id = commit_id self.commit_id = commit_id
self.file = file self.file = file
self.version_repo = none
def __read_commit_message(self): def __read_commit_message(self):
pass pass
@ -12,12 +13,12 @@ class InitReleaseService():
def get_version(self): def get_version(self):
current_version = VersionRepository.get_current_version(self.file) version_repo = VersionRepository.get_current_version(self.file)
commit_message = self.read_commit_message(self.commit_id) commit_message = self.read_commit_message(self.commit_id)
release_type = self.calculate_release_type(commit_message) release_type = self.calculate_release_type(commit_message)
release_version = create_release_version(current_version,release_type) release_version = create_release_version(version_repo.version ,release_type)
bump_version = create_bump_version(current_version,release_type) bump_version = create_bump_version(version_repo.version,release_type)
release_and_bump_version = tuple(release_version, bump_version) release_and_bump_version = tuple(release_version, bump_version)

View file

@ -24,23 +24,7 @@ class Version():
def __init__(self, version, is_snapshot): def __init__(self, version, is_snapshot):
self.version = version self.version = version
self.is_snapshot = is_snapshot self.is_snapshot = is_snapshot
self.file_handler = None
@classmethod
def from_file(cls, config_file_path):
file_handler = FileHandler.from_file_path(config_file_path)
version, is_snapshot = file_handler.parse()
inst = cls(version, is_snapshot)
inst.file_handler = file_handler
return inst
def to_file(self):
if self.file_handler is None:
raise Exception('Version was not created by from_file method.')
else:
self.file_handler.write(self.get())
def increment(self, level: ReleaseLevel): def increment(self, level: ReleaseLevel):
self.is_snapshot = False self.is_snapshot = False

View file

@ -4,14 +4,35 @@ class VersionRepository():
def __init__(self, file): def __init__(self, file):
self.file = file self.file = file
self.file_handler = None
self.version = None
self.is_snapshot = None
def load_file(self):
self.file_handler = FileHandler.from_file_path(self.file)
return file_handler
def write_file(self):
if self.file_handler is None:
raise Exception('Version was not created by load_file method.')
if self.version is None or self.is_snapshot is None:
raise Exception('Version or is_snapshot attribute not set.')
else:
self.file_handler.write(self.version)
def parse_file(self, file_handler):
version, is_snapshot = file_handler.parse()
return version, is_snapshot
@classmethod @classmethod
def load_file(cls, file): def get_current_version(cls, file):
file_handler = FileHandler.from_file_path(file) inst = cls(file)
version, is_snapshot = file_handler.parse()
inst = cls(version, is_snapshot)
inst.file_handler = file_handler
return inst
file_handler= inst.load_file(file)
version, is_snapshot = inst.parse_file(file_handler)
inst.version = version
inst.is_snapshot = is_snapshot
inst.file_handler = file_handler
return inst