Improved workspace handling to new command for console type

This commit is contained in:
Sven Heidemann 2021-04-09 22:41:04 +02:00
parent 167f813bcf
commit 2b5c75cc67
6 changed files with 84 additions and 47 deletions

View File

@ -182,7 +182,8 @@ class NewService(CommandABC):
self._use_startup,
self._use_service_providing,
self._project.name,
self._project_json
self._project_json,
self._workspace
)
except Exception as e:
Console.error('Could not create project', str(e))

View File

@ -1,8 +1,11 @@
import json
import os
from typing import Optional
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
@ -34,7 +37,7 @@ class ConsoleBuilder:
@classmethod
def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool,
project_name: str, project_settings: dict):
project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]):
"""
Builds the console project files
:param project_path:
@ -43,35 +46,65 @@ class ConsoleBuilder:
:param use_service_providing:
:param project_name:
:param project_settings:
:param workspace:
:return:
"""
if not os.path.isdir(project_path):
os.makedirs(project_path)
project_name_snake = String.convert_to_snake_case(project_name)
if workspace is None:
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
TestsInitTemplate(),
AppsettingsTemplate(),
MainInitTemplate(project_name)
MainInitTemplate(project_name, 'src')
]
else:
project_path = os.path.join(
os.path.dirname(project_path),
project_name_snake
)
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
AppsettingsTemplate(),
MainInitTemplate('', '')
]
if not os.path.isdir(project_path):
os.makedirs(project_path)
src_rel_path = ''
src_name = ''
if workspace is None:
src_rel_path = 'src/'
src_name = project_name_snake
if use_application_api:
templates.append(ApplicationTemplate(project_name))
templates.append(ApplicationTemplate(src_name, src_rel_path))
if use_startup:
templates.append(StartupTemplate(project_name))
templates.append(MainWithApplicationHostAndStartupTemplate(project_name))
templates.append(StartupTemplate(src_name, src_rel_path))
templates.append(MainWithApplicationHostAndStartupTemplate(src_name, src_rel_path))
else:
templates.append(MainWithApplicationBaseTemplate(project_name))
templates.append(MainWithApplicationBaseTemplate(src_name, src_rel_path))
else:
if use_service_providing:
templates.append(MainWithDependencyInjection(project_name))
templates.append(MainWithDependencyInjection(src_name, src_rel_path))
else:
templates.append(MainWithoutApplicationBaseTemplate(project_name))
templates.append(MainWithoutApplicationBaseTemplate(src_name, src_rel_path))
proj_name = project_name
if workspace is not None:
proj_name = project_name_snake
project_file_path = f'{project_name_snake}/{project_name}.json'
if workspace is None:
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'
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,
@ -79,18 +112,17 @@ class ConsoleBuilder:
{
WorkspaceSettingsNameEnum.default_project.value: project_name,
WorkspaceSettingsNameEnum.projects.value: {
project_name: project_file_rel_path
project_name: project_file_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_file_path if workspace is None else f'src/{project_file_path}',
project_settings,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
@ -98,7 +130,7 @@ class ConsoleBuilder:
for template in templates:
Console.spinner(
f'Creating {project_name}/{template.path}{template.name}',
f'Creating {proj_name}/{template.path}{template.name}',
TemplateBuilder.build,
project_path,
template,

View File

@ -1,3 +1,4 @@
import os.path
import textwrap
from cpl.utils.string import String
@ -6,12 +7,12 @@ from cpl_cli.templates.template_file_abc import TemplateFileABC
class ApplicationTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'application.py'
self._path = f'src/{name}/'
self._path = os.path.join(path, name)
self._value = textwrap.dedent("""\
from cpl.application import ApplicationABC
from cpl.configuration import ConfigurationABC

View File

@ -1,3 +1,4 @@
import os.path
import textwrap
from cpl.utils.string import String
@ -6,12 +7,12 @@ from cpl_cli.templates.template_file_abc import TemplateFileABC
class MainInitTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = '__init__.py'
self._path = f'src/{name}/'
self._path = os.path.join(path, name)
self._value = textwrap.dedent("""\
# imports:
""")

View File

@ -1,3 +1,4 @@
import os.path
import textwrap
from cpl.utils.string import String
@ -6,12 +7,12 @@ from cpl_cli.templates.template_file_abc import TemplateFileABC
class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'main.py'
self._path = f'src/{name}/'
self._path = os.path.join(path, name)
self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder
@ -44,12 +45,12 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
class MainWithApplicationBaseTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'main.py'
self._path = f'src/{name}'
self._path = os.path.join(path, name)
self._value = textwrap.dedent(f"""\
from cpl.application import ApplicationBuilder
@ -80,12 +81,12 @@ class MainWithApplicationBaseTemplate(TemplateFileABC):
class MainWithoutApplicationBaseTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'main.py'
self._path = f'src/{name}'
self._path = os.path.join(path, name)
self._value = textwrap.dedent("""\
from cpl.console import Console
@ -113,12 +114,12 @@ class MainWithoutApplicationBaseTemplate(TemplateFileABC):
class MainWithDependencyInjection(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'main.py'
self._path = f'src/{name}'
self._path = os.path.join(path, name)
self._value = textwrap.dedent("""\
from cpl.configuration import Configuration, ConfigurationABC
from cpl.console import Console

View File

@ -1,3 +1,4 @@
import os.path
import textwrap
from cpl.utils.string import String
@ -6,12 +7,12 @@ from cpl_cli.templates.template_file_abc import TemplateFileABC
class StartupTemplate(TemplateFileABC):
def __init__(self, name: str):
def __init__(self, name: str, path: str):
TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name)
self._name = 'startup.py'
self._path = f'src/{name}/'
self._path = os.path.join(path, name)
self._value = textwrap.dedent("""\
from cpl.application import StartupABC
from cpl.configuration import ConfigurationABC