HowTo make Xcode recognize custom templates
Asked Answered
E

3

10

I've been using Generamba to create template files for each module under VIPER architecture for iOS.

It saves a lot of time, but still needs commands through terminal to run Generamba and create the files. Does anyone have a clue on how to embed those templates generated directly into XCode 8?

Eclosion answered 6/11, 2016 at 10:57 Comment(0)
E
6

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.

Exenterate answered 9/11, 2016 at 13:10 Comment(0)
P
3

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).

Penitentiary answered 6/11, 2016 at 11:13 Comment(3)
How to custom set the templates under the macOS section?Doit
@A.C: I think you can only create new sections.Penitentiary
Yes, I had create a new sections for macOS. Thanks!Doit
T
0

Take a look at ViperC it supports both Objective-C and Swift. Also you can create test classes for the modules you created. Test classes use Quick and Expecta for Objective-C and Quick and Nimble for Swift.

Thomasson answered 21/9, 2017 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.