Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: Demo: post-generation hooks

Login or purchase this course to watch this video and the rest of the course contents.
0:01 It's time to work on the final finish product by creating a post generation hook. So these are very useful, and one of the key reasons we might create
0:10 a post generation hook would be to display instructions about getting started. So once your project is created, you might want to say hey user,
0:18 thanks for creating my project, here is the next steps you have to take maybe you have to pip install the requirements,
0:24 or you've got to setup your system this way, or bunches of things. So we can so a little nice message right at the end by using a post generation hook.
0:31 One of the things that is very common in these templates, but is not built in to Cookiecutter at the time of this recording,
0:37 is the ability to include or exclude files and directories conditionally.
0:42 So, we can write a little bit of code to do that, and I've changed our project,
0:46 just a tiny bit to set this up before us, so over here notice there is this folder
0:50 called color_files and it has a blue_file a green_file and a yellow_file.
0:55 If we look at the cookiecutter.json, I trimmed this down just for sanity sake, so we have blue, green and yellow,
1:02 these three you can all create projects of that type, remember our pre generation hook already will not allow you
1:07 to create a black one, because we were just playing around with that idea. So we have these three realistic options for a color.
1:13 Now, when you choose a color, what we want to do is only include the blue_file, which simply says this is the file that appears only in blue projects,
1:21 when you choose blue. If you choose green, we want to have only green, we want to get rid of blue and yellow, if you choose green. Okay.
1:27 We could take this in a much more advanced direction but I think if you see this,
1:32 this will be enough for you to sort of bring this concept of conditionally including files, directories and various things in your own projects.
1:41 So, how do we do that, we don't need any of these, let's work on this file one first, and then we'll come back with the instructions second.
1:51 So, let's go here and let's go and set this up to have this main convention here
1:55 we'll go up here, we'll write at the top, I like to put this right at the top, have the function that orchestrates what we're going to do.
2:03 So the first thing we're going to do is we're going to remove unused project files,
2:07 let's say, so that is going to go here and then we are also going to display
2:12 getting started instructions, so we're going to fill that out here in a minute. Okay, so how are we going to remove our files,
2:22 let's try one more little helper function called delete_files, this will be file_list, and this will be a working_dir.
2:31 Okay so we're going to need to use an os module in Python, so we are going to import os and let's just go down here and say
2:39 for file in file_list: full_file = os.path.join(working_dir, file) we want to take the working directory
2:46 and in a platform independent way combine it with this file. So this is going to be the file and then we're going to test whether it exists,
2:53 so we'll say if os.path.exists(full_file):, we're going to say os.remove(full_file).
2:59 So, we were given a set of files and if they exist, we want to delete them so up here we are going to choose which ones do we send to this function,
3:08 which ones do we actually want to delete based on some sort of input. Let's go ahead and also here say else:
3:16 print("WARNING: Could not find file to remove in hook: " + full_file) And we'll just print out the full file here. Hopefully, if we do things right,
3:23 we'll never ever see that warning, but just in case. Now what we need to do is figure out the favorite_color.
3:28 And as long as we spell this correctly, this will be cookiecutter.that Like so, alright, so that is going to be the color,
3:39 and then we just need to test is it blue, green or yellow, and first of all, let's get the working directory that is going to be the same,
3:44 so we'll say os.path.abspath(os.path.curdir) So, when our hook executes, it's going to execute in the context
3:55 in the current directory, it's going to be where that template is, where the files were created, okay.
4:04 Then we'll say like this, file_list = [] (is empty), we'll say if favorite_color == 'yellow'
4:10 say file_list is going to be and we'll just put "blue_file.txt" and "yellow_file.txt"
4:16 Sorry green_file, okay, we got "blue_file.txt" and "green_file.txt", now there is a bit of a problem in that,
4:24 this current directory here is actually this directory, and we need to get into the colors file, so we could do one more step,
4:32 if we know we're only working in the colors file folder, we'll just do like this. Join that directory, with color files.
4:41 And then we can just say the short names here. And let's just keep going, and finally in all of these we just say delete_files(),
4:50 we give it the file_list and the working_dir. Okay, if everything is working correctly, we should be able to delete these files,
5:04 okay, now what we're going to do is we want to create a few projects
5:07 and we should see if we choose blue, we'll only have that left, if we choose yellow, we'll only have this, and so on.
5:13 Alright, let's go give this a shot. So we're going to run cookiecutter cookiecutter-colors I am in the directory
5:19 that has the template that is why I can say it this way, it says my project, this will be green project, green project is good,
5:25 this is going to be Michael, that's fine, let's pick green, alright, let's see what happens.
5:31 Boom okay, so now we have the green project, so let's see what's in there, color_files, ta-da, we have only the green one, beautiful.
5:40 Alright, let's try this again and this time let's do it with yellow. So yellow project, yellow project like this, on this date,
5:49 make sure we pick yellow, and we'll say open yellow and this time, we got the color_files, here, yellow, okay, and so that is how this works,
6:00 we created a post generation hook and all of these files here were copied over in the project creation, and if we had Jinja expressions in them,
6:12 they would have been expanded and filled out and possibly conditionally done and so on. Then, after all that was done, we said okay,
6:20 well probably some extra files were copied, like blue_files were copied, even in the yellow projects,
6:25 we're just going to go and do a little bit of code here to clean them up, right, figure out what they choose and then
6:30 write just a tiny bit of code to go and delete them and hey, how about that, we didn't mess up and see this warning. That's cool.

Talk Python's Mastodon Michael Kennedy's Mastodon