Enhance Xcode templates using Git
Xcode provides a selection of default file and project templates. They act as a basis anytime you create a new file or a new project in Xcode.
Since old times, we can create our own templates and integrate them easily in Xcode. Paul Hudson
wrote a great post about them.
Let see how we can use Git to power them up and create a distributed sample code codebase available right inside Xcode!
Creating custom Xcode templates
An Xcode template is basically a folder suffixed with the .xctemplate
path extension containing some source files and some metadata. Here is an example of a CoreData template.
To populate its template selection window Xcode looks for templates in two distinctive folders on the hard drive:
- The Xcode built-in folder:
/Applications/Xcode.app/.../Templates
- The current user folder:
~/Library/Developer/Xcode/Templates/
Adding a custom template in Xcode is as simple as copying and pasting a correctly structured xctemplate
folder into ~/Library/Developer/Xcode/Templates/
.
Paul Hudson
’s post explains in details the Xcode template structure. Consider reading it if you want to master the template creation!
Installing Xcode templates from git
In his post, Paul does not offer advice on how to manage Xcode templates. If we store our Xcode templates inside an Xcode configuration folder, we are at an increased risk of losing them and we would not be able to share them accross multiple machines. Storing our templates inside a git repository sounds like the best way to go.
But manually downloading a git repository into the Xcode configuration folder anytime we need to update our templates sounds fastidious though. How can we link templates from a repository to Xcode?
xcresource-cli can do just that. It is a small tool aimed at managing Xcode templates. In particular, it can download templates from a specified repository URL and integrate them in Xcode:
During the installation, we can specify a namespace
parameter. It acts as an installation folder and allows us to install templates from different sources:
Using Xcode templates as a distributed sample code database
Coupling Xcode templates with git can be very powerful. On one hand, Git allows us to distribute our templates and make contributing to them easy. On the other hand, thanks to the flexibility offered by Xcode, our templates can be displayed and selected right inside Xcode.
At Fabernovel, we use such sample code database. All our sample codes are stored in the code snippets repository which acts as a sample code reference repository. Anytime a sample code wouldn’t fit inside a library because it is too small, too variable or simple boilerplate code, we store it inside this repository. It mainly contains basic UI elements, boilerplate code, helper extensions, algorithms etc. Because all the sample codes are written as Xcode templates, they are all available in Xcode anytime we create a new file thanks to xcresource-cli
.
The code snippet repository perfectly complements the tools we already use in our Fabernovel developer day-to-day life to be more productive and avoid boilerplate:
- A project templater we use anytime we start a new iOS project
- ADUtils a set of helpers, shortcuts or other tools providing simplified interactions with UIKit and more generally with Swift.
- ccios an Xcode file generator for Clean Code architecture
What’s next?
Try to create your own Xcode template repository and display them in Xcode of course!
You can read Hudson’s post and xcresource-cli’s readme to get a better overview of what you can do with Xcode templates and Git.
Discover how you can use xcresource
to enhance your Xcode codesnippets ;)