Added generate abc command
This commit is contained in:
parent
0ff9920e9e
commit
82ffdfe278
@ -2,6 +2,7 @@ from typing import Optional
|
||||
|
||||
from cpl.application.application_abc import ApplicationABC
|
||||
from cpl_cli.command.build import Build
|
||||
from cpl_cli.command.generate import Generate
|
||||
from cpl_cli.command.new import New
|
||||
from cpl_cli.command.publish import Publish
|
||||
from cpl_cli.command_handler import CommandHandler
|
||||
@ -22,6 +23,7 @@ class CLI(ApplicationABC):
|
||||
self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
|
||||
|
||||
self._command_handler.add_command(CommandModel('build', ['h', 'B'], Build, True))
|
||||
self._command_handler.add_command(CommandModel('generate', ['g', 'G'], Generate, True))
|
||||
self._command_handler.add_command(CommandModel('help', ['h', 'H'], Help, False))
|
||||
self._command_handler.add_command(CommandModel('new', ['n', 'N'], New, False))
|
||||
self._command_handler.add_command(CommandModel('publish', ['p', 'P'], Publish, True))
|
||||
|
@ -0,0 +1,99 @@
|
||||
import os
|
||||
|
||||
from cpl.application.application_abc import ApplicationRuntimeABC
|
||||
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl.console import ForegroundColor
|
||||
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
|
||||
|
||||
|
||||
class Generate(CommandABC):
|
||||
|
||||
def __init__(self, configuration: ConfigurationABC, runtime: ApplicationRuntimeABC):
|
||||
CommandABC.__init__(self)
|
||||
|
||||
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()
|
||||
|
||||
def _generate_abc(self, name: str):
|
||||
rel_path = ''
|
||||
if '/' in name:
|
||||
parts = name.split('/')
|
||||
rel_path = '/'.join(parts[:-1])
|
||||
name = parts[len(parts) - 1]
|
||||
|
||||
file_path = os.path.join(self._runtime.working_directory, rel_path, f'{String.convert_to_snake_case(name)}.py')
|
||||
print(file_path)
|
||||
if not os.path.isdir(os.path.dirname(file_path)):
|
||||
os.makedirs(os.path.dirname(file_path))
|
||||
|
||||
if os.path.isfile(file_path):
|
||||
Console.error('ABC already exists!')
|
||||
exit()
|
||||
|
||||
message = f'Creating {self._runtime.working_directory}/{rel_path}/{String.convert_to_snake_case(name)}.py'
|
||||
if rel_path == '':
|
||||
message = f'Creating {self._runtime.working_directory}/{String.convert_to_snake_case(name)}.py'
|
||||
|
||||
Console.spinner(
|
||||
message,
|
||||
self._create_file,
|
||||
file_path,
|
||||
ABCTemplate.get_abc_py(name),
|
||||
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]}: ')
|
||||
|
||||
if schematic == 'abc':
|
||||
self._generate_abc(name)
|
||||
|
||||
elif schematic == 'class':
|
||||
print(schematic)
|
||||
|
||||
elif schematic == 'configmodel':
|
||||
print(schematic)
|
||||
|
||||
elif schematic == 'enum':
|
||||
print(schematic)
|
||||
|
||||
elif schematic == 'service':
|
||||
print(schematic)
|
||||
|
||||
else:
|
||||
Console.error('')
|
||||
exit()
|
||||
|
||||
Console.write('\n')
|
@ -7,6 +7,7 @@ from cpl.configuration.console_argument import ConsoleArgument
|
||||
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl.dependency_injection.service_provider_abc import ServiceProviderABC
|
||||
from cpl_cli.command.build import Build
|
||||
from cpl_cli.command.generate import Generate
|
||||
from cpl_cli.command.new import New
|
||||
from cpl_cli.command.publish import Publish
|
||||
from cpl_cli.command_handler import CommandHandler
|
||||
@ -42,6 +43,13 @@ class Startup(StartupABC):
|
||||
self._configuration.add_environment_variables('CPL_')
|
||||
self._configuration.add_json_file('cpl.json', optional=True, output=False)
|
||||
self._configuration.add_console_argument(ConsoleArgument('', 'build', ['b', 'B'], ''))
|
||||
self._configuration.add_console_argument(ConsoleArgument('', 'generate', ['g', 'G'], '', [
|
||||
ConsoleArgument('', 'abc', ['a', 'A'], ' '),
|
||||
ConsoleArgument('', 'class', ['c', 'C'], ' '),
|
||||
ConsoleArgument('', 'configmodel', ['cm', 'CM'], ' '),
|
||||
ConsoleArgument('', 'enum', ['e', 'E'], ' '),
|
||||
ConsoleArgument('', 'service', ['s', 'S'], ' ')
|
||||
]))
|
||||
self._configuration.add_console_argument(ConsoleArgument('', 'help', ['h', 'H'], ''))
|
||||
self._configuration.add_console_argument(ConsoleArgument('', 'new', ['n', 'N'], '', [
|
||||
ConsoleArgument('', 'console', ['c', 'C'], ' ')
|
||||
@ -58,6 +66,7 @@ class Startup(StartupABC):
|
||||
self._services.add_transient(PublisherABC, Publisher)
|
||||
|
||||
self._services.add_transient(Build)
|
||||
self._services.add_transient(Generate)
|
||||
self._services.add_transient(Help)
|
||||
self._services.add_transient(New)
|
||||
self._services.add_transient(Publish)
|
||||
|
0
src/cpl_cli/templates/generate/__init__.py
Normal file
0
src/cpl_cli/templates/generate/__init__.py
Normal file
22
src/cpl_cli/templates/generate/abc_template.py
Normal file
22
src/cpl_cli/templates/generate/abc_template.py
Normal file
@ -0,0 +1,22 @@
|
||||
import textwrap
|
||||
from string import Template
|
||||
|
||||
|
||||
class ABCTemplate:
|
||||
|
||||
@staticmethod
|
||||
def get_abc_py(name: str) -> str:
|
||||
string = textwrap.dedent("""\
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class $Name(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
""")
|
||||
|
||||
return Template(string).substitute(
|
||||
Name=name
|
||||
)
|
Loading…
Reference in New Issue
Block a user