Module Templates for VIPER: A way to minimize the hecticity

Manish Kumar
6 min readFeb 6, 2018

--

The Intro

It’s been 5 years since I started working on iOS. In these years, I observed that everyting around you changes with time, whether it is programming guidelines, development language or be it initial scope of requirement given from client(for Agile development process). But one thing never changes…the DEADLINE. It is a word that can prove fatal depending upon situations.

If software development had its own dictionary, then the word DEADLINE would always fight it’s way to the top…

When you are working on a scalable project, you always tend to use shortcuts that can reduce the time behind your efforts. Some of these shortcuts are meant to be a piece of code and can be found online as ‘third party libraries’. On the other hand, some are only meant to speed up the overall development process by minimizing/clubbing the miscellaneous process involved in development process.

Working with VIPER as your solution architecture familiarizes you with the pain of losing every single minute of the time allotted😢. In these type of cases, you should have some hacks to speed up the development process(If you are using VIPER, you are already writing 3x amount of code as compared to MVC😓).

As promised in my last blog, today I will illustrate how to create a module template for VIPER Project.

Getting Started…

The module template that we need to develop will have the same process like Xcode has for creating a new file. For that, you need to go to location

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/developer/Library/Xcode/Templates/File\ Templates/Source/

At this location, you can find many folders with extension .xctemplate.

Copy the folder Cocoa Touch Class.xctemplate and paste it to your Desktop before you start exploring its contents as you don’t want to mess around with Cocoa Touch default file template.

Now in the Cocoa Touch Class.xctemplate (on Desktop), delete everything except TemplateIcon.png, TemplateIcon@2x.png, TemplateInfo.plist.

Now open TemplateInfo.plist. This is the file most important to you for now as it will contain many different configurations. In the file, you can see many sets of key-value pairs. But all are not necessary to be discussed. Let’s go through the one’s necessary one by one.

a. Description(Type: String)- The Description refers to objective/final output of the template you are making. Choose whatever words you wish to describe what this template will do.

b. Summary(Type: String)- It is same as description.

c. MainTemplateFile(Type: String)- It is used for naming files for the module. Since all the files in VIPER module have the module name as their prefix like LoginView.swift, LoginPresenter.swift etc. this will act as a type of variable whose value is the module name. Since this key is not present in the plist file, you need to add this with value ___FILEBASENAME___ .

TemplateInfo.plist key-value pairs

d. Options(Type: Array)- These are a set of options/input you would like to have while creating the file/module. For creating the module, we need only the module name, and so we will create one item under the same(type Dictionary).The item will consist of following sets of key-value pairs:

  • Description(Type: String)- It refers to a small description of the input needed for this item.
  • Identifier(Type: String)- It refers to a variable to hold the name of the VIPER module to be created. It will help in differentiating it from other items in the Options.
  • Name(Type: String)- It refers to the field label.
  • Required(Type: String)- It refers to whether the input that we require through the item is required or optional.
  • Type(Type: String)- It refers to the data type of the input for which the item is created.
  • NotPersisted(Type: Boolean)- It refers to persistence of the value in the field.
Item 0 configuration

Along with this item, we will be needing one more item to rebase our module file contents. The template that we are making will contain module files with a generic filename. When we will generate the module, the item will rename file contents as per the module name. The item will consist of following sets of key-value pairs:

  • Default(Type: String)- The variable through which you will be renaming file contents. In other words, a default value for the item. Use ___VARIABLE_viperModuleName___ as its value.
  • Identifier(Type: String)- Again a value to identify the item’s uniqueness
  • Type(Type: String)- Since the item is for configuring file contents and not take any inputs, its type will be static.
Item 1 configuration

And here ends our .plist configuration… 😀😀

To complete it fully, just rename theCocoa Touch Class.xctemplate to Module VIPER.xctemplate .

Module File Configurations.

After TemplateInfo.plist file configurations this is what your folder contains

Module VIPER.xctemplate folder

Now, we need the module template files.

The module template files will contain some code for basic module configuration. But apart from that, what we have to care about it is to make all these files generic for all modules. It will require playing with some TemplateInfo.plist key value pairs that we used earlier.

Now, for making it easy, you can download module files from here that are not configured yet. The files are just of a Test Module that will be converted to generic module. Please download these files and paste it into folder Module VIPER.xctemplate.

Pick anyone file that you wish. For example, I take TestWireframe.swift. Now open the same. At the very top, you often see information like shown in below image.

File Header

These are the File Headers and are intended to deliver basic information like FileName, ProjectName, Author, CreatedDate, CopyrightInfo etc. We will be working first on these informations to make it generic. Replace each of the given below words with the respective words:

  • TestWireframe.swift : ___FILENAME___
  • ViperSampleApp : ___PROJECTNAME___
  • Manish Kumar : ___FULLUSERNAME___
  • 08/01/18 : ___DATE___
  • 2018 : ___YEAR___
  • Manish Kumar : ___ORGANIZATIONNAME___

Now we will be working on making the code generic. But when I say this, I don’t mean any complex work. Just a bit of Find and Replace work in Xcode😉.

Now since we only have to make the file module specific, just find the word “Test” in class TestWireframe (along with word Test in Class name itself.). Replace the word with ___VARIABLE_viperModuleName___. If you remember, we kept it to be used as a default value for renaming file contents. And with the completion of same, we are ready with the generic file contents.

You have to follow the same process for each file. After all the files are converted, just copy Module VIPER.xctemplate folder and paste it in the same place from where you copied Cocoa Touch Class.xctemplate.

Just quit the Xcode(if open) and start it with any existing VIPER project. Try to add a new file, and you could see the option to create Module VIPER.☺️

With this, I rest my case.

You can find module templates at the following Github Repository: https://github.com/manish-1612/Viper_Module_Template.

Upcoming

In the next blog, I will write a blog on how to create your own Project Template(with 5 inbuilt modules like Login, Dashboard, Settings, SideMenu, Logout).

If you like the blog, please don’t forget to clap. 😊😊😊😊😊

--

--