sh_cpl/src/cpl_cli/command/new_service.py

239 lines
8.0 KiB
Python
Raw Normal View History

2021-03-09 22:29:14 +01:00
import os
import sys
2021-03-10 11:28:04 +01:00
from typing import Optional
2021-03-09 22:29:14 +01:00
2021-03-14 11:27:01 +01:00
from packaging import version
import cpl
2021-03-09 22:29:14 +01:00
from cpl.configuration.configuration_abc import ConfigurationABC
2021-03-12 16:06:30 +01:00
from cpl.console.foreground_color_enum import ForegroundColorEnum
2021-03-09 22:29:14 +01:00
from cpl.console.console import Console
2021-03-31 10:47:14 +02:00
from cpl.utils.string import String
2021-03-09 22:29:14 +01:00
from cpl_cli.command_abc import CommandABC
from cpl_cli.configuration.build_settings import BuildSettings
2021-03-13 22:53:28 +01:00
from cpl_cli.configuration.build_settings_name_enum import BuildSettingsNameEnum
2021-03-09 22:29:14 +01:00
from cpl_cli.configuration.project_settings import ProjectSettings
2021-03-13 22:53:28 +01:00
from cpl_cli.configuration.project_settings_name_enum import ProjectSettingsNameEnum
2021-03-30 12:44:31 +02:00
from cpl_cli.configuration.project_type_enum import ProjectTypeEnum
2021-03-12 16:10:43 +01:00
from cpl_cli.configuration.version_settings_name_enum import VersionSettingsNameEnum
from cpl_cli.configuration.workspace_settings import WorkspaceSettings
2021-03-30 12:44:31 +02:00
from cpl_cli.source_creator.console_builder import ConsoleBuilder
from cpl_cli.source_creator.library_builder import LibraryBuilder
2021-03-09 22:29:14 +01:00
2021-03-12 16:06:30 +01:00
class NewService(CommandABC):
2021-03-09 22:29:14 +01:00
def __init__(self, configuration: ConfigurationABC):
2021-03-14 16:01:15 +01:00
"""
Service for the CLI command new
:param configuration:
"""
2021-03-09 22:29:14 +01:00
CommandABC.__init__(self)
self._config = configuration
self._env = self._config.environment
2021-03-09 22:29:14 +01:00
self._workspace = self._config.get_configuration(WorkspaceSettings)
2021-03-09 22:29:14 +01:00
self._project: ProjectSettings = ProjectSettings()
self._project_dict = {}
self._build: BuildSettings = BuildSettings()
self._build_dict = {}
self._project_json = {}
self._command: str = ''
2021-03-10 11:28:04 +01:00
self._use_application_api: bool = False
self._use_startup: bool = False
self._use_service_providing: bool = False
2021-03-09 22:29:14 +01:00
2021-03-15 18:50:53 +01:00
@staticmethod
def _help(message: str):
"""
Internal help output
:param message:
:return:
"""
Console.error(message)
schematics = [
'console (c|C) <name>',
]
Console.write_line('Available Schematics:')
for name in schematics:
Console.write(f'\n\t{name} ')
2021-03-09 22:29:14 +01:00
def _create_project_settings(self, name: str):
self._project_dict = {
2021-03-13 22:53:28 +01:00
ProjectSettingsNameEnum.name.value: name,
ProjectSettingsNameEnum.version.value: {
2021-03-12 16:10:43 +01:00
VersionSettingsNameEnum.major.value: '0',
VersionSettingsNameEnum.minor.value: '0',
VersionSettingsNameEnum.micro.value: '0'
2021-03-09 22:29:14 +01:00
},
2021-03-13 22:53:28 +01:00
ProjectSettingsNameEnum.author.value: '',
ProjectSettingsNameEnum.author_email.value: '',
ProjectSettingsNameEnum.description.value: '',
ProjectSettingsNameEnum.long_description.value: '',
ProjectSettingsNameEnum.url.value: '',
ProjectSettingsNameEnum.copyright_date.value: '',
ProjectSettingsNameEnum.copyright_name.value: '',
ProjectSettingsNameEnum.license_name.value: '',
ProjectSettingsNameEnum.license_description.value: '',
2021-03-14 11:27:01 +01:00
ProjectSettingsNameEnum.dependencies.value: [
f'sh_cpl=={version.parse(cpl.__version__)}'
],
2021-03-14 12:05:57 +01:00
ProjectSettingsNameEnum.python_version.value: f'>={sys.version.split(" ")[0]}',
2021-03-17 08:55:23 +01:00
ProjectSettingsNameEnum.python_path.value: {
sys.platform: ''
},
2021-03-14 12:05:57 +01:00
ProjectSettingsNameEnum.classifiers.value: []
2021-03-09 22:29:14 +01:00
}
self._project.from_dict(self._project_dict)
def _create_build_settings(self):
2021-03-31 10:47:14 +02:00
main = f'{String.convert_to_snake_case(self._project.name)}.main'
2021-03-30 12:44:31 +02:00
if self._command == ProjectTypeEnum.library.value:
main = f'{String.convert_to_snake_case(self._project.name)}.main'
2021-03-30 12:44:31 +02:00
2021-03-09 22:29:14 +01:00
self._build_dict = {
2021-03-30 11:53:07 +02:00
BuildSettingsNameEnum.project_type.value: self._command,
BuildSettingsNameEnum.source_path.value: '',
BuildSettingsNameEnum.output_path.value: '../../dist',
2021-03-30 12:44:31 +02:00
BuildSettingsNameEnum.main.value: main,
2021-03-13 22:53:28 +01:00
BuildSettingsNameEnum.entry_point.value: self._project.name,
2021-03-17 09:12:10 +01:00
BuildSettingsNameEnum.include_package_data.value: False,
2021-03-13 22:53:28 +01:00
BuildSettingsNameEnum.included.value: [],
BuildSettingsNameEnum.excluded.value: [
2021-03-09 22:29:14 +01:00
'*/__pycache__',
'*/logs',
'*/tests'
],
2021-03-13 22:53:28 +01:00
BuildSettingsNameEnum.package_data.value: {}
2021-03-09 22:29:14 +01:00
}
self._build.from_dict(self._build_dict)
def _create_project_json(self):
2021-03-14 16:01:15 +01:00
"""
Creates cpl.json content
:return:
"""
2021-03-09 22:29:14 +01:00
self._project_json = {
ProjectSettings.__name__: self._project_dict,
BuildSettings.__name__: self._build_dict
}
2021-03-10 11:28:04 +01:00
def _get_project_path(self) -> Optional[str]:
2021-03-14 16:01:15 +01:00
"""
Gets project path
:return:
"""
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)
2021-03-09 22:29:14 +01:00
if os.path.isdir(project_path) and len(os.listdir(project_path)) > 0:
Console.error('Project path is not empty\n')
2021-03-10 11:28:04 +01:00
return None
2021-03-09 22:29:14 +01:00
2021-03-10 11:28:04 +01:00
return project_path
def _get_project_information(self):
2021-03-14 16:01:15 +01:00
"""
Gets project information's from user
2021-03-14 16:01:15 +01:00
:return:
"""
result = Console.read('Do you want to use application base? (y/n) ')
2021-03-10 11:28:04 +01:00
if result.lower() == 'y':
self._use_application_api = True
2021-03-30 09:54:04 +02:00
result = Console.read('Do you want to use startup? (y/n) ')
if result.lower() == 'y':
self._use_startup = True
else:
result = Console.read('Do you want to use service providing? (y/n) ')
if result.lower() == 'y':
self._use_service_providing = True
2021-03-10 17:04:53 +01:00
Console.set_foreground_color(ForegroundColorEnum.default)
2021-03-10 11:28:04 +01:00
2021-03-30 12:44:31 +02:00
def _console(self, args: list[str]):
2021-03-14 16:01:15 +01:00
"""
2021-03-30 12:44:31 +02:00
Generates new console project
:param args:
2021-03-14 16:01:15 +01:00
:return:
"""
2021-03-30 12:44:31 +02:00
name = self._config.get_configuration(self._command)
2021-03-30 12:44:31 +02:00
self._create_project_settings(name)
self._create_build_settings()
self._create_project_json()
path = self._get_project_path()
if path is None:
return
2021-03-10 11:28:04 +01:00
2021-03-30 12:44:31 +02:00
self._get_project_information()
try:
ConsoleBuilder.build(
path,
self._use_application_api,
self._use_startup,
self._use_service_providing,
self._project.name,
self._project_json,
self._workspace
2021-03-10 17:04:53 +01:00
)
2021-03-30 12:44:31 +02:00
except Exception as e:
Console.error('Could not create project', str(e))
2021-03-10 11:28:04 +01:00
2021-03-30 12:44:31 +02:00
def _library(self, args: list[str]):
2021-03-14 16:01:15 +01:00
"""
2021-03-30 12:44:31 +02:00
Generates new library project
2021-03-14 16:01:15 +01:00
:param args:
:return:
"""
2021-03-09 22:29:14 +01:00
name = self._config.get_configuration(self._command)
2021-03-10 11:28:04 +01:00
2021-03-10 17:04:53 +01:00
self._create_project_settings(name)
self._create_build_settings()
2021-03-14 11:27:01 +01:00
self._create_project_json()
2021-03-10 11:28:04 +01:00
path = self._get_project_path()
if path is None:
return
self._get_project_information()
2021-03-10 11:28:04 +01:00
try:
2021-03-30 12:44:31 +02:00
LibraryBuilder.build(
path,
self._use_application_api,
self._use_startup,
self._use_service_providing,
self._project.name,
self._project_json,
self._workspace
2021-03-30 12:44:31 +02:00
)
2021-03-10 11:28:04 +01:00
except Exception as e:
Console.error('Could not create project', str(e))
2021-03-09 22:29:14 +01:00
def run(self, args: list[str]):
2021-03-14 16:01:15 +01:00
"""
Entry point of command
:param args:
:return:
"""
2021-03-15 18:50:53 +01:00
if len(args) == 0:
self._help('Usage: cpl new <schematic> [options]')
2021-03-30 11:53:07 +02:00
return
2021-03-15 18:50:53 +01:00
2021-03-30 12:44:31 +02:00
self._command = str(args[0]).lower()
if self._command == ProjectTypeEnum.console.value:
2021-03-09 22:29:14 +01:00
self._console(args)
2021-03-10 14:29:35 +01:00
2021-03-30 12:44:31 +02:00
elif self._command == ProjectTypeEnum.library.value:
self._library(args)
2021-03-15 18:50:53 +01:00
else:
self._help('Usage: cpl new <schematic> [options]')
2021-03-30 11:53:07 +02:00
return