First steps for BuildFile

This commit is contained in:
Michael Jerger 2023-05-11 09:20:59 +02:00
parent 8359406330
commit ac7f5b8dd1
6 changed files with 110 additions and 11 deletions

View file

@ -43,8 +43,10 @@ classDiagram
class BuildFile {
<<AggregateRoot>>
file_path [id]
file_type
content
build_file_type()
getVersion()
setVersion(version)
}
class Version {

View file

@ -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

View file

@ -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

View file

@ -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]:

View file

@ -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,
)

View file

@ -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