Proposal: `swift package add-target-plugin` Command to SwiftPM

Problem statement

The SwiftPM currently lacks a command for adding build plugins to targets. This proposal introduces the swift package add-target-plugin command which is similar to add-target-dependency.

This feature was requested issue #8169.

Proposed interface:

> swift package add-target-plugin --help
OVERVIEW: Add a new target plugin to the manifest

USAGE: swift package add-target-plugin <plugin-name> <target-name> [--package <package>]

ARGUMENTS:
  <plugin-name>           The name of the new plugin
  <target-name>           The name of the target to update

OPTIONS:
  --package <package>     The package in which the plugin resides
  --version               Show the version.
  -h, -help, --help       Show help information.

Use case (as mentioned in the original issue):

swift package add-target-plugin OpenAPIGenerator myTarget --package swift-openapi-generator

which would add OpenAPIGenerator to the myTarget target.

.target(
  name: "myTarget",
  plugins: [
    .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")
  ]
)

Impact on Interface:

This introduces a new Package manifest editing command but does not interfere with existing commands. It follows the same pattern as add-target-dependency, allowing users to add properties to .target(...) in a consistent manner.

Alternatives Considered:

For alternatives, see the discussion in this post, which suggests revisiting the organization of commands to introduce additional hierarchy for manifest-editing commands.

Conclusion

This is my first proposal, and I’m unsure if it needs to go through the full evolution process since it’s adding a new command. I’m looking for some public feedback before submitting the PR.

edit: typo OpenAPI-Generator -> OpenAPIGenerator

5 Likes

+1, Looks good to me.

Should probably be add-target-plugin OpenAPIGenerator, looks like a typo.

1 Like