|
1 | 1 | import Foundation
|
2 | 2 | import PackagePlugin
|
3 | 3 |
|
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 |
| - |
12 | 4 | /// Represents an SwiftPM target.
|
13 | 5 | ///
|
14 | 6 | /// Uses `SourceModuleTarget` to provide conformances.
|
15 | 7 | struct SwiftPackageTarget {
|
16 | 8 | /// The actual module for this target.
|
17 | 9 | ///
|
18 | 10 | /// The conformances provided uses this module.
|
19 |
| - let module: any CompatSourceModuleTarget |
| 11 | + let module: any SourceModuleTarget |
20 | 12 |
|
21 | 13 | }
|
22 | 14 |
|
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 |
36 | 34 | }
|
37 | 35 | }
|
38 |
| -} |
39 |
| - |
| 36 | +#endif |
| 37 | + |
40 | 38 |
|
41 | 39 | extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
|
42 | 40 | /// The name of the module produced
|
|
0 commit comments