Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: Concept: post-generation hooks
Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now you've seen post generation hooks in action, let's look at them as a concept.
0:05 So, we can put whatever script or code we want here, you can see at the end, the very bottom I call sys.exit() clean unused color files.
0:14 And, that is just going to call this function I've written above, so one of the things that is very common to do in Cookiecutter
0:20 is to remove files that you don't need anymore, and maybe some day this will be built into some part of the system, I don't really know,
0:28 like a conditional statement in the cookiecutter.json or something, but for now, this is left up to you, the template creator, in these hooks,
0:37 so here we can write a little bit of code, it's going to grab the selected color,
0:41 in this case the favorite color, we called this variable selected_color,
0:44 we are using the os.path.cur_dir() to find the root of the generated project folder, and then, we are just looking in the colors directory
0:54 and grabbing like the yellow.txt file if it's not needed and we're calling remove on that.
0:59 Okay, and then finally, notice at the very bottom we're returning zero,
1:02 maybe if there was some kind of error, we have really basically no error checking, but if we checked and there was something wrong,
1:08 we could return a non zero value and we're calling sys.exit() which would actually stop and fail the project creation,
1:15 if we say this hook didn't run successfully. You say it ran successfully by sys.exit() is zero
1:21 and it doesn't run successfully by exiting something non zero.