From b6d508526a219cb9e6d262ae5022f647063f84ef Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 31 Mar 2021 09:46:41 +0200 Subject: [PATCH] Bugfixes for the command new --- src/cpl_cli/publish/publisher_service.py | 52 +++++++++++++------ src/cpl_cli/source_creator/console_builder.py | 22 ++++---- .../new/console/src/name/__init__.py | 0 .../new/console/src/{ => name}/application.py | 6 ++- .../templates/new/console/src/name/init.py | 29 +++++++++++ .../new/console/src/{ => name}/main.py | 31 ++++++----- .../new/console/src/{ => name}/startup.py | 6 ++- .../templates/new/library/src/cli/main.py | 2 +- 8 files changed, 104 insertions(+), 44 deletions(-) create mode 100644 src/cpl_cli/templates/new/console/src/name/__init__.py rename src/cpl_cli/templates/new/console/src/{ => name}/application.py (87%) create mode 100644 src/cpl_cli/templates/new/console/src/name/init.py rename src/cpl_cli/templates/new/console/src/{ => name}/main.py (81%) rename src/cpl_cli/templates/new/console/src/{ => name}/startup.py (89%) diff --git a/src/cpl_cli/publish/publisher_service.py b/src/cpl_cli/publish/publisher_service.py index 18c757ec..3918fd48 100644 --- a/src/cpl_cli/publish/publisher_service.py +++ b/src/cpl_cli/publish/publisher_service.py @@ -13,6 +13,7 @@ from setuptools import sandbox from cpl.console.foreground_color_enum import ForegroundColorEnum from cpl.console.console import Console from cpl.environment.application_environment_abc import ApplicationEnvironmentABC +from cpl.utils.string import String from cpl_cli.configuration.build_settings import BuildSettings from cpl_cli.configuration.project_settings import ProjectSettings from cpl_cli.configuration.project_type_enum import ProjectTypeEnum @@ -292,11 +293,11 @@ class PublisherService(PublisherABC): main = None try: - main_name = '' + main_name = self._build_settings.main if '.' in self._build_settings.main: length = len(self._build_settings.main.split('.')) - main_name = self._build_settings.main.split('.')[length-1] + main_name = self._build_settings.main.split('.')[length - 1] sys.path.insert(0, self._source_path) main_mod = __import__(self._build_settings.main) @@ -406,25 +407,44 @@ class PublisherService(PublisherABC): 4. Remove all included source from dist_path/publish :return: """ - if self._build_settings.project_type != ProjectTypeEnum.library.value: - Console.error(f'Project must be a {ProjectTypeEnum.library.value} for publishing.') - return self._output_path = os.path.join(self._output_path, 'publish') Console.write_line('Build:') - Console.spinner('Reading source files:', self._read_sources, text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.blue) - Console.spinner('Creating internal packages:', self._create_packages, - text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.blue) - Console.spinner('Building application:', self._dist_files, text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.blue) + Console.spinner( + 'Reading source files:', + self._read_sources, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.blue + ) + + Console.spinner( + 'Creating internal packages:', + self._create_packages, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.blue + ) + + Console.spinner( + 'Building application:', + self._dist_files, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.blue + ) Console.write_line('\nPublish:') - Console.spinner('Generating setup.py:', self._create_setup, text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.blue) + Console.spinner( + 'Generating setup.py:', + self._create_setup, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.blue + ) + Console.write_line('Running setup.py:\n') self._run_setup() - Console.spinner('Cleaning dist path:', self._clean_dist_files, text_foreground_color=ForegroundColorEnum.green, - spinner_foreground_color=ForegroundColorEnum.blue) + Console.spinner( + 'Cleaning dist path:', + self._clean_dist_files, + text_foreground_color=ForegroundColorEnum.green, + spinner_foreground_color=ForegroundColorEnum.blue + ) diff --git a/src/cpl_cli/source_creator/console_builder.py b/src/cpl_cli/source_creator/console_builder.py index 86ea8494..8d6ad878 100644 --- a/src/cpl_cli/source_creator/console_builder.py +++ b/src/cpl_cli/source_creator/console_builder.py @@ -6,10 +6,11 @@ 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 from cpl_cli.templates.new.console.readme_py import ReadmeTemplate -from cpl_cli.templates.new.console.src.application import ApplicationTemplate -from cpl_cli.templates.new.console.src.main import MainWithApplicationHostAndStartupTemplate, \ +from cpl_cli.templates.new.console.src.name.application import ApplicationTemplate +from cpl_cli.templates.new.console.src.name.init import MainInitTemplate +from cpl_cli.templates.new.console.src.name.main import MainWithApplicationHostAndStartupTemplate, \ MainWithoutApplicationBaseTemplate, MainWithApplicationBaseTemplate, MainWithDependencyInjection -from cpl_cli.templates.new.console.src.startup import StartupTemplate +from cpl_cli.templates.new.console.src.name.startup import StartupTemplate from cpl_cli.templates.new.console.src.tests.init import TestsInitTemplate from cpl_cli.templates.template_file_abc import TemplateFileABC @@ -43,22 +44,23 @@ class ConsoleBuilder: LicenseTemplate(), ReadmeTemplate(), TestsInitTemplate(), - AppsettingsTemplate() + AppsettingsTemplate(), + MainInitTemplate(project_name) ] if use_application_api: - templates.append(ApplicationTemplate()) + templates.append(ApplicationTemplate(project_name)) if use_startup: - templates.append(StartupTemplate()) - templates.append(MainWithApplicationHostAndStartupTemplate()) + 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()) + templates.append(MainWithDependencyInjection(project_name)) else: - templates.append(MainWithoutApplicationBaseTemplate()) + templates.append(MainWithoutApplicationBaseTemplate(project_name)) for template in templates: Console.spinner( diff --git a/src/cpl_cli/templates/new/console/src/name/__init__.py b/src/cpl_cli/templates/new/console/src/name/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/cpl_cli/templates/new/console/src/application.py b/src/cpl_cli/templates/new/console/src/name/application.py similarity index 87% rename from src/cpl_cli/templates/new/console/src/application.py rename to src/cpl_cli/templates/new/console/src/name/application.py index 65c33d12..516c77aa 100644 --- a/src/cpl_cli/templates/new/console/src/application.py +++ b/src/cpl_cli/templates/new/console/src/name/application.py @@ -1,15 +1,17 @@ import textwrap +from cpl.utils.string import String from cpl_cli.templates.template_file_abc import TemplateFileABC class ApplicationTemplate(TemplateFileABC): - def __init__(self): + def __init__(self, name: str): TemplateFileABC.__init__(self) + name = String.convert_to_snake_case(name) self._name = 'application.py' - self._path = 'src/' + 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/console/src/name/init.py b/src/cpl_cli/templates/new/console/src/name/init.py new file mode 100644 index 00000000..9cdd21ad --- /dev/null +++ b/src/cpl_cli/templates/new/console/src/name/init.py @@ -0,0 +1,29 @@ +import textwrap + +from cpl.utils.string import String +from cpl_cli.templates.template_file_abc import TemplateFileABC + + +class MainInitTemplate(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("""\ + # 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/console/src/main.py b/src/cpl_cli/templates/new/console/src/name/main.py similarity index 81% rename from src/cpl_cli/templates/new/console/src/main.py rename to src/cpl_cli/templates/new/console/src/name/main.py index 9ed2afa1..7a3cb7ac 100644 --- a/src/cpl_cli/templates/new/console/src/main.py +++ b/src/cpl_cli/templates/new/console/src/name/main.py @@ -1,20 +1,22 @@ import textwrap +from cpl.utils.string import String from cpl_cli.templates.template_file_abc import TemplateFileABC class MainWithApplicationHostAndStartupTemplate(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("""\ + self._path = f'src/{name}/' + self._value = textwrap.dedent(f"""\ from cpl.application import ApplicationBuilder - from application import Application - from startup import Startup + from {name}.application import Application + from {name}.startup import Startup def main(): @@ -42,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("""\ + self._path = f'src/{name}' + self._value = textwrap.dedent(f"""\ from cpl.application import ApplicationBuilder - from application import Application + from {name}.application import Application def main(): @@ -77,11 +80,12 @@ class MainWithApplicationBaseTemplate(TemplateFileABC): class MainWithoutApplicationBaseTemplate(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._path = f'src/{name}' self._value = textwrap.dedent("""\ from cpl.console import Console @@ -109,11 +113,12 @@ class MainWithoutApplicationBaseTemplate(TemplateFileABC): class MainWithDependencyInjection(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._path = f'src/{name}' self._value = textwrap.dedent("""\ from cpl.configuration import Configuration, ConfigurationABC from cpl.console import Console diff --git a/src/cpl_cli/templates/new/console/src/startup.py b/src/cpl_cli/templates/new/console/src/name/startup.py similarity index 89% rename from src/cpl_cli/templates/new/console/src/startup.py rename to src/cpl_cli/templates/new/console/src/name/startup.py index 89a1cf40..90881b96 100644 --- a/src/cpl_cli/templates/new/console/src/startup.py +++ b/src/cpl_cli/templates/new/console/src/name/startup.py @@ -1,15 +1,17 @@ import textwrap +from cpl.utils.string import String from cpl_cli.templates.template_file_abc import TemplateFileABC class StartupTemplate(TemplateFileABC): - def __init__(self): + def __init__(self, name: str): TemplateFileABC.__init__(self) + name = String.convert_to_snake_case(name) self._name = 'startup.py' - self._path = 'src/' + self._path = f'src/{name}/' self._value = textwrap.dedent("""\ from cpl.application import StartupABC from cpl.configuration import ConfigurationABC diff --git a/src/cpl_cli/templates/new/library/src/cli/main.py b/src/cpl_cli/templates/new/library/src/cli/main.py index 655f638b..6d96f256 100644 --- a/src/cpl_cli/templates/new/library/src/cli/main.py +++ b/src/cpl_cli/templates/new/library/src/cli/main.py @@ -47,7 +47,7 @@ class MainWithApplicationBaseTemplate(TemplateFileABC): self._name = 'main.py' self._path = 'src/' - self._value = textwrap.dedent("""\ + self._value = textwrap.dedent(f"""\ from cpl.application import ApplicationBuilder from {name}_cli.application import Application