Skip to content

Add swift package add-target-plugin command #8432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hi2gage
Copy link
Contributor

@hi2gage hi2gage commented Mar 30, 2025

Adds swift package add-target-plugin command for adding plugins to target from CLI

Motivation:

Featured requested in #8169

Allows users to add package plugins to targets via command line.

Forums Discussion

Modifications:

Add new add-target-plugin command which aligns with the existing add-target-dependency command

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

Result:

After running the following command:

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

The following addition will be made to the Package.swift manifest:

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

@dschaefer2
Copy link
Member

@swift-ci please test

@dschaefer2
Copy link
Member

@swift-ci please test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 1, 2025

@swift-ci please test windows

@MaxDesiatov MaxDesiatov changed the title Add add plugin command Add swift package add-target-plugin command Apr 1, 2025
@hi2gage
Copy link
Contributor Author

hi2gage commented Apr 1, 2025

@bkhouri is windows (non self hosted) being flakey or is this somehow related to changes I made?

It looks like #8434 passed so maybe I need rebase in the new changes?

@hi2gage hi2gage force-pushed the add_add-plugin_command branch from 31f2020 to d0fe9a8 Compare April 7, 2025 15:32
@hi2gage
Copy link
Contributor Author

hi2gage commented Apr 7, 2025

@dschaefer2 @bkhouri just want to bump this, I rebased the new changes.

@hi2gage
Copy link
Contributor Author

hi2gage commented Apr 7, 2025

@swift-ci please test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 7, 2025

@swift-ci please test

@bkhouri
Copy link
Contributor

bkhouri commented Apr 7, 2025

@swift-ci test self hosted windows

1 similar comment
@bkhouri
Copy link
Contributor

bkhouri commented Apr 8, 2025

@swift-ci test self hosted windows

Copy link
Contributor

@bkhouri bkhouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could I ask that we add a bit more automated tests?

let contents: String = try fs.readFileContents(manifest)

XCTAssertMatch(contents, .contains(#".plugin(name: "other-product"#))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: I really appreciate the automated tests to validate the behaviour we want. Could I trouble you to add a few more that would check different use cases and fault injections?

  • Calling add-target-plugin on a non-existing package
  • Calling add-target-plugin providing all four combination of a valid/invalid plugin name and target name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bkhouri,
Thanks for the feedback! I've added two additional tests to cover this scenario:

Calling add-target-plugin on a non-existing package

Regarding your second request about validating the plugin and target names: the CLI currently just passes those strings through to the Package.swift manifest without performing validation, similar to how add-target-dependency behaves. Because of that, it's not currently possible to add meaningful tests that assert on the existence of the plugin or target.

That being said, I'm happy to discuss adding validation to some of these commands, but I believe it's outside the scope of this PR.

@hi2gage hi2gage force-pushed the add_add-plugin_command branch from d0fe9a8 to f1ab936 Compare April 11, 2025 18:38
@hi2gage hi2gage requested a review from bkhouri April 11, 2025 18:39
@bkhouri
Copy link
Contributor

bkhouri commented Apr 11, 2025

@swift-ci test

@bkhouri
Copy link
Contributor

bkhouri commented Apr 11, 2025

@swift-ci test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 14, 2025

Though I don't see how, for some reason, the Windows Self hosted pipeline seems to hang with this change.

@bkhouri
Copy link
Contributor

bkhouri commented Apr 14, 2025

@swift-ci test

@bkhouri
Copy link
Contributor

bkhouri commented Apr 14, 2025

@swift-ci test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 14, 2025

@swift-ci test windows

@hi2gage hi2gage force-pushed the add_add-plugin_command branch from 7f54d24 to c3a3b5d Compare April 14, 2025 23:39
@hi2gage
Copy link
Contributor Author

hi2gage commented Apr 14, 2025

@bkhouri ok I think I figured it out. Messed up the naming of the files.

Let's try it again!

@bkhouri
Copy link
Contributor

bkhouri commented Apr 15, 2025

@swift-ci test

@bkhouri
Copy link
Contributor

bkhouri commented Apr 15, 2025

@swift-ci test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 15, 2025

@swift-ci test windows

@hi2gage hi2gage force-pushed the add_add-plugin_command branch from c3a3b5d to d2ef799 Compare April 15, 2025 13:58
@hi2gage
Copy link
Contributor Author

hi2gage commented Apr 15, 2025

@bkhouri I think the Swift Test macOS Platform is borked across other PRs as well. But I just pushed the final 🤞 windows fix.

@bkhouri
Copy link
Contributor

bkhouri commented Apr 15, 2025

@swift-ci test

@bkhouri
Copy link
Contributor

bkhouri commented Apr 15, 2025

@swift-ci test self hosted windows

@bkhouri
Copy link
Contributor

bkhouri commented Apr 21, 2025

@swift-ci test

@plemarquand
Copy link
Contributor

@hi2gage This looks like it might benefit from the same code as in #8534 to allow add-target-plugin to be idempotent when adding a target plugin that already exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants