Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2022 Developer ToolsSwift

WWDC22 · 15 min · Developer Tools / Swift

Meet Swift Package plugins

Discover how you can perform actions on Swift packages and Xcode projects with Swift package plugins. We’ll go over how these plugins work and explore how you can use them to generate source code and automate your development workflow.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 3 snippets

General structure of a package plugin with conditional support for Xcode projects when running in Xcode swift · at 6:59 ↗
import PackagePlugin

@main
struct MyPlugin: ... {

    // Entry points specific to plugin capability. These entry points are invoked
    // when the plugin is applied to a package.

}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension MyPlugin: ... {

    // Entry points specific to plugin capability. These entry points are invoked
    // when the plugin is applied to an Xcdeo project.

}
#endif
Structure of a command plugin with conditional support for Xcode projects when running in Xcode swift · at 8:33 ↗
import PackagePlugin

@main
struct MyPlugin: CommandPlugin {

    /// This entry point is called when operating on a Swift package.
    func performCommand(context: PluginContext, arguments: [String]) throws {
        debugPrint(context)
    }
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension MyPlugin: XcodeCommandPlugin {

    /// This entry point is called when operating on an Xcode project.
    func performCommand(context: XcodePluginContext, arguments: [String]) throws {
        debugPrint(context)
    }
}
#endif
Structure of a build tool plugin with conditional support for Xcode projects when running in Xcode swift · at 11:13 ↗
import PackagePlugin

@main
struct MyPlugin: BuildToolPlugin {

    /// This entry point is called when operating on a Swift package.
    func createBuildCommands(context: PluginContext, target: Target) throws -> [Command]
        debugPrint(context)
        return []
    }
}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension MyPlugin: XcodeBuildToolPlugin {

    /// This entry point is called when operating on an Xcode project.
    func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command]
        debugPrint(context)
        return []
    }
}
#endif