From ac7f5b8dd1b06cded85b98c521fc2e64e6b25bfc Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Thu, 11 May 2023 09:20:59 +0200 Subject: [PATCH] First steps for BuildFile --- doc/architecture/Domain.md | 4 +- src/main/python/ddadevops/domain/__init__.py | 1 + .../python/ddadevops/domain/build_file.py | 47 +++++++++++++++++++ src/main/python/ddadevops/domain/common.py | 11 +++-- .../infrastructure/infrastructure.py | 25 ++++++++-- src/test/python/domain/test_build_file.py | 33 +++++++++++++ 6 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 src/main/python/ddadevops/domain/build_file.py create mode 100644 src/test/python/domain/test_build_file.py diff --git a/doc/architecture/Domain.md b/doc/architecture/Domain.md index e99055d..f1d3e33 100644 --- a/doc/architecture/Domain.md +++ b/doc/architecture/Domain.md @@ -43,8 +43,10 @@ classDiagram class BuildFile { <> file_path [id] - file_type content + build_file_type() + getVersion() + setVersion(version) } class Version { diff --git a/src/main/python/ddadevops/domain/__init__.py b/src/main/python/ddadevops/domain/__init__.py index 739acdf..3662bc8 100644 --- a/src/main/python/ddadevops/domain/__init__.py +++ b/src/main/python/ddadevops/domain/__init__.py @@ -4,3 +4,4 @@ from .image import Image from .c4k import C4k from .release import Release, EnvironmentKeys from .version import Version +from .build_file import BuildFileType, BuildFile diff --git a/src/main/python/ddadevops/domain/build_file.py b/src/main/python/ddadevops/domain/build_file.py new file mode 100644 index 0000000..2e70893 --- /dev/null +++ b/src/main/python/ddadevops/domain/build_file.py @@ -0,0 +1,47 @@ +from enum import Enum +from typing import Optional +from pathlib import Path +from .common import ( + Validateable, + Devops, + ReleaseType, +) +from .version import ( + Version, +) + +class BuildFileType(Enum): + JS = '.json' + JAVA_GRADLE = '.gradle' + JAVA_CLOJURE = ".clj" + PYTHON = '.py' + +class BuildFile(Validateable): + def __init__(self, file_path: Path, content: str): + self.file_path = file_path + self.content = content + + def validate(self): + result = [] + result += self.__validate_is_not_empty__("file_path") + result += self.__validate_is_not_empty__("content") + if not self.build_file_type(): + result += [f"Suffix {self.file_path} is unknown."] + return result + + def build_file_type(self): + if not self.file_path: + return None + config_file_type = self.file_path.suffix + match config_file_type: + case '.json': + result = BuildFileType.JS + case '.gradle': + result = BuildFileType.JAVA_GRADLE + case '.clj': + result = BuildFileType.JAVA_CLOJURE + case '.py': + result = BuildFileType.PYTHON + case _: + result = None + return result diff --git a/src/main/python/ddadevops/domain/common.py b/src/main/python/ddadevops/domain/common.py index ee8c3cd..ced80c6 100644 --- a/src/main/python/ddadevops/domain/common.py +++ b/src/main/python/ddadevops/domain/common.py @@ -34,11 +34,12 @@ class Validateable: def __validate_is_not_empty__(self, field_name: str) -> List[str]: result = self.__validate_is_not_none__(field_name) - value = self.__dict__[field_name] - if type(value) is str and value == "": - result += [f"Field '{field_name}' must not be empty."] - elif type(value) is list and len(value) == 0: - result += [f"Field '{field_name}' must not be empty."] + if len(result) == 0: + value = self.__dict__[field_name] + if type(value) is str and value == "": + result += [f"Field '{field_name}' must not be empty."] + elif type(value) is list and len(value) == 0: + result += [f"Field '{field_name}' must not be empty."] return result def validate(self) -> List[str]: diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index dbbd98f..c165ae9 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -5,9 +5,10 @@ from subprocess import run from pkg_resources import resource_string import yaml import deprecation -from ..domain import Devops, Image, C4k, Release +from ..domain import Devops, Image, C4k, Release, BuildFile from ..python_util import execute + class DevopsRepository: def get_devops(self, project) -> Devops: devops = project.get_property("devops") @@ -19,10 +20,24 @@ class DevopsRepository: project.set_property("devops", devops) -class ProjectRepository: - def set_build(self, project, build): - project.set_property("devops_build", build) +class BuildFileRepository: + def get(self, path: Path) -> BuildFile: + with open(path, "r", encoding="utf-8") as file: + content = file.read() + result = BuildFile(path, content) + result.throw_if_invalid() + return result + def write(self, build_file: BuildFile): + build_file.throw_if_invalid() + with open(build_file.file_path, "r+", encoding="utf-8") as file: + file.seek(0) + file.write(build_file.content) + + + +class ProjectRepository: + pass class ResourceApi: def read_resource(self, path: str) -> bytes: @@ -60,7 +75,7 @@ class ImageApi: def drun(self, name: str): run( - f"docker run -it --entrypoint=\"\" {name} /bin/bash", + f'docker run -it --entrypoint="" {name} /bin/bash', shell=True, check=True, ) diff --git a/src/test/python/domain/test_build_file.py b/src/test/python/domain/test_build_file.py new file mode 100644 index 0000000..c505438 --- /dev/null +++ b/src/test/python/domain/test_build_file.py @@ -0,0 +1,33 @@ +from pathlib import Path +from src.main.python.ddadevops.domain import ( + BuildFileType, + BuildFile, +) + + +def test_sould_validate_build_file(): + sut = BuildFile( + Path("./project.clj"), + "content" + ) + assert sut.is_valid() + + sut = BuildFile( + None, + "" + ) + assert not sut.is_valid() + + sut = BuildFile( + Path("./unknown.extension"), + "content" + ) + assert not sut.is_valid() + + +def test_sould_calculate_build_type(): + sut = BuildFile( + Path("./project.clj"), + "content" + ) + assert sut.build_file_type() == BuildFileType.JAVA_CLOJURE