ColorUp
An easy way to generate strongly typed Swift extensions for either UIColor or Color based off of your colors within the project's asset catalog.
Motivations
At Buffer we use color catalogs extensively across our own iOS apps. But, we find that we often can mistype the names (resulting in a nil color instance) and that we have quite a few of them. This is the main problem we set out to solve. Using a generator such as this gives us the flexibility of color catalogs with the advantages of concrete function calls:
There are other things in the pipeline for the future, such as supporting Objective-C and bundle asset catalog lookups, but this is our start!
Usage
To install ColorUp, download this repository locally to your machine.
Then navigate to its location:
From there, you must provide two things at a minimum:
- The fully formed file location of the asset catalog with the colors,
-p
. - The fully formed file location of where you'd like to save the generated file,
-s
.
To get a feel for what you can use, run the --help
command:
Here is what a command would look like:
If the asset catalog has one color named "MyColor", the result would look like this for UIKit:
...and for SwiftUI:
If you'd like to run ColorUp from anywhere and not have to nagivate to its location on the file system, you build it for release and move it to the executable binaries folder:
This allows you to just open Terminal and run it from anywhere.
Options
Xcode Project: Required
The complete path to the asset catalog that contains the colors you wish to generate an extension file for.
Save Location: Required
The complete path where you wish to save the file at.
Use force unwrapping: Optional
Use this option to generate a force-unwrapped color call. Note that SwiftUI code will ignore this option, as it's Color(name)
initializer doesn't produce an optional type.
Example:
versus the default:
Function Prefix: Optional
Puts the supplied string in front of the generated functions.
Example:
versus the default:
File Name: Optional
The name of the generated file containing the extensions. Defaults to ColorCatalogExtensions-UIKit.swift
for UIKit, and ColorCatalogExtensions-SwiftUI.swift
for SwiftUI.
Generate SwiftUI Code: Optional
A boolean
, if present the generated code is for SwiftUI's Color
type. If not, it'll default to UIKit and UIColor
.
Contributing
ColorUp welcomes anyone to contribute. Here's a quick start guide:
1. Clone the project
2. Generate an Xcode Project
3. Build the Project
4. Run It
I find it's much easier to run it locally from an Xcode project and use the CommandLineUtil
class to provide debug values using debugValues()
.
For development, all the files you'll need to use are within ColorUpCore
:
ColorUp.swift
runs the actual program, and has the code to generate the file.CommandLineUtil.swift
houses logic to get input from the command the user has run, and houses them withinFileGenOptions
.