Improved cpl g templating & added custom templating #137
This commit is contained in:
@@ -2,19 +2,10 @@ import os
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
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.init_template import InitTemplate
|
||||
from cpl_cli._templates.generate.pipe_template import PipeTemplate
|
||||
from cpl_cli._templates.generate.service_template import ServiceTemplate
|
||||
from cpl_cli._templates.generate.test_case_template import TestCaseTemplate
|
||||
from cpl_cli._templates.generate.thread_template import ThreadTemplate
|
||||
from cpl_cli._templates.generate.validator_template import ValidatorTemplate
|
||||
from cpl_cli._templates.template_file_abc import TemplateFileABC
|
||||
from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC
|
||||
from cpl_cli.command_abc import CommandABC
|
||||
from cpl_cli.configuration import WorkspaceSettings
|
||||
from cpl_cli.configuration.schematic_collection import SchematicCollection
|
||||
from cpl_core.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl_core.console.console import Console
|
||||
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
|
||||
@@ -37,44 +28,44 @@ class GenerateService(CommandABC):
|
||||
self._config = configuration
|
||||
self._workspace = workspace
|
||||
|
||||
self._schematics = {
|
||||
"abc": {
|
||||
"Upper": "ABC",
|
||||
"Template": ABCTemplate
|
||||
},
|
||||
"class": {
|
||||
"Upper": "Class",
|
||||
"Template": ClassTemplate
|
||||
},
|
||||
"enum": {
|
||||
"Upper": "Enum",
|
||||
"Template": EnumTemplate
|
||||
},
|
||||
"pipe": {
|
||||
"Upper": "Pipe",
|
||||
"Template": PipeTemplate
|
||||
},
|
||||
"service": {
|
||||
"Upper": "Service",
|
||||
"Template": ServiceTemplate
|
||||
},
|
||||
"settings": {
|
||||
"Upper": "Settings",
|
||||
"Template": ConfigModelTemplate
|
||||
},
|
||||
"test_case": {
|
||||
"Upper": "TestCase",
|
||||
"Template": TestCaseTemplate
|
||||
},
|
||||
"thread": {
|
||||
"Upper": "Thread",
|
||||
"Template": ThreadTemplate
|
||||
},
|
||||
"validator": {
|
||||
"Upper": "Validator",
|
||||
"Template": ValidatorTemplate
|
||||
}
|
||||
}
|
||||
self._schematics = {}
|
||||
# "abc": {
|
||||
# "Upper": "ABC",
|
||||
# "Template": ABCTemplate
|
||||
# },
|
||||
# "class": {
|
||||
# "Upper": "Class",
|
||||
# "Template": ClassTemplate
|
||||
# },
|
||||
# "enum": {
|
||||
# "Upper": "Enum",
|
||||
# "Template": EnumTemplate
|
||||
# },
|
||||
# "pipe": {
|
||||
# "Upper": "Pipe",
|
||||
# "Template": PipeTemplate
|
||||
# },
|
||||
# "service": {
|
||||
# "Upper": "Service",
|
||||
# "Template": ServiceTemplate
|
||||
# },
|
||||
# "settings": {
|
||||
# "Upper": "Settings",
|
||||
# "Template": ConfigModelTemplate
|
||||
# },
|
||||
# "test_case": {
|
||||
# "Upper": "TestCase",
|
||||
# "Template": TestCaseTemplate
|
||||
# },
|
||||
# "thread": {
|
||||
# "Upper": "Thread",
|
||||
# "Template": ThreadTemplate
|
||||
# },
|
||||
# "validator": {
|
||||
# "Upper": "Validator",
|
||||
# "Template": ValidatorTemplate
|
||||
# }
|
||||
# }
|
||||
|
||||
self._config = configuration
|
||||
self._env = self._config.environment
|
||||
@@ -137,7 +128,7 @@ class GenerateService(CommandABC):
|
||||
template.write(value)
|
||||
template.close()
|
||||
|
||||
def _create_init_files(self, file_path: str, template: TemplateFileABC, class_name: str, schematic: str, rel_path: str):
|
||||
def _create_init_files(self, file_path: str, template: GenerateSchematicABC, class_name: str, schematic: str, rel_path: str):
|
||||
if not os.path.isdir(os.path.dirname(file_path)):
|
||||
os.makedirs(os.path.dirname(file_path))
|
||||
directory = ''
|
||||
@@ -146,7 +137,7 @@ class GenerateService(CommandABC):
|
||||
if subdir == 'src':
|
||||
continue
|
||||
|
||||
file = InitTemplate(class_name, schematic, self._schematics[schematic]["Upper"], rel_path)
|
||||
file = self._schematics['init']['Template'](class_name, 'init', rel_path)
|
||||
if os.path.exists(os.path.join(os.path.abspath(directory), file.name)):
|
||||
continue
|
||||
|
||||
@@ -154,12 +145,12 @@ class GenerateService(CommandABC):
|
||||
f'Creating {os.path.abspath(directory)}/{file.name}',
|
||||
self._create_file,
|
||||
os.path.join(os.path.abspath(directory), file.name),
|
||||
file.value,
|
||||
file.get_code(),
|
||||
text_foreground_color=ForegroundColorEnum.green,
|
||||
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||
)
|
||||
|
||||
def _generate(self, schematic: str, name: str, template: TemplateFileABC):
|
||||
def _generate(self, schematic: str, name: str, template: type):
|
||||
"""
|
||||
Generates files by given schematic, name and template
|
||||
:param schematic:
|
||||
@@ -175,9 +166,9 @@ class GenerateService(CommandABC):
|
||||
class_name = parts[len(parts) - 1]
|
||||
|
||||
if self._workspace is not None and parts[0] in self._workspace.projects:
|
||||
rel_path = os.path.dirname(self._workspace.projects[parts[0]])
|
||||
rel_path = os.path.join(os.path.dirname(self._workspace.projects[parts[0]]), *parts[1:-1])
|
||||
|
||||
template = template(class_name, schematic, self._schematics[schematic]["Upper"], rel_path)
|
||||
template = template(class_name, String.convert_to_snake_case(schematic), rel_path)
|
||||
|
||||
file_path = os.path.join(self._env.working_directory, template.path, template.name)
|
||||
self._create_init_files(file_path, template, class_name, schematic, rel_path)
|
||||
@@ -194,17 +185,47 @@ class GenerateService(CommandABC):
|
||||
message,
|
||||
self._create_file,
|
||||
file_path,
|
||||
template.value,
|
||||
template.get_code(),
|
||||
text_foreground_color=ForegroundColorEnum.green,
|
||||
spinner_foreground_color=ForegroundColorEnum.cyan
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _read_custom_schematics_from_path(path: str):
|
||||
if not os.path.exists(os.path.join(path, '.cpl')):
|
||||
return
|
||||
|
||||
for r, d, f in os.walk(os.path.join(path, '.cpl')):
|
||||
for file in f:
|
||||
if not file.endswith('_schematic.py'):
|
||||
continue
|
||||
|
||||
code = ''
|
||||
with open(os.path.join(r, file), 'r') as py_file:
|
||||
code = py_file.read()
|
||||
py_file.close()
|
||||
|
||||
exec(code)
|
||||
|
||||
def _get_schematic_by_alias(self, schematic: str) -> str:
|
||||
for key in self._schematics:
|
||||
if schematic in self._schematics[key]['Aliases']:
|
||||
return key
|
||||
|
||||
return schematic
|
||||
|
||||
def execute(self, args: list[str]):
|
||||
"""
|
||||
Entry point of command
|
||||
:param args:
|
||||
:return:
|
||||
"""
|
||||
self._read_custom_schematics_from_path(self._env.runtime_directory)
|
||||
self._read_custom_schematics_from_path(self._env.working_directory)
|
||||
for schematic in GenerateSchematicABC.__subclasses__():
|
||||
schematic.register()
|
||||
self._schematics = SchematicCollection.get_schematics()
|
||||
|
||||
schematic = None
|
||||
value = None
|
||||
for s in self._schematics:
|
||||
@@ -213,6 +234,12 @@ class GenerateService(CommandABC):
|
||||
schematic = s
|
||||
break
|
||||
|
||||
schematic_by_alias = self._get_schematic_by_alias(args[0])
|
||||
if schematic is None and len(args) >= 1 and (args[0] in self._schematics or schematic_by_alias != args[0]):
|
||||
schematic = schematic_by_alias
|
||||
self._config.add_configuration(schematic, args[1])
|
||||
value = args[1]
|
||||
|
||||
if schematic is None:
|
||||
self._help('Usage: cpl generate <schematic> [options]')
|
||||
Console.write_line()
|
||||
|
Reference in New Issue
Block a user