Improved new command to use workspace when found

This commit is contained in:
Sven Heidemann 2021-04-09 22:09:55 +02:00
parent d19c47e571
commit 7ad01d4bdd
8 changed files with 105 additions and 59 deletions

View File

@ -16,7 +16,7 @@ class String:
pattern1 = re.compile(r'(.)([A-Z][a-z]+)') pattern1 = re.compile(r'(.)([A-Z][a-z]+)')
pattern2 = re.compile(r'([a-z0-9])([A-Z])') pattern2 = re.compile(r'([a-z0-9])([A-Z])')
file_name = re.sub(pattern1, r'\1_\2', name) file_name = re.sub(pattern1, r'\1_\2', name)
return re.sub(pattern2, r'\1_\2', file_name).lower() return re.sub(pattern2, r'\1_\2', file_name).lower().replace('-', '_')
@staticmethod @staticmethod
def first_to_upper(string: str) -> str: def first_to_upper(string: str) -> str:

View File

@ -18,6 +18,7 @@ from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
from cpl_cli.configuration.project_type_enum import ProjectTypeEnum from cpl_cli.configuration.project_type_enum import ProjectTypeEnum
from cpl_cli.configuration.version_settings_name_enum import VersionSettingsNameEnum from cpl_cli.configuration.version_settings_name_enum import VersionSettingsNameEnum
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
from cpl_cli.source_creator.console_builder import ConsoleBuilder from cpl_cli.source_creator.console_builder import ConsoleBuilder
from cpl_cli.source_creator.library_builder import LibraryBuilder from cpl_cli.source_creator.library_builder import LibraryBuilder
@ -34,6 +35,7 @@ class NewService(CommandABC):
self._config = configuration self._config = configuration
self._env = self._config.environment self._env = self._config.environment
self._workspace = self._config.get_configuration(WorkspaceSettings)
self._project: ProjectSettings = ProjectSettings() self._project: ProjectSettings = ProjectSettings()
self._project_dict = {} self._project_dict = {}
self._build: BuildSettings = BuildSettings() self._build: BuildSettings = BuildSettings()
@ -127,7 +129,11 @@ class NewService(CommandABC):
Gets project path Gets project path
:return: :return:
""" """
project_path = os.path.join(self._env.working_directory, self._project.name) if self._workspace is None:
project_path = os.path.join(self._env.working_directory, self._project.name)
else:
project_path = os.path.join(self._env.working_directory, 'src', self._project.name)
if os.path.isdir(project_path) and len(os.listdir(project_path)) > 0: if os.path.isdir(project_path) and len(os.listdir(project_path)) > 0:
Console.error('Project path is not empty\n') Console.error('Project path is not empty\n')
return None return None
@ -204,7 +210,8 @@ class NewService(CommandABC):
self._use_startup, self._use_startup,
self._use_service_providing, self._use_service_providing,
self._project.name, self._project.name,
self._project_json self._project_json,
self._workspace
) )
except Exception as e: except Exception as e:
Console.error('Could not create project', str(e)) Console.error('Could not create project', str(e))
@ -219,6 +226,8 @@ class NewService(CommandABC):
self._help('Usage: cpl new <schematic> [options]') self._help('Usage: cpl new <schematic> [options]')
return return
Console.write_line(1, self._workspace)
self._command = str(args[0]).lower() self._command = str(args[0]).lower()
if self._command == ProjectTypeEnum.console.value: if self._command == ProjectTypeEnum.console.value:
self._console(args) self._console(args)

View File

