Skip to content

Commit a568168

Browse files
committed
fix: Attempt to work around PackageDescription.Path deprecation
1 parent 412ce20 commit a568168

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

Plugins/MetaProtocolCodable/Plugin.swift

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

5252
// Setup folder
53-
let genFolder = context.pluginWorkDirectory.appending(["ProtocolGen"])
53+
let genFolder = context.pluginWorkDirectoryURL
5454
try FileManager.default.createDirectory(
55-
atPath: genFolder.string, withIntermediateDirectories: true
55+
at: genFolder, withIntermediateDirectories: true
5656
)
5757

5858
// Create source scan commands
59-
var intermFiles: [Path] = []
59+
var intermFiles: [URL] = []
6060
var buildCommands = allTargets.flatMap { target in
6161
return target.sourceFiles(withSuffix: "swift").map { file in
6262
let moduleName = target.moduleName
63-
let fileName = file.path.stem
63+
let fileName = file.url.lastPathComponent
6464
let genFileName = "\(moduleName)-\(fileName)-gen.json"
65-
let genFile = genFolder.appending([genFileName])
65+
let genFile = genFolder.appending(path: genFileName)
6666
intermFiles.append(genFile)
6767
return Command.buildCommand(
6868
displayName: """
6969
Parse source file "\(fileName)" in module "\(moduleName)"
7070
""",
71-
executable: tool.path,
71+
executable: tool.url,
7272
arguments: [
7373
"parse",
74-
file.path.string,
74+
file.url.absoluteString,
7575
"--output",
76-
genFile.string,
76+
genFile.absoluteString,
7777
],
78-
inputFiles: [file.path],
78+
inputFiles: [file.url],
7979
outputFiles: [genFile]
8080
)
8181
}
@@ -84,20 +84,20 @@ struct MetaProtocolCodable: BuildToolPlugin {
8484
// Create syntax generation command
8585
let moduleName = target.moduleName
8686
let genFileName = "\(moduleName)+ProtocolHelperCoders.swift"
87-
let genPath = genFolder.appending(genFileName)
88-
var genArgs = ["generate", "--output", genPath.string]
87+
let genPath = genFolder.appending(path:genFileName)
88+
var genArgs = ["generate", "--output", genPath.absoluteString]
8989
for `import` in imports {
9090
genArgs.append(contentsOf: ["--module", `import`])
9191
}
9292
for file in intermFiles {
93-
genArgs.append(file.string)
93+
genArgs.append(file.absoluteString)
9494
}
9595
buildCommands.append(
9696
.buildCommand(
9797
displayName: """
9898
Generate protocol decoding/encoding syntax for "\(moduleName)"
9999
""",
100-
executable: tool.path,
100+
executable: tool.url,
101101
arguments: genArgs,
102102
inputFiles: intermFiles,
103103
outputFiles: [genPath]

Plugins/MetaProtocolCodable/PluginContext.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import PackagePlugin
2+
import Foundation
23

34
/// Provides information about the package for which the plugin is invoked,
45
/// as well as contextual information based on the plugin's stated intent
@@ -24,6 +25,8 @@ protocol MetaProtocolCodablePluginContext {
2425
/// directories for cache files and other file system content that either
2526
/// it or the command will need.
2627
var pluginWorkDirectory: Path { get }
28+
var pluginWorkDirectoryURL: URL { get }
29+
2730
/// The targets which are local to current context.
2831
///
2932
/// These targets are included in the same package/project as this context.

Plugins/MetaProtocolCodable/SourceTarget/SwiftPackageTarget.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
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+
412
/// Represents an SwiftPM target.
513
///
614
/// Uses `SourceModuleTarget` to provide conformances.
715
struct SwiftPackageTarget {
816
/// The actual module for this target.
917
///
1018
/// The conformances provided uses this module.
11-
let module: any SourceModuleTarget
19+
let module: any CompatSourceModuleTarget
20+
21+
}
22+
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")
36+
}
37+
}
1238
}
39+
1340

1441
extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
1542
/// The name of the module produced
@@ -29,7 +56,7 @@ extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
2956
default:
3057
nil
3158
}
32-
}.map { Self.init(module: $0) }
59+
}.map { (target: any SourceModuleTarget) in Self.init(module: target) }
3360
}
3461

3562
/// All the targets on which current target depends on.
@@ -64,17 +91,16 @@ extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
6491
/// - Returns: The config file path.
6592
func configPath(named name: String) throws -> String? {
6693
let fileManager = FileManager.default
67-
let directory = module.directory.string
68-
let contents = try fileManager.contentsOfDirectory(atPath: directory)
94+
let directory = module.directoryURL
95+
let contents = try fileManager.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
6996
let file = contents.first { file in
70-
let path = Path(file)
7197
return name.lowercased()
72-
== path.stem
98+
== file.lastPathComponent
7399
.components(separatedBy: .alphanumerics.inverted)
74100
.joined(separator: "")
75101
.lowercased()
76102
}
77103
guard let file else { return nil }
78-
return module.directory.appending([file]).string
104+
return directory.appending(path: file.absoluteString).absoluteString
79105
}
80106
}

0 commit comments

Comments
 (0)