Skip to content

Commit fda21d4

Browse files
authored
Set version info using info from the package context (#66)
Motivation: The version for the package included in protoc-gen-grpc-swift is updated manually and is currently incorrect. We can get the appropriate information from the context provided by SwiftPM. Modifications: - Add a C shim module which provides a version string from the package context Result: - Version string is kept up-to-date - Resolves #60
1 parent 43cede7 commit fda21d4

File tree

6 files changed

+122
-19
lines changed

6 files changed

+122
-19
lines changed

.github/workflows/pull_request.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
1212
with:
1313
license_header_check_project_name: "gRPC"
14+
# This is done by a similar job defined in soundness.yml. It needs to be
15+
# separate in order to export an environment variable.
16+
api_breakage_check_enabled: false
1417

1518
grpc-soundness:
1619
name: Soundness

.github/workflows/soundness.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,28 @@ jobs:
3535
- name: Check generated code
3636
run: |
3737
./dev/check-generated-code.sh
38+
39+
api-breakage-check:
40+
name: API breakage check
41+
runs-on: ubuntu-latest
42+
container:
43+
image: swift:latest
44+
steps:
45+
- name: Checkout repository
46+
uses: actions/checkout@v4
47+
with:
48+
persist-credentials: false
49+
fetch-depth: 0 # Fetching tags requires fetch-depth: 0 (https://github.com/actions/checkout/issues/1471)
50+
- name: Mark the workspace as safe
51+
# https://github.com/actions/checkout/issues/766
52+
run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
53+
- name: Run API breakage check
54+
shell: bash
55+
# See package.swift for why we set GRPC_SWIFT_PROTOBUF_NO_VERSION=1
56+
run: |
57+
export GRPC_SWIFT_PROTOBUF_NO_VERSION=1
58+
59+
git fetch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} ${GITHUB_BASE_REF}:pull-base-ref
60+
BASELINE_REF='pull-base-ref'
61+
echo "Using baseline: $BASELINE_REF"
62+
swift package diagnose-api-breaking-changes "$BASELINE_REF"

Package.swift

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let defaultSwiftSettings: [SwiftSetting] = [
5454
.enableUpcomingFeature("MemberImportVisibility"),
5555
]
5656

57-
let targets: [Target] = [
57+
var targets: [Target] = [
5858
// protoc plugin for grpc-swift
5959
.executableTarget(
6060
name: "protoc-gen-grpc-swift",
@@ -143,6 +143,51 @@ let targets: [Target] = [
143143
),
144144
]
145145

146+
// -------------------------------------------------------------------------------------------------
147+
148+
extension Context {
149+
fileprivate static var versionString: String {
150+
guard let git = Self.gitInformation else { return "" }
151+
152+
if let tag = git.currentTag {
153+
return tag
154+
} else {
155+
let suffix = git.hasUncommittedChanges ? " (modified)" : ""
156+
return git.currentCommit + suffix
157+
}
158+
}
159+
160+
fileprivate static var buildCGRPCProtobuf: Bool {
161+
let noVersion = Context.environment.keys.contains("GRPC_SWIFT_PROTOBUF_NO_VERSION")
162+
return !noVersion
163+
}
164+
}
165+
166+
// Having a C module as a transitive dependency of a plugin seems to trip up the API breakage
167+
// checking tool. See also https://github.com/swiftlang/swift-package-manager/issues/8081
168+
//
169+
// The CGRPCProtobuf module (which only includes package version information) is conditionally
170+
// compiled and included based on an environment variable. This is set in CI only for the API
171+
// breakage checking job to avoid tripping up SwiftPM.
172+
if Context.buildCGRPCProtobuf {
173+
targets.append(
174+
.target(
175+
name: "CGRPCProtobuf",
176+
cSettings: [
177+
.define("CGRPC_GRPC_SWIFT_PROTOBUF_VERSION", to: "\"\(Context.versionString)\"")
178+
]
179+
)
180+
)
181+
182+
for target in targets {
183+
if target.name == "protoc-gen-grpc-swift" {
184+
target.dependencies.append(.target(name: "CGRPCProtobuf"))
185+
}
186+
}
187+
}
188+
189+
// -------------------------------------------------------------------------------------------------
190+
146191
let package = Package(
147192
name: "grpc-swift-protobuf",
148193
platforms: [
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2025, gRPC Authors All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "CGRPCProtobuf.h"
16+
17+
const char *cgrprc_grpc_swift_protobuf_version() {
18+
return CGRPC_GRPC_SWIFT_PROTOBUF_VERSION;
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2025, gRPC Authors All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef CGRPC_PROTOBUF_H_
16+
#define CGRPC_PROTOBUF_H_
17+
18+
const char *cgrprc_grpc_swift_protobuf_version();
19+
20+
#endif // CGRPC_PROTOBUF_H_

Sources/protoc-gen-grpc-swift/Version.swift

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
internal enum Version {
18-
/// The major version.
19-
internal static let major = 1
20-
21-
/// The minor version.
22-
internal static let minor = 0
23-
24-
/// The patch version.
25-
internal static let patch = 0
26-
27-
/// Any additional label.
28-
internal static let label = "development"
17+
#if canImport(CGRPCProtobuf)
18+
private import CGRPCProtobuf
19+
#endif
2920

21+
internal enum Version {
3022
/// The version string.
3123
internal static var versionString: String {
32-
let version = "\(Self.major).\(Self.minor).\(Self.patch)"
33-
if Self.label.isEmpty {
34-
return version
35-
} else {
36-
return version + "-" + Self.label
37-
}
24+
#if canImport(CGRPCProtobuf)
25+
String(cString: cgrprc_grpc_swift_protobuf_version())
26+
#else
27+
"unknown"
28+
#endif
3829
}
3930
}

0 commit comments

Comments
 (0)