Architectural Pattern .xctemplate
Architectural Design Pattern code generation sometimes is long journey at least for me. Repeating code grouping in some folder.
And sometimes code not directly running when we call it. So why we don’t automate that code generation for Speed-up our development process.
So we just focus on Logic not on the pattern. Here we are creating custom file template for Xcode so we can get All classes directly after we create the module.
Main file for generating file template is TemplateInfo.plist, you can check mine to make it easier to understand.
<?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>Platforms</key>
<array/>
<key>Kind</key>
<string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>
<key>Options</key>
<array>
<dict>
<key>Identifier</key>
<string>productName</string>
<key>Required</key>
<true/>
<key>Name</key>
<string>Module Name:</string>
<key>Type</key>
<string>text</string>
<key>Default</key>
<string>Module Name</string>
<key>NotPersisted</key>
<true/>
</dict>
</array>
</dict>
</plist>
- Kind — because we create file template we just use this kind value Xcode.IDEFoundation.TextSubstitutionFileTemplateKind
- Options — Input that we will show to user, for us we just use Module Name with type text
- Identifier — Like the name just identifier for input
- Name — Name label that will we show to user
- Default — Default value for Module Name
- NotPersisted — Not persist pervious Module Name
Then the file just copy your sample File template to folder with TemplateInfo.plist like this
Check one of my File template for ViewModel bellow
// Created with MVVM 2024
// Git Repo: https://github.com/alfian0/Clean-Architecture.git
// ___FILENAME___
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
//
import Foundation
import Combine
//sourcery: Injected
final class ___VARIABLE_productName___PageViewModel: ObservableObject {
@Published var isLoading: Bool = false
private var coordinator: ___VARIABLE_productName___Coordinator?
private let repository: ___VARIABLE_productName___Repository
private var cancellables: Set<AnyCancellable> = []
init(repository: ___VARIABLE_productName___Repository, params: ___VARIABLE_productName___PageViewModelParams) {
self.repository = repository
}
deinit {
self.cancellables.forEach { cancellable in
cancellable.cancel()
}
}
}
extension ___VARIABLE_productName___PageViewModel {
func set(coordinator: ___VARIABLE_productName___Coordinator) {
self.coordinator = coordinator
}
}
- ___FILENAME___ — It’s a name of your File that generated by Module Name
- ___PROJECTNAME___ — Your Project Name
- ___FULLUSERNAME___ — Your User Name
- ___DATE___ — Date that File created
- ___VARIABLE_productName___ — This is Module Name that you created before
Oke if you are Done just copied Folder with extension .xctemplate to
/Users/[User Name]/Library/Developer/Xcode/Templates
Then open your project and Add new File, your template will Appear on the menu like this
Xcode will generate All file that you need automatically so you don’t need to create each file and folder by your self.
You can check my CeanArchitecture.xctemplate from my Github.