WIP: dev into master #184
@@ -1,5 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import click
|
import click
|
||||||
@@ -10,9 +9,9 @@ from cpl.cli.model.project import Project
|
|||||||
from cpl.cli.model.workspace import Workspace
|
from cpl.cli.model.workspace import Workspace
|
||||||
from cpl.cli.utils.structure import get_project_by_name_or_path
|
from cpl.cli.utils.structure import get_project_by_name_or_path
|
||||||
from cpl.cli.utils.template_collector import TemplateCollector
|
from cpl.cli.utils.template_collector import TemplateCollector
|
||||||
|
from cpl.cli.utils.template_renderer import TemplateRenderer
|
||||||
from cpl.core.configuration import Configuration
|
from cpl.core.configuration import Configuration
|
||||||
from cpl.core.console import Console
|
from cpl.core.console import Console
|
||||||
from cpl.core.utils import String
|
|
||||||
|
|
||||||
|
|
||||||
@cli.command("generate", aliases=["g"])
|
@cli.command("generate", aliases=["g"])
|
||||||
@@ -69,7 +68,9 @@ def generate(schematic: str, name: str, verbose: bool) -> None:
|
|||||||
Console.write_line(f"Generating {str(path / name)} ...")
|
Console.write_line(f"Generating {str(path / name)} ...")
|
||||||
with open(path / f"{name}.py", "w") as f:
|
with open(path / f"{name}.py", "w") as f:
|
||||||
f.write(
|
f.write(
|
||||||
_render_template(schematics[schematic].split(".")[0], templates[schematics[schematic]], name, str(path))
|
TemplateRenderer.render_template(
|
||||||
|
schematics[schematic].split(".")[0], templates[schematics[schematic]], name, str(path)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -101,22 +102,3 @@ def _get_name_and_path_from_name(in_name: str, project: Project = None) -> tuple
|
|||||||
selected_project = workspace.get_project_by_name(workspace.default_project)
|
selected_project = workspace.get_project_by_name(workspace.default_project)
|
||||||
|
|
||||||
return (Path(selected_project.path).parent / selected_project.directory / path).resolve().absolute(), name
|
return (Path(selected_project.path).parent / selected_project.directory / path).resolve().absolute(), name
|
||||||
|
|
||||||
|
|
||||||
def _render_template(schematic, template_str: str, name: str, path: str) -> str:
|
|
||||||
context = {
|
|
||||||
"schematic": schematic,
|
|
||||||
"Name": String.to_pascal_case(name),
|
|
||||||
"name": String.to_snake_case(name),
|
|
||||||
"NAME": String.to_snake_case(name).upper(),
|
|
||||||
"camelName": String.to_camel_case(name),
|
|
||||||
"multi_Name": f"{String.to_pascal_case(name)}s",
|
|
||||||
"multi_name": f"{String.to_snake_case(name)}s",
|
|
||||||
"multi_NAME": f"{String.to_snake_case(name).upper()}s",
|
|
||||||
"multi_camelName": f"{String.to_camel_case(name)}s",
|
|
||||||
"path": path.replace("\\", "/"),
|
|
||||||
}
|
|
||||||
|
|
||||||
for key, value in context.items():
|
|
||||||
template_str = template_str.replace(f"<{key}>", value)
|
|
||||||
return template_str
|
|
||||||
|
|||||||
23
src/cli/cpl/cli/utils/template_renderer.py
Normal file
23
src/cli/cpl/cli/utils/template_renderer.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from cpl.core.utils import String
|
||||||
|
|
||||||
|
|
||||||
|
class TemplateRenderer:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def render_template(schematic, template_str: str, name: str, path: str) -> str:
|
||||||
|
context = {
|
||||||
|
"schematic": schematic,
|
||||||
|
"Name": String.to_pascal_case(name),
|
||||||
|
"name": String.to_snake_case(name),
|
||||||
|
"NAME": String.to_snake_case(name).upper(),
|
||||||
|
"camelName": String.to_camel_case(name),
|
||||||
|
"multi_Name": f"{String.to_pascal_case(name)}s",
|
||||||
|
"multi_name": f"{String.to_snake_case(name)}s",
|
||||||
|
"multi_NAME": f"{String.to_snake_case(name).upper()}s",
|
||||||
|
"multi_camelName": f"{String.to_camel_case(name)}s",
|
||||||
|
"path": path.replace("\\", "/"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value in context.items():
|
||||||
|
template_str = template_str.replace(f"<{key}>", value)
|
||||||
|
return template_str
|
||||||
Reference in New Issue
Block a user