Skip to content

Commit

Permalink
Add Swift registry config to swift plugins (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmitche authored May 10, 2023
1 parent e0eecb3 commit b8c413e
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
aead.dev/minisign v0.2.0
github.com/bufbuild/buf v1.18.0
github.com/bufbuild/buf v1.18.1-0.20230509142145-4eb0443a8de1
github.com/google/go-github/v50 v50.2.0
github.com/hashicorp/go-retryablehttp v0.7.2
github.com/sethvargo/go-envconfig v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ aead.dev/minisign v0.2.0 h1:kAWrq/hBRu4AARY6AlciO83xhNnW9UaC8YipS2uhLPk=
aead.dev/minisign v0.2.0/go.mod h1:zdq6LdSd9TbuSxchxwhpA9zEb9YXcVGoE8JakuiGaIQ=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
github.com/bufbuild/buf v1.18.0 h1:21VUsBACeRLwJ+7XfzCc8z173M4iH8shcKDO7ASwp7A=
github.com/bufbuild/buf v1.18.0/go.mod h1:Wp6mb9HCLDPI1woVCZux154AA0y8aLiPM+fr7Qufmto=
github.com/bufbuild/buf v1.18.1-0.20230509142145-4eb0443a8de1 h1:GG3/8qL9mSFUmBtbHHs+qxSows/Ux4HrDLMJgq1S83k=
github.com/bufbuild/buf v1.18.1-0.20230509142145-4eb0443a8de1/go.mod h1:6sOGv7Hqb/pvAEmEovScg9VELIrSuoS7KnZiLrd32kg=
github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY=
github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I=
Expand Down
1 change: 1 addition & 0 deletions plugins/apple/swift/v1.21.0/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*
!Dockerfile
!extramoduleimports.patch
2 changes: 2 additions & 0 deletions plugins/apple/swift/v1.21.0/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# syntax=docker/dockerfile:1.4
FROM swift:5.7.3-focal AS build

COPY extramoduleimports.patch /app/extramoduleimports.patch
WORKDIR /app
RUN git clone --depth 1 --branch 1.21.0 https://github.com/apple/swift-protobuf --recursive
WORKDIR /app/swift-protobuf
RUN git apply /app/extramoduleimports.patch
RUN swift build -c release --static-swift-stdlib -Xlinker -s

FROM gcr.io/distroless/cc-debian11
Expand Down
11 changes: 11 additions & 0 deletions plugins/apple/swift/v1.21.0/buf.plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,16 @@ source_url: https://github.com/apple/swift-protobuf
description: Base types for Swift. Generates message and enum types.
output_languages:
- swift
registry:
swift:
deps:
- source: https://github.com/apple/swift-protobuf.git
package: swift-protobuf
swift_versions: [ ".v5" ]
products: [ SwiftProtobuf ]
version: 1.21.0
opts:
- Visibility=Public
- FileNaming=PathToUnderscores
spdx_license_id: Apache-2.0
license_url: https://github.com/apple/swift-protobuf/blob/1.21.0/LICENSE.txt
65 changes: 65 additions & 0 deletions plugins/apple/swift/v1.21.0/extramoduleimports.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
diff --git a/Sources/protoc-gen-swift/FileGenerator.swift b/Sources/protoc-gen-swift/FileGenerator.swift
index 87c05aa7..4083d490 100644
--- a/Sources/protoc-gen-swift/FileGenerator.swift
+++ b/Sources/protoc-gen-swift/FileGenerator.swift
@@ -101,6 +101,13 @@ class FileGenerator {
p.print("import \(i)\n")
}
}
+ let neededCustomImports = generatorOptions.extraModuleImports
+ if !neededCustomImports.isEmpty{
+ p.print()
+ for i in neededCustomImports {
+ p.print("import \(i)\n")
+ }
+ }

