Prefire Documentation

repository·main·Indexed 19 days ago

https://github.com/barredewe/prefire

Prefire transforms SwiftUI #Preview blocks into automated snapshot tests, interactive component playbooks, and visual user stories. It provides a CLI, Xcode build tool plugins, and SPM plugins to help developers maintain visual consistency and living documentation. The tool supports custom configuration via .prefire.yml, allows for parameterized previews, and integrates with swift-snapshot-testing for snapshot execution.

Tokens
7.7K
Snippets
22
Records
31
Agent score
66%

What's inside Prefire

  1. Understand the Stencil template engine and context

    main

    Prefire uses the Stencil templating language combined with Sourcery extensions for Swift code generation.

    When rendering a template, the context provides three main top-level objects:

    1. argument: Contains configuration values from .prefire.yml.
    2. types: A collection of parsed Swift types (via Sourcery) used to identify PreviewProvider or PrefireProvider implementations.
    3. argument.previewsMacrosDict: An array of models representing #Preview macro blocks found in your source code.
  2. Install Prefire as a Swift Package Manager (SPM) Plugin

    main

    If you are working with a Swift package using a Package.swift manifest, add Prefire as a dependency and attach the appropriate plugins to your targets.

    1. Add Dependency: Add the Prefire package to your dependencies array.
    2. Attach Plugins:
      • Use PrefirePlaybookPlugin in a standard .target to generate Playbook (Demo) views.
      • Use PrefireTestsPlugin in a .testTarget to automatically generate snapshot tests when the test target is built.
    dependencies: [
        .package(url: "https://github.com/BarredEwe/Prefire", from: "4.0.0")
    ]
    
    // ...
    
    .target(
        plugins: [
            // For Playbook (Demo) view
            .plugin(name: "PrefirePlaybookPlugin", package: "Prefire")
        ]
    ),
    .testTarget(
        plugins: [
            // For Snapshot Tests
            .plugin(name: "PrefireTestsPlugin", package: "Prefire")
        ]
    )
  3. Configure Prefire via .prefire.yml

    main

    To customize how Prefire locates previews, generates files, and selects devices or templates, create a .prefire.yml file in the root of your project. You can define both test_configuration and playbook_configuration in a single file. For multi-module projects, you can use multiple .prefire.yml files (one per package) to support different configurations per module.

    Prefire applies these settings when running via the CLI (prefire tests, prefire playbook) or via the Xcode/SwiftPM plugin.

    test_configuration:
      target: PrefireExample
      test_target_path: ${PROJECT_DIR}/Tests
      test_file_path: PrefireExampleTests/PreviewTests.generated.swift
      template_file_path: CustomPreviewTests.stencil
      simulator_device: "iPhone15,2"
      required_os: 16
      preview_default_enabled: true
      use_grouped_snapshots: true
      split_snapshot_directories: false
      sources:
        - ${PROJECT_DIR}/Sources/
      snapshot_devices:
        - iPhone 14
        - iPad
      imports:
        - UIKit
        - SwiftUI
      testable_imports:
        - Prefire
    
    playbook_configuration:
      preview_default_enabled: true
      template_file_path: CustomModels.stencil
      imports:
        - UIKit
        - Foundation
      testable_imports:
        - SwiftUI
  4. Install Prefire via Mint

    main

    You can install the self-contained prefire CLI wrapper using Mint. The wrapper includes the PrefireBinary artifactbundle as an embedded resource.

    To override the path of the embedded binary (useful for local development), set the PREFIRE_BINARY_PATH environment variable to the absolute path of your prefire executable.

    mint install BarredEwe/Prefire
  5. Use default Stencil templates for Prefire

    main

    Prefire provides built-in Stencil templates that serve as excellent starting points for custom configurations. You can copy these templates, modify them to suit your needs, and then reference the new .stencil file in your .prefire.yml configuration.

    Available built-in templates:

    • Tests template: EmbeddedTemplates.previewTests (used for generating snapshot tests).
    • Playbook template: EmbeddedTemplates.previewModels (used for generating preview models).

    To use a custom template, ensure you specify the template_file_path in your .prefire.yml.

    test_configuration:
      target: MyApp
      test_target_path: ${PROJECT_DIR}/MyAppTests
      test_file_path: MyAppTests/PreviewTests.generated.swift
      template_file_path: MyAppTests/MinimalPreviewTests.stencil
      imports:
        - UIKit
  6. Quick Start: Add Prefire to your Swift project

    main

    To use Prefire, add it as a dependency in your Package.swift and include the PrefireTestsPlugin in your test target. You will also need swift-snapshot-testing for the generated snapshot tests to run.

    1. Add dependencies: Include Prefire and swift-snapshot-testing.
    2. Configure test target: Add Prefire as a dependency and register PrefireTestsPlugin in the plugins array.
    3. Write Previews: Use standard #Preview macros or PreviewProvider conforming to PrefireProvider.
    4. Run tests: Run your test target in Xcode; Prefire automatically generates snapshots based on your previews.
    // Package.swift
    dependencies: [
        .package(url: "https://github.com/BarredEwe/Prefire.git", from: "5.4.0"),
        .package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.0"),
    ],
    .testTarget(
        dependencies: [
          .product(name: "Prefire", package: "Prefire"),
          .product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
        ],
        plugins: [
            .plugin(name: "PrefireTestsPlugin", package: "Prefire")
        ]
    )
  7. Exclude Prefire models from release builds

    main

    To prevent PreviewProvider and mock data from being included in your production binary, you can disable the playbook using compiler flags.

    Swift Package Manager: Add the PLAYBOOK_DISABLED setting to your package configuration.

    Xcode: Add PLAYBOOK_DISABLED to your SWIFT_ACTIVE_COMPILATION_CONDITIONS build settings.

    // SPM
    swiftSettings: [
        .define("PLAYBOOK_DISABLED", .when(configuration: .release)),
    ]
  8. Install Prefire as an Xcode Build Tool Plugin

    main

    To integrate Prefire into an Xcode project, follow these steps to enable automatic snapshot test generation during builds:

    1. Add Dependency: Add Prefire as a package dependency to your project. You do not need to link any products directly.
    2. Prepare Test Target: Create or select a unit test target. Snapshot tests will be generated into this target. (If you don't have one, use File → New → Target → Unit Testing Bundle).
    3. Configure Build Phase:
      • Select the target you want to add linting to.
      • Open the Build Phases inspector.
      • Locate Run Build Tool Plug-ins and click the + button.
      • Select PrefireTestsPlugin from the list.
    4. Build: When you build this target, Prefire scans your sources and automatically generates snapshot tests for all #Preview declarations.

    Note: You can also attach PrefirePlaybookPlugin to a different build target to generate preview-based component models.

  9. Install and use the Prefire CLI

    main

    You can install the Prefire Command Line Interface (CLI) via Homebrew to manually trigger test or playbook generation.

    Installation:

    brew tap barredewe/prefire
    brew install prefire

    Commands:

    • prefire tests: Generates snapshot tests from your previews.
    • prefire playbook: Generates preview-based component models.
    • Use --help with any command to view available options (e.g., prefire tests --help).
    brew tap barredewe/prefire
    brew install prefire
    
    prefire tests
    prefire playbook
  10. Configure custom Stencil templates in .prefire.yml

    main

    To use a custom Stencil template for generating test files, add the template_file_path key under the test_configuration section of your .prefire.yml file. You can also provide additional imports that should be included in the generated file.

    test_configuration:
      target: MyApp
      test_target_path: ${PROJECT_DIR}/MyAppTests
      test_file_path: MyAppTests/PreviewTests.generated.swift
      template_file_path: MyAppTests/MinimalPreviewTests.stencil
      imports:
        - UIKit
  11. Configure custom Stencil templates

    main

    Prefire uses Stencil templates to generate snapshot tests and Playbook models. You can override the default templates by specifying a custom .stencil file path in your .prefire.yml configuration.

    Use the template_file_path key under either test_configuration or playbook_configuration.

    Important: The path is resolved relative to the target directory (test_target_path for tests or target for playbook), not relative to the .prefire.yml file location.

    # .prefire.yml
    
    test_configuration:
      target: MyApp
      test_target_path: ${PROJECT_DIR}/MyAppTests
      test_file_path: MyAppTests/PreviewTests.generated.swift
      template_file_path: MyAppTests/CustomPreviewTests.stencil
    
    playbook_configuration:
      target: ${PROJECT_DIR}/MyApp
      template_file_path: MyApp/CustomPreviewModels.stencil
  12. Configure Prefire via .prefire.yml

    main

    Use a .prefire.yml file to configure test and playbook behavior.

    Key settings:

    • test_configuration.target: Specifies the app target.
    • playbook_configuration.preview_default_enabled: A boolean that determines if all previews are included by default. If set to false, you must use .prefireEnabled() to include specific previews.
    test_configuration:
      target: MyApp
    
    playbook_configuration:
      preview_default_enabled: true