Skip to content

fix: Attempt to work around PackagePlugin.Path deprecation #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions Plugins/MetaProtocolCodable/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,48 @@ struct MetaProtocolCodable: BuildToolPlugin {
) throws -> [Command] where Context: MetaProtocolCodablePluginContext {
// Get config
let tool = try context.tool(named: "ProtocolGen")
#if swift(<6)
let toolUrl = URL(string: tool.path.string)!
#else
let toolUrl = tool.url
#endif
let config = try fetchConfig(for: target)
let (allTargets, imports) = config.scanInput(for: target, in: context)

// Setup folder
let genFolder = context.pluginWorkDirectory.appending(["ProtocolGen"])
let genFolder = context.pluginWorkDirectoryURL.appending(path: "ProtocolGen")
try FileManager.default.createDirectory(
atPath: genFolder.string, withIntermediateDirectories: true
at: genFolder, withIntermediateDirectories: true
)

// Create source scan commands
var intermFiles: [Path] = []
var intermFiles: [URL] = []
var buildCommands = allTargets.flatMap { target in
return target.sourceFiles(withSuffix: "swift").map { file in
let moduleName = target.moduleName
let fileName = file.path.stem
#if swift(<6)
let fileUrl = URL(string: file.path.string)!
#else
let fileUrl = file.url
#endif
let fileName = fileUrl.deletingPathExtension().lastPathComponent
let genFileName = "\(moduleName)-\(fileName)-gen.json"
let genFile = genFolder.appending([genFileName])
let genFile = genFolder.appending(path: genFileName)
intermFiles.append(genFile)


return Command.buildCommand(
displayName: """
Parse source file "\(fileName)" in module "\(moduleName)"
""",
executable: tool.path,
executable: toolUrl,
arguments: [
"parse",
file.path.string,
fileUrl.absoluteString,
"--output",
genFile.string,
genFile.absoluteString,
],
inputFiles: [file.path],
inputFiles: [fileUrl],
outputFiles: [genFile]
)
}
Expand All @@ -84,20 +96,20 @@ struct MetaProtocolCodable: BuildToolPlugin {
// Create syntax generation command
let moduleName = target.moduleName
let genFileName = "\(moduleName)+ProtocolHelperCoders.swift"
let genPath = genFolder.appending(genFileName)
var genArgs = ["generate", "--output", genPath.string]
let genPath = genFolder.appending(path: genFileName)
var genArgs = ["generate", "--output", genPath.absoluteString]
for `import` in imports {
genArgs.append(contentsOf: ["--module", `import`])
}
for file in intermFiles {
genArgs.append(file.string)
genArgs.append(file.absoluteString)
}
buildCommands.append(
.buildCommand(
displayName: """
Generate protocol decoding/encoding syntax for "\(moduleName)"
""",
executable: tool.path,
executable: toolUrl,
arguments: genArgs,
inputFiles: intermFiles,
outputFiles: [genPath]
Expand Down
3 changes: 3 additions & 0 deletions Plugins/MetaProtocolCodable/PluginContext.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PackagePlugin
import Foundation

/// Provides information about the package for which the plugin is invoked,
/// as well as contextual information based on the plugin's stated intent
Expand All @@ -24,6 +25,8 @@ protocol MetaProtocolCodablePluginContext {
/// directories for cache files and other file system content that either
/// it or the command will need.
var pluginWorkDirectory: Path { get }
var pluginWorkDirectoryURL: URL { get }

/// The targets which are local to current context.
///
/// These targets are included in the same package/project as this context.
Expand Down
32 changes: 27 additions & 5 deletions Plugins/MetaProtocolCodable/SourceTarget/SwiftPackageTarget.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import PackagePlugin


/// Represents an SwiftPM target.
///
/// Uses `SourceModuleTarget` to provide conformances.
Expand All @@ -11,6 +12,27 @@ struct SwiftPackageTarget {
let module: any SourceModuleTarget
}

/// This is a workaround because PackageDescription.Target.directoryURL will not be available until version 6.1
/// See: https://github.com/swiftlang/swift-package-manager/blob/735ddd97fa651729921315c8e46bd790429362cb/Sources/PackagePlugin/PackageModel.swift#L184-L186
extension PackagePlugin.Target {
var directoryURL: URL {
#if swift(<6)
// No `directoryURL` but `Path` is not deprecated yet
return URL(string: directory.string)!
#else
// Concrete types have `directoryURL`
switch self {
case let target as ClangSourceModuleTarget:
return target.directoryURL
case let target as SwiftSourceModuleTarget:
return target.directoryURL
default:
fatalError("Unsupported target type")
}
#endif
}
}

extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
/// The name of the module produced
/// by the target.
Expand Down Expand Up @@ -64,17 +86,17 @@ extension SwiftPackageTarget: MetaProtocolCodableSourceTarget {
/// - Returns: The config file path.
func configPath(named name: String) throws -> String? {
let fileManager = FileManager.default
let directory = module.directory.string
let contents = try fileManager.contentsOfDirectory(atPath: directory)
let directory = module.directoryURL
// let directory = URL(string: module.directory.string)!
let contents = try fileManager.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil)
let file = contents.first { file in
let path = Path(file)
return name.lowercased()
== path.stem
== file.deletingPathExtension().lastPathComponent
.components(separatedBy: .alphanumerics.inverted)
.joined(separator: "")
.lowercased()
}
guard let file else { return nil }
return module.directory.appending([file]).string
return directory.appending(path: file.absoluteString).absoluteString
}
}
2 changes: 1 addition & 1 deletion Sources/ProtocolGen/Generate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ extension ProtocolGen {
)
).description
let sourceData = sourceText.data(using: .utf8)
fileManager.createFile(atPath: output, contents: sourceData)
_ = fileManager.createFile(atPath: output, contents: sourceData)
}
}
}
Loading