from domain import Release, Version, ReleaseType from infrastructure_api import FileHandler, SystemAPI from pathlib import Path class VersionRepository(): def __init__(self, file): self.file = file self.file_handler = None def load_file(self): self.file_handler = FileHandler.from_file_path(self.file) return self.file_handler def write_file(self, version_string): if self.file_handler is None: raise Exception('Version was not created by load_file method.') else: self.file_handler.write(version_string) def parse_file(self): version_list, is_snapshot = self.file_handler.parse() return version_list, is_snapshot def get_version(self) -> Version: self.file_handler = self.load_file() version_list, is_snapshot = self.parse_file() version = Version(version_list) version.is_snapshot = is_snapshot return version class ReleaseRepository(): def __init__(self, version_repository: VersionRepository): self.version_repository = version_repository def get_current_release(self) -> Release: pass class ReleaseTypeRepository(): def __init__(self): pass class GitRepository(): def __init__(self): self.latest_commit = None self.system_repository = SystemAPI() @classmethod def create_from_commit_string(cls, commit_string): inst = cls() inst.latest_commit = commit_string return inst def get_latest_n_commits(self, n: int): self.system_repository.run_checked('git', 'log', '--oneline', '--format="%s %b"', f'-n {n}') return self.system_repository.stdout def get_latest_commit(self): output = self.get_latest_n_commits(1) self.latest_commit = " ".join(output) # returns a list of strings otherwise return self.latest_commit def get_release_type_from_latest_commit(self): if self.latest_commit is None: self.get_latest_commit() if ReleaseType.MAJOR.name in self.latest_commit.upper(): return ReleaseType.MAJOR elif ReleaseType.MINOR.name in self.latest_commit.upper(): return ReleaseType.MINOR elif ReleaseType.PATCH.name in self.latest_commit.upper(): return ReleaseType.PATCH elif ReleaseType.SNAPSHOT.name in self.latest_commit.upper(): return ReleaseType.SNAPSHOT else: return None def tag_annotated(self, annotation: str, message: str): self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message) return self.system_repository.stdout def tag_annotated(self, annotation: str, message: str, count: int): self.system_repository.run_checked('git', 'tag', '-a', annotation, '-m', message, f'HEAD~{count}') return self.system_repository.stdout def get_current_branch(self): self.system_repository.run_checked('git', 'branch', '--show-current') return ''.join(self.system_repository.stdout).rstrip() def add_file(self, file_path: Path): self.system_repository.run_checked('git', 'add', file_path) return self.system_repository.stdout def commit(self, commit_message: str): self.system_repository.run_checked('git', 'commit', '-m', commit_message) return self.system_repository.stdout def push(self): self.system_repository.run_checked('git', 'push') return self.system_repository.stdout def checkout(self, branch: str): self.system_repository.run_checked('git', 'checkout', branch) return self.system_repository.stdout