From 1f8d420749b64a94e05c02ba46b462a7accbe2f6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 10 Apr 2021 11:46:36 +0200 Subject: [PATCH] Improved workspace handling in new command --- cpl-workspace.json | 2 +- src/cpl_cli/source_creator/console_builder.py | 39 +++++++++------ src/cpl_cli/source_creator/library_builder.py | 47 ++++++++++++------- 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/cpl-workspace.json b/cpl-workspace.json index 617e8345..28077a0b 100644 --- a/cpl-workspace.json +++ b/cpl-workspace.json @@ -1,5 +1,5 @@ { - "Workspace": { + "WorkspaceSettings": { "DefaultProject": "cpl_cli", "Projects": { "cpl": "src/cpl/cpl.json", diff --git a/src/cpl_cli/source_creator/console_builder.py b/src/cpl_cli/source_creator/console_builder.py index 5ac7baf6..c9746590 100644 --- a/src/cpl_cli/source_creator/console_builder.py +++ b/src/cpl_cli/source_creator/console_builder.py @@ -35,6 +35,24 @@ class ConsoleBuilder: project_json.write(json.dumps(content, indent=2)) project_json.close() + @classmethod + def _create_workspace(cls, path: str, project_name, projects: dict): + ws_dict = { + WorkspaceSettings.__name__: { + WorkspaceSettingsNameEnum.default_project.value: project_name, + WorkspaceSettingsNameEnum.projects.value: projects + } + } + + Console.spinner( + f'Creating {path}', + cls._create_file, + path, + ws_dict, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + @classmethod def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]): @@ -104,22 +122,13 @@ class ConsoleBuilder: src_path = f'{proj_name}/src/{project_name_snake}' workspace_file_path = f'{proj_name}/cpl-workspace.json' project_file_path = f'{src_path}/{project_name}.json' + cls._create_workspace(workspace_file_path, project_name, { + project_name: project_file_path + }) - Console.spinner( - f'Creating {workspace_file_path}', - cls._create_file, - workspace_file_path, - { - WorkspaceSettings.__name__: { - WorkspaceSettingsNameEnum.default_project.value: project_name, - WorkspaceSettingsNameEnum.projects.value: { - project_name: project_file_path - } - } - }, - text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.cyan - ) + else: + workspace.projects[project_name] = f'src/{project_file_path}' + cls._create_workspace('cpl-workspace.json', workspace.default_project, workspace.projects) Console.spinner( f'Creating {project_file_path}', diff --git a/src/cpl_cli/source_creator/library_builder.py b/src/cpl_cli/source_creator/library_builder.py index 08e1e660..bb233e6a 100644 --- a/src/cpl_cli/source_creator/library_builder.py +++ b/src/cpl_cli/source_creator/library_builder.py @@ -27,6 +27,9 @@ class LibraryBuilder: @staticmethod def _create_file(file_name: str, content: dict): + if not os.path.isabs(file_name): + file_name = os.path.abspath(file_name) + path = os.path.dirname(file_name) if not os.path.isdir(path): os.makedirs(path) @@ -36,8 +39,27 @@ class LibraryBuilder: project_json.close() @classmethod - def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, - project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]): + def _create_workspace(cls, path: str, project_name, projects: dict): + ws_dict = { + WorkspaceSettings.__name__: { + WorkspaceSettingsNameEnum.default_project.value: project_name, + WorkspaceSettingsNameEnum.projects.value: projects + } + } + + Console.spinner( + f'Creating {path}', + cls._create_file, + path, + ws_dict, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + @classmethod + def build(cls, project_path: str, use_application_api: bool, use_startup: bool, + use_service_providing: bool, project_name: str, project_settings: dict, + workspace: Optional[WorkspaceSettings]): """ Builds the library project files :param project_path: @@ -104,22 +126,13 @@ class LibraryBuilder: src_path = f'{proj_name}/src/{project_name_snake}' workspace_file_path = f'{proj_name}/cpl-workspace.json' project_file_path = f'{src_path}/{project_name}.json' + cls._create_workspace(workspace_file_path, project_name, { + project_name: project_file_path + }) - Console.spinner( - f'Creating {workspace_file_path}', - cls._create_file, - workspace_file_path, - { - WorkspaceSettings.__name__: { - WorkspaceSettingsNameEnum.default_project.value: project_name, - WorkspaceSettingsNameEnum.projects.value: { - project_name: project_file_path - } - } - }, - text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.cyan - ) + else: + workspace.projects[project_name] = f'src/{project_file_path}' + cls._create_workspace('cpl-workspace.json', workspace.default_project, workspace.projects) Console.spinner( f'Creating {project_file_path}',