sh_cpl/src/cpl_cli/command/generate_service.py

187 lines
5.9 KiB
Python
Raw Normal View History

2021-03-10 22:29:42 +01:00
import os
2022-01-19 12:44:23 +01:00
import sys
2021-04-11 16:36:59 +02:00
import textwrap
from collections import Callable
2021-03-10 22:29:42 +01:00
2021-08-05 14:21:42 +02:00
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_core.console.console import Console
from cpl_core.utils.string import String
2021-03-10 22:29:42 +01:00
from cpl_cli.command_abc import CommandABC
from cpl_cli._templates.generate.init_template import InitTemplate
from cpl_cli._templates.generate.abc_template import ABCTemplate
from cpl_cli._templates.generate.class_template import ClassTemplate
from cpl_cli._templates.generate.configmodel_template import ConfigModelTemplate
from cpl_cli._templates.generate.enum_template import EnumTemplate
from cpl_cli._templates.generate.service_template import ServiceTemplate
from cpl_cli._templates.generate.thread_template import ThreadTemplate
from cpl_cli._templates.template_file_abc import TemplateFileABC
2021-03-10 22:29:42 +01:00
2021-03-12 16:06:30 +01:00
class GenerateService(CommandABC):
2021-03-10 22:29:42 +01:00
2021-03-29 09:21:58 +02:00
def __init__(self, configuration: ConfigurationABC):
2021-03-14 16:01:15 +01:00
"""
Service for the CLI command generate
:param configuration:
"""
2021-03-10 22:29:42 +01:00
CommandABC.__init__(self)
self._schematics = {
"abc": {
"Upper": "ABC",
"Template": ABCTemplate
},
"class": {
2021-03-12 15:58:30 +01:00
"Upper": "",
"Template": ClassTemplate
},
"enum": {
"Upper": "Enum",
"Template": EnumTemplate
},
"service": {
"Upper": "Service",
"Template": ServiceTemplate
2021-03-12 15:44:55 +01:00
},
"settings": {
"Upper": "Settings",
"Template": ConfigModelTemplate
2021-03-12 16:01:44 +01:00
},
"thread": {
"Upper": "Thread",
"Template": ThreadTemplate
}
}
2021-03-10 22:29:42 +01:00
self._config = configuration
2021-03-29 09:21:58 +02:00
self._env = self._config.environment
2021-03-10 22:29:42 +01:00
2021-04-11 16:36:59 +02:00
@property
def help_message(self) -> str:
return textwrap.dedent("""\
2021-04-12 19:44:29 +02:00
Generate a file based on schematic.
2021-04-12 20:05:55 +02:00
Usage: cpl generate <schematic> <name>
2021-04-12 19:44:29 +02:00
2021-04-12 20:05:55 +02:00
Arguments:
2021-04-12 19:44:29 +02:00
schematic: The schematic to generate.
name: The name of the generated file
2021-04-12 20:05:55 +02:00
Schematics:
2021-04-12 19:44:29 +02:00
abc
class
enum
service
settings
thread
2021-04-11 16:36:59 +02:00
""")
2021-03-10 22:29:42 +01:00
@staticmethod
def _help(message: str):
2021-03-14 16:01:15 +01:00
"""
Internal help output
:param message:
:return:
"""
2021-03-10 22:29:42 +01:00
Console.error(message)
schematics = [
2021-03-12 15:44:55 +01:00
'abc (a|A)',
'class (c|C)',
'enum (e|E)',
'service (s|S)',
'settings (st|ST)'
2021-03-10 22:29:42 +01:00
]
Console.write_line('Available Schematics:')
for name in schematics:
Console.write(f'\n\t{name} ')
@staticmethod
def _create_file(file_path: str, value: str):
2021-03-14 16:01:15 +01:00
"""
Creates the given file with content
:param file_path:
:param value:
:return:
"""
2021-03-10 22:29:42 +01:00
with open(file_path, 'w') as template:
template.write(value)
template.close()
def _generate(self, schematic: str, name: str, template: Callable[TemplateFileABC]):
2021-03-14 16:01:15 +01:00
"""
Generates files by given schematic, name and template
:param schematic:
:param name:
:param template:
:return:
"""
class_name = name
2021-03-10 22:29:42 +01:00
rel_path = ''
if '/' in name:
parts = name.split('/')
rel_path = '/'.join(parts[:-1])
class_name = parts[len(parts) - 1]
if 'src' not in rel_path:
rel_path = f'src/{rel_path}'
template = template(class_name, schematic, self._schematics[schematic]["Upper"], rel_path)
2021-03-10 22:29:42 +01:00
2021-03-29 09:21:58 +02:00
file_path = os.path.join(self._env.working_directory, template.path, template.name)
2021-03-10 22:29:42 +01:00
if not os.path.isdir(os.path.dirname(file_path)):
os.makedirs(os.path.dirname(file_path))
2021-03-30 09:47:47 +02:00
directory = ''
for subdir in template.path.split('/'):
directory = os.path.join(directory, subdir)
if subdir != 'src':
file = InitTemplate(class_name, schematic, self._schematics[schematic]["Upper"], rel_path)
Console.spinner(
f'Creating {os.path.abspath(directory)}/{file.name}',
self._create_file,
os.path.join(os.path.abspath(directory), file.name),
file.value,
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
)
2021-03-10 22:29:42 +01:00
if os.path.isfile(file_path):
Console.error(f'{String.first_to_upper(schematic)} already exists!')
2022-01-19 12:44:23 +01:00
sys.exit()
2021-03-10 22:29:42 +01:00
2021-03-29 09:21:58 +02:00
message = f'Creating {self._env.working_directory}/{template.path}/{template.name}'
if template.path == '':
2021-03-29 09:21:58 +02:00
message = f'Creating {self._env.working_directory}/{template.name}'
2021-03-10 22:29:42 +01:00
Console.spinner(
message,
self._create_file,
file_path,
template.value,
2021-03-12 16:06:30 +01:00
text_foreground_color=ForegroundColorEnum.green,
spinner_foreground_color=ForegroundColorEnum.cyan
2021-03-10 22:29:42 +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-10 22:29:42 +01:00
if len(args) == 0:
self._help('Usage: cpl generate <schematic> [options]')
2022-01-19 12:44:23 +01:00
sys.exit()
2021-03-10 22:29:42 +01:00
schematic = args[0]
name = self._config.get_configuration(schematic)
if name is None:
name = Console.read(f'Name for the {args[0]}: ')
if schematic in self._schematics:
s = self._schematics[schematic]
self._generate(schematic, name, s["Template"])
2021-03-10 22:29:42 +01:00
else:
2021-03-11 09:43:45 +01:00
self._help('Usage: cpl generate <schematic> [options]')
2022-01-19 12:44:23 +01:00
sys.exit()