HowTo make Xcode recognize custom templates

You can use templates inside Xcode to generate your VIPER classes. Take a look on this repository, it already implements the basic VIPER files for you.

Hope it helps.


I am not familiar with Generamba, but to make Xcode recognize your templates in general:

  • put your Template.swift file in a folder called MyTemplate.xctemplate

  • tell Xcode some details about your template by adding a Templateinfo.plist to MyTemplate.xctemplate (find example below).

  • copy MyTemplate.xctemplate to ~/Library/Developer/Xcode/Templates/File\ Templates/Custom.

After doing so, the templates show up on the bottom of Xcodes new File template selection menu.

Example:

enter image description here


You can use environmental variable placeholders that get replaced by Xcode.

Here is a simple example template called Worker.swift:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright (c) ___YEAR___ ___ORGANIZATIONNAME___. All rights reserved.
//
//  This file was generated. DO NOT MODIFY !
//

import Foundation

class ___FILEBASENAMEASIDENTIFIER___Worker {

    //implementation goes here
}

And its example Templateinfo.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>DefaultCompletionName</key>
    <string>MyWorker</string>
    <key>Description</key>
    <string>This generates a new worker.</string>
    <key>Kind</key>
    <string>Xcode.IDEKit.TextSubstitutionFileTemplateKind</string>
    <key>Options</key>
    <array>
        <dict>
            <key>Default</key>
            <string>___VARIABLE_sceneName:identifier___Worker</string>
            <key>Description</key>
            <string>The worker name</string>
            <key>Identifier</key>
            <string>workerName</string>
            <key>Name</key>
            <string>Worker Name:</string>
            <key>Required</key>
            <true/>
            <key>Type</key>
            <string>static</string>
        </dict>
    </array>
    <key>Platforms</key>
    <array>
        <string>com.apple.platform.iphoneos</string>
    </array>
    <key>SortOrder</key>
    <string>4</string>
    <key>Summary</key>
    <string>Summery</string>
</dict>

You can also place multiple files within your MyTemplate.xctemplate directory to make Xcode create multiple files at once. For your VIPER templates, you can make Xcode create a whole VIPER scene at once.

You can find working examples plus a makefile in this "Clean Swift" template repo (Clean Swift is yet another Clean Architecture approach for Swift).