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 cruft compatibility, read from .cruft.json #756

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ when you're only importing a handful of commits at a time.

**Important:**

Retrocookie relies on a ``.cookiecutter.json`` file in the generated project
to work out how to rewrite commits.
This file is similar to the ``cookiecutter.json`` file in the template,
Retrocookie relies on a ``.cookiecutter.json`` or ``.cruft.json`` context file in
the generated project to work out how to rewrite commits.
The ``.cookiecutter.json`` file is similar to ``cookiecutter.json`` in the template,
but contains the specific values chosen during project generation.
You can generate this file by putting it into the template directory in the Cookiecutter,
with the following contents:
Expand All @@ -72,6 +72,9 @@ with the following contents:

{{ cookiecutter | jsonify }}

The ``.cruft.json`` file is generated automatically by cruft_ when using it instead of
Cookiecutter.


Requirements
------------
Expand Down Expand Up @@ -315,6 +318,7 @@ This project was generated from `@cjolowicz`_'s `Hypermodern Python Cookiecutter

.. _@cjolowicz: https://github.com/cjolowicz
.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _cruft: https://github.com/cruft/cruft
.. _Dependabot: https://dependabot.com/
.. _GitHub: https://github.com/
.. _Hypermodern Python Cookiecutter: https://github.com/cjolowicz/cookiecutter-hypermodern-python
Expand Down
4 changes: 2 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def mypy(session: Session) -> None:
def tests(session: Session) -> None:
"""Run the test suite."""
session.install(".[pr]")
session.install("cookiecutter", "coverage[toml]", "pygments", "pytest")
session.install("cookiecutter", "cruft", "coverage[toml]", "pygments", "pytest")
try:
session.run("coverage", "run", "--parallel", "-m", "pytest", *session.posargs)
finally:
Expand All @@ -156,7 +156,7 @@ def coverage(session: Session) -> None:
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
session.install(".[pr]")
session.install("cookiecutter", "pytest", "typeguard", "pygments")
session.install("cookiecutter", "cruft", "pytest", "typeguard", "pygments")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)


