Skip to content

Commit 1de6456

Browse files
authored
Rename the package name property in the plugin APIs to displayName, as the evolution proposal says. Also pass in the package identity, and use that as the Package.id in the plugin-visible API. (#3854)
1 parent 44aff27 commit 1de6456

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

Sources/PackagePlugin/PackageModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct Package {
1515
public typealias ID = String
1616

1717
/// The name of the package (for display purposes only).
18-
public let name: String
18+
public let displayName: String
1919

2020
/// The absolute path of the package directory in the local file system.
2121
public let directory: Path

Sources/PackagePlugin/PluginInput.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ fileprivate struct PluginInputDeserializer {
264264
let products = try wirePackage.productIds.map { try self.product(for: $0) }
265265
let targets = try wirePackage.targetIds.map { try self.target(for: $0) }
266266
let package = Package(
267-
id: String(id),
268-
name: wirePackage.name,
267+
id: wirePackage.identity,
268+
displayName: wirePackage.displayName,
269269
directory: directory,
270270
origin: .root,
271271
toolsVersion: toolsVersion,
@@ -312,7 +312,8 @@ fileprivate struct WireInput: Decodable {
312312
/// their ID numbers.
313313
struct Package: Decodable {
314314
typealias Id = Int
315-
let name: String
315+
let identity: String
316+
let displayName: String
316317
let directoryId: Path.Id
317318
let origin: Origin
318319
let toolsVersion: ToolsVersion

Sources/PackagePlugin/Protocols.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@ extension BuildToolPlugin {
7272
builtProductsDirectory: context.builtProductsDirectory,
7373
toolNamesToPaths: context.toolNamesToPaths))
7474
}
75+
76+
/// Default implementation that does nothing.
77+
public func createBuildCommands(
78+
context: TargetBuildContext
79+
) throws -> [Command] {
80+
return []
81+
}
7582
}
83+

Sources/SPMBuildCore/PluginInvocation.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ struct PluginScriptRunnerInput: Codable {
395395
/// their ID numbers.
396396
struct Package: Codable {
397397
typealias Id = Int
398-
let name: String
398+
let identity: String
399+
let displayName: String
399400
let directoryId: Path.Id
400401
let origin: Origin
401402
let toolsVersion: ToolsVersion
@@ -790,8 +791,8 @@ struct PluginScriptRunnerInputSerializer {
790791
// Assign the next wire ID to the package and append a serialized Package record.
791792
let id = packages.count
792793
packages.append(.init(
793-
// FIXME: can we use package identity instead?
794-
name: package.manifest.displayName,
794+
identity: package.identity.description,
795+
displayName: package.manifest.displayName,
795796
directoryId: try serialize(path: package.path),
796797
origin: try origin(for: package),
797798
toolsVersion: .init(

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,67 @@ final class PackageToolTests: CommandsTestCase {
10391039
#endif
10401040
}
10411041

1042+
func testBuildToolPlugin() throws {
1043+
1044+
try testWithTemporaryDirectory { tmpPath in
1045+
// Create a sample package with a library target and a plugin.
1046+
let packageDir = tmpPath.appending(components: "MyPackage")
1047+
try localFileSystem.writeFileContents(packageDir.appending(component: "Package.swift")) {
1048+
$0 <<< """
1049+
// swift-tools-version: 999.0
1050+
import PackageDescription
1051+
let package = Package(
1052+
name: "MyPackage",
1053+
targets: [
1054+
.target(
1055+
name: "MyLibrary",
1056+
plugins: [
1057+
"MyPlugin",
1058+
]
1059+
),
1060+
.plugin(
1061+
name: "MyPlugin",
1062+
capability: .buildTool()
1063+
),
1064+
]
1065+
)
1066+
"""
1067+
}
1068+
try localFileSystem.writeFileContents(packageDir.appending(components: "Sources", "MyLibrary", "library.swift")) {
1069+
$0 <<< """
1070+
public func Foo() { }
1071+
"""
1072+
}
1073+
try localFileSystem.writeFileContents(packageDir.appending(components: "Plugins", "MyPlugin", "plugin.swift")) {
1074+
$0 <<< """
1075+
import PackagePlugin
1076+
1077+
@main
1078+
struct MyBuildToolPlugin: BuildToolPlugin {
1079+
func createBuildCommands(
1080+
context: PluginContext,
1081+
target: Target
1082+
) throws -> [Command] {
1083+
guard context.package.displayName == "MyPackage" else {
1084+
throw Error.inconsistentDisplayName(context.package.displayName)
1085+
}
1086+
return []
1087+
}
1088+
1089+
enum Error : Swift.Error {
1090+
case inconsistentDisplayName(String)
1091+
}
1092+
}
1093+
"""
1094+
}
1095+
1096+
// Invoke it, and check the results.
1097+
let result = try SwiftPMProduct.SwiftBuild.executeProcess([], packagePath: packageDir)
1098+
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1099+
XCTAssert(try result.utf8Output().contains("Build complete!"))
1100+
}
1101+
}
1102+
10421103
func testArchiveSource() throws {
10431104
fixture(name: "DependencyResolution/External/Simple") { prefix in
10441105
let packageRoot = prefix.appending(component: "Bar")

0 commit comments

Comments
 (0)