feat: add gui config for cli command
This commit is contained in:
@@ -6,12 +6,14 @@ from pathlib import Path
|
||||
import copier
|
||||
|
||||
from agebd.utils import get_git_root
|
||||
from core.enums import GuiOption
|
||||
from core.exceptions import UVError
|
||||
from core.git import GitRepoManager
|
||||
from models import (
|
||||
Service,
|
||||
)
|
||||
from models.context import ServicesContext
|
||||
from models.gui_config import GuiConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -30,7 +32,7 @@ class ServiceCreator:
|
||||
name: str,
|
||||
ioc_owner: str,
|
||||
ioc_description: str,
|
||||
ui_filename: str | None,
|
||||
gui_config: GuiConfig,
|
||||
):
|
||||
self.repo_manager.assert_clean_repo()
|
||||
original_branch = self.repo_manager.get_active_branch_name()
|
||||
@@ -41,9 +43,11 @@ class ServiceCreator:
|
||||
self.ctx.master_hla_names.add_hla_name(service.name_upper)
|
||||
self.ctx.iocs_overview.add_ioc(service, description=ioc_description)
|
||||
self.ctx.master_ioc_subs.update_content(service_name=service.name_upper)
|
||||
if ui_filename:
|
||||
if gui_config.should_create_ui():
|
||||
self.ctx.service_manager_ui.update_xml(
|
||||
service=service, service_name_camel=service.name_camel, ui_filename=ui_filename
|
||||
service=service,
|
||||
service_name_camel=service.name_camel,
|
||||
ui_filename=gui_config.ui_filename,
|
||||
)
|
||||
|
||||
branch_name = f"feature/add-service-{service.dir_name}"
|
||||
@@ -54,7 +58,7 @@ class ServiceCreator:
|
||||
self._render_new_service_templates(
|
||||
service=service,
|
||||
ioc_owner=ioc_owner,
|
||||
ui_filename=ui_filename,
|
||||
gui_config=gui_config,
|
||||
)
|
||||
self._generate_uv_lock(service=service)
|
||||
|
||||
@@ -77,7 +81,9 @@ class ServiceCreator:
|
||||
self.repo_manager.switch_branch(original_branch)
|
||||
raise e
|
||||
|
||||
def _render_new_service_templates(self, service: Service, ioc_owner: str, ui_filename: str):
|
||||
def _render_new_service_templates(
|
||||
self, service: Service, ioc_owner: str, gui_config: GuiConfig
|
||||
):
|
||||
"""
|
||||
Render template files for a new service
|
||||
"""
|
||||
@@ -91,13 +97,13 @@ class ServiceCreator:
|
||||
"ioc_owner": ioc_owner,
|
||||
},
|
||||
)
|
||||
if ui_filename:
|
||||
if gui_config.should_create_ui():
|
||||
copier.run_copy(
|
||||
src_path=str(QT_TEMPLATES_DIR),
|
||||
dst_path=str(self.ctx.write_paths.qt_dir),
|
||||
data={
|
||||
"service_name_upper": service.name_upper,
|
||||
"ui_filename": ui_filename,
|
||||
"ui_filename": gui_config.ui_filename,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, model_validator
|
||||
|
||||
from core.enums import GuiOption
|
||||
|
||||
|
||||
class GuiConfig(BaseModel):
|
||||
gui_option: GuiOption
|
||||
raw_ui_filename: Optional[str] = None
|
||||
|
||||
def should_create_ui(self):
|
||||
return self.gui_option == GuiOption.NEW
|
||||
|
||||
@property
|
||||
def ui_filename(self) -> str:
|
||||
if self.raw_ui_filename is None:
|
||||
raise ValueError("ui_filename is None")
|
||||
|
||||
return self.raw_ui_filename
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_dependent_options(self):
|
||||
if self.gui_option == GuiOption.EXISTING and self.raw_ui_filename is None:
|
||||
raise ValueError("--ui-filename is required when --gui-option is 'existing'.")
|
||||
|
||||
if self.gui_option == GuiOption.NONE and self.raw_ui_filename:
|
||||
raise ValueError("Cannot specify --ui-filename when --gui-option is 'none'.")
|
||||
|
||||
return self
|
||||
+4
-9
@@ -7,6 +7,7 @@ from core.git import GitRepoManager
|
||||
from core.service_creator import ServiceCreator
|
||||
from models import ServiceRegistry
|
||||
from models.context import ServicesContext
|
||||
from models.gui_config import GuiConfig
|
||||
|
||||
service = typer.Typer(no_args_is_help=True)
|
||||
|
||||
@@ -39,7 +40,7 @@ def add(
|
||||
"-d",
|
||||
help="See <repo-root>/docs/user/ioc/iocs_overview.md for examples",
|
||||
),
|
||||
gui: GuiOption = typer.Option(
|
||||
gui_option: GuiOption = typer.Option(
|
||||
GuiOption.NEW,
|
||||
"--gui",
|
||||
"-g",
|
||||
@@ -54,20 +55,14 @@ def add(
|
||||
),
|
||||
):
|
||||
init_logging()
|
||||
if gui == GuiOption.EXISTING:
|
||||
ui_filename = ui_filename
|
||||
elif gui == GuiOption.NONE:
|
||||
ui_filename = None
|
||||
elif gui == GuiOption.NEW:
|
||||
ui_filename = name
|
||||
|
||||
gui_config = GuiConfig(_gui_option=gui_option, _ui_filename=ui_filename)
|
||||
ctx = ServicesContext.load_from_disk(Paths())
|
||||
service_creator = ServiceCreator(ctx=ctx, repo_manager=GitRepoManager(REPO_ROOT))
|
||||
service_creator.add_service(
|
||||
name=name,
|
||||
ioc_owner=ioc_owner,
|
||||
ioc_description=ioc_description,
|
||||
ui_filename=ui_filename,
|
||||
gui_config=gui_config,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -7,9 +7,11 @@ from pytest_mock import MockerFixture
|
||||
|
||||
from agebd.utils import get_git_root
|
||||
from config.paths import Paths
|
||||
from core.enums import GuiOption
|
||||
from core.git import GitRepoManager
|
||||
from core.service_creator import ServiceCreator
|
||||
from models.context import ServicesContext
|
||||
from models.gui_config import GuiConfig
|
||||
|
||||
REPO_ROOT = get_git_root(__file__)
|
||||
TEST_REPO_ROOT = REPO_ROOT / "cli" / "tests" / "fixtures" / "test_repo_root"
|
||||
@@ -66,12 +68,13 @@ def test_add_new_service(tmp_path, mocker: MockerFixture):
|
||||
ctx = ServicesContext.load_from_disk(paths)
|
||||
ctx._write_paths = write_paths
|
||||
|
||||
gui_config = GuiConfig(gui_option=GuiOption.NEW, raw_ui_filename="TestService")
|
||||
creator = ServiceCreator(ctx=ctx, repo_manager=mocker.Mock(spec=GitRepoManager))
|
||||
creator.add_service(
|
||||
name="TestService99-X00",
|
||||
ioc_owner="test_user",
|
||||
ioc_description="Test Description",
|
||||
ui_filename="TestService",
|
||||
gui_config=gui_config,
|
||||
)
|
||||
|
||||
assert_are_dir_trees_equal(tmp_path, EXPECTED_OUTPUT_DIR, tmp_path)
|
||||
|
||||
Reference in New Issue
Block a user