2021-03-30 12:44:31 +02:00
|
|
|
import json
|
|
|
|
import os
|
2021-04-09 22:41:04 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-03-30 12:44:31 +02:00
|
|
|
from cpl.console.foreground_color_enum import ForegroundColorEnum
|
|
|
|
from cpl.console.console import Console
|
2021-04-08 20:58:55 +02:00
|
|
|
from cpl.utils.string import String
|
2021-04-09 22:41:04 +02:00
|
|
|
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
|
2021-04-08 20:58:55 +02:00
|
|
|
from cpl_cli.configuration.workspace_settings_name_enum import WorkspaceSettingsNameEnum
|
2021-03-30 12:44:31 +02:00
|
|
|
from cpl_cli.source_creator.template_builder import TemplateBuilder
|
2021-04-14 10:58:47 +02:00
|
|
|
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.source.name.application import ApplicationTemplate
|
|
|
|
from cpl_cli._templates.new.console.source.name.init import MainInitTemplate
|
|
|
|
from cpl_cli._templates.new.console.source.name.main import MainWithApplicationHostAndStartupTemplate, \
|
2021-03-30 12:44:31 +02:00
|
|
|
MainWithoutApplicationBaseTemplate, MainWithApplicationBaseTemplate, MainWithDependencyInjection
|
2021-04-14 10:58:47 +02:00
|
|
|
from cpl_cli._templates.new.console.source.name.startup import StartupTemplate
|
|
|
|
from cpl_cli._templates.new.console.source.tests.init import TestsInitTemplate
|
|
|
|
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
2021-03-30 12:44:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConsoleBuilder:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
2021-04-08 20:58:55 +02:00
|
|
|
def _create_file(file_name: str, content: dict):
|
2021-04-10 11:50:35 +02:00
|
|
|
if not os.path.isabs(file_name):
|
|
|
|
file_name = os.path.abspath(file_name)
|
|
|
|
|
2021-04-08 20:58:55 +02:00
|
|
|
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()
|
|
|
|
|
2021-04-10 11:46:36 +02:00
|
|
|
@classmethod
|
|
|
|
def _create_workspace(cls, path: str, project_name, projects: dict):
|
|
|
|
ws_dict = {
|
|
|
|
WorkspaceSettings.__name__: {
|
|
|
|
WorkspaceSettingsNameEnum.default_project.value: project_name,
|
2021-07-25 19:18:06 +02:00
|
|
|
WorkspaceSettingsNameEnum.projects.value: projects,
|
|
|
|
WorkspaceSettingsNameEnum.scripts: {}
|
2021-04-10 11:46:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.spinner(
|
|
|
|
f'Creating {path}',
|
|
|
|
cls._create_file,
|
|
|
|
path,
|
|
|
|
ws_dict,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
|
|
|
|
2021-04-08 20:58:55 +02:00
|
|
|
@classmethod
|
|
|
|
def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool,
|
2021-04-09 22:41:04 +02:00
|
|
|
project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]):
|
2021-03-30 12:44:31 +02:00
|
|
|
"""
|
|
|
|
Builds the console project files
|
|
|
|
:param project_path:
|
|
|
|
:param use_application_api:
|
|
|
|
:param use_startup:
|
|
|
|
:param use_service_providing:
|
|
|
|
:param project_name:
|
|
|
|
:param project_settings:
|
2021-04-09 22:41:04 +02:00
|
|
|
:param workspace:
|
2021-03-30 12:44:31 +02:00
|
|
|
:return:
|
|
|
|
"""
|
2021-04-09 22:41:04 +02:00
|
|
|
project_name_snake = String.convert_to_snake_case(project_name)
|
|
|
|
|
|
|
|
if workspace is None:
|
|
|
|
templates: list[TemplateFileABC] = [
|
|
|
|
LicenseTemplate(),
|
|
|
|
ReadmeTemplate(),
|
|
|
|
TestsInitTemplate(),
|
|
|
|
AppsettingsTemplate(),
|
2021-05-19 08:28:59 +02:00
|
|
|
MainInitTemplate(project_name, os.path.join('src/', project_name_snake))
|
2021-04-09 22:41:04 +02:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
project_path = os.path.join(
|
|
|
|
os.path.dirname(project_path),
|
|
|
|
project_name_snake
|
|
|
|
)
|
|
|
|
|
|
|
|
templates: list[TemplateFileABC] = [
|
|
|
|
LicenseTemplate(),
|
|
|
|
ReadmeTemplate(),
|
|
|
|
AppsettingsTemplate(),
|
|
|
|
MainInitTemplate('', '')
|
|
|
|
]
|
|
|
|
|
2021-03-30 12:44:31 +02:00
|
|
|
if not os.path.isdir(project_path):
|
|
|
|
os.makedirs(project_path)
|
|
|
|
|
2021-04-09 22:41:04 +02:00
|
|
|
src_rel_path = ''
|
2021-05-19 08:01:02 +02:00
|
|
|
src_name = project_name_snake
|
2021-04-09 22:41:04 +02:00
|
|
|
if workspace is None:
|
2021-05-19 08:01:02 +02:00
|
|
|
src_rel_path = os.path.join('src/', src_name)
|
2021-03-30 12:44:31 +02:00
|
|
|
|
|
|
|
if use_application_api:
|
2021-04-09 22:41:04 +02:00
|
|
|
templates.append(ApplicationTemplate(src_name, src_rel_path))
|
2021-03-30 12:44:31 +02:00
|
|
|
|
|
|
|
if use_startup:
|
2021-04-09 22:41:04 +02:00
|
|
|
templates.append(StartupTemplate(src_name, src_rel_path))
|
|
|
|
templates.append(MainWithApplicationHostAndStartupTemplate(src_name, src_rel_path))
|
2021-03-30 12:44:31 +02:00
|
|
|
else:
|
2021-04-09 22:41:04 +02:00
|
|
|
templates.append(MainWithApplicationBaseTemplate(src_name, src_rel_path))
|
2021-03-30 12:44:31 +02:00
|
|
|
else:
|
|
|
|
if use_service_providing:
|
2021-04-09 22:41:04 +02:00
|
|
|
templates.append(MainWithDependencyInjection(src_name, src_rel_path))
|
2021-03-30 12:44:31 +02:00
|
|
|
else:
|
2021-04-09 22:41:04 +02:00
|
|
|
templates.append(MainWithoutApplicationBaseTemplate(src_name, src_rel_path))
|
2021-03-30 12:44:31 +02:00
|
|
|
|
2021-04-09 22:41:04 +02:00
|
|
|
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:
|
2021-05-19 08:01:02 +02:00
|
|
|
src_path = f'src/{project_name_snake}'
|
2021-04-09 22:41:04 +02:00
|
|
|
workspace_file_path = f'{proj_name}/cpl-workspace.json'
|
2021-05-19 08:01:02 +02:00
|
|
|
project_file_rel_path = f'{src_path}/{project_name}.json'
|
|
|
|
project_file_path = f'{proj_name}/{src_path}/{project_name}.json'
|
2021-04-10 11:46:36 +02:00
|
|
|
cls._create_workspace(workspace_file_path, project_name, {
|
2021-05-19 08:01:02 +02:00
|
|
|
project_name: project_file_rel_path
|
2021-04-10 11:46:36 +02:00
|
|
|
})
|
2021-04-09 22:41:04 +02:00
|
|
|
|
2021-04-10 11:46:36 +02:00
|
|
|
else:
|
|
|
|
workspace.projects[project_name] = f'src/{project_file_path}'
|
|
|
|
cls._create_workspace('cpl-workspace.json', workspace.default_project, workspace.projects)
|
2021-04-08 20:58:55 +02:00
|
|
|
|
|
|
|
Console.spinner(
|
|
|
|
f'Creating {project_file_path}',
|
|
|
|
cls._create_file,
|
2021-04-09 22:41:04 +02:00
|
|
|
project_file_path if workspace is None else f'src/{project_file_path}',
|
2021-04-08 20:58:55 +02:00
|
|
|
project_settings,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|
|
|
|
|
2021-03-30 12:44:31 +02:00
|
|
|
for template in templates:
|
2021-04-10 11:01:56 +02:00
|
|
|
divider = ''
|
2021-04-10 11:14:37 +02:00
|
|
|
if template.path != '' and not template.path.endswith('/'):
|
2021-04-10 11:01:56 +02:00
|
|
|
divider = '/'
|
|
|
|
|
2021-03-30 12:44:31 +02:00
|
|
|
Console.spinner(
|
2021-04-10 11:01:56 +02:00
|
|
|
f'Creating {proj_name}/{template.path}{divider}{template.name}',
|
2021-03-30 12:44:31 +02:00
|
|
|
TemplateBuilder.build,
|
|
|
|
project_path,
|
|
|
|
template,
|
|
|
|
text_foreground_color=ForegroundColorEnum.green,
|
|
|
|
spinner_foreground_color=ForegroundColorEnum.cyan
|
|
|
|
)
|