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