Skip to content

Commit 8342d2c

Browse files
authored
Filter buf.gen.yaml files based on protoc version (#300)
Update the buf.gen.yaml files to filter based on the protoc version, removing a manual step required when updating to newer protoc versions.
1 parent 53aa216 commit 8342d2c

File tree

9 files changed

+41
-23
lines changed

9 files changed

+41
-23
lines changed

buf.gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: v2
22
clean: true
33
plugins:
4-
- remote: buf.build/protocolbuffers/java:v31.1
4+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
55
out: build/generated/sources/bufgen

build.gradle.kts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,46 @@ tasks.register<Exec>("licenseHeader") {
7272
)
7373
}
7474

75+
tasks.register<Copy>("filterBufGenYaml") {
76+
from(".")
77+
include("buf.gen.yaml", "src/**/buf*gen*.yaml")
78+
includeEmptyDirs = false
79+
into(layout.buildDirectory.dir("buf-gen-templates"))
80+
expand("protocJavaPluginVersion" to "v${libs.versions.protobuf.get().substringAfter('.')}")
81+
filteringCharset = "UTF-8"
82+
}
83+
7584
tasks.register<Exec>("generateTestSourcesImports") {
76-
dependsOn("exportProtovalidateModule")
85+
dependsOn("exportProtovalidateModule", "filterBufGenYaml")
7786
description = "Generates code with buf generate --include-imports for unit tests."
7887
commandLine(
7988
buf.asPath,
8089
"generate",
8190
"--template",
82-
"src/test/resources/proto/buf.gen.imports.yaml",
91+
"${layout.buildDirectory.get()}/buf-gen-templates/src/test/resources/proto/buf.gen.imports.yaml",
8392
"--include-imports",
8493
)
8594
}
8695

8796
tasks.register<Exec>("generateTestSourcesNoImports") {
88-
dependsOn("exportProtovalidateModule")
97+
dependsOn("exportProtovalidateModule", "filterBufGenYaml")
8998
description = "Generates code with buf generate --include-imports for unit tests."
90-
commandLine(buf.asPath, "generate", "--template", "src/test/resources/proto/buf.gen.noimports.yaml")
99+
commandLine(
100+
buf.asPath,
101+
"generate",
102+
"--template",
103+
"${layout.buildDirectory.get()}/buf-gen-templates/src/test/resources/proto/buf.gen.noimports.yaml",
104+
)
91105
}
92106

