Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Xcode 9 custom templates changed?

after downloading Xcode 9 beta I noticed change in file templates system.

For example I had a simple template that was creating 2 files (it probably shouldn't work that way at all). Base file names are

___FILEBASENAME___.swift

and

___FILEBASENAME___View.swift

and its creates TableCell.swift and TableCellView.swift, here are codes:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

import Foundation
import UIKit

class ___FILEBASENAME___: UITableViewCell {

    let mainView = ___FILEBASENAME___View()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
        contentView.addSubview(mainView)
        mainView.setupView()
    }

    fileprivate func setupCell() {
        backgroundColor = UIColor.clear
        selectionStyle = .none
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

And View file:

//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

import Foundation
import UIKit
import SnapKit

fileprivate struct Constants {

}

class ___FILEBASENAME___View: UIView {

    func setupView() {
        setupSelf()
    }

    fileprivate func setupSelf() {
            snp.makeConstraints { (make) in
                make.leading.trailing.top.bottom.equalTo(0)
        }
    }

}

To create this files I was just picking template from

New Files...

menu, add an Name for ex. TableCell and hit enter. Now when I do this my output looked like this:

import Foundation
import UIKit

class TableCell: UITableViewCell {

    let mainView = TableCellView()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupCell()
        contentView.addSubview(mainView)
        mainView.setupView()
    }

    fileprivate func setupCell() {
        backgroundColor = UIColor.clear
        selectionStyle = .none
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

and

import Foundation
import UIKit
import SnapKit

fileprivate struct Constants {

}

class NewCellView: UIView {

    func setupView() {
        setupSelf()
    }

    fileprivate func setupSelf() {
        snp.makeConstraints { (make) in
            make.leading.trailing.top.bottom.equalTo(0)
        }
    }

}

Now the problem is, in Xcode 9 they changed things in templates (something with templates in playgrounds, how newbie would use templates in playgrounds anyway?).

Back to the problem, now after creating files from template, TableCell.swift looks the same but TableCellView.swift goes wild due to this change.

___FILEBASENAME___View

becomes new

___FILEBASENAME___

so now when I create TableCellView it looks like this:

import Foundation
import UIKit
import SnapKit

fileprivate struct Constants {

}

class TableCellViewView: UIView {

    func setupView() {
        setupSelf()
    }

    fileprivate func setupSelf() {
        snp.makeConstraints { (make) in
            make.leading.trailing.top.bottom.equalTo(0)
        }
    }

}

Now the problem becomes more complicated when I create multiple files with dependencies from one another for example I have TableCellManager with delegate to TableCellViewControllerDelegate, when creating files now it looks like this

TableCellManagerViewControllerDelegate

instead just

TableViewControllerDelegate

___FILEBASENAME___ is replaced depending on scope, if newly created file is for example

___FILEBASENAME___View.swift

using keyword "Table" creates TableView.swift - in which ___FILEBASENAME___ is not "Table" but "TableView"

Can anyone tell me if there is a way to handle with it? Maybe there is something new like ___KEYWORD___? At creation of new file I want to enter a keyword that would work like ___FILEBASENAME___ in the older versions of Xcode. HELP!

like image 628
Maciej Zajda Avatar asked Jun 29 '17 06:06

Maciej Zajda


1 Answers

I have a working solution on my github for Xcode 9, Beta 4. Check it out: https://github.com/reni99/Xcode-VIPER-Template

I got the solution from here: https://github.com/infinum/iOS-VIPER-Xcode-Templates/tree/master/VIPER%20Templates/Module.xctemplate/UIViewController

What you are looking for is this variable: ___VARIABLE_moduleName___

Make sure to adjust your TemplateInfo.plist for your needs.

Hope this helps :)

Cheers

like image 136
Renato Stauffer Avatar answered Sep 19 '22 16:09

Renato Stauffer