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

The documentation could show a cookiecutter.json rewrite pre-prompt example #2011

Open
andredias opened this issue Feb 3, 2024 · 3 comments
Labels
documentation This issue/PR relates to or includes documentation.

Comments

@andredias
Copy link

  • Cookiecutter version: 2.5.0
  • Template project url:
  • Python version: 3.11
  • Operating System: Ubuntu 23.10

Description:

The documentation about hook execution says:

pre_prompt: Scripts run in the root directory of a copy of the repository directory. That allows the rewrite of cookiecutter.json to your own needs.

However, it is not clear how to rewrite cookiecutter.json as the provided example does something else.

I'd love an example showing how pre_prompt could be used to inject a variable into the cookiecutter.json.

@devmcp
Copy link

devmcp commented Mar 19, 2024

I'd like to see this too (did you find an example of this anywhere online @andredias?)

@andredias
Copy link
Author

I created an example to insert the current Python version into the cookiecutter.json file:

# hooks/pre_prompt.py

import json
import sys
from pathlib import Path

config = Path("cookiecutter.json")
data = json.loads(config.read_text())
data["python_version"] = f"{sys.version_info.major}.{sys.version_info.minor}"
config.write_text(json.dumps(data, indent=4))

@jensens jensens added the documentation This issue/PR relates to or includes documentation. label Apr 5, 2024
@gregcaporaso
Copy link

Thanks for the example @andredias, that worked for me.

I have a working example of this here now, in case it's helpful for others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This issue/PR relates to or includes documentation.
Projects
None yet
Development

No branches or pull requests

4 participants