introduce init service

This commit is contained in:
Michael Jerger 2023-05-13 16:01:35 +02:00
parent 17f41dbd7a
commit c13b70150b
10 changed files with 107 additions and 35 deletions

View file

@ -45,8 +45,8 @@ classDiagram
file_path [id]
content
build_file_type()
getVersion()
setVersion(version)
get_version()
set_version(version)
}
class Version {

View file

@ -5,3 +5,4 @@ from .c4k import C4k
from .release import Release, EnvironmentKeys
from .version import Version
from .build_file import BuildFileType, BuildFile
from .init_service import InitService

View file

@ -12,7 +12,7 @@ class DevopsFactory:
def __init__(self):
pass
def build_devops(self, input: dict, version: Version) -> Devops:
def build_devops(self, input: dict, version: Version = None) -> Devops:
build_types = self.__parse_build_types__(input["build_types"])
mixin_types = self.__parse_mixin_types__(input["mixin_types"])

View file

@ -0,0 +1,29 @@
from pathlib import Path
from .common import Devops, MixinType
from .devops_factory import DevopsFactory
from .version import Version
from src.main.python.ddadevops.infrastructure import (
BuildFileRepository
)
class InitService:
def __init__(self, devops_factory, build_file_repository):
self.devops_factory = devops_factory
self.build_file_repository = build_file_repository
@classmethod
def prod(cls):
return cls(
DevopsFactory(),
BuildFileRepository(),
)
def initialize(self, input: dict) -> Devops:
mixin_types = self.devops_factory.__parse_mixin_types__(input["mixin_types"])
if MixinType.RELEASE in mixin_types:
primary_build_file_id = input.get("release_primary_build_file", "./project.clj")
primary_build_file = self.build_file_repository.get(Path(primary_build_file_id))
version = primary_build_file.get_version()
return self.devops_factory.build_devops(input, version=version)

View file

@ -1 +1,2 @@
from .infrastructure import FileApi, ImageApi, ResourceApi, ExecutionApi, ProjectRepository
from .repository import DevopsRepository, BuildFileRepository

View file

@ -8,34 +8,6 @@ import deprecation
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")
devops.throw_if_invalid()
return devops
def set_devops(self, project, devops: Devops):
devops.throw_if_invalid()
project.set_property("devops", devops)
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

View file

@ -0,0 +1,35 @@
from pathlib import Path
from sys import stdout
from os import chmod
from subprocess import run
from pkg_resources import resource_string
import yaml
import deprecation
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")
devops.throw_if_invalid()
return devops
def set_devops(self, project, devops: Devops):
devops.throw_if_invalid()
project.set_property("devops", devops)
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)

View file

@ -26,8 +26,7 @@ def test_devops_factory():
"image_dockerhub_user": "dockerhub_user",
"image_dockerhub_password": "dockerhub_password",
"image_tag": "docker_image_tag",
},
Version.from_str("1.0.0")
}
)
assert sut is not None

View file

@ -1,4 +1,5 @@
from src.main.python.ddadevops.domain import DevopsFactory, Devops, Version
from pathlib import Path
from src.main.python.ddadevops.domain import DevopsFactory, Devops, Version, BuildFile
def devops_config(overrides: dict) -> dict:
@ -21,7 +22,7 @@ def devops_config(overrides: dict) -> dict:
"release_type": "NONE",
"release_main_branch": "main",
"release_current_branch": "my_feature",
"release_primary_build_file": "./project.clj",
"release_primary_build_file": "./package.json",
"release_secondary_build_file": [],
}
input = default.copy()
@ -33,3 +34,18 @@ def build_devops(
overrides: dict, version: Version = Version.from_str("1.0.0-SNAPSHOT")
) -> Devops:
return DevopsFactory().build_devops(devops_config(overrides), version=version)
class BuildFileRepositoryMock:
def get(self, path: Path) -> BuildFile:
return BuildFile(
Path("./package.json"),
"""
{
"version": "1.1.5-SNAPSHOT"
}
""",
)
def write(self, build_file: BuildFile):
pass

View file

@ -0,0 +1,19 @@
import pytest
from src.main.python.ddadevops.domain import (
InitService,
DevopsFactory,
Version,
MixinType,
)
from .test_helper import BuildFileRepositoryMock, devops_config
def test_sould_load_build_file():
sut = InitService(
DevopsFactory(),
BuildFileRepositoryMock(),
)
assert (
Version.from_str("1.1.5-SNAPSHOT")
== sut.initialize(devops_config({})).mixins[MixinType.RELEASE].version
)