|
1 | 1 | # ToggleGen |
2 | 2 |
|
3 | | -A description of this package. |
| 3 | +[](https://swift.org) |
| 4 | +[](https://github.com/TogglesPlatform/ToggleGen) |
| 5 | + |
| 6 | +**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. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- 🚀 **Type-Safe Code Generation**: Generates Swift enums and accessor classes with proper type safety |
| 11 | +- 📝 **Template-Based**: Uses Stencil templates for flexible code generation |
| 12 | +- 🔧 **Multiple Data Types**: Supports boolean, integer, double, string, secure, and object toggle types |
| 13 | +- 🎯 **Access Control**: Configurable access control levels (open, public, package, internal) |
| 14 | +- 🏗️ **Custom Property Names**: Supports custom property names through metadata |
| 15 | +- ⚡ **Command-Line Interface**: Easy-to-use CLI with comprehensive options |
| 16 | + |
| 17 | +## Supported Toggle Types |
| 18 | + |
| 19 | +ToggleGen supports the following toggle value types: |
| 20 | + |
| 21 | +- **Boolean** (`bool`): True/false values |
| 22 | +- **Integer** (`int`): Whole numbers |
| 23 | +- **Number** (`number`): Floating-point numbers |
| 24 | +- **String** (`string`): Text values |
| 25 | +- **Secure** (`secure`): Encrypted/encoded values |
| 26 | +- **Object** (`object`): Complex nested objects |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +### Download Pre-built Binary |
| 31 | + |
| 32 | +Download the latest pre-built binary from [GitHub Releases](https://github.com/TogglesPlatform/ToggleGen/releases): |
| 33 | + |
| 34 | +```bash |
| 35 | +# Download and install (replace VERSION with the latest version) |
| 36 | +curl -L https://github.com/TogglesPlatform/ToggleGen/releases/download/VERSION/ToggleGen -o /usr/local/bin/ToggleGen |
| 37 | +chmod +x /usr/local/bin/ToggleGen |
| 38 | +``` |
| 39 | + |
| 40 | +### Building from Source |
| 41 | + |
| 42 | +```bash |
| 43 | +git clone https://github.com/TogglesPlatform/ToggleGen.git |
| 44 | +cd ToggleGen |
| 45 | +swift build -c release |
| 46 | +``` |
| 47 | + |
| 48 | +The executable will be available at `.build/release/ToggleGen`. |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +### Basic Command |
| 53 | + |
| 54 | +```bash |
| 55 | +ToggleGen \ |
| 56 | + --datasource-path ./Demo/DemoDatasource.json \ |
| 57 | + --variables-template-path ./Templates/Variables.stencil \ |
| 58 | + --accessor-template-path ./Templates/Accessor.stencil \ |
| 59 | + --variables-enum-name "ToggleVariables" \ |
| 60 | + --accessor-class-name "ToggleAccessor" \ |
| 61 | + --variables-output-path ./GeneratedCode \ |
| 62 | + --accessor-output-path ./GeneratedCode |
| 63 | +``` |
| 64 | + |
| 65 | +### With Access Control |
| 66 | + |
| 67 | +```bash |
| 68 | +ToggleGen \ |
| 69 | + --variables-template-path ./Templates/Variables.stencil \ |
| 70 | + --accessor-template-path ./Templates/Accessor.stencil \ |
| 71 | + --variables-enum-name "ToggleVariables" \ |
| 72 | + --accessor-class-name "ToggleAccessor" \ |
| 73 | + --datasource-path ./Demo/DemoDatasource.json \ |
| 74 | + --accessor-output-path ./GeneratedCode \ |
| 75 | + --variables-output-path ./GeneratedCode \ |
| 76 | + --variables-access-control public \ |
| 77 | + --accessor-access-control public |
| 78 | +``` |
| 79 | + |
| 80 | +### Command Options |
| 81 | + |
| 82 | +| Option | Description | Required | |
| 83 | +|--------|-------------|----------| |
| 84 | +| `--variables-template-path` | Path to the variables template file | ✅ | |
| 85 | +| `--accessor-template-path` | Path to the accessor template file | ✅ | |
| 86 | +| `--variables-enum-name` | Name for the generated variables enum | ✅ | |
| 87 | +| `--accessor-class-name` | Name for the generated accessor class | ✅ | |
| 88 | +| `--datasource-path` | Path to the JSON datasource file | ✅ | |
| 89 | +| `--accessor-output-path` | Output directory for the accessor file | ✅ | |
| 90 | +| `--variables-output-path` | Output directory for the variables file | ✅ | |
| 91 | +| `--variables-access-control` | Access level for variables (open/public/package/internal) | ❌ | |
| 92 | +| `--accessor-access-control` | Access level for accessor (open/public/package/internal) | ❌ | |
| 93 | + |
| 94 | +## Configuration File Format |
| 95 | + |
| 96 | +Create a JSON file with your toggle configuration: |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "toggles": [ |
| 101 | + { |
| 102 | + "variable": "enable_new_feature", |
| 103 | + "bool": true |
| 104 | + }, |
| 105 | + { |
| 106 | + "variable": "max_retry_count", |
| 107 | + "int": 3 |
| 108 | + }, |
| 109 | + { |
| 110 | + "variable": "api_timeout", |
| 111 | + "number": 30.0 |
| 112 | + }, |
| 113 | + { |
| 114 | + "variable": "welcome_message", |
| 115 | + "string": "Welcome to our app!" |
| 116 | + }, |
| 117 | + { |
| 118 | + "variable": "api_key", |
| 119 | + "secure": "eDUxAQXW6dobqAMxhZIJLkyQKb8+36bFHc36eabacXDahMipVnGy/Q==" |
| 120 | + }, |
| 121 | + { |
| 122 | + "variable": "custom_toggle", |
| 123 | + "bool": false, |
| 124 | + "propertyName": "isCustomFeatureEnabled" |
| 125 | + } |
| 126 | + ] |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## Generated Code Example |
| 131 | + |
| 132 | +### Variables Enum (`ToggleVariables.swift`) |
| 133 | + |
| 134 | +```swift |
| 135 | +import Foundation |
| 136 | + |
| 137 | +public enum ToggleVariables { |
| 138 | + public static let enableNewFeature = "enable_new_feature" |
| 139 | + public static let maxRetryCount = "max_retry_count" |
| 140 | + public static let apiTimeout = "api_timeout" |
| 141 | + public static let welcomeMessage = "welcome_message" |
| 142 | + public static let apiKey = "api_key" |
| 143 | + public static let isCustomFeatureEnabled = "custom_toggle" |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### Accessor Class (`ToggleAccessor.swift`) |
| 148 | + |
| 149 | +```swift |
| 150 | +import Foundation |
| 151 | +import Toggles |
| 152 | + |
| 153 | +public class ToggleAccessor { |
| 154 | + private(set) var manager: ToggleManager |
| 155 | + |
| 156 | + public init(manager: ToggleManager) { |
| 157 | + self.manager = manager |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +extension ToggleAccessor { |
| 162 | + public var enableNewFeature: Bool { |
| 163 | + get { manager.value(for: ToggleVariables.enableNewFeature).boolValue! } |
| 164 | + } |
| 165 | + |
| 166 | + public var maxRetryCount: Int { |
| 167 | + get { manager.value(for: ToggleVariables.maxRetryCount).intValue! } |
| 168 | + } |
| 169 | + |
| 170 | + public var apiTimeout: Double { |
| 171 | + get { manager.value(for: ToggleVariables.apiTimeout).numberValue! } |
| 172 | + } |
| 173 | + |
| 174 | + public var welcomeMessage: String { |
| 175 | + get { manager.value(for: ToggleVariables.welcomeMessage).stringValue! } |
| 176 | + } |
| 177 | + |
| 178 | + public var apiKey: String { |
| 179 | + get { manager.value(for: ToggleVariables.apiKey).secureValue! } |
| 180 | + } |
| 181 | + |
| 182 | + public var isCustomFeatureEnabled: Bool { |
| 183 | + get { manager.value(for: ToggleVariables.isCustomFeatureEnabled).boolValue! } |
| 184 | + } |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## Templates |
| 189 | + |
| 190 | +ToggleGen uses [Stencil](https://github.com/stencilproject/Stencil) templates for code generation. You can customize the generated code by modifying the template files: |
| 191 | + |
| 192 | +- **Variables Template**: Generates the enum with toggle variable names |
| 193 | +- **Accessor Template**: Generates the accessor class with type-safe properties |
| 194 | + |
| 195 | +Example templates are provided in the `Templates/` directory. |
| 196 | + |
| 197 | +## Integration with Toggles Framework |
| 198 | + |
| 199 | +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. |
| 200 | + |
| 201 | +## Development |
| 202 | + |
| 203 | +### Running Tests |
| 204 | + |
| 205 | +```bash |
| 206 | +swift test |
| 207 | +``` |
| 208 | + |
| 209 | +### Building |
| 210 | + |
| 211 | +```bash |
| 212 | +swift build |
| 213 | +``` |
| 214 | + |
| 215 | +## Requirements |
| 216 | + |
| 217 | +- Swift 6.0 or later |
| 218 | +- iOS 15.0+ / macOS 12.0+ |
| 219 | + |
| 220 | +## Dependencies |
| 221 | + |
| 222 | +- [Swift Argument Parser](https://github.com/apple/swift-argument-parser) - Command-line interface |
| 223 | +- [Stencil](https://github.com/stencilproject/Stencil) - Template engine |
| 224 | + |
| 225 | +## License |
| 226 | + |
| 227 | +This project is licensed under the terms specified in the [LICENSE](LICENSE) file. |
| 228 | + |
| 229 | +## Contributing |
| 230 | + |
| 231 | +Contributions are welcome! Please feel free to submit a Pull Request. |
0 commit comments