Skip to content

Commit

Permalink
Merge pull request #2 from i-dot-ai/EN-621/enriched-context
Browse files Browse the repository at this point in the history
Pass additional metadata to cookiecutter context
  • Loading branch information
RyanWhite25 authored Oct 14, 2024
2 parents e982dab + b1d7e96 commit cc69e11
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cruft/_commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from . import utils
from .utils import example
from .utils.clean import clean_context
from .utils.iohelper import AltTemporaryDirectory


Expand Down Expand Up @@ -43,6 +44,8 @@ def create(
default_config,
extra_context,
no_input,
output_dir,
checkout,
)

project_dir = Path(
Expand All @@ -58,7 +61,7 @@ def create(
"template": template_git_url,
"commit": last_commit,
"checkout": checkout,
"context": context,
"context": clean_context(context),
"directory": directory,
}

Expand Down
5 changes: 4 additions & 1 deletion cruft/_commands/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from . import utils
from .utils import example
from .utils.clean import clean_context
from .utils.iohelper import AltTemporaryDirectory


Expand Down Expand Up @@ -39,6 +40,8 @@ def link(
default_config,
extra_context,
no_input,
project_dir,
checkout,
)
if no_input:
use_commit = last_commit
Expand All @@ -56,7 +59,7 @@ def link(
"template": template_git_url,
"commit": use_commit,
"checkout": checkout,
"context": context,
"context": clean_context(context),
"directory": directory,
}
)
Expand Down
9 changes: 9 additions & 0 deletions cruft/_commands/utils/clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def clean_context(context):
cleaned_context = context
keys_to_remove = ["_output_dir", "_repo_dir"]

for key in keys_to_remove:
if key in cleaned_context["cookiecutter"]:
del cleaned_context["cookiecutter"][key]

return cleaned_context
13 changes: 13 additions & 0 deletions cruft/_commands/utils/cookiecutter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from pathlib import Path
from typing import Any, Dict, Optional
from urllib.parse import urlparse
Expand Down Expand Up @@ -80,6 +81,8 @@ def generate_cookiecutter_context(
default_config: bool = False,
extra_context: Optional[Dict[str, Any]] = None,
no_input: bool = False,
project_dir: Path = Path("."),
checkout: Optional[str] = None,
) -> CookiecutterContext:
_validate_cookiecutter(cookiecutter_template_dir)

Expand All @@ -99,6 +102,16 @@ def generate_cookiecutter_context(
context["cookiecutter"] = prompt_for_config(context, no_input)
context["cookiecutter"]["_template"] = template_git_url

# include output dir in the context dict
context["cookiecutter"]["_output_dir"] = os.path.abspath(project_dir)

# include repo dir in the context dict
context["cookiecutter"]["_repo_dir"] = os.path.abspath(cookiecutter_template_dir)

# include checkout details in the context dict
if checkout is not None:
context["cookiecutter"]["_checkout"] = checkout

return context


Expand Down
2 changes: 2 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def test_link(cruft_runner, cookiecutter_dir):
# compare the 2 .cruft.json
cruft_file = utils.cruft.get_cruft_file(cookiecutter_dir)
cruft_config_from_link = json.loads(cruft_file.read_text())
assert "_output_dir" not in cruft_config_from_create
assert "_repo_dir" not in cruft_config_from_create
assert cruft_config_from_create == cruft_config_from_link


Expand Down

0 comments on commit cc69e11

Please sign in to comment.