Automating iOS Color Management and Code Snippet Generation with CocoaPods, Ruby, and ERB
This article describes a complete workflow for iOS UI development that centralizes color definitions in a JSON palette, synchronizes them via a CocoaPods post‑integrate hook, and automatically generates Objective‑C/Swift color constants and Xcode code snippets using Ruby and ERB templates, dramatically improving efficiency and reducing manual errors.
Introduction
In everyday UI development, handling colors—especially when supporting night mode—requires maintaining two sets of color values, which can be error‑prone and time‑consuming for engineers.
Unified Color Source
A single source of truth called a color palette is maintained as a JSON file containing roughly 50 color entries, each with a default and night variant. Designers maintain the palette, and engineers consume it.
{
"T010": {
"default": "#333333",
"night": "#C2C2C2"
},
"T020": {
"default": "#666666",
"night": "#888888"
},
"T030": {
"default": "#AAAAAA",
"night": "#666666"
}
}Synchronizing the Palette
To avoid manual errors, the palette is stored in a remote configuration center and automatically synced to the project. Designers update the palette through a managed process, and engineers pull the latest version via a post_integrate hook provided by CocoaPods.
post_integrate Hook
post_integrate do |installer|
# download and sync the color palette
endLocating Files with Xcodeproj
The Ruby xcodeproj library is used to locate the pod project, its targets, and the JSON resource files:
require 'xcodeproj'
# Open the pod project
project = Xcodeproj::Project.open(pod_project_path)
# Get all targets
targets = project.targets
# Example: retrieve resource files
resources_files = resource_target.resources_build_phase.files
path = file.file_ref.real_path.to_sAutomatic Generation of Color Constants
After syncing, a script generates Objective‑C header and implementation files that expose the colors as SNBColor string enums.
#ifndef Color_h
#define Color_h
typedef NSString *SNBColor NS_EXTENSIBLE_STRING_ENUM;
/// default: #333333, night: #C2C2C2
FOUNDATION_EXTERN SNBColor const SNBColorT010;
/// default: #666666, night: #888888
FOUNDATION_EXTERN SNBColor const SNBColorT020;
/// default: #AAAAAA, night: #666666
FOUNDATION_EXTERN SNBColor const SNBColorT030;
#endif /* Color_h */ #import "Color.h"
SNBColor const SNBColorT010 = @"T010";
SNBColor const SNBColorT020 = @"T020";
SNBColor const SNBColorT030 = @"T030";Using ERB Templates
ERB (Embedded Ruby) renders the header file from the palette data. A simple example:
require 'erb'
x = 42
template = ERB.new <<-EOF
The value of x is: <%= x %>
EOF
puts template.result(binding)A more complex template iterates over color objects to produce the Color.h content:
template = %{#ifndef Color_h
#define Color_h
typedef NSString *SNBColor NS_EXTENSIBLE_STRING_ENUM;
<% colors.each do |c| %>
/// default: <%= c.default %>, night: <%= c.night %>
FOUNDATION_EXTERN SNBColor const SNBColor<%= c.name %>;
<% end %>
#endif /* Color_h */}
color_h = ERB.new(template)
puts color_h.result(binding)Code Snippet Automation
Design‑to‑code conversion is further accelerated by generating Xcode Code Snippets for each color. Snippets are stored as XML files under ~/Library/Developer/Xcode/UserData/CodeSnippets/ . The script uses ERB to fill in the variable parts (title, identifier, completion text, etc.) and writes the resulting XML files.
# post_integrate hook to generate snippets
post_integrate do |installer|
# download palette
# generate Code Snippets
endSummary
The end‑to‑end solution requires no special configuration from developers. When a new color is added, a pod install triggers palette sync, constant file regeneration, and Code Snippet updates, ensuring that UI developers can reference colors by name rather than raw hex values, improving consistency, reducing manual work, and supporting night mode effortlessly.
References
post_integrate Hook DSL: https://www.rubydoc.info/github/CocoaPods/Core/Pod/Podfile/DSL#post_integrate-instance_method
Xcodeproj gem: https://www.rubydoc.info/gems/xcodeproj
ERB documentation: https://www.rubydoc.info/gems/erb
Xcode Snippet guide: https://nshipster.com/xcode-snippets/
Snowball Engineer Team
Proactivity, efficiency, professionalism, and empathy are the core values of the Snowball Engineer Team; curiosity, passion, and sharing of technology drive their continuous progress.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.