preface

Every time we create a new project, we always do these things: create the project, initialize the POD, install the POD, configure the project, and write (or introduce) some skeleton code. When you write a lot of projects, you will find that it is basically a lot of repetitive work, with almost the same things to do each time, but it is inevitable. Especially when you sometimes want to write a small toy project, the amount of labor involved in creating the project becomes even more obvious.

It would be nice if we could edit a project template and then create a new project based on that template each time. Xcode already provides templates, and YEARS ago I did some simple file templates and project templates based on Xcode’s templates. However, the configuration of Xcode template is quite tedious, and the official documents are almost impossible to find (or MAYBE I didn’t look carefully).

So is there any other good way? Which brings us to the hero of the show: the Cookiecutter

Introduction to the

Cookiecutter is a project template tool developed in Python. The source code is available on Github. The project currently has nearly 10,000 stars.

Cookiecutter is not an ios-specific tool. It has a wide range of support and can be used to create templates for almost any major project, thanks to its very simple implementation principles: First, create a project framework as the template, specify a series of parameters such as the project name in the form of keywords in the configuration file, and then replace the names of all directories and subdirectories in the template and all matched keywords in the text according to the configured template parameters when creating a project. Of course, the Cookiecutter also has many advanced uses, and you can query its documentation (possibly by ladder 😔).

This article is just a primer on how I created a simple App template based on the Cookiecutter.

The installation

First you need to install the Cookiecutter, which offers a number of ways to install it for different platforms and environments. But iOS developers mostly work on macOS, so we’ll skip the rest.

Homebrew is recommended for MAC installation:

brew install Cookiecutter
Copy the code

The entire installation process is automated and very simple.

Create a template

New project

First we need to create a template: open Up Xcode and create a new one-page application.


Create a one-page application

Click ‘Next’ and set the corresponding property to the template keyword:

  • Product Name: {{cookiecutter.product_name}}
  • Organization Name: {{cookiecutter.organization_name}}
  • Organization Identifier: {{cookiecutter.organization_identifier}}

Then click Next to create the project as shown below:


Create a project

After the project is created, open info.plist, The Bundle identifier is specified for the com. {{cookiecutter.org anization_identifier}}. {{cookiecutter. Product_name}} otherwise Xcode will default {and} If this parameter is replaced with -, the replacement fails during subsequent project creation.

The configuration file

Next, create a Cookiecutter. Json file at the same level as the newly created project root, where the {{cookiecutter. Product_name}} folder is located, with the following contents:

{
    "product_name": "Hello",
    "organization_name": "Someone Co.,Ltd.",
    "organization_identifier": "someone"
}
Copy the code

The key in JSON is the double bracketed name we just filled in when creating the project, with the cookiecutter prefix removed, and the value corresponding to the key represents the default value. When we use Cookiecutter to create a project from the template, it will ask for these keys in the configuration file, asking us to name it, and if not, using the default values in the configuration file.

In addition, which keys are required and their names are not fixed, you can define as many keys as you want, as long as you use the same name in your template project with the Cookiecutter prefix.

At this point, a simple project template is created, but you can also add additional code to the project as needed. All the additional content will be left intact when you create a new project.

hook

The project created above is very simple, but we often use Cocoapods to manage third-party dependencies in our development, and there are libraries like Alamofire, SnapKit, and so on that almost all projects need. Is it possible to configure and install pods using a template?

This allows you to use the Cookiecutter’s hooks, also known colloquially as callbacks, to trigger events, perform actions, and so on under certain features.

Cookiecutter’s hook configuration is very simple. Just create a new hooks directory in the same directory as the configuration file, place the script in that directory, and the Cookiecutter will call it automatically.

Currently, the Cookiecutter supports two events: before and after creating an item, the corresponding script file is called. Currently, shell scripts and Python scripts are supported. The script to be invoked before installation should be named pre_gen_project.sh or pre_gen_project.py. Similarly, scripts called after installation need to be named post_gen_project.sh or post_gen_project.py, depending on the language used by the script.

Automatic mounting pods

Create a Podfile in the {{cookiecutter. Product_name}}/ directory and configure the common dependency libraries:

target '{{cookiecutter.product_name}}' do

  use_frameworks!

  # Pods for {{cookiecutter.product_name}}

  # Networking
  pod 'Alamofire'
  # ImageCache
  pod 'Kingfisher'
  # AutoLayout
  pod 'SnapKit'

end
Copy the code

Ok, do not execute Pod Install because this is just a template and the libraries may have been updated when the project was actually created, so you need to install the dependencies when the project was created.

Next open post_gen_project.sh and write the pod setup command:

#! /bin/bash GREEN='\033[0;32m' echo -e "${GREEN}Project successfuly generated! " echo -e "${GREEN}Installing Pods..." pod install echo -e "${GREEN}Pods Install Finished"Copy the code

All you need to do is write pod Install, but to make the command line output more user-friendly when creating the project, I’ve added some additional status output 😏, where you can also add other transactions that need to be handled.

At this point, our project framework is basically complete. At this point, our project template directory should look something like this:

Ios_template / ├ ─ ─ {{cookiecutter. Product_name}} / │ ├ ─ ─ {{cookiecutter. Product_name}}. Xcodeproj │ ├ ─ ─ Podfile │ └ ─ ─... ├ ─ ─ hooks │ ├ ─ ─ pre_gen_project. Sh │ └ ─ ─ post_gen_project. Sh └ ─ ─ cookiecutter. JsonCopy the code

use

Distribute the template

The template is done. Where can I put it? Local directories are also available. Cookiecutter supports loading project templates from any accessible path, but for ease of use and team sharing, it’s best to push templates to a Git repository for later modifications, multi-party maintenance, etc.

In addition, if your template project is very large, you can also upload it as a ZIP package. Cookiecutter supports creating projects from zip packages, which will automatically decompress when created.

Create a project

Creating projects is also pretty easy, once you’ve made sure the Cookiecutter is properly installed, just CD to the directory where you want to create projects and then execute the following command:

cookiecutter https://github.com/Harley-xk/iOS_Template_Comet.git
Copy the code

The following path can be local or remote, git or zip. The Cookiecutter also supports many other warehouse forms, as you can see in its documentation for details.

The Cookiecutter will ask you for every keyword you set in your configuration file. You can either type the value you want to use for your new project, or you can press Enter and use the default value.

You can see the following output from the command line:


Create a project

The Cookiecutter caches all installed project templates in the user directory and will ask you if you need to update the templates the next time you install:

You’ve downloaded /Users/Harley-xk/.cookiecutters/iOS_Template_Comet before. Is it okay to delete and re-download it?

Type yes and it will automatically update the template to create the project.

You can also see that the pod Install directive automatically executes after the project is created, and the three dependencies we specified in our Podfile have all been successfully installed. Go to the MyApp directory and open myApp.xcworkspace to see the project created:


Completed project

conclusion

As you can see, the Cookiecutter’s principles and usage are pretty simple. Project now doing more and more huge, architecture is becoming more and more complex, although we can maximize the reuse of code by means of componentization, reduce the workload, but still need to write a lot of glue code to integrate various components, a large amount of project configuration, the project initialization work, through the use of Cookiecutter, can save a lot of further work.

Ps. I created my own iOS project template on Github. If you don’t want to write your own, you can also use my template:Github.com/Harley-xk/i…