Skip to content
Open
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
50 changes: 25 additions & 25 deletions Plugins/SwiftProtobufPlugin/plugin.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import PackagePlugin
@preconcurrency import PackagePlugin

@main
struct SwiftProtobufPlugin {
Expand Down Expand Up @@ -107,40 +107,40 @@ struct SwiftProtobufPlugin {
/// - tool: The tool method from the context.
/// - Returns: The build commands configured based on the arguments.
func createBuildCommands(
pluginWorkDirectory: PackagePlugin.Path,
pluginWorkDirectory: URL,
sourceFiles: FileList,
tool: (String) throws -> PackagePlugin.PluginContext.Tool
) throws -> [Command] {
guard
let configurationFilePath = sourceFiles.first(
where: {
$0.path.lastComponent == Self.configurationFileName
$0.url.lastPathComponent == Self.configurationFileName
}
)?.path
)?.url
else {
throw PluginError.noConfigFound(Self.configurationFileName)
}
let data = try Data(contentsOf: URL(fileURLWithPath: "\(configurationFilePath)"))
let data = try Data(contentsOf: configurationFilePath)
let configuration = try JSONDecoder().decode(Configuration.self, from: data)
try validateConfiguration(configuration)

// We need to find the path of protoc and protoc-gen-swift
let protocPath: Path
let protocPath: URL
if let configuredProtocPath = configuration.protocPath {
// The user set the config path in the file. So let's take that
protocPath = Path(configuredProtocPath)
protocPath = URL(fileURLWithPath: configuredProtocPath)
} else if let environmentPath = ProcessInfo.processInfo.environment["PROTOC_PATH"] {
// The user set the env variable. So let's take that
protocPath = Path(environmentPath)
protocPath = URL(fileURLWithPath: environmentPath)
} else {
// The user didn't set anything so let's try see if SPM can find a binary for us
protocPath = try tool("protoc").path
protocPath = try tool("protoc").url
}
let protocGenSwiftPath = try tool("protoc-gen-swift").path
let protocGenSwiftPath = try tool("protoc-gen-swift").url

return configuration.invocations.map { invocation in
self.invokeProtoc(
directory: configurationFilePath.removingLastComponent(),
directory: configurationFilePath.deletingLastPathComponent(),
invocation: invocation,
protocPath: protocPath,
protocGenSwiftPath: protocGenSwiftPath,
Expand All @@ -159,22 +159,22 @@ struct SwiftProtobufPlugin {
/// - outputDirectory: The output directory for the generated files.
/// - Returns: The build command configured based on the arguments.
private func invokeProtoc(
directory: PackagePlugin.Path,
directory: URL,
invocation: Configuration.Invocation,
protocPath: Path,
protocGenSwiftPath: Path,
outputDirectory: Path
protocPath: URL,
protocGenSwiftPath: URL,
outputDirectory: URL
) -> Command {
// Construct the `protoc` arguments.
var protocArgs = [
"--plugin=protoc-gen-swift=\(protocGenSwiftPath)",
"--swift_out=\(outputDirectory)",
"--plugin=protoc-gen-swift=\(protocGenSwiftPath.path())",
"--swift_out=\(outputDirectory.path())",
]

// We need to add the target directory as a search path since we require the user to specify
// the proto files relative to it.
protocArgs.append("-I")
protocArgs.append("\(directory)")
protocArgs.append(directory.path())

// Add the visibility if it was set
if let visibility = invocation.visibility {
Expand All @@ -196,20 +196,20 @@ struct SwiftProtobufPlugin {
protocArgs.append("--swift_opt=UseAccessLevelOnImports=\(useAccessLevelOnImports)")
}

var inputFiles = [Path]()
var outputFiles = [Path]()
var inputFiles = [URL]()
var outputFiles = [URL]()

for var file in invocation.protoFiles {
// Append the file to the protoc args so that it is used for generating
protocArgs.append("\(file)")
inputFiles.append(directory.appending(file))
protocArgs.append(file)
inputFiles.append(directory.appending(path: file))

// The name of the output file is based on the name of the input file.
// We validated in the beginning that every file has the suffix of .proto
// This means we can just drop the last 5 elements and append the new suffix
file.removeLast(5)
file.append("pb.swift")
let protobufOutputPath = outputDirectory.appending(file)
let protobufOutputPath = outputDirectory.appending(path: file)

// Add the outputPath as an output file
outputFiles.append(protobufOutputPath)
Expand Down Expand Up @@ -248,7 +248,7 @@ extension SwiftProtobufPlugin: BuildToolPlugin {
throw PluginError.invalidTarget(target)
}
return try createBuildCommands(
pluginWorkDirectory: context.pluginWorkDirectory,
pluginWorkDirectory: context.pluginWorkDirectoryURL,
sourceFiles: swiftTarget.sourceFiles,
tool: context.tool
)
Expand All @@ -264,7 +264,7 @@ extension SwiftProtobufPlugin: XcodeBuildToolPlugin {
target: XcodeTarget
) throws -> [Command] {
try createBuildCommands(
pluginWorkDirectory: context.pluginWorkDirectory,
pluginWorkDirectory: context.pluginWorkDirectoryURL,
sourceFiles: target.inputFiles,
tool: context.tool
)
Expand Down
Loading