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
Need help with generating GUID/UUID values for context variables #1101
Comments
Computing new Jinja context variables in the pre hook would be a great feature. |
Having a pre-hook for computing values that are injected into the context for use in templates would be very useful. |
Was also looking to do this exact thing. Fixing the vcxproj in a post hook script is not too hard, but I have other cases where I need to generate unique ids in unstructured files (cpp). I was surprised when I started using cookiecutter that the scripts are rendered and ran from temp folder, rather than simply importing the script and giving access to the context dict. Seems like that would make it much simpler and more powerful! |
As a solution, what I do is: in |
@agrant3d could you show a bit more of your code? I used |
Sure, looks something like this: import os
import uuid
CURDIR = os.path.realpath(os.path.curdir)
TEMPLATEDIR = os.path.normpath(os.path.join(CURDIR, '..', '{{cookiecutter._template}}'))
TMPDIR = os.path.join(TEMPLATEDIR, '{{'{{'}}cookiecutter.project_slug{{'}}'}}', 'tmp')
GUIDS = ['PROJECT_GUID', 'SOLUTION_GUID']
for guid in GUIDS:
file_path = os.path.join(TMPDIR, guid + '.txt')
with open(file_path, 'w') as f:
f.write("{" + str(uuid.uuid4()).upper() + "}") The thing you're probably missing is that you need to put the files inside the {{cookiecutter.project_slug}} directory. |
Note - this method only works when you are working with a local copy of the cookiecutter template. If it's a cached version - you cannot simply write to the directory. |
worked a dream, thanks @agrant3d |
I am trying to write some Lambda CookieCutter projects because AWS SAM uses this project. Though I do like a good challenge, I've not had a fun time at it. Hoping someone can help. I need to take the
So my |
Confirmed on putting these tmp files in the project_name = '{{cookiecutter.project_name}}'
cc_template = '{{cookiecutter._template}}'
template_name = cc_template.split("/")[-1]
file_name = singularize(tableize(project_name))
class_name = singularize(camelize(tableize(project_name)))
dash_name = singularize(dasherize(project_name))
all_names = {
'file_name': file_name,
'class_name': class_name,
'dash_name': dash_name
}
import os
import pathlib
from os.path import expanduser
home_dir = expanduser("~")
template_dir = os.path.join(home_dir, '.cookiecutters', template_name)
temp_dir = os.path.join(template_dir, '{{'{{'}}cookiecutter.project_name{{'}}'}}', '_cctmp')
pathlib.Path(temp_dir).mkdir(parents=True, exist_ok=True)
for name in all_names.keys():
file_path = os.path.join(temp_dir, name + '.txt')
with open(file_path, 'w') as f:
f.write(all_names[name]) |
Description:
First off many thanks for creating this project !
Here is some context of what I am trying to do and where I need some guidance
pre_gen_project.py
inside thehooks
folder and update/create new variables that could be added to the ones loaded fromcookiecutter.json
or entered by the usercli.py
) that passes the guids via theextra_context
tocookiecutter.main(...)
but ran into some other problems that I am still trying to figure outAppreciate any pointers on how I can inject GUID values for the context variables
The text was updated successfully, but these errors were encountered: