Skip to content

Commit fd7d22b

Browse files
committed
Adjust to SE-0303: Package plugins default to a "Plugins" directory next to "Sources" and "Tests"
1 parent e58b594 commit fd7d22b

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension ModuleError: CustomStringConvertible {
8383
let packages = packages.joined(separator: "', '")
8484
return "multiple targets named '\(name)' in: '\(packages)'"
8585
case .moduleNotFound(let target, let type):
86-
let folderName = type == .test ? "Tests" : "Sources"
86+
let folderName = (type == .test) ? "Tests" : (type == .plugin) ? "Plugins" : "Sources"
8787
return "Source files for target \(target) should be located under '\(folderName)/\(target)', or a custom sources path can be set with the 'path' property in Package.swift"
8888
case .artifactNotFound(let target):
8989
return "artifact not found for target '\(target)'"
@@ -535,9 +535,12 @@ public final class PackageBuilder {
535535

536536
/// Predefined test directories, in order of preference.
537537
public static let predefinedTestDirectories = ["Tests", "Sources", "Source", "src", "srcs"]
538+
539+
/// Predefined plugin directories, in order of preference.
540+
public static let predefinedPluginDirectories = ["Plugins"]
538541

539-
/// Finds the predefined directories for regular and test targets.
540-
private func findPredefinedTargetDirectory() -> (targetDir: String, testTargetDir: String) {
542+
/// Finds the predefined directories for regular targets, test targets, and plugin targets.
543+
private func findPredefinedTargetDirectory() -> (targetDir: String, testTargetDir: String, pluginTargetDir: String) {
541544
let targetDir = PackageBuilder.predefinedSourceDirectories.first(where: {
542545
fileSystem.isDirectory(packagePath.appending(component: $0))
543546
}) ?? PackageBuilder.predefinedSourceDirectories[0]
@@ -546,7 +549,11 @@ public final class PackageBuilder {
546549
fileSystem.isDirectory(packagePath.appending(component: $0))
547550
}) ?? PackageBuilder.predefinedTestDirectories[0]
548551

549-
return (targetDir, testTargetDir)
552+
let pluginTargetDir = PackageBuilder.predefinedPluginDirectories.first(where: {
553+
fileSystem.isDirectory(packagePath.appending(component: $0))
554+
}) ?? PackageBuilder.predefinedPluginDirectories[0]
555+
556+
return (targetDir, testTargetDir, pluginTargetDir)
550557
}
551558

552559
struct PredefinedTargetDirectory {
@@ -566,6 +573,7 @@ public final class PackageBuilder {
566573

567574
let predefinedTargetDirectory = PredefinedTargetDirectory(fs: fileSystem, path: packagePath.appending(component: predefinedDirs.targetDir))
568575
let predefinedTestTargetDirectory = PredefinedTargetDirectory(fs: fileSystem, path: packagePath.appending(component: predefinedDirs.testTargetDir))
576+
let predefinedPluginTargetDirectory = PredefinedTargetDirectory(fs: fileSystem, path: packagePath.appending(component: predefinedDirs.pluginTargetDir))
569577

570578
/// Returns the path of the given target.
571579
func findPath(for target: TargetDescription) throws -> AbsolutePath {
@@ -598,7 +606,15 @@ public final class PackageBuilder {
598606
}
599607

600608
// Check if target is present in the predefined directory.
601-
let predefinedDir = target.isTest ? predefinedTestTargetDirectory : predefinedTargetDirectory
609+
let predefinedDir: PredefinedTargetDirectory
610+
switch target.type {
611+
case .test:
612+
predefinedDir = predefinedTestTargetDirectory
613+
case .plugin:
614+
predefinedDir = predefinedPluginTargetDirectory
615+
default:
616+
predefinedDir = predefinedTargetDirectory
617+
}
602618
let path = predefinedDir.path.appending(component: target.name)
603619

604620
// Return the path if the predefined directory contains it.

Tests/PackageLoadingTests/PackageBuilderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ class PackageBuilderTests: XCTestCase {
21642164

21652165
func testExtensionTargetsAreGuardededByFeatureFlag() throws {
21662166
let fs = InMemoryFileSystem(emptyFiles:
2167-
"/Foo/Sources/MyPlugin/plugin.swift",
2167+
"/Foo/Plugins/MyPlugin/plugin.swift",
21682168
"/Foo/Sources/MyLibrary/library.swift"
21692169
)
21702170

Tests/SPMBuildCoreTests/PluginInvocationTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ class PluginInvocationTests: XCTestCase {
2323
func testBasics() throws {
2424
// Construct a canned file system and package graph with a single package and a library that uses a plugin that uses a tool.
2525
let fileSystem = InMemoryFileSystem(emptyFiles:
26+
"/Foo/Plugins/FooPlugin/source.swift",
27+
"/Foo/Sources/FooTool/source.swift",
2628
"/Foo/Sources/Foo/source.swift",
27-
"/Foo/Sources/Foo/SomeFile.abc",
28-
"/Foo/Sources/FooExt/source.swift",
29-
"/Foo/Sources/FooTool/source.swift"
29+
"/Foo/Sources/Foo/SomeFile.abc"
3030
)
3131
let diagnostics = DiagnosticsEngine()
3232
let graph = try loadPackageGraph(fs: fileSystem, diagnostics: diagnostics,
@@ -46,11 +46,11 @@ class PluginInvocationTests: XCTestCase {
4646
targets: [
4747
TargetDescription(
4848
name: "Foo",
49-
dependencies: ["FooExt"],
50-
type: .regular
49+
type: .regular,
50+
pluginUsages: [.plugin(name: "FooPlugin", package: nil)]
5151
),
5252
TargetDescription(
53-
name: "FooExt",
53+
name: "FooPlugin",
5454
dependencies: ["FooTool"],
5555
type: .plugin,
5656
pluginCapability: .buildTool
@@ -70,11 +70,11 @@ class PluginInvocationTests: XCTestCase {
7070
XCTAssertNoDiagnostics(diagnostics)
7171
PackageGraphTester(graph) { graph in
7272
graph.check(packages: "Foo")
73-
graph.check(targets: "Foo", "FooExt", "FooTool")
73+
graph.check(targets: "Foo", "FooPlugin", "FooTool")
7474
graph.checkTarget("Foo") { target in
75-
target.check(dependencies: "FooExt")
75+
target.check(dependencies: "FooPlugin")
7676
}
77-
graph.checkTarget("FooExt") { target in
77+
graph.checkTarget("FooPlugin") { target in
7878
target.check(type: .plugin)
7979
target.check(dependencies: "FooTool")
8080
}
@@ -97,7 +97,7 @@ class PluginInvocationTests: XCTestCase {
9797
fileSystem: FileSystem
9898
) throws -> (outputJSON: Data, stdoutText: Data) {
9999
// Check that we were given the right sources.
100-
XCTAssertEqual(sources.root, AbsolutePath("/Foo/Sources/FooExt"))
100+
XCTAssertEqual(sources.root, AbsolutePath("/Foo/Plugins/FooPlugin"))
101101
XCTAssertEqual(sources.relativePaths, [RelativePath("source.swift")])
102102

103103
// Deserialize and check the input.

0 commit comments

Comments
 (0)