2021-03-10 22:29:42 +01:00
|
|
|
import os
|
2021-03-12 15:32:37 +01:00
|
|
|
from collections import Callable
|
2021-03-10 22:29:42 +01:00
|
|
|
|
|
|
|
from cpl.application.application_abc import ApplicationRuntimeABC
|
|
|
|
from cpl.configuration.configuration_abc import ConfigurationABC
|
2021-03-12 15:32:37 +01:00
|
|
|
from cpl.console.foreground_color import ForegroundColor
|
2021-03-10 22:29:42 +01:00
|
|
|
from cpl.console.console import Console
|
|
|
|
from cpl.utils.string import String
|
|
|
|
from cpl_cli.command_abc import CommandABC
|
|
|
|
from cpl_cli.templates.generate.abc_template import ABCTemplate
|
2021-03-12 15:32:37 +01:00
|
|
|
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.template_file_abc import TemplateFileABC
|
2021-03-10 22:29:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Generate(CommandABC):
|
|
|
|
|
|
|
|
def __init__(self, configuration: ConfigurationABC, runtime: ApplicationRuntimeABC):
|
|
|
|
CommandABC.__init__(self)
|
|
|
|
|
2021-03-12 15:32:37 +01:00
|
|
|
self._schematics = {
|
|
|
|
"abc": {
|
|
|
|
"Upper": "ABC",
|
|
|
|
"Template": ABCTemplate
|
|
|
|
},
|
|
|
|
"class": {
|
|
|
|
"Upper": "Class",
|
|
|
|
"Template": ClassTemplate
|
|
|
|
},
|
|
|
|
"configmodel": {
|
|
|
|
"Upper": "Settings",
|
|
|
|
"Template": ConfigModelTemplate
|
|
|
|
},
|
|
|
|
"enum": {
|
|
|
|
"Upper": "Enum",
|
|
|
|
"Template": EnumTemplate
|
|
|
|
},
|
|
|
|
"service": {
|
|
|
|
"Upper": "Service",
|
|
|
|
"Template": ServiceTemplate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:29:42 +01:00
|
|
|
self._config = configuration
|
|
|
|
self._runtime = runtime
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _help(message: str):
|
|
|
|
Console.error(message)
|
|
|
|
|
|
|
|
schematics = [
|
|
|
|
['abc (a|A)'],
|
|
|
|
['class (c|C)'],
|
|
|
|
['configmodel (cm|CM)'],
|
|
|
|
['enum (e|E)'],
|
|
|
|
['service (s|S)'],
|
|
|
|
]
|
|
|
|
Console.write_line('Available Schematics:')
|
|
|
|
for name in schematics:
|
|
|
|
Console.write(f'\n\t{name} ')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _create_file(file_path: str, value: str):
|
|
|
|
with open(file_path, 'w') as template:
|
|
|
|
template.write(value)
|
|
|
|
template.close()
|
|
|
|
|
2021-03-12 15:32:37 +01:00
|
|
|
def _generate(self, schematic: str, name: str, template: Callable[TemplateFileABC]):
|
|
|
|
class_name = name
|
2021-03-10 22:29:42 +01:00
|
|
|
rel_path = ''
|
|
|
|
if '/' in name:
|
|
|
|
parts = name.split('/')
|
|
|
|
rel_path = '/'.join(parts[:-1])
|
2021-03-12 15:32:37 +01:00
|
|
|
class_name = parts[len(parts) - 1]
|
|
|
|
|
|
|
|
template = template(class_name, schematic, self._schematics[schematic]["Upper"], rel_path)
|
2021-03-10 22:29:42 +01:00
|
|
|
|
2021-03-12 15:32:37 +01:00
|
|
|
file_path = os.path.join(self._runtime.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))
|
|
|
|
|
|
|
|
if os.path.isfile(file_path):
|
2021-03-12 15:32:37 +01:00
|
|
|
Console.error(f'{String.first_to_upper(schematic)} already exists!')
|
2021-03-10 22:29:42 +01:00
|
|
|
exit()
|
|
|
|
|
2021-03-12 15:32:37 +01:00
|
|
|
message = f'Creating {self._runtime.working_directory}/{template.path}/{template.name}'
|
|
|
|
if template.path == '':
|
|
|
|
message = f'Creating {self._runtime.working_directory}/{template.name}'
|
2021-03-10 22:29:42 +01:00
|
|
|
|
|
|
|
Console.spinner(
|
|
|
|
message,
|
|
|
|
self._create_file,
|
|
|
|
file_path,
|
2021-03-12 15:32:37 +01:00
|
|
|
template.value,
|
2021-03-10 22:29:42 +01:00
|
|
|
text_foreground_color=ForegroundColor.green,
|
|
|
|
spinner_foreground_color=ForegroundColor.cyan
|
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, args: list[str]):
|
|
|
|
if len(args) == 0:
|
|
|
|
self._help('Usage: cpl generate <schematic> [options]')
|
|
|
|
exit()
|
|
|
|
|
|
|
|
schematic = args[0]
|
|
|
|
name = self._config.get_configuration(schematic)
|
|
|
|
if name is None:
|
|
|
|
name = Console.read(f'Name for the {args[0]}: ')
|
|
|
|
|
2021-03-12 15:32:37 +01:00
|
|
|
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]')
|
2021-03-10 22:29:42 +01:00
|
|
|
exit()
|
|
|
|
|
|
|
|
Console.write('\n')
|