Skip to content

TogglesPlatform/ToggleGen

Repository files navigation

ToggleGen

Swift 6.0 Platform

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.

Features

  • πŸš€ 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

Supported Toggle Types

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

Installation

Download Pre-built Binary

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/ToggleGen

Building from Source

git clone https://github.com/TogglesPlatform/ToggleGen.git
cd ToggleGen
swift build -c release

The executable will be available at .build/release/ToggleGen.

Usage

Basic Command

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 ./GeneratedCode

With Access Control

ToggleGen \
  --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

Command Options

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) ❌

Configuration File Format

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"
    }
  ]
}

Generated Code Example

Variables Enum (ToggleVariables.swift)

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"
}

Accessor Class (ToggleAccessor.swift)

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! }
    }
}

Templates

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.

Integration with Toggles Framework

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.

License

This project is licensed under the terms specified in the LICENSE file.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages