Skip to content

Commit 8c8a3c4

Browse files
authored
Add a convenience accessor for SourceModuleTarget nature of a target (#6110)
This essentially just does a cast to the protocol, but that makes plugin source code slightly nicer to read since this is a very common pattern. resolves #5504
1 parent 466dfe3 commit 8c8a3c4

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

Documentation/Plugins.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ import PackagePlugin
149149
struct MyPlugin: BuildToolPlugin {
150150

151151
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
152-
guard let target = target as? SourceModuleTarget else { return [] }
152+
guard let target = target.sourceModule else { return [] }
153153
let inputFiles = target.sourceFiles.filter({ $0.path.extension == "dat" })
154154
return try inputFiles.map {
155155
let inputFile = $0
@@ -300,7 +300,7 @@ struct MyCommandPlugin: CommandPlugin {
300300
for target in targets {
301301
// Skip any type of target that doesn't have source files.
302302
// Note: We could choose to instead emit a warning or error here.
303-
guard let target = target as? SourceModuleTarget else { continue }
303+
guard let target = target.sourceModule else { continue }
304304

305305
// Invoke `sometool` on the target directory, passing a configuration
306306
// file from the package directory.

Sources/PackagePlugin/PackageModel.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public struct Package {
4343
public let targets: [Target]
4444
}
4545

46+
public extension Package {
47+
var sourceModules: [SourceModuleTarget] {
48+
return targets.compactMap { $0.sourceModule }
49+
}
50+
}
51+
4652
/// Represents the origin of a package as it appears in the graph.
4753
public enum PackageOrigin {
4854
/// A root package (unversioned).
@@ -107,6 +113,12 @@ public protocol Product {
107113
var targets: [Target] { get }
108114
}
109115

116+
public extension Product {
117+
var sourceModules: [SourceModuleTarget] {
118+
return targets.compactMap { $0.sourceModule }
119+
}
120+
}
121+
110122
/// Represents an executable product defined in a package.
111123
public struct ExecutableProduct: Product {
112124
/// Unique identifier for the product.
@@ -387,6 +399,13 @@ public struct SystemLibraryTarget: Target {
387399
public let linkerFlags: [String]
388400
}
389401

402+
public extension Target {
403+
/// Convenience accessor which casts the receiver to`SourceModuleTarget` if possible.
404+
var sourceModule: SourceModuleTarget? {
405+
return self as? SourceModuleTarget
406+
}
407+
}
408+
390409
/// Provides information about a list of files. The order is not defined
391410
/// but is guaranteed to be stable. This allows the implementation to be
392411
/// more efficient than a static file list.

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ final class PackageToolTests: CommandsTestCase {
14651465
}
14661466
14671467
// Create and return a build command that uses all the `.foo` files in the target as inputs, so they get counted as having been handled.
1468-
let fooFiles = (target as? SourceModuleTarget)?.sourceFiles.compactMap{ $0.path.extension == "foo" ? $0.path : nil } ?? []
1468+
let fooFiles = target.sourceModule?.sourceFiles.compactMap{ $0.path.extension == "foo" ? $0.path : nil } ?? []
14691469
return [ .buildCommand(displayName: "A command", executable: Path("/bin/echo"), arguments: fooFiles, inputFiles: fooFiles) ]
14701470
}
14711471
@@ -1751,7 +1751,7 @@ final class PackageToolTests: CommandsTestCase {
17511751
let targets = try context.package.targets(named: targetNames)
17521752
17531753
// Print out the source files so that we can check them.
1754-
if let sourceFiles = (targets.first{ $0.name == "MyLibrary" } as? SourceModuleTarget)?.sourceFiles {
1754+
if let sourceFiles = targets.first(where: { $0.name == "MyLibrary" })?.sourceModule?.sourceFiles {
17551755
for file in sourceFiles {
17561756
print(" \\(file.path): \\(file.type)")
17571757
}
@@ -2467,9 +2467,14 @@ final class PackageToolTests: CommandsTestCase {
24672467
let swiftSources = swiftTargets.flatMap{ $0.sourceFiles(withSuffix: ".swift") }
24682468
print("swiftSources: \\(swiftSources.map{ $0.path.lastComponent })")
24692469
2470-
if let target = target as? SourceModuleTarget {
2470+
if let target = target.sourceModule {
24712471
print("Module kind of '\\(target.name)': \\(target.kind)")
24722472
}
2473+
2474+
var sourceModules = context.package.sourceModules
2475+
print("sourceModules in package: \\(sourceModules.map { $0.name })")
2476+
sourceModules = context.package.products.first?.sourceModules ?? []
2477+
print("sourceModules in first product: \\(sourceModules.map { $0.name })")
24732478
}
24742479
}
24752480
extension String: Error {}

0 commit comments

Comments
 (0)