Expand Down
1,978 changes: 969 additions & 1,009 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sphinx = ">=4.3.0"
sphinx-autobuild = ">=2020.9.1"
pre-commit = ">=2.15.0"
cookiecutter = ">=1.7.3"
cruft = ">=2.15.0"
pygments = ">=2.10.0"
flake8 = ">=4.0.1"
black = ">=21.10b0"
Expand Down
6 changes: 3 additions & 3 deletions src/retrocookie/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def main(

The path to the source repository is passed as a positional argument.
This should be an instance of the Cookiecutter template with a
.cookiecutter.json file.
.cookiecutter.json or .cruft.json context file.

Additional positional arguments are the commits to be imported.
See gitrevisions(7) for a list of ways to spell commits and commit
Expand All @@ -95,11 +95,11 @@ def main(
\b
- Files are moved to the template directory
- Tokens with special meaning in Jinja are escaped
- Values from .cookiecutter.json are replaced by templating tags
- Values from the context file are replaced by templating tags
with the corresponding variable name

Use the --include-variable and --exclude-variable options to include or
exclude specific variables from .cookiecutter.json.
exclude specific variables from the context file.
"""
if create:
if create_branch:
Expand Down
17 changes: 11 additions & 6 deletions src/retrocookie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ def find_template_directory(repository: git.Repository) -> Path:


def load_context(repository: git.Repository, ref: str) -> Dict[str, Any]:
"""Load the context from the .cookiecutter.json file."""
path = Path(".cookiecutter.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)
"""Load the context from the .cookiecutter.json or .cruft.json file."""
try:
path = Path(".cookiecutter.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)
except KeyError:
Copy link
Owner

Choose a reason for hiding this comment

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

Why KeyError?

Copy link
Owner

@cjolowicz cjolowicz Jul 3, 2024

Choose a reason for hiding this comment

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

Is that what git.Repository.read_text raises when it doesn't find the file?

Copy link
Author

Choose a reason for hiding this comment

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

It seems to be!

repository.read_text(path, ref=ref)
Traceback (most recent call last):
  File "/home/micael/projects/retrocookie/src/retrocookie/core.py", line 42, in load_context
    text = repository.read_text(path, ref=ref)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/micael/projects/retrocookie/src/retrocookie/git.py", line 200, in read_text
    blob = functools.reduce(operator.truediv, path.parts, commit.tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '.cookiecutter.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/micael/projects/retrocookie/src/retrocookie/git.py", line 200, in read_text
    blob = functools.reduce(operator.truediv, path.parts, commit.tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: '.cookiecutter.json'

Copy link
Author

Choose a reason for hiding this comment

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

Do you think it'd be better to, for example, modify the read_text method to raise a clearer error, like a FileNotFoundError, instead?

Copy link
Owner

Choose a reason for hiding this comment

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

Possibly, but not in this PR. Sorry for the slow review, have little bandwidth atm

Copy link
Author

Choose a reason for hiding this comment

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

No worries. And sorry for pinging you. I had forgotten about this PR, then realized it had been over a year with no activity.

path = Path(".cruft.json")
text = repository.read_text(path, ref=ref)
data = json.loads(text)["context"]["cookiecutter"]
if not isinstance(data, dict) or not all(isinstance(key, str) for key in data):
raise TypeError(".cookiecutter.json does not contain an object")
raise TypeError(f"{path} does not contain an object")
return cast(Dict[str, Any], data)


Expand Down Expand Up @@ -124,7 +129,7 @@ def retrocookie(

Args:
instance_path: The source repository, an instance of the Cookiecutter
template with a ``.cookiecutter.json`` file.
template with a ``.cookiecutter.json`` or ``.cruft.json`` context file.

commits: The commits to be imported from the source repository.

Expand Down
6 changes: 4 additions & 2 deletions src/retrocookie/pr/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ def _(event: events.TemplateNotFound) -> None:
console.failure(
f"Project template for [repository]{event.project.full_name}[/] not found"
)
console.hint("Does the project contain a .cookiecutter.json file?")
console.hint(
'Does the .cookiecutter.json contain the repository URL under "_template"?'
"Does the project contain a .cookiecutter.json or .cruft.json context file?"
)
console.hint(
'Does the context file contain the repository URL under "_template"?'
)
console.hint("Is the template repository on GitHub?")

Expand Down
49 changes: 47 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Dict
from typing import TYPE_CHECKING

import cruft # type: ignore[import]
import pytest
from cookiecutter.main import cookiecutter

Expand Down Expand Up @@ -81,6 +82,17 @@ def cookiecutter_project(
return cookiecutter_path


@pytest.fixture
def cruft_project(
cookiecutter_path: Path,
cookiecutter_json: Path,
cookiecutter_readme: Path,
) -> Path:
"""The cookiecutter using cruft."""
# This still has `.cookiecutter.json` for some reason
return cookiecutter_path
MicaelJarniac marked this conversation as resolved.
Show resolved Hide resolved


def make_repository(path: Path) -> git.Repository:
"""Turn a directory into a git repository."""
from retrocookie import git
Expand All @@ -97,6 +109,12 @@ def cookiecutter_repository(cookiecutter_project: Path) -> git.Repository:
return make_repository(cookiecutter_project)


@pytest.fixture
def cruft_repository(cruft_project: Path) -> git.Repository:
"""The cookiecutter repository using cruft."""
return make_repository(cruft_project)


@pytest.fixture
def cookiecutter_instance(
cookiecutter_repository: git.Repository,
Expand All @@ -110,6 +128,33 @@ def cookiecutter_instance(


@pytest.fixture
def cookiecutter_instance_repository(cookiecutter_instance: Path) -> git.Repository:
"""The cookiecutter instance repository."""
def cruft_instance(
cruft_repository: git.Repository,
tmp_path: Path,
) -> Path:
"""The cookiecutter instance using cruft."""
template = str(cruft_repository.path)
path: Path = cruft.create(
template_git_url=template, no_input=True, output_dir=tmp_path
)
return path


@pytest.fixture
def vanilla_instance_repository(cookiecutter_instance: Path) -> git.Repository:
"""The vanilla cookiecutter instance repository."""
return make_repository(cookiecutter_instance)


@pytest.fixture
def cruft_instance_repository(cruft_instance: Path) -> git.Repository:
"""The cookiecutter instance repository using cruft."""
return make_repository(cruft_instance)


@pytest.fixture(params=["cruft_instance_repository"])
def cookiecutter_instance_repository(request: pytest.FixtureRequest) -> git.Repository:
"""The cookiecutter instance repository."""
param = request.param # type: ignore[attr-defined]
repo: git.Repository = request.getfixturevalue(param)
return repo