93107
tasks.register<Exec>("generateCelConformance") {
94-
dependsOn("generateCelConformanceTestTypes")
108+
dependsOn("generateCelConformanceTestTypes", "filterBufGenYaml")
95109
description = "Generates CEL conformance code with buf generate for unit tests."
96110
commandLine(
97111
buf.asPath,
98112
"generate",
99113
"--template",
100-
"src/test/resources/proto/buf.gen.cel.yaml",
114+
"${layout.buildDirectory.get()}/buf-gen-templates/src/test/resources/proto/buf.gen.cel.yaml",
101115
"buf.build/google/cel-spec:${project.findProperty("cel.spec.version")}",
102116
"--exclude-path",
103117
"cel/expr/conformance/proto2",
@@ -112,13 +126,13 @@ tasks.register<Exec>("generateCelConformance") {
112126
// specified in these proto files is "dev.cel.expr.conformance.proto3". So, to get around this,
113127
// we're generating these separately and specifying a java_package override of the package we need.
114128
tasks.register<Exec>("generateCelConformanceTestTypes") {
115-
dependsOn("exportProtovalidateModule")
129+
dependsOn("exportProtovalidateModule", "filterBufGenYaml")
116130
description = "Generates CEL conformance test types with buf generate for unit tests using a Java package override."
117131
commandLine(
118132
buf.asPath,
119133
"generate",
120134
"--template",
121-
"src/test/resources/proto/buf.gen.cel.testtypes.yaml",
135+
"${layout.buildDirectory.get()}/buf-gen-templates/src/test/resources/proto/buf.gen.cel.testtypes.yaml",
122136
"buf.build/google/cel-spec:${project.findProperty("cel.spec.version")}",
123137
"--path",
124138
"cel/expr/conformance/proto3",
@@ -166,9 +180,9 @@ tasks.register<Exec>("exportProtovalidateModule") {
166180
}
167181

168182
tasks.register<Exec>("generateSources") {
169-
dependsOn("exportProtovalidateModule")
183+
dependsOn("exportProtovalidateModule", "filterBufGenYaml")
170184
description = "Generates sources for the bufbuild/protovalidate module sources to build/generated/sources/bufgen."
171-
commandLine(buf.asPath, "generate", "--template", "buf.gen.yaml", "src/main/resources")
185+
commandLine(buf.asPath, "generate", "--template", "${layout.buildDirectory.get()}/buf-gen-templates/buf.gen.yaml", "src/main/resources")
172186
}
173187

174188
tasks.register("generate") {

conformance/buf.gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ managed:
66
- file_option: java_package_prefix
77
value: build
88
plugins:
9-
- remote: buf.build/protocolbuffers/java:v31.1
9+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
1010
out: build/generated/sources/bufgen

conformance/build.gradle.kts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,23 @@ tasks.register<Exec>("conformance") {
5353
commandLine(*(listOf(conformanceCLIPath) + conformanceArgs + listOf(conformanceAppScript)).toTypedArray())
5454
}
5555

56+
tasks.register<Copy>("filterBufGenYaml") {
57+
from(".")
58+
include("buf.gen.yaml")
59+
includeEmptyDirs = false
60+
into(layout.buildDirectory.dir("buf-gen-templates"))
61+
expand("protocJavaPluginVersion" to "v${libs.versions.protobuf.get().substringAfter('.')}")
62+
filteringCharset = "UTF-8"
63+
}
64+
5665
tasks.register<Exec>("generateConformance") {
57-
dependsOn("configureBuf")
66+
dependsOn("configureBuf", "filterBufGenYaml")
5867
description = "Generates sources for the bufbuild/protovalidate-testing module to build/generated/sources/bufgen."
5968
commandLine(
6069
buf.asPath,
6170
"generate",
6271
"--template",
63-
"buf.gen.yaml",
72+
"${layout.buildDirectory.get()}/buf-gen-templates/buf.gen.yaml",
6473
"buf.build/bufbuild/protovalidate-testing:${project.findProperty("protovalidate.version")}",
6574
)
6675
}

gradle/libs.versions.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ cel = "0.5.3"
55
error-prone = "2.38.0"
66
junit = "5.13.0"
77
maven-publish = "0.32.0"
8-
# When updating, make sure to update versions in the following files to match and regenerate code with 'make generate'.
9-
# - buf.gen.yaml
10-
# - conformance/buf.gen.yaml
11-
# - src/test/resources/proto/buf.gen.imports.yaml
12-
# - src/test/resources/proto/buf.gen.noimports.yaml
138
protobuf = "4.31.1"
149

1510
[libraries]

src/test/resources/proto/buf.gen.cel.testtypes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ managed:
55
- file_option: java_package
66
value: cel.expr.conformance.proto3
77
plugins:
8-
- remote: buf.build/protocolbuffers/java:v31.1
8+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
99
out: build/generated/test-sources/bufgen

src/test/resources/proto/buf.gen.cel.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ version: v2
22
managed:
33
enabled: true
44
plugins:
5-
- remote: buf.build/protocolbuffers/java:v31.1
5+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
66
out: build/generated/test-sources/bufgen

src/test/resources/proto/buf.gen.imports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ managed:
55
- file_option: java_package_prefix
66
value: com.example.imports
77
plugins:
8-
- remote: buf.build/protocolbuffers/java:v31.1
8+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
99
out: build/generated/test-sources/bufgen
1010
inputs:
1111
- directory: src/test/resources/proto

src/test/resources/proto/buf.gen.noimports.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ managed:
88
- file_option: java_package_prefix
99
value: com.example.noimports
1010
plugins:
11-
- remote: buf.build/protocolbuffers/java:v31.1
11+
- remote: buf.build/protocolbuffers/java:$protocJavaPluginVersion
1212
out: build/generated/test-sources/bufgen
1313
inputs:
1414
- directory: src/main/resources

0 commit comments

Comments
 (0)