Skip to content

Commit e9555cb

Browse files
committed
preserve backwards compatibility
1 parent a568168 commit e9555cb

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

Plugins/MetaProtocolCodable/Plugin.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct MetaProtocolCodable: BuildToolPlugin {
5050
let (allTargets, imports) = config.scanInput(for: target, in: context)
5151

5252
// Setup folder
53-
let genFolder = context.pluginWorkDirectoryURL
53+
let genFolder = context.pluginWorkDirectoryURL.appending(path: "ProtocolGen")
5454
try FileManager.default.createDirectory(
5555
at: genFolder, withIntermediateDirectories: true
5656
)
@@ -60,7 +60,11 @@ struct MetaProtocolCodable: BuildToolPlugin {
6060
var buildCommands = allTargets.flatMap { target in
6161
return target.sourceFiles(withSuffix: "swift").map { file in
6262
let moduleName = target.moduleName
63+
#if swift(<6)
64+
let fileName = URL(string: file.path.string)!
65+
#else
6366
let fileName = file.url.lastPathComponent
67+
#endif
6468
let genFileName = "\(moduleName)-\(fileName)-gen.json"
6569
let genFile = genFolder.appending(path: genFileName)
6670
intermFiles.append(genFile)

Plugins/MetaProtocolCodable/SourceTarget/SwiftPackageTarget.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
import Foundation
22
import PackagePlugin
33

4-
/// This is a workaround because PackageDescription.Target.directoryURL will not be available until version 6.1
5-
/// See: https://github.com/swiftlang/swift-package-manager/blob/735ddd97fa651729921315c8e46bd790429362cb/Sources/PackagePlugin/PackageModel.swift#L184-L186///
6-
/// The workaround defines a custom protocol that adds the missing property, and then introduces
7-
/// a new initializer that accepts the actual target protocol and attempts to downcast.
8-
protocol CompatSourceModuleTarget: SourceModuleTarget {
9-
var directoryURL: URL { get }
10-
}
11-
124
/// Represents an SwiftPM target.
135
///
146
/// Uses `SourceModuleTarget` to provide conformances.
157
struct SwiftPackageTarget {
168
/// The actual module for this target.
179
///
1810
/// The conformances provided uses this module.
19-
let module: any CompatSourceModuleTarget
11+
let module: any SourceModuleTarget
2012

2113
}
2214

23-
/// Workaround for 6.1 compatibility
24-
extension ClangSourceModuleTarget: CompatSourceModuleTarget {}
25-
extension SwiftSourceModuleTarget: CompatSourceModuleTarget {}
26-
27-
extension SwiftPackageTarget {
28-
init<M>(module: M) where M: SourceModuleTarget {
29-
switch module {
30-
case let module as ClangSourceModuleTarget:
31-
self.module = module
32-
case let module as SwiftSourceModuleTarget:
33-
self.module = module
34-
default:
35-
fatalError("Unsupported module type")
15+
/// This is a workaround because PackageDescription.Target.directoryURL will not be available until version 6.1
16+
/// See: https://github.com/swiftlang/swift-package-manager/blob/735ddd97fa651729921315c8e46bd790429362cb/Sources/PackagePlugin/PackageModel.swift#L184-L186///
17+
#if swift(<6.1)
18+
extension Target {
19+
var directoryURL: URL {
20+
#if swift(<6)
21+
// No `directoryURL` but `Path` is not deprecated yet
22+
return URL(string: directory.string)!
23+
#else
24+
// Concrete types have `directoryURL`
25+
switch self {
26+
case let target as ClangSourceModuleTarget:
27+
return target.directoryURL
28+
case let target as SwiftSourceModuleTarget:
29+
return target.directoryURL
30+
default:
31+
fatalError("Unsupported target type")
32+
}
33+
#endif
3634
}
3735
}
38-
}
39-
36+
#endif
37+
4038

4139
extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
4240
/// The name of the module produced

0 commit comments

Comments
 (0)