@ -1,5 +1,4 @@
import os import os
import sys
from abc import ABC from abc import ABC
from typing import Optional from typing import Optional
@ -49,16 +48,16 @@ class CommandHandler(ABC):
""" """
for command in self._commands: for command in self._commands:
if cmd == command.name or cmd in command.aliases: if cmd == command.name or cmd in command.aliases:
error = None
project_name: Optional[str] = None
workspace: Optional[WorkspaceSettings] = None
if os.path.isfile(os.path.join(self._env.working_directory, 'cpl-workspace.json')):
self._config.add_json_file('cpl-workspace.json', optional=True, output=False)
workspace = self._config.get_configuration(WorkspaceSettings)
if command.is_project_needed: if command.is_project_needed:
error = None if os.path.isfile(
project_name: Optional[str] = None
workspace: Optional[WorkspaceSettings] = None
if os.path.isfile(os.path.join(self._env.working_directory, 'cpl-workspace.json')):
self._config.add_json_file('cpl-workspace.json', optional=True, output=False)
workspace = self._config.get_configuration(WorkspaceSettings)
elif os.path.isfile(
os.path.join( os.path.join(
self._env.working_directory, self._env.working_directory,
f'{os.path.basename(self._env.working_directory)}.json' f'{os.path.basename(self._env.working_directory)}.json'

View File

@ -1,8 +1,11 @@
import json import json
import os import os
from typing import Optional
from cpl.console.foreground_color_enum import ForegroundColorEnum from cpl.console.foreground_color_enum import ForegroundColorEnum
from cpl.console.console import Console from cpl.console.console import Console
from cpl.utils.string import String 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.configuration.workspace_settings_name_enum import WorkspaceSettingsNameEnum
from cpl_cli.source_creator.template_builder import TemplateBuilder 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.appsettings_json import AppsettingsTemplate
@ -34,7 +37,7 @@ class LibraryBuilder:
@classmethod @classmethod
def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, 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 library project files Builds the library project files
:param project_path: :param project_path:
@ -43,54 +46,85 @@ class LibraryBuilder:
:param use_service_providing: :param use_service_providing:
:param project_name: :param project_name:
:param project_settings: :param project_settings:
:param workspace:
:return: :return:
""" """
project_name_snake = String.convert_to_snake_case(project_name)
if workspace is None:
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
TestsInitTemplate(),
NameInitTemplate(project_name, 'src/'),
AppsettingsTemplate()
]
else:
project_path = os.path.join(
os.path.dirname(project_path),
project_name_snake
)
Console.write_line(project_path)
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
NameInitTemplate('', ''),
AppsettingsTemplate()
]
if not os.path.isdir(project_path): if not os.path.isdir(project_path):
os.makedirs(project_path) os.makedirs(project_path)
templates: list[TemplateFileABC] = [ src_rel_path = ''
LicenseTemplate(), src_name = ''
ReadmeTemplate(), if workspace is None:
TestsInitTemplate(), src_rel_path = 'src/'
NameInitTemplate(project_name), src_name = project_name_snake
AppsettingsTemplate()
]
if use_application_api: if use_application_api:
templates.append(ApplicationTemplate(project_name)) templates.append(ApplicationTemplate(src_name, src_rel_path))
if use_startup: if use_startup:
templates.append(StartupTemplate(project_name)) templates.append(StartupTemplate(src_name, src_rel_path))
templates.append(MainWithApplicationHostAndStartupTemplate(project_name)) templates.append(MainWithApplicationHostAndStartupTemplate(src_name, src_rel_path))
else: else:
templates.append(MainWithApplicationBaseTemplate(project_name)) templates.append(MainWithApplicationBaseTemplate(src_name, src_rel_path))
else: else:
if use_service_providing: if use_service_providing:
templates.append(MainWithDependencyInjection()) templates.append(MainWithDependencyInjection(src_name, src_rel_path))
else: else:
templates.append(MainWithoutApplicationBaseTemplate()) templates.append(MainWithoutApplicationBaseTemplate(src_name, src_rel_path))
workspace_file_path = f'{project_name}/cpl-workspace.json' proj_name = project_name
project_file_rel_path = f'src/{String.convert_to_snake_case(project_name)}/{project_name}.json' if workspace is not None:
Console.spinner( proj_name = project_name_snake
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}' 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'
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_path
}
},
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)
Console.write_line(project_file_path)
Console.spinner( Console.spinner(
f'Creating {project_file_path}', f'Creating {project_file_path}',
cls._create_file, cls._create_file,
project_file_path, project_file_path if workspace is None else f'src/{project_file_path}',
project_settings, project_settings,
text_foreground_color=ForegroundColorEnum.green, text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan spinner_foreground_color=ForegroundColorEnum.cyan
@ -98,7 +132,7 @@ class LibraryBuilder:
for template in templates: for template in templates:
Console.spinner( Console.spinner(
f'Creating {project_name}/{template.path}{template.name}', f'Creating {proj_name}/{template.path}{template.name}',
TemplateBuilder.build, TemplateBuilder.build,
project_path, project_path,
template, template,

View File

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

View File

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

View File

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

View File

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