Skip to content

Commit 6f6f837

Browse files
committed
Set version info using info from the package context
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 6f6f837

File tree

4 files changed

+67
-18
lines changed

4 files changed

+67
-18
lines changed

Package.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,26 @@ let defaultSwiftSettings: [SwiftSetting] = [
5454
.enableUpcomingFeature("MemberImportVisibility"),
5555
]
5656

57+
extension Context {
58+
fileprivate static var versionString: String {
59+
guard let git = Self.gitInformation else { return "" }
60+
61+
if let tag = git.currentTag {
62+
return tag
63+
} else {
64+
let suffix = git.hasUncommittedChanges ? " (modified)" : ""
65+
return git.currentCommit + suffix
66+
}
67+
}
68+
}
69+
5770
let targets: [Target] = [
5871
// protoc plugin for grpc-swift
5972
.executableTarget(
6073
name: "protoc-gen-grpc-swift",
6174
dependencies: [
6275
.target(name: "GRPCProtobufCodeGen"),
76+
.target(name: "CGRPCProtobuf"),
6377
.product(name: "GRPCCodeGen", package: "grpc-swift"),
6478
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
6579
.product(name: "SwiftProtobufPluginLibrary", package: "swift-protobuf"),
@@ -110,6 +124,13 @@ let targets: [Target] = [
110124
swiftSettings: defaultSwiftSettings
111125
),
112126

127+
.target(
128+
name: "CGRPCProtobuf",
129+
cSettings: [
130+
.define("CGRPC_GRPC_SWIFT_PROTOBUF_VERSION", to: "\"\(Context.versionString)\"")
131+
]
132+
),
133+
113134
// Code generator build plugin
114135
.plugin(
115136
name: "GRPCProtobufGenerator",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2025, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "CGRPCProtobuf.h"
18+
19+
const char *cgrprc_grpc_swift_protobuf_version() {
20+
return CGRPC_GRPC_SWIFT_PROTOBUF_VERSION;
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2025, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CGRPC_PROTOBUF_H_
18+
#define CGRPC_PROTOBUF_H_
19+
20+
const char *cgrprc_grpc_swift_protobuf_version();
21+
22+
#endif // CGRPC_PROTOBUF_H_

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,11 @@
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+
private import CGRPCProtobuf
2918

19+
internal enum Version {
3020
/// The version string.
3121
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-
}
22+
String(cString: cgrprc_grpc_swift_protobuf_version())
3823
}
3924
}

0 commit comments

Comments
 (0)