sh_cpl/src/cpl_cli/source_creator/console_builder.py

108 lines
4.2 KiB
Python
Raw Normal View History

2021-03-30 12:44:31 +02:00
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
2021-03-30 12:44:31 +02:00
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.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
from cpl_cli.templates.new.console.source.name.startup import StartupTemplate
from cpl_cli.templates.new.console.source.tests.init import TestsInitTemplate
2021-03-30 12:44:31 +02:00
from cpl_cli.templates.template_file_abc import TemplateFileABC
class ConsoleBuilder:
def __init__(self):
pass
@staticmethod
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,
2021-03-30 12:44:31 +02:00
project_name: str, project_settings: dict):
"""
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:
:return:
"""
if not os.path.isdir(project_path):
os.makedirs(project_path)
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
TestsInitTemplate(),
2021-03-31 09:46:41 +02:00
AppsettingsTemplate(),
MainInitTemplate(project_name)
2021-03-30 12:44:31 +02:00
]
if use_application_api:
2021-03-31 09:46:41 +02:00
templates.append(ApplicationTemplate(project_name))
2021-03-30 12:44:31 +02:00
if use_startup:
2021-03-31 09:46:41 +02:00
templates.append(StartupTemplate(project_name))
templates.append(MainWithApplicationHostAndStartupTemplate(project_name))
2021-03-30 12:44:31 +02:00
else:
2021-03-31 09:46:41 +02:00
templates.append(MainWithApplicationBaseTemplate(project_name))
2021-03-30 12:44:31 +02:00
else:
if use_service_providing:
2021-03-31 09:46:41 +02:00
templates.append(MainWithDependencyInjection(project_name))
2021-03-30 12:44:31 +02:00
else:
2021-03-31 09:46:41 +02:00
templates.append(MainWithoutApplicationBaseTemplate(project_name))
2021-03-30 12:44:31 +02:00
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
)
2021-03-30 12:44:31 +02:00
for template in templates:
Console.spinner(
f'Creating {project_name}/{template.path}{template.name}',
TemplateBuilder.build,
project_path,
template,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)