Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VSCode module to scaffolder #137

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/web/Ligare/web/scaffolding/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ScaffoldInputArgs(Namespace):
name: Operation # pyright: ignore[reportUninitializedInstanceVariable]
endpoints: list[Operation] | None = None
template_type: Literal["basic", "openapi"] = "basic"
modules: list[Literal["database", "test"]] | None = None
modules: list[str] | None = None
output_directory: str | None = None


Expand All @@ -35,7 +35,7 @@ class ScaffoldParsedArgs(NamedTuple):
name: Operation
endpoints: list[Operation]
template_type: Literal["basic", "openapi"]
modules: list[Literal["database", "test"]] | None
modules: list[str] | None
Copy link

@dan-knight dan-knight Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change directly related to the VScode module? Do we lose anything by giving up the specificity of Literal["foo"]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more related to the ability to create more Scaffold modules without having to update the modules type or its values. We don't lose anything in practice; it has always been a list of strings, but now the specific values are unimportant to the type checker because they are dynamically set.

output_directory: str


Expand All @@ -62,6 +62,14 @@ def __init__(self, log_level: int | str | None = None) -> None:

self._log = logging.getLogger()

def _find_modules(self):
return [
module_path.name
for module_path in Path(
Path(__file__).parent.parent, Scaffolder.MODULE_TEMPLATE_DIRECTORY
).iterdir()
]

def _parse_args(self, args: list[str]) -> ScaffoldParsedArgs:
parser = ArgumentParser(description="Ligare Web Project Scaffolding Tool")

Expand Down Expand Up @@ -95,7 +103,7 @@ def _parse_args(self, args: list[str]) -> ScaffoldParsedArgs:
default="basic",
help="The type of template to scaffold.",
)
modules = ["database", "test"]
modules = self._find_modules()
_ = create_parser.add_argument(
"-m",
choices=modules,
Expand Down
6 changes: 5 additions & 1 deletion src/web/Ligare/web/scaffolding/scaffolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class Scaffolder:
_manager: Any
_provider: IResourceProvider

MODULE_TEMPLATE_DIRECTORY = Path(
f"scaffolding/templates/optional/{{{{application.module_name}}}}/modules"
)

def __init__(self, config: ScaffoldConfig, log: logging.Logger) -> None:
self._config = config
self._config_dict = asdict(config)
Expand Down Expand Up @@ -338,7 +342,7 @@ def _scaffold_modules(self, overwrite_existing_files: bool = True):
# module templates are stored under `optional/`
# because we don't always want to render any given module.
module_template_directory = Path(
f"scaffolding/templates/optional/{{{{application.module_name}}}}/modules/{module.module_name}"
Scaffolder.MODULE_TEMPLATE_DIRECTORY, module.module_name
)
# executed module hooks before any rendering is done
# so the hooks can modify the config or do other
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ render_module_template(".vscode/launch.json.j2", module_config, ".") }}
21 changes: 18 additions & 3 deletions src/web/test/unit/scaffolding/test_scaffolding.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,12 @@ def test__scaffold__uses_argv_when_sys_argv_not_set(mocker: MockerFixture):
def test__scaffold__uses_argv_for_basic_scaffold_configuration(
mode: str, config_name: str, config_value: str, mocker: MockerFixture
):
_ = mocker.patch("Ligare.web.scaffolding.__main__.Scaffolder")
from Ligare.web.scaffolding.scaffolder import Scaffolder

_ = mocker.patch(
"Ligare.web.scaffolding.__main__.Scaffolder",
MODULE_TEMPLATE_DIRECTORY=Scaffolder.MODULE_TEMPLATE_DIRECTORY,
)
config_mock = mocker.patch("Ligare.web.scaffolding.__main__.ScaffoldConfig")

argv_values = [mode, "-n", "test"]
Expand All @@ -429,7 +434,12 @@ def test__scaffold__uses_argv_for_endpoint_configuration(
mode: str,
mocker: MockerFixture,
):
_ = mocker.patch("Ligare.web.scaffolding.__main__.Scaffolder")
from Ligare.web.scaffolding.scaffolder import Scaffolder

_ = mocker.patch(
"Ligare.web.scaffolding.__main__.Scaffolder",
MODULE_TEMPLATE_DIRECTORY=Scaffolder.MODULE_TEMPLATE_DIRECTORY,
)
config_mock = mocker.patch("Ligare.web.scaffolding.__main__.ScaffoldConfig")

argv_values = [mode, "-n", "test", "-e", "foo", "-e", "bar"]
Expand All @@ -450,7 +460,12 @@ def test__scaffold__uses_argv_for_endpoint_configuration(
def test__scaffold__create_mode_uses_argv_for_module_configuration(
mocker: MockerFixture,
):
_ = mocker.patch("Ligare.web.scaffolding.__main__.Scaffolder")
from Ligare.web.scaffolding.scaffolder import Scaffolder

_ = mocker.patch(
"Ligare.web.scaffolding.__main__.Scaffolder",
MODULE_TEMPLATE_DIRECTORY=Scaffolder.MODULE_TEMPLATE_DIRECTORY,
)
config_mock = mocker.patch("Ligare.web.scaffolding.__main__.ScaffoldConfig")

argv_values = ["create", "-n", "test", "-m", "database"]
Expand Down
Loading