You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adjust SE-0332 to reflect the removal of the targets parameter (#1562)
* Adjust SE-0332 to reflect the removal of the `targets` parameter, which was considered redundant (too specialized for general-purpose command plugins) - names of targets are available to the plugin in the `arguments` array if the user passes it on the command line.
* Fix a few syntax errors in the manifests of the examples (not relevant to the content of the proposal).
Copy file name to clipboardExpand all lines: proposals/0332-swiftpm-command-plugins.md
+14-28Lines changed: 14 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -133,11 +133,6 @@ public protocol CommandPlugin: Plugin {
133
133
/// directories, etc.
134
134
context: PluginContext,
135
135
136
-
/// The targets to which the command should be applied. If the invoker of
137
-
/// the command has not specified particular targets, this will be a list
138
-
/// of all the targets in the package to which the command is applied.
139
-
targets: [Target],
140
-
141
136
/// Any literal arguments passed after the verb in the command invocation.
142
137
arguments: [String],
143
138
) asyncthrows
@@ -148,7 +143,7 @@ public protocol CommandPlugin: Plugin {
148
143
}
149
144
```
150
145
151
-
This defines a basic entry point for a command plugin, passing it information about the context in which the plugin is invoked (including information about the package graph), the set of targets on which the command should operate, and the arguments passed by the user after the verb in the `swift``package` invocation.
146
+
This defines a basic entry point for a command plugin, passing it information about the context in which the plugin is invoked (including information about the package graph) and the arguments passed by the user after the verb in the `swift``package` invocation.
152
147
153
148
The `context` parameter provides access to the package to which the user applies the plugin, including any dependencies, and it also provides access to a working directory that the plugin can use for any purposes, as well as a way to look up command line tools with a given name. This is the same as the support that is available to all plugins via SE-0325.
154
149
@@ -158,6 +153,8 @@ Many command plugins will invoke tools using subprocesses in order to do the act
158
153
159
154
Plugins can also use Foundation APIs for reading and writing files, encoding and decoding JSON, and other actions.
160
155
156
+
The arguments are a literal array of strings that the user specified when invoking the plugin. Plugins that operate on individual targets or products would typically support a `--target` or `--product` option that allows users to specify the names of targets or products to operate on in the package to which the plugin command is applied.
157
+
161
158
#### Accessing Package Manager Services
162
159
163
160
In addition to invoking invoking tool executables and using Foundation APIs, command plugins can use the `packageManager` property to obtain more specialized information and to invoke certain SwiftPM services. This is a proxy to SwiftPM or to the IDE that is hosting the plugin, and provides access to some of its functionality. The set of services provided in this API is expected to grow over time, and would ideally, over time, comprise most of the SwiftPM functionality available in its CLI.
@@ -414,19 +411,13 @@ In the SwiftPM CLI, command plugins provided by a package or its direct dependen
414
411
415
412
This will invoke the plugin and only return when it completes. Since no other options were provided, this will pass all regular targets in the package to the plugin ("special" targets such as those that define plugins will be excluded).
416
413
417
-
To pass a subset of the targets to the plugin, one or more `--target`options can be used in the invocation:
414
+
Any parameters passed after the name of the plugin command are passed verbatim to the entry point of the plugin. For example, if a plugin accepts a `--target`option, a subset of the targets to operate on can be passed on the command line that invokes the plugin:
418
415
419
416
```shell
420
-
❯ swift package --target Foo --target Bar do-something
417
+
❯ swift package do-something --target Foo --target Bar --someOtherFlag
421
418
```
422
419
423
-
This will pass the `Foo` and `Bar` targets to the plugin (assuming those are names of regular targets defined in the package — if they are not, an error is emitted).
424
-
425
-
The user can also provide additional arguments that are passed directly to the plugin. In the following example, the plugin will receive the arguments `aParam` and `-aFlag`, in addition to the targets named `Foo` and `Bar`.
426
-
427
-
```shell
428
-
❯ swift package --target Foo --target Bar do-something aParam -aFlag
429
-
```
420
+
It is the responsibility of the plugin to interpret any command line arguments passed to it.
430
421
431
422
Arguments are currently passed to the plugin exactly as they are written after the command’s verb. A future proposal could allow the plugin to define parameters (using SwiftArgumentParser) that SwiftPM could interpret and that would integrate better with SwiftPM’s own command line arguments.
432
423
@@ -503,7 +494,6 @@ import Foundation
503
494
structMyDocCPlugin: CommandPlugin {
504
495
funcperformCommand(
505
496
context: PluginContext,
506
-
targets: [Target],
507
497
arguments: [String]
508
498
) asyncthrows {
509
499
// We'll be creating commands that invoke `docc`, so start by locating it.
@@ -582,8 +572,6 @@ Users can then invoke this command plugin using the `swift` `package` invocation
582
572
❯ swift package generate-documentation
583
573
```
584
574
585
-
Since no `--target` options are provided, SwiftPM passes all the package’s regular targets to the plugin (in this simple example, just the `MyLibrary` target).
586
-
587
575
The plugin would usually print the path at which it generated the documentation.
588
576
589
577
## Example 2: Formatting Source Code
@@ -603,7 +591,7 @@ let package = Package(
603
591
],
604
592
targets: [
605
593
.plugin(
606
-
"MyFormatterPlugin",
594
+
name: "MyFormatterPlugin",
607
595
capability: .command(
608
596
intent: .sourceCodeFormatting(),
609
597
permissions: [
@@ -628,7 +616,6 @@ import Foundation
628
616
structMyFormatterPlugin: CommandPlugin {
629
617
funcperformCommand(
630
618
context: PluginContext,
631
-
targets: [Target],
632
619
arguments: [String]
633
620
) asyncthrows {
634
621
// We'll be invoking `swift-format`, so start by locating it.
0 commit comments