Trouble Adding OpenAPIGenerator Plugin to Run Build Tool Plug-ins in Xcode Using SwiftPM

I'm using SPM in my project and am trying to integrate Apple/swift-openapi-generator. My project is divided into several modules, for which I'm using SPM. To manage dependencies, I have a Package.swift file.

// swift-tools-version:5.8

import Foundation
import PackageDescription

let openAPIRuntime = Target.Dependency.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")
let openAPIURLSession = Target.Dependency.product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession")
let openAPIGenerator = Target.PluginUsage.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")

let targets: [Target] = [
    .target(
        name: "OpenAPIClient",
        dependencies: [openAPIRuntime, openAPIURLSession],
        plugins: [openAPIGenerator]
    ),
    //...
    ]

let package = Package(
    name: "Modules",
    platforms: [.iOS("16.0"), .macOS(.v11)],
    products: targets.filter { !$0.isTest }.map { .library(name: $0.name, targets: [$0.name]) },
    dependencies: [
        .package(url: "https://github.com/apple/swift-openapi-generator", from: "0.1.0"),
        .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.0.0"),
        .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"),
        //...
    ],
    targets: targets
)

As demonstrated, I've added openAPIRuntime and openAPIURLSession products and the openAPIGenerator plugin to this file. As a result, I'm able to successfully build the project.

Now, I want to add the openAPIGenerator plugin in MyTarget -> Build Phases -> Run Build Tool Plug-ins, but when I press the + button, I can't find openAPIGenerator.

Does MyTarget depend on OpenAPIClient? If so, you don't need to explicitly add the generator to MyTarget, as whenever OpenAPIClient is being built, it'll get invoked automatically behind the scenes.

If your question is about how to use Swift OpenAPI Generator with an Xcode target, that's documented in this tutorial and also shown in the WWDC2023 talk.

1 Like