Building Your Own Swift Code Generator Using Swift Script

Code generator is nice to improve productivity

Tifo Audi Alif Putra
Geek Culture
4 min readJun 17, 2023

--

Photo by Jayphen Simpson on Unsplash

Swift is a programming language that has many advantages. Did you know that we can also create executable scripts using Swift? By creating executable scripts using Swift, we can do many things such as executing scripts in the Xcode build process. But in this article, I tried to make an executable script to make a code generator.

Maybe you already know a lot about swift code generators out there like for example SwiftGen. However, if you are interested in making a custom implementation code generator, then keep reading this article until it’s finished :].

Getting Started

I’ve made a simple project that has a code generator in it. Please clone and let’s try to make a simple script.

After completing the clone project, try opening the file SampleScript.swift

When creating a script using Swift, make sure to add the @main attribute and make sure there is a static function named main. When the script is executed, the code inside the function will be executed.

Then to change the swift code into an executable script, you can open a terminal and execute a command like this:

xcrun swiftc -parse-as-library SampleScript.swift

Make sure in your terminal you have opened the directory where the swift code is located. After you execute the command, This will compile your Swift file and create an executable binary with the same name.

After the executable script has been created, you can run the following command to execute the script:

./SampleScript

The result will print “execute script!” on your terminal. Those are the basic things you can do to create executable scripts using Swift.

Code Generator

There are lots of real cases that require a code generator so that we can increase our productivity as software engineers. In iOS development, sometimes we need a code generator for localization, assets, etc. to make it easier for us to use it.

In this article, the code generator that I created is to create an extension of the token design that is ready to be used immediately. Generally the UI/UX team will have a design system as a single source of truth for styling that is used on all platforms and then we engineers are required to use these tokens, for example, such as colors or typography. The code generator in this case will be very helpful, if the UI/UX team plans to change the token design, then we don’t need to do manual migration. Simply by executing the script and the results can be directly used in our application.

The token design can take many formats, but in general it is usually in the form of json. So in the script that we are going to make, first we have to load the json and make it in a dictionary format.

After successfully loading json and generating it in dictionary format, you can write your own algorithm to create a code generator that suits your needs.

In the code above, I created extensions for each token type such as UIColor and UIFont. Then do a recursive traversal to the dictionary that we created earlier. After the token extension has been successfully created, we can create a Swift file from the token extension.

After the script has been created, as before we will create an executable script using the following command:

xcrun swiftc -parse-as-library TokenGenerator.swift

Then just execute the script using the command like this:

./TokenGenerator

Then there will be a new Swift file called TokenExtension.swift, open your finder and insert the file into Xcode. Here is the result of the code from the script that has been made:

import UIKit

extension UIFont {
static var title: UIFont {
UIFont(name: "OpenSans Bold", size: 22) ?? .systemFont(ofSize: 22)
}
static var caption: UIFont {
UIFont(name: "OpenSans Regular", size: 12) ?? .systemFont(ofSize: 12)
}
static var body: UIFont {
UIFont(name: "OpenSans Medium", size: 14) ?? .systemFont(ofSize: 14)
}
}

extension UIColor {
static var blueLight: UIColor {
UIColor.hexColor("#ADD8E6") ?? .black
}
static var blueDark: UIColor {
UIColor.hexColor("#00008B") ?? .black
}
static var blueRegular: UIColor {
UIColor.hexColor("#89CFF0") ?? .black
}
static var greenLight: UIColor {
UIColor.hexColor("#90EE90") ?? .black
}
static var greenDark: UIColor {
UIColor.hexColor("#013220") ?? .black
}
static var greenRegular: UIColor {
UIColor.hexColor("#32CD32") ?? .black
}
static var redLight: UIColor {
UIColor.hexColor("#FFCCCB") ?? .black
}
static var redDark: UIColor {
UIColor.hexColor("#8b0000") ?? .black
}
static var redRegular: UIColor {
UIColor.hexColor("#880808") ?? .black
}
}

Conclusion

Swift is a programming language that has many advantages, one of which is that we can create executable scripts. Hopefully this article can inspire you and create your own script to increase productivity and make your daily work easier. Thank you for reading this article and see you in another article.

--

--