From fc53f90ecd7b7a57dcc72e4f421f05393527402f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 8 Apr 2021 20:58:55 +0200 Subject: [PATCH] Added workspace handling for new command --- src/cpl_cli/command/new_service.py | 6 +- src/cpl_cli/configuration/__init__.py | 2 + src/cpl_cli/source_creator/console_builder.py | 45 +++++++++++++-- src/cpl_cli/source_creator/library_builder.py | 56 +++++++++++++++---- .../templates/new/library/src/cli/__init__.py | 25 --------- .../templates/new/library/src/cli/init.py | 29 ---------- .../library/src/{cli => name}/application.py | 2 +- .../templates/new/library/src/name/init.py | 4 +- .../new/library/src/{cli => name}/main.py | 11 ++-- .../new/library/src/{cli => name}/startup.py | 2 +- 10 files changed, 101 insertions(+), 81 deletions(-) delete mode 100644 src/cpl_cli/templates/new/library/src/cli/__init__.py delete mode 100644 src/cpl_cli/templates/new/library/src/cli/init.py rename src/cpl_cli/templates/new/library/src/{cli => name}/application.py (96%) rename src/cpl_cli/templates/new/library/src/{cli => name}/main.py (93%) rename src/cpl_cli/templates/new/library/src/{cli => name}/startup.py (97%) diff --git a/src/cpl_cli/command/new_service.py b/src/cpl_cli/command/new_service.py index 7b945c25..5f8b92d1 100644 --- a/src/cpl_cli/command/new_service.py +++ b/src/cpl_cli/command/new_service.py @@ -93,12 +93,12 @@ class NewService(CommandABC): def _create_build_settings(self): main = f'{String.convert_to_snake_case(self._project.name)}.main' if self._command == ProjectTypeEnum.library.value: - main = f'{String.convert_to_snake_case(self._project.name)}_cli.main' + main = f'{String.convert_to_snake_case(self._project.name)}.main' self._build_dict = { BuildSettingsNameEnum.project_type.value: self._command, - BuildSettingsNameEnum.source_path.value: 'src', - BuildSettingsNameEnum.output_path.value: 'dist', + BuildSettingsNameEnum.source_path.value: '', + BuildSettingsNameEnum.output_path.value: '../../dist', BuildSettingsNameEnum.main.value: main, BuildSettingsNameEnum.entry_point.value: self._project.name, BuildSettingsNameEnum.include_package_data.value: False, diff --git a/src/cpl_cli/configuration/__init__.py b/src/cpl_cli/configuration/__init__.py index 85d09174..d6a9ffa1 100644 --- a/src/cpl_cli/configuration/__init__.py +++ b/src/cpl_cli/configuration/__init__.py @@ -26,6 +26,8 @@ from .project_settings import ProjectSettings from .project_settings_name_enum import ProjectSettingsNameEnum from .version_settings import VersionSettings from .version_settings_name_enum import VersionSettingsNameEnum +from .workspace_settings import WorkspaceSettings +from .workspace_settings_name_enum import WorkspaceSettingsNameEnum VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major='2021', minor='4', micro='None') diff --git a/src/cpl_cli/source_creator/console_builder.py b/src/cpl_cli/source_creator/console_builder.py index 8d6ad878..6662e80a 100644 --- a/src/cpl_cli/source_creator/console_builder.py +++ b/src/cpl_cli/source_creator/console_builder.py @@ -2,6 +2,9 @@ import json import os from cpl.console.foreground_color_enum import ForegroundColorEnum from cpl.console.console import Console +from cpl.utils.string import String +from cpl_cli.configuration.workspace_settings import WorkspaceSettings +from cpl_cli.configuration.workspace_settings_name_enum import WorkspaceSettingsNameEnum from cpl_cli.source_creator.template_builder import TemplateBuilder from cpl_cli.templates.new.console.appsettings_json import AppsettingsTemplate from cpl_cli.templates.new.console.license import LicenseTemplate @@ -21,7 +24,17 @@ class ConsoleBuilder: pass @staticmethod - def build(project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, + def _create_file(file_name: str, content: dict): + path = os.path.dirname(file_name) + if not os.path.isdir(path): + os.makedirs(path) + + with open(file_name, 'w') as project_json: + project_json.write(json.dumps(content, indent=2)) + 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): """ Builds the console project files @@ -36,10 +49,6 @@ class ConsoleBuilder: if not os.path.isdir(project_path): os.makedirs(project_path) - with open(os.path.join(project_path, 'cpl.json'), 'w') as project_json: - project_json.write(json.dumps(project_settings, indent=2)) - project_json.close() - templates: list[TemplateFileABC] = [ LicenseTemplate(), ReadmeTemplate(), @@ -62,6 +71,32 @@ class ConsoleBuilder: else: templates.append(MainWithoutApplicationBaseTemplate(project_name)) + workspace_file_path = f'{project_name}/cpl-workspace.json' + project_file_rel_path = f'src/{String.convert_to_snake_case(project_name)}/{project_name}.json' + Console.spinner( + f'Creating {workspace_file_path}', + cls._create_file, + workspace_file_path, + { + WorkspaceSettingsNameEnum.default_project.value: project_name, + WorkspaceSettingsNameEnum.projects.value: { + project_name: project_file_rel_path + } + }, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + project_file_path = f'{project_name}/{project_file_rel_path}' + Console.spinner( + f'Creating {project_file_path}', + cls._create_file, + project_file_path, + project_settings, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + for template in templates: Console.spinner( f'Creating {project_name}/{template.path}{template.name}', diff --git a/src/cpl_cli/source_creator/library_builder.py b/src/cpl_cli/source_creator/library_builder.py index bc22ccc4..cbee2bcd 100644 --- a/src/cpl_cli/source_creator/library_builder.py +++ b/src/cpl_cli/source_creator/library_builder.py @@ -2,15 +2,17 @@ import json import os from cpl.console.foreground_color_enum import ForegroundColorEnum from cpl.console.console import Console +from cpl.utils.string import String +from cpl_cli.configuration.workspace_settings_name_enum import WorkspaceSettingsNameEnum from cpl_cli.source_creator.template_builder import TemplateBuilder from cpl_cli.templates.new.library.appsettings_json import AppsettingsTemplate from cpl_cli.templates.new.library.license import LicenseTemplate from cpl_cli.templates.new.library.readme_py import ReadmeTemplate -from cpl_cli.templates.new.library.src.cli.application import ApplicationTemplate -from cpl_cli.templates.new.library.src.cli.init import CLIInitTemplate -from cpl_cli.templates.new.library.src.cli.main import MainWithApplicationHostAndStartupTemplate, \ +from cpl_cli.templates.new.library.src.name.application import ApplicationTemplate +from cpl_cli.templates.new.library.src.name.init import NameInitTemplate +from cpl_cli.templates.new.library.src.name.main import MainWithApplicationHostAndStartupTemplate, \ MainWithoutApplicationBaseTemplate, MainWithApplicationBaseTemplate, MainWithDependencyInjection -from cpl_cli.templates.new.library.src.cli.startup import StartupTemplate +from cpl_cli.templates.new.library.src.name.startup import StartupTemplate from cpl_cli.templates.new.library.src.tests.init import TestsInitTemplate from cpl_cli.templates.template_file_abc import TemplateFileABC @@ -21,7 +23,17 @@ class LibraryBuilder: pass @staticmethod - def build(project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, + def _create_file(file_name: str, content: dict): + path = os.path.dirname(file_name) + if not os.path.isdir(path): + os.makedirs(path) + + with open(file_name, 'w') as project_json: + project_json.write(json.dumps(content, indent=2)) + 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): """ Builds the library project files @@ -36,15 +48,11 @@ class LibraryBuilder: if not os.path.isdir(project_path): os.makedirs(project_path) - with open(os.path.join(project_path, 'cpl.json'), 'w') as project_json: - project_json.write(json.dumps(project_settings, indent=2)) - project_json.close() - templates: list[TemplateFileABC] = [ LicenseTemplate(), ReadmeTemplate(), TestsInitTemplate(), - CLIInitTemplate(project_name), + NameInitTemplate(project_name), AppsettingsTemplate() ] @@ -55,13 +63,39 @@ class LibraryBuilder: templates.append(StartupTemplate(project_name)) templates.append(MainWithApplicationHostAndStartupTemplate(project_name)) else: - templates.append(MainWithApplicationBaseTemplate()) + templates.append(MainWithApplicationBaseTemplate(project_name)) else: if use_service_providing: templates.append(MainWithDependencyInjection()) else: templates.append(MainWithoutApplicationBaseTemplate()) + workspace_file_path = f'{project_name}/cpl-workspace.json' + project_file_rel_path = f'src/{String.convert_to_snake_case(project_name)}/{project_name}.json' + Console.spinner( + f'Creating {workspace_file_path}', + cls._create_file, + workspace_file_path, + { + WorkspaceSettingsNameEnum.default_project.value: project_name, + WorkspaceSettingsNameEnum.projects.value: { + project_name: project_file_rel_path + } + }, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + + project_file_path = f'{project_name}/{project_file_rel_path}' + Console.spinner( + f'Creating {project_file_path}', + cls._create_file, + project_file_path, + project_settings, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.cyan + ) + for template in templates: Console.spinner( f'Creating {project_name}/{template.path}{template.name}', diff --git a/src/cpl_cli/templates/new/library/src/cli/__init__.py b/src/cpl_cli/templates/new/library/src/cli/__init__.py deleted file mode 100644 index 4f440b4a..00000000 --- a/src/cpl_cli/templates/new/library/src/cli/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -sh_cpl-cli sh-edraft Common Python library CLI -~~~~~~~~~~~~~~~~~~~ - -sh-edraft Common Python library Command Line Interface - -:copyright: (c) 2020 - 2021 sh-edraft.de -:license: MIT, see LICENSE for more details. - -""" - -__title__ = 'cpl_cli.templates.new.library.cli' -__author__ = 'Sven Heidemann' -__license__ = 'MIT' -__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de' -__version__ = '2021.4' - -from collections import namedtuple - -# imports: - -VersionInfo = namedtuple('VersionInfo', 'major minor micro') -version_info = VersionInfo(major='2021', minor='4', micro='None') diff --git a/src/cpl_cli/templates/new/library/src/cli/init.py b/src/cpl_cli/templates/new/library/src/cli/init.py deleted file mode 100644 index 28634bf3..00000000 --- a/src/cpl_cli/templates/new/library/src/cli/init.py +++ /dev/null @@ -1,29 +0,0 @@ -import textwrap - -from cpl.utils.string import String -from cpl_cli.templates.template_file_abc import TemplateFileABC - - -class CLIInitTemplate(TemplateFileABC): - - def __init__(self, name: str): - TemplateFileABC.__init__(self) - - name = String.convert_to_snake_case(name) - self._name = '__init__.py' - self._path = f'src/{name}_cli/' - self._value = textwrap.dedent("""\ - # imports: - """) - - @property - def name(self) -> str: - return self._name - - @property - def path(self) -> str: - return self._path - - @property - def value(self) -> str: - return self._value diff --git a/src/cpl_cli/templates/new/library/src/cli/application.py b/src/cpl_cli/templates/new/library/src/name/application.py similarity index 96% rename from src/cpl_cli/templates/new/library/src/cli/application.py rename to src/cpl_cli/templates/new/library/src/name/application.py index 0d92c2cc..516c77aa 100644 --- a/src/cpl_cli/templates/new/library/src/cli/application.py +++ b/src/cpl_cli/templates/new/library/src/name/application.py @@ -11,7 +11,7 @@ class ApplicationTemplate(TemplateFileABC): name = String.convert_to_snake_case(name) self._name = 'application.py' - self._path = f'src/{name}_cli/' + self._path = f'src/{name}/' self._value = textwrap.dedent("""\ from cpl.application import ApplicationABC from cpl.configuration import ConfigurationABC diff --git a/src/cpl_cli/templates/new/library/src/name/init.py b/src/cpl_cli/templates/new/library/src/name/init.py index ebb078a7..d9f74786 100644 --- a/src/cpl_cli/templates/new/library/src/name/init.py +++ b/src/cpl_cli/templates/new/library/src/name/init.py @@ -1,13 +1,15 @@ import textwrap +from cpl.utils.string import String from cpl_cli.templates.template_file_abc import TemplateFileABC -class TestsInitTemplate(TemplateFileABC): +class NameInitTemplate(TemplateFileABC): def __init__(self, name: str): TemplateFileABC.__init__(self) + name = String.convert_to_snake_case(name) self._name = '__init__.py' self._path = f'src/{name}/' self._value = textwrap.dedent("""\ diff --git a/src/cpl_cli/templates/new/library/src/cli/main.py b/src/cpl_cli/templates/new/library/src/name/main.py similarity index 93% rename from src/cpl_cli/templates/new/library/src/cli/main.py rename to src/cpl_cli/templates/new/library/src/name/main.py index 248cdb3e..a3e80509 100644 --- a/src/cpl_cli/templates/new/library/src/cli/main.py +++ b/src/cpl_cli/templates/new/library/src/name/main.py @@ -11,12 +11,12 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC): name = String.convert_to_snake_case(name) self._name = 'main.py' - self._path = f'src/{name}_cli/' + self._path = f'src/{name}/' self._value = textwrap.dedent(f"""\ from cpl.application import ApplicationBuilder - from {name}_cli.application import Application - from {name}_cli.startup import Startup + from {name}.application import Application + from {name}.startup import Startup def main(): @@ -44,15 +44,16 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC): class MainWithApplicationBaseTemplate(TemplateFileABC): - def __init__(self): + def __init__(self, name: str): TemplateFileABC.__init__(self) + name = String.convert_to_snake_case(name) self._name = 'main.py' self._path = 'src/' self._value = textwrap.dedent(f"""\ from cpl.application import ApplicationBuilder - from {name}_cli.application import Application + from {name}.application import Application def main(): diff --git a/src/cpl_cli/templates/new/library/src/cli/startup.py b/src/cpl_cli/templates/new/library/src/name/startup.py similarity index 97% rename from src/cpl_cli/templates/new/library/src/cli/startup.py rename to src/cpl_cli/templates/new/library/src/name/startup.py index 0e33e643..90881b96 100644 --- a/src/cpl_cli/templates/new/library/src/cli/startup.py +++ b/src/cpl_cli/templates/new/library/src/name/startup.py @@ -11,7 +11,7 @@ class StartupTemplate(TemplateFileABC): name = String.convert_to_snake_case(name) self._name = 'startup.py' - self._path = f'src/{name}_cli/' + self._path = f'src/{name}/' self._value = textwrap.dedent("""\ from cpl.application import StartupABC from cpl.configuration import ConfigurationABC