Browse Source

Swift 6-ify the package manifest (#1955)

Motivation:

v2 will require the Swift 6 language mode. In order to continue
developing v1 and v2 on main we'll need a Swift 6 specific manifest
version in addition to the 5.x version.

Moving forward we'll have two package manifests, `Package.swift` for 5.x
containing only v1, and `Package@swift-6.swift` using tools version 6.0
targetting v1 and v2.

Modifications:

- Remove v2 targets from Package.swift
- Add a second manifest and make it warning free with Swift 6, this
  requires turning `static let`s to `static var`s.
- Conditionalise some imports in `protoc-gen-grpc-swift` (as it is
  shared by v1 and v2)
- Fix an error in the plugin for Swift 6

Result:

v2 now only compiles with a Swift 6 compiler
George Barnett 1 year ago
parent
commit
0534fff208

+ 13 - 2
Sources/protoc-gen-grpc-swift/main.swift

@@ -14,11 +14,14 @@
  * limitations under the License.
  */
 import Foundation
-import GRPCCodeGen
-import GRPCProtobufCodeGen
 import SwiftProtobuf
 import SwiftProtobufPluginLibrary
 
+#if compiler(>=6.0)
+import GRPCCodeGen
+import GRPCProtobufCodeGen
+#endif
+
 func Log(_ message: String) {
   FileHandle.standardError.write((message + "\n").data(using: .utf8)!)
 }
@@ -168,6 +171,8 @@ func main(args: [String]) throws {
           fileNamingOption: options.fileNaming,
           generatedFiles: &generatedFiles
         )
+
+        #if compiler(>=6.0)
         if options.v2 {
           let grpcGenerator = ProtobufCodeGenerator(
             configuration: SourceGenerator.Configuration(options: options)
@@ -181,6 +186,10 @@ func main(args: [String]) throws {
           let grpcGenerator = Generator(fileDescriptor, options: options)
           grpcFile.content = grpcGenerator.code
         }
+        #else
+        let grpcGenerator = Generator(fileDescriptor, options: options)
+        grpcFile.content = grpcGenerator.code
+        #endif
         grpcFile.name = grpcFileName
         response.file.append(grpcFile)
       }
@@ -198,6 +207,7 @@ do {
   Log("ERROR: \(error)")
 }
 
+#if compiler(>=6.0)
 extension SourceGenerator.Configuration {
   init(options: GeneratorOptions) {
     let accessLevel: SourceGenerator.Configuration.AccessLevel
@@ -216,3 +226,4 @@ extension SourceGenerator.Configuration {
     )
   }
 }
+#endif

+ 4 - 0
Sources/protoc-gen-grpc-swift/options.swift

@@ -68,7 +68,9 @@ final class GeneratorOptions {
   private(set) var gRPCModuleName = "GRPC"
   private(set) var swiftProtobufModuleName = "SwiftProtobuf"
   private(set) var generateReflectionData = false
+  #if compiler(>=6.0)
   private(set) var v2 = false
+  #endif
 
   init(parameter: String?) throws {
     for pair in GeneratorOptions.parseParameter(string: parameter) {
@@ -155,12 +157,14 @@ final class GeneratorOptions {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
 
+      #if compiler(>=6.0)
       case "_V2":
         if let value = Bool(pair.value) {
           self.v2 = value
         } else {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
+      #endif
 
       default:
         throw GenerationError.unknownParameter(name: pair.key)

+ 5 - 0
scripts/license-check.sh

@@ -74,6 +74,11 @@ check_copyright_headers() {
         drop_first=1
         expected_lines=15
         ;;
+      */Package@swift-*.swift)
+        expected_sha="$SWIFT_SHA"
+        drop_first=1
+        expected_lines=15
+        ;;
       */Package@swift-*.*.swift)
         expected_sha="$SWIFT_SHA"
         drop_first=1