ToggleGen is a Swift command-line tool that generates type-safe Swift code for feature toggles (feature flags) from JSON configuration files. It creates strongly-typed accessor classes and variable enums to help you manage feature toggles in your iOS and macOS applications safely and efficiently.
- π Type-Safe Code Generation: Generates Swift enums and accessor classes with proper type safety
- π Template-Based: Uses Stencil templates for flexible code generation
- π§ Multiple Data Types: Supports boolean, integer, double, string, secure, and object toggle types
- π― Access Control: Configurable access control levels (open, public, package, internal)
- ποΈ Custom Property Names: Supports custom property names through metadata
- β‘ Command-Line Interface: Easy-to-use CLI with comprehensive options
ToggleGen supports the following toggle value types:
- Boolean (
bool): True/false values - Integer (
int): Whole numbers - Number (
number): Floating-point numbers - String (
string): Text values - Secure (
secure): Encrypted/encoded values - Object (
object): Complex nested objects
Download the latest pre-built binary from GitHub Releases:
# Download and install (replace VERSION with the latest version)
curl -L https://github.com/TogglesPlatform/ToggleGen/releases/download/VERSION/ToggleGen -o /usr/local/bin/ToggleGen
chmod +x /usr/local/bin/ToggleGengit clone https://github.com/TogglesPlatform/ToggleGen.git
cd ToggleGen
swift build -c releaseThe executable will be available at .build/release/ToggleGen.
ToggleGen \
--datasource-path ./Demo/DemoDatasource.json \
--variables-template-path ./Templates/Variables.stencil \
--accessor-template-path ./Templates/Accessor.stencil \
--variables-enum-name "ToggleVariables" \
--accessor-class-name "ToggleAccessor" \
--variables-output-path ./GeneratedCode \
--accessor-output-path ./GeneratedCodeToggleGen \
--variables-template-path ./Templates/Variables.stencil \
--accessor-template-path ./Templates/Accessor.stencil \
--variables-enum-name "ToggleVariables" \
--accessor-class-name "ToggleAccessor" \
--datasource-path ./Demo/DemoDatasource.json \
--accessor-output-path ./GeneratedCode \
--variables-output-path ./GeneratedCode \
--variables-access-control public \
--accessor-access-control public| Option | Description | Required |
|---|---|---|
--variables-template-path |
Path to the variables template file | β |
--accessor-template-path |
Path to the accessor template file | β |
--variables-enum-name |
Name for the generated variables enum | β |
--accessor-class-name |
Name for the generated accessor class | β |
--datasource-path |
Path to the JSON datasource file | β |
--accessor-output-path |
Output directory for the accessor file | β |
--variables-output-path |
Output directory for the variables file | β |
--variables-access-control |
Access level for variables (open/public/package/internal) | β |
--accessor-access-control |
Access level for accessor (open/public/package/internal) | β |
Create a JSON file with your toggle configuration:
{
"toggles": [
{
"variable": "enable_new_feature",
"bool": true
},
{
"variable": "max_retry_count",
"int": 3
},
{
"variable": "api_timeout",
"number": 30.0
},
{
"variable": "welcome_message",
"string": "Welcome to our app!"
},
{
"variable": "api_key",
"secure": "eDUxAQXW6dobqAMxhZIJLkyQKb8+36bFHc36eabacXDahMipVnGy/Q=="
},
{
"variable": "custom_toggle",
"bool": false,
"propertyName": "isCustomFeatureEnabled"
}
]
}import Foundation
public enum ToggleVariables {
public static let enableNewFeature = "enable_new_feature"
public static let maxRetryCount = "max_retry_count"
public static let apiTimeout = "api_timeout"
public static let welcomeMessage = "welcome_message"
public static let apiKey = "api_key"
public static let isCustomFeatureEnabled = "custom_toggle"
}import Foundation
import Toggles
public class ToggleAccessor {
private(set) var manager: ToggleManager
public init(manager: ToggleManager) {
self.manager = manager
}
}
extension ToggleAccessor {
public var enableNewFeature: Bool {
get { manager.value(for: ToggleVariables.enableNewFeature).boolValue! }
}
public var maxRetryCount: Int {
get { manager.value(for: ToggleVariables.maxRetryCount).intValue! }
}
public var apiTimeout: Double {
get { manager.value(for: ToggleVariables.apiTimeout).numberValue! }
}
public var welcomeMessage: String {
get { manager.value(for: ToggleVariables.welcomeMessage).stringValue! }
}
public var apiKey: String {
get { manager.value(for: ToggleVariables.apiKey).secureValue! }
}
public var isCustomFeatureEnabled: Bool {
get { manager.value(for: ToggleVariables.isCustomFeatureEnabled).boolValue! }
}
}ToggleGen uses Stencil templates for code generation. You can customize the generated code by modifying the template files:
- Variables Template: Generates the enum with toggle variable names
- Accessor Template: Generates the accessor class with type-safe properties
Example templates are provided in the Templates/ directory.
The generated code is designed to work with a ToggleManager class that provides the actual toggle values at runtime. The generated accessor acts as a type-safe wrapper around your toggle management system.
This project is licensed under the terms specified in the LICENSE file.
Contributions are welcome! Please feel free to submit a Pull Request.