From 454684d53f856ceb8a81fcd6ca0c8a00b8fbbda1 Mon Sep 17 00:00:00 2001 From: Rick Newton-Rogers Date: Tue, 24 Dec 2024 15:42:36 +0000 Subject: [PATCH] tweaking --- .github/workflows/matrices/plugin-tests.json | 2 +- .../Package.swift | 76 +++++++++++-------- .../Sources/Protos/HelloWorld.proto | 2 +- .../Sources/grpc-swift-config.json | 1 - .../test_02_peer_config_file/Package.swift | 76 +++++++++++-------- .../Sources/Protos/HelloWorld.proto | 2 +- .../Sources/Protos/grpc-swift-config.json | 1 - .../Package.swift | 76 +++++++++++-------- .../Sources/Protos/Messages.proto | 2 +- .../Sources/Protos/Service.proto | 2 +- .../Sources/Protos/grpc-swift-config.json | 1 - Package.swift | 39 ++++++++++ .../ConfigurationArguments.swift | 11 ++- Plugins/GRPCGeneratorCommand/Plugin.swift | 16 ++-- .../ConfigurationFile.swift | 7 +- Plugins/GRPCGeneratorPlugin/Plugin.swift | 52 +++++++++---- .../PluginsShared/CommonConfiguration.swift | 4 +- Plugins/PluginsShared/PluginUtils.swift | 23 ++++-- .../protoc-gen-grpc-swift/GenerateGRPC.swift | 4 - dev/plugin-tests.sh | 4 +- 20 files changed, 257 insertions(+), 144 deletions(-) diff --git a/.github/workflows/matrices/plugin-tests.json b/.github/workflows/matrices/plugin-tests.json index 3558d5a..d27466e 100644 --- a/.github/workflows/matrices/plugin-tests.json +++ b/.github/workflows/matrices/plugin-tests.json @@ -1 +1 @@ -{"config":[{"name":"Plugin tests (6.0)","swift_version":"6.0","runner":"ubuntu-latest","image":"swift:6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl","command":"curl -s https://raw.githubusercontent.com/rnro/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-6.0)","swift_version":"nightly-6.0","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl","command":"curl -s https://raw.githubusercontent.com/rnro/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-main)","swift_version":"nightly-main","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-main-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl","command":"curl -s https://raw.githubusercontent.com/rnro/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""}]} +{"config":[{"name":"Plugin tests (6.0)","swift_version":"6.0","runner":"ubuntu-latest","image":"swift:6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-6.0)","swift_version":"nightly-6.0","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-6.0-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""},{"name":"Plugin tests (nightly-main)","swift_version":"nightly-main","runner":"ubuntu-latest","image":"swiftlang/swift:nightly-main-jammy","platform":"Linux","setup_command":"apt-get update -y -q && apt-get install -y -q curl protobuf-compiler","command":"curl -s https://raw.githubusercontent.com/grpc/grpc-swift-protobuf/package_plugins/dev/plugin-tests.sh | bash","command_arguments":""}]} diff --git a/IntegrationTests/PluginTests/test_01_top_level_config_file/Package.swift b/IntegrationTests/PluginTests/test_01_top_level_config_file/Package.swift index 7ac9db7..70d8ce2 100644 --- a/IntegrationTests/PluginTests/test_01_top_level_config_file/Package.swift +++ b/IntegrationTests/PluginTests/test_01_top_level_config_file/Package.swift @@ -1,37 +1,51 @@ // swift-tools-version: 6.0 -// The swift-tools-version declares the minimum version of Swift required to build this package. +/* + * Copyright 2024, gRPC Authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import PackageDescription let package = Package( - name: "grpc-adopter", - platforms: [ - .macOS(.v15), - .iOS(.v18), - .tvOS(.v18), - .watchOS(.v11), - .visionOS(.v2), - ], - dependencies: [ - .package( - path: "../../../../grpc-swift-protobuf" - ), - .package( - url: "https://github.com/grpc/grpc-swift.git", - from: "1.28.2" - ) - ], - targets: [ - .executableTarget( - name: "grpc-adopter", - dependencies: [ - .product(name: "GRPCCore", package: "grpc-swift"), - .product(name: "GRPCInProcessTransport", package: "grpc-swift"), - .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), - ], - plugins: [ - .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") - ] - ) - ] + name: "grpc-adopter", + platforms: [ + .macOS(.v15), + .iOS(.v18), + .tvOS(.v18), + .watchOS(.v11), + .visionOS(.v2), + ], + dependencies: [ + .package( + path: "../../../../grpc-swift-protobuf" + ), + .package( + url: "https://github.com/grpc/grpc-swift.git", + exact: "2.0.0-beta.2" + ), + ], + targets: [ + .executableTarget( + name: "grpc-adopter", + dependencies: [ + .product(name: "GRPCCore", package: "grpc-swift"), + .product(name: "GRPCInProcessTransport", package: "grpc-swift"), + .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), + ], + plugins: [ + .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") + ] + ) + ] ) diff --git a/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/Protos/HelloWorld.proto b/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/Protos/HelloWorld.proto index 2be480c..32aab3a 100644 --- a/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/Protos/HelloWorld.proto +++ b/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/Protos/HelloWorld.proto @@ -1,4 +1,4 @@ -// Copyright 2015 gRPC authors. +// Copyright 2015, gRPC Authors All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/grpc-swift-config.json b/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/grpc-swift-config.json index dc543c2..ddfa409 100644 --- a/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/grpc-swift-config.json +++ b/IntegrationTests/PluginTests/test_01_top_level_config_file/Sources/grpc-swift-config.json @@ -1,4 +1,3 @@ { - "protocPath": "/opt/homebrew/bin/protoc", "visibility": "internal" } diff --git a/IntegrationTests/PluginTests/test_02_peer_config_file/Package.swift b/IntegrationTests/PluginTests/test_02_peer_config_file/Package.swift index 7ac9db7..70d8ce2 100644 --- a/IntegrationTests/PluginTests/test_02_peer_config_file/Package.swift +++ b/IntegrationTests/PluginTests/test_02_peer_config_file/Package.swift @@ -1,37 +1,51 @@ // swift-tools-version: 6.0 -// The swift-tools-version declares the minimum version of Swift required to build this package. +/* + * Copyright 2024, gRPC Authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import PackageDescription let package = Package( - name: "grpc-adopter", - platforms: [ - .macOS(.v15), - .iOS(.v18), - .tvOS(.v18), - .watchOS(.v11), - .visionOS(.v2), - ], - dependencies: [ - .package( - path: "../../../../grpc-swift-protobuf" - ), - .package( - url: "https://github.com/grpc/grpc-swift.git", - from: "1.28.2" - ) - ], - targets: [ - .executableTarget( - name: "grpc-adopter", - dependencies: [ - .product(name: "GRPCCore", package: "grpc-swift"), - .product(name: "GRPCInProcessTransport", package: "grpc-swift"), - .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), - ], - plugins: [ - .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") - ] - ) - ] + name: "grpc-adopter", + platforms: [ + .macOS(.v15), + .iOS(.v18), + .tvOS(.v18), + .watchOS(.v11), + .visionOS(.v2), + ], + dependencies: [ + .package( + path: "../../../../grpc-swift-protobuf" + ), + .package( + url: "https://github.com/grpc/grpc-swift.git", + exact: "2.0.0-beta.2" + ), + ], + targets: [ + .executableTarget( + name: "grpc-adopter", + dependencies: [ + .product(name: "GRPCCore", package: "grpc-swift"), + .product(name: "GRPCInProcessTransport", package: "grpc-swift"), + .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), + ], + plugins: [ + .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") + ] + ) + ] ) diff --git a/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/HelloWorld.proto b/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/HelloWorld.proto index 2be480c..32aab3a 100644 --- a/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/HelloWorld.proto +++ b/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/HelloWorld.proto @@ -1,4 +1,4 @@ -// Copyright 2015 gRPC authors. +// Copyright 2015, gRPC Authors All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/grpc-swift-config.json b/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/grpc-swift-config.json index dc543c2..ddfa409 100644 --- a/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/grpc-swift-config.json +++ b/IntegrationTests/PluginTests/test_02_peer_config_file/Sources/Protos/grpc-swift-config.json @@ -1,4 +1,3 @@ { - "protocPath": "/opt/homebrew/bin/protoc", "visibility": "internal" } diff --git a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Package.swift b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Package.swift index 7ac9db7..70d8ce2 100644 --- a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Package.swift +++ b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Package.swift @@ -1,37 +1,51 @@ // swift-tools-version: 6.0 -// The swift-tools-version declares the minimum version of Swift required to build this package. +/* + * Copyright 2024, gRPC Authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ import PackageDescription let package = Package( - name: "grpc-adopter", - platforms: [ - .macOS(.v15), - .iOS(.v18), - .tvOS(.v18), - .watchOS(.v11), - .visionOS(.v2), - ], - dependencies: [ - .package( - path: "../../../../grpc-swift-protobuf" - ), - .package( - url: "https://github.com/grpc/grpc-swift.git", - from: "1.28.2" - ) - ], - targets: [ - .executableTarget( - name: "grpc-adopter", - dependencies: [ - .product(name: "GRPCCore", package: "grpc-swift"), - .product(name: "GRPCInProcessTransport", package: "grpc-swift"), - .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), - ], - plugins: [ - .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") - ] - ) - ] + name: "grpc-adopter", + platforms: [ + .macOS(.v15), + .iOS(.v18), + .tvOS(.v18), + .watchOS(.v11), + .visionOS(.v2), + ], + dependencies: [ + .package( + path: "../../../../grpc-swift-protobuf" + ), + .package( + url: "https://github.com/grpc/grpc-swift.git", + exact: "2.0.0-beta.2" + ), + ], + targets: [ + .executableTarget( + name: "grpc-adopter", + dependencies: [ + .product(name: "GRPCCore", package: "grpc-swift"), + .product(name: "GRPCInProcessTransport", package: "grpc-swift"), + .product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"), + ], + plugins: [ + .plugin(name: "GRPCGeneratorPlugin", package: "grpc-swift-protobuf") + ] + ) + ] ) diff --git a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Messages.proto b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Messages.proto index e5658f3..ce0db32 100644 --- a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Messages.proto +++ b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Messages.proto @@ -1,4 +1,4 @@ -// Copyright 2015 gRPC authors. +// Copyright 2015, gRPC Authors All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Service.proto b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Service.proto index c1e0872..aadc01e 100644 --- a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Service.proto +++ b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/Service.proto @@ -1,4 +1,4 @@ -// Copyright 2015 gRPC authors. +// Copyright 2015, gRPC Authors All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/grpc-swift-config.json b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/grpc-swift-config.json index dc543c2..ddfa409 100644 --- a/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/grpc-swift-config.json +++ b/IntegrationTests/PluginTests/test_03_separate_service_message_protos/Sources/Protos/grpc-swift-config.json @@ -1,4 +1,3 @@ { - "protocPath": "/opt/homebrew/bin/protoc", "visibility": "internal" } diff --git a/Package.swift b/Package.swift index f059a2e..ea19942 100644 --- a/Package.swift +++ b/Package.swift @@ -26,6 +26,14 @@ let products: [Product] = [ name: "protoc-gen-grpc-swift", targets: ["protoc-gen-grpc-swift"] ), + .plugin( + name: "GRPCGeneratorPlugin", + targets: ["GRPCGeneratorPlugin"] + ), + .plugin( + name: "GRPCGeneratorCommand", + targets: ["GRPCGeneratorCommand"] + ), ] let dependencies: [Package.Dependency] = [ @@ -101,6 +109,37 @@ let targets: [Target] = [ ], swiftSettings: defaultSwiftSettings ), + + // Code generator build plugin + .plugin( + name: "GRPCGeneratorPlugin", + capability: .buildTool(), + dependencies: [ + "protoc-gen-grpc-swift", + .product(name: "protoc-gen-swift", package: "swift-protobuf"), + ] + ), + + // // Code generator SwiftPM command + .plugin( + name: "GRPCGeneratorCommand", + capability: .command( + intent: .custom( + verb: "generate-grpc-code-from-protos", + description: "Generate Swift code for gRPC services from protobuf definitions." + ), + permissions: [ + .writeToPackageDirectory( + reason: + "To write the generated Swift files back into the source directory of the package." + ) + ] + ), + dependencies: [ + "protoc-gen-grpc-swift", + .product(name: "protoc-gen-swift", package: "swift-protobuf"), + ] + ), ] let package = Package( diff --git a/Plugins/GRPCGeneratorCommand/ConfigurationArguments.swift b/Plugins/GRPCGeneratorCommand/ConfigurationArguments.swift index 9a24f02..3d12a09 100644 --- a/Plugins/GRPCGeneratorCommand/ConfigurationArguments.swift +++ b/Plugins/GRPCGeneratorCommand/ConfigurationArguments.swift @@ -77,7 +77,8 @@ extension CommandConfiguration { case .useAccessLevelOnImports: self.common.useAccessLevelOnImports = .init(value) case .importPath: - self.common.importPaths!.append(value) // ! is safe because we set it to an empty array at the top of the method + // ! is safe because we set it to an empty array at the top of the method + self.common.importPaths!.append(value) case .protocPath: self.common.protocPath = value case .output: @@ -134,7 +135,7 @@ enum Flag: CaseIterable { case dryRun - init (_ argument: String) throws { + init(_ argument: String) throws { switch argument { case "--visibility": self = .visibility @@ -179,7 +180,8 @@ extension Flag { case .message: return "--message Whether message code is generated." case .fileNaming: - return "--file-naming The naming of output files with respect to the path of the source file." + return + "--file-naming The naming of output files with respect to the path of the source file." case .protoPathModuleMappings: return "--proto-path-module-mappings Path to module map .asciipb file." case .useAccessLevelOnImports: @@ -191,7 +193,8 @@ extension Flag { case .dryRun: return "--dry-run Print but do not execute the protoc commands." case .output: - return "--output The path into which the generated source files are created." + return + "--output The path into which the generated source files are created." } } diff --git a/Plugins/GRPCGeneratorCommand/Plugin.swift b/Plugins/GRPCGeneratorCommand/Plugin.swift index 538a0ef..f0d3ba2 100644 --- a/Plugins/GRPCGeneratorCommand/Plugin.swift +++ b/Plugins/GRPCGeneratorCommand/Plugin.swift @@ -14,8 +14,8 @@ * limitations under the License. */ -import PackagePlugin import Foundation +import PackagePlugin @main struct GRPCGeneratorCommandPlugin: CommandPlugin { @@ -28,7 +28,7 @@ struct GRPCGeneratorCommandPlugin: CommandPlugin { commandConfig = try CommandConfiguration(arguments: arguments) } catch PluginError.helpRequested { Flag.printHelp() - return // don't throw, the user requested this + return // don't throw, the user requested this } catch { Flag.printHelp() throw error @@ -42,10 +42,11 @@ struct GRPCGeneratorCommandPlugin: CommandPlugin { let protocGenGRPCSwiftPath = try context.tool(named: "protoc-gen-grpc-swift").url let protocGenSwiftPath = try context.tool(named: "protoc-gen-swift").url - let outputDirectory = config.outputPath.map{ URL(fileURLWithPath:$0) } ?? context.pluginWorkDirectoryURL - print("Generated files will be written to: '\(outputDirectory.formatted())'") + let outputDirectory = + config.outputPath.map { URL(fileURLWithPath: $0) } ?? context.pluginWorkDirectoryURL + print("Generated files will be written to: '\(outputDirectory.relativePath)'") - let inputFileURLs = inputFiles.map{ URL(fileURLWithPath: $0) } + let inputFileURLs = inputFiles.map { URL(fileURLWithPath: $0) } // MARK: proto-gen-grpc-swift if config.client != false || config.server != false { @@ -53,7 +54,7 @@ struct GRPCGeneratorCommandPlugin: CommandPlugin { config: config, using: config.fileNaming, inputFiles: inputFileURLs, - protoDirectoryPaths: inputFileURLs.map{ $0.deletingLastPathComponent() }, + protoDirectoryPaths: inputFileURLs.map { $0.deletingLastPathComponent() }, protocGenGRPCSwiftPath: protocGenGRPCSwiftPath, outputDirectory: outputDirectory ) @@ -78,7 +79,7 @@ struct GRPCGeneratorCommandPlugin: CommandPlugin { config: config, using: config.fileNaming, inputFiles: inputFileURLs, - protoDirectoryPaths: inputFileURLs.map{ $0.deletingLastPathComponent() }, + protoDirectoryPaths: inputFileURLs.map { $0.deletingLastPathComponent() }, protocGenSwiftPath: protocGenSwiftPath, outputDirectory: outputDirectory ) @@ -113,4 +114,3 @@ func printProtocInvocation(_ executableURL: URL, _ arguments: [String]) { print(" \(lastArgument)") } } - diff --git a/Plugins/GRPCGeneratorPlugin/ConfigurationFile.swift b/Plugins/GRPCGeneratorPlugin/ConfigurationFile.swift index 2544f97..97808cc 100644 --- a/Plugins/GRPCGeneratorPlugin/ConfigurationFile.swift +++ b/Plugins/GRPCGeneratorPlugin/ConfigurationFile.swift @@ -15,7 +15,8 @@ */ /// The configuration of the plugin. -struct ConfigurationFile: Codable { /// The visibility of the generated files. +struct ConfigurationFile: Codable { + /// The visibility of the generated files. enum Visibility: String, Codable { /// The generated files should have `internal` access level. case `internal` @@ -33,8 +34,8 @@ struct ConfigurationFile: Codable { /// The visibility of the generated files. var client: Bool? /// Whether message code is generated. var message: Bool? -// /// Whether reflection data is generated. -// var reflectionData: Bool? + // /// Whether reflection data is generated. + // var reflectionData: Bool? /// Path to module map .asciipb file. var protoPathModuleMappings: String? /// Whether imports should have explicit access levels. diff --git a/Plugins/GRPCGeneratorPlugin/Plugin.swift b/Plugins/GRPCGeneratorPlugin/Plugin.swift index 65ec17d..2565155 100644 --- a/Plugins/GRPCGeneratorPlugin/Plugin.swift +++ b/Plugins/GRPCGeneratorPlugin/Plugin.swift @@ -14,8 +14,8 @@ * limitations under the License. */ -import PackagePlugin import Foundation +import PackagePlugin @main struct GRPCGeneratorPlugin { @@ -34,7 +34,8 @@ struct GRPCGeneratorPlugin { var commands: [Command] = [] for inputFile in inputFiles { - guard let configFile = findApplicableConfigFor(file: inputFile, from: configs.keys.map{ $0 } ) else { + guard let configFile = findApplicableConfigFor(file: inputFile, from: configs.keys.map { $0 }) + else { throw PluginError.noConfigurationFilesFound } guard let config = configs[configFile] else { @@ -78,15 +79,20 @@ struct GRPCGeneratorPlugin { /// Reads the configuration files at the supplied URLs into memory /// - Parameter configurationFiles: URLs from which to load configuration /// - Returns: A map of source URLs to loaded configuration -func readConfigurationFiles(_ configurationFiles: [URL], pluginWorkDirectory: URL) throws -> [URL : CommonConfiguration] { +func readConfigurationFiles( + _ configurationFiles: [URL], + pluginWorkDirectory: URL +) throws -> [URL: CommonConfiguration] { var configs: [URL: CommonConfiguration] = [:] for configFile in configurationFiles { let data = try Data(contentsOf: configFile) let configuration = try JSONDecoder().decode(ConfigurationFile.self, from: data) var config = CommonConfiguration(configurationFile: configuration) - config.fileNaming = .fullPath // hard-code full-path to avoid collisions since this goes into a temporary directory anyway - config.outputPath = String(pluginWorkDirectory.relativePath) // the output directory mandated by the plugin system + // hard-code full-path to avoid collisions since this goes into a temporary directory anyway + config.fileNaming = .fullPath + // the output directory mandated by the plugin system + config.outputPath = String(pluginWorkDirectory.relativePath) configs[configFile] = config } return configs @@ -99,9 +105,11 @@ func readConfigurationFiles(_ configurationFiles: [URL], pluginWorkDirectory: UR /// - Returns: The path to the most precisely relevant config file if one is found, otherwise `nil`. func findApplicableConfigFor(file: URL, from configFiles: [URL]) -> URL? { let filePathComponents = file.pathComponents - for endComponent in (0.. URL? { /// - protocPath: The path to `protoc` /// - protocGenGRPCSwiftPath: The path to `proto-gen-grpc-swift`. /// - Returns: The command to invoke `protoc` with the `proto-gen-grpc-swift` plugin. -func protocGenGRPCSwiftCommand(inputFile: URL, configFile: URL, config: CommonConfiguration, protoDirectoryPath: URL, protocPath: URL, protocGenGRPCSwiftPath: URL) throws -> PackagePlugin.Command { +func protocGenGRPCSwiftCommand( + inputFile: URL, + configFile: URL, + config: CommonConfiguration, + protoDirectoryPath: URL, + protocPath: URL, + protocGenGRPCSwiftPath: URL +) throws -> PackagePlugin.Command { guard let fileNaming = config.fileNaming else { assertionFailure("Missing file naming strategy - should be hard-coded.") throw PluginError.missingFileNamingStrategy @@ -166,7 +181,14 @@ func protocGenGRPCSwiftCommand(inputFile: URL, configFile: URL, config: CommonCo /// - protocPath: The path to `protoc` /// - protocGenSwiftPath: The path to `proto-gen-grpc-swift`. /// - Returns: The command to invoke `protoc` with the `proto-gen-swift` plugin. -func protocGenSwiftCommand(inputFile: URL, configFile: URL, config: CommonConfiguration, protoDirectoryPath: URL, protocPath: URL, protocGenSwiftPath: URL) throws -> PackagePlugin.Command { +func protocGenSwiftCommand( + inputFile: URL, + configFile: URL, + config: CommonConfiguration, + protoDirectoryPath: URL, + protocPath: URL, + protocGenSwiftPath: URL +) throws -> PackagePlugin.Command { guard let fileNaming = config.fileNaming else { assertionFailure("Missing file naming strategy - should be hard-coded.") throw PluginError.missingFileNamingStrategy @@ -211,8 +233,8 @@ extension GRPCGeneratorPlugin: BuildToolPlugin, LocalizedError { guard let swiftTarget = target as? SwiftSourceModuleTarget else { throw PluginError.incompatibleTarget(target.name) } - let configFiles = swiftTarget.sourceFiles(withSuffix: "grpc-swift-config.json").map{ $0.url } - let inputFiles = swiftTarget.sourceFiles(withSuffix: ".proto").map{ $0.url } + let configFiles = swiftTarget.sourceFiles(withSuffix: "grpc-swift-config.json").map { $0.url } + let inputFiles = swiftTarget.sourceFiles(withSuffix: ".proto").map { $0.url } return try createBuildCommands( pluginWorkDirectory: context.pluginWorkDirectoryURL, tool: context.tool, @@ -230,8 +252,12 @@ import XcodeProjectPlugin extension GRPCGeneratorPlugin: XcodeBuildToolPlugin { /// Create build commands, the entry-point when using an Xcode project. func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] { - let configFiles = target.inputFiles.filter{ $0.url.lastPathComponent == "grpc-swift-config.json" }.map{ $0.url } - let inputFiles = target.inputFiles.filter{ $0.url.lastPathComponent.hasSuffix(".proto") }.map{ $0.url } + let configFiles = target.inputFiles.filter { + $0.url.lastPathComponent == "grpc-swift-config.json" + }.map { $0.url } + let inputFiles = target.inputFiles.filter { $0.url.lastPathComponent.hasSuffix(".proto") }.map { + $0.url + } return try createBuildCommands( pluginWorkDirectory: context.pluginWorkDirectoryURL, tool: context.tool, diff --git a/Plugins/PluginsShared/CommonConfiguration.swift b/Plugins/PluginsShared/CommonConfiguration.swift index f4eeddd..c705c2e 100644 --- a/Plugins/PluginsShared/CommonConfiguration.swift +++ b/Plugins/PluginsShared/CommonConfiguration.swift @@ -49,8 +49,8 @@ struct CommonConfiguration: Codable { var client: Bool? /// Whether message code is generated. var message: Bool? -// /// Whether reflection data is generated. -// var reflectionData: Bool? + // /// Whether reflection data is generated. + // var reflectionData: Bool? /// The naming of output files with respect to the path of the source file. var fileNaming: FileNaming? /// Path to module map .asciipb file. diff --git a/Plugins/PluginsShared/PluginUtils.swift b/Plugins/PluginsShared/PluginUtils.swift index 9c020f0..9e9afc5 100644 --- a/Plugins/PluginsShared/PluginUtils.swift +++ b/Plugins/PluginsShared/PluginUtils.swift @@ -14,15 +14,18 @@ * limitations under the License. */ -import PackagePlugin import Foundation +import PackagePlugin /// Derive the path to the instance of `protoc` to be used. /// - Parameters: /// - config: The supplied configuration. If no path is supplied then one is discovered using the `PROTOC_PATH` environment variable or the `findTool`. /// - findTool: The context-supplied tool which is used to attempt to discover the path to a `protoc` binary. /// - Returns: The path to the instance of `protoc` to be used. -func deriveProtocPath(using config: CommonConfiguration, tool findTool: (String) throws -> PackagePlugin.PluginContext.Tool) throws -> URL { +func deriveProtocPath( + using config: CommonConfiguration, + tool findTool: (String) throws -> PackagePlugin.PluginContext.Tool +) throws -> URL { if let configuredProtocPath = config.protocPath { return URL(fileURLWithPath: configuredProtocPath) } else if let environmentPath = ProcessInfo.processInfo.environment["PROTOC_PATH"] { @@ -78,9 +81,9 @@ func constructProtocGenSwiftArguments( protocArgs.append("--swift_opt=UseAccessLevelOnImports=\(useAccessLevelOnImports)") } - protocArgs.append(contentsOf: protoDirectoryPaths.map{ "--proto_path=\($0.relativePath)" }) + protocArgs.append(contentsOf: protoDirectoryPaths.map { "--proto_path=\($0.relativePath)" }) - protocArgs.append(contentsOf: inputFiles.map{ $0.relativePath} ) + protocArgs.append(contentsOf: inputFiles.map { $0.relativePath }) return protocArgs } @@ -144,9 +147,9 @@ func constructProtocGenGRPCSwiftArguments( protocArgs.append("--grpc-swift_opt=UseAccessLevelOnImports=\(useAccessLevelOnImports)") } - protocArgs.append(contentsOf: protoDirectoryPaths.map{ "--proto_path=\($0.relativePath)" }) + protocArgs.append(contentsOf: protoDirectoryPaths.map { "--proto_path=\($0.relativePath)" }) - protocArgs.append(contentsOf: inputFiles.map{ $0.relativePath} ) + protocArgs.append(contentsOf: inputFiles.map { $0.relativePath }) return protocArgs } @@ -159,7 +162,13 @@ func constructProtocGenGRPCSwiftArguments( /// - outputDirectory: The directory in which generated source files are created. /// - outputExtension: The file extension to be appended to generated files in-place of `.proto`. /// - Returns: The expected output file path. -func deriveOutputFilePath(for inputFile: URL, using fileNaming: CommonConfiguration.FileNaming, protoDirectoryPath: URL, outputDirectory: URL, outputExtension: String) -> URL { +func deriveOutputFilePath( + for inputFile: URL, + using fileNaming: CommonConfiguration.FileNaming, + protoDirectoryPath: URL, + outputDirectory: URL, + outputExtension: String +) -> URL { // 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 diff --git a/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift b/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift index 98141d1..336e173 100644 --- a/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift +++ b/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift @@ -63,10 +63,6 @@ final class GenerateGRPC: CodeGenerator { ) } -// if descriptor.services.isEmpty { -// continue -// } - try self.generateV2Stubs(descriptor, options: options, outputs: outputs) } } diff --git a/dev/plugin-tests.sh b/dev/plugin-tests.sh index aee92bb..81478fa 100755 --- a/dev/plugin-tests.sh +++ b/dev/plugin-tests.sh @@ -19,8 +19,8 @@ log() { printf -- "** %s\n" "$*" >&2; } error() { printf -- "** ERROR: %s\n" "$*" >&2; } fatal() { error "$@"; exit 1; } -here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -pluginTests="$here/../IntegrationTests/PluginTests" +source_dir=$(pwd) +pluginTests="${source_dir}/IntegrationTests/PluginTests" for dir in "$pluginTests"/test_*/ ; do if [[ -f "$dir/Package.swift" ]]; then