p.print("\n")
generateVersionCheck(printer: &p)
diff --git a/Sources/protoc-gen-swift/GeneratorOptions.swift b/Sources/protoc-gen-swift/GeneratorOptions.swift
index d505fdf1..20353d18 100644
--- a/Sources/protoc-gen-swift/GeneratorOptions.swift
+++ b/Sources/protoc-gen-swift/GeneratorOptions.swift
@@ -49,6 +49,7 @@ class GeneratorOptions {
let outputNaming: OutputNaming
let protoToModuleMappings: ProtoFileToModuleMappings
let visibility: Visibility
+ let extraModuleImports: [String]

/// A string snippet to insert for the visibility
let visibilitySourceSnippet: String
@@ -58,6 +59,7 @@ class GeneratorOptions {
var moduleMapPath: String?
var visibility: Visibility = .internal
var swiftProtobufModuleName: String? = nil
+ var externalModuleImports: [String] = []

for pair in parseParameter(string:parameter) {
switch pair.key {
@@ -88,9 +90,15 @@ class GeneratorOptions {
throw GenerationError.invalidParameterValue(name: pair.key,
value: pair.value)
}
- default:
- throw GenerationError.unknownParameter(name: pair.key)
- }
+ case "ExtraModuleImports":
+ if !pair.value.isEmpty {
+ externalModuleImports.append(pair.value)
+ } else {
+ throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+ }
+ default:
+ throw GenerationError.unknownParameter(name: pair.key)
+ }
}

if let moduleMapPath = moduleMapPath {
@@ -107,6 +115,7 @@ class GeneratorOptions {

self.outputNaming = outputNaming
self.visibility = visibility
+ self.extraModuleImports = externalModuleImports

switch visibility {
case .internal:
15 changes: 15 additions & 0 deletions plugins/bufbuild/connect-swift-mocks/v0.5.0/buf.plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,20 @@ deps:
- plugin: buf.build/apple/swift:v1.21.0
output_languages:
- swift
registry:
swift:
deps:
- source: https://github.com/bufbuild/connect-swift.git
package: connect-swift
products: [ ConnectMocks ]
swift_versions: [ ".v5" ]
version: 0.5.0
platforms:
macos: v10_15
ios: v12
tvos: v13
opts:
- Visibility=Public
- FileNaming=PathToUnderscores
spdx_license_id: Apache-2.0
license_url: https://github.com/bufbuild/connect-swift/blob/0.5.0/LICENSE
15 changes: 15 additions & 0 deletions plugins/bufbuild/connect-swift/v0.5.0/buf.plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,20 @@ deps:
- plugin: buf.build/apple/swift:v1.21.0
output_languages:
- swift
registry:
swift:
deps:
- source: https://github.com/bufbuild/connect-swift.git
package: connect-swift
products: [ Connect ]
swift_versions: [ ".v5" ]
version: 0.5.0
platforms:
macos: v10_15
ios: v12
tvos: v13
opts:
- Visibility=Public
- FileNaming=PathToUnderscores
spdx_license_id: Apache-2.0
license_url: https://github.com/bufbuild/connect-swift/blob/0.5.0/LICENSE
10 changes: 10 additions & 0 deletions plugins/grpc/swift/v1.15.0/buf.plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@ deps:
- plugin: buf.build/apple/swift:v1.21.0
output_languages:
- swift
registry:
swift:
deps:
- source: https://github.com/grpc/grpc-swift.git
package: grpc-swift
products: [ GRPC ]
version: 1.15.0
opts:
- Visibility=Public
- FileNaming=PathToUnderscores
spdx_license_id: Apache-2.0
license_url: https://github.com/grpc/grpc-swift/blob/1.15.0/LICENSE
2 changes: 1 addition & 1 deletion tests/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestGeneration(t *testing.T) {
if allowEmpty && existingPluginSum == "" {
t.Log("allowing empty plugin.sum file (used by fetcher command)")
} else {
assert.Equal(t, genDirHash, existingPluginSum)
assert.Equal(t, existingPluginSum, genDirHash)
}
require.NoError(t, os.WriteFile(pluginImageSumFile, []byte(genDirHash+"\n"), 0644))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:ZXTPSfPU1oy++xlxWB+qwoDIlNv+tU+3lu9spwL1k7Y=
h1:ffGsj903OkejaJHBJ9J0lmZkRiA58mar/OznBW1O9Wo=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:QjPCAsCGHD4ysR2z85BUuXnqN8IIcCtnck1HPHY2Hdw=
h1:0b5iqMifZQ1mLRYdO83BFuNf3JagkBeYg5O++DTSFhE=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:DZIF/6P1wHTkNQXRwlBY44hMvQbhJDeT5/R3kAhqK6w=
h1:ryyGfdjE+6xSE6LxYx66pDaPATE5wyhCPdH/Tnb1PT4=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:MrtIkM8S5XpjebDhLIR7Sl9IjntBIc0Kb3K91s4N2Ck=
h1:2HOAos9kseDVKDxLcXzESkiteNEnWuMYYWJujRbvons=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:j5lRDEmZgsrBHZBd314DmnkkQB2benXKOt7jOLYrB2s=
h1:vpe8c/VzVamU9ttCJWtk2iT2aWsCD47KWVN6Xd4jhDw=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:WcntIMhINysZCRKRBmM80n1+8ePJDzU5EF+7+ATlAm8=
h1:E8QVNgzAzK1BpanCUwE7YTP5Ha/l8ysydC5Aw+MaMS0=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:AyTBCyR7NTQf+n2zmThBq9/Nveb5Mx/QZnQ4ZvovhF4=
h1:lE0vsY09+gnaUaHtnQ62TXKwcSu5MWUO1UPbom7xK3s=
Original file line number Diff line number Diff line change
@@ -1 +1 @@
h1:nxLl9QVtjt+8+FrGOVBBSZnGQLT+GrD+NjxshOJh7xc=
h1:ZQjqQTXECbm0x4TZ1Oec1mnrMO97IgfBXSlYPdNhUoE=

0 comments on commit b8c413e

Please sign in to comment.