From c2c1a94080cc745e0addc9f93b81ccb36443278d Mon Sep 17 00:00:00 2001 From: un1024 Date: Sun, 2 Jun 2024 22:25:17 +0900 Subject: [PATCH 1/4] Prevent the error when explicitly specifying default buf gen template file path --- .../kotlin/build/buf/gradle/GenerateTask.kt | 6 +++-- .../build/buf/gradle/AbstractGenerateTest.kt | 5 ++++ .../buf.gen.yaml | 4 ++++ .../buf/test/v1/test.proto | 21 ++++++++++++++++ .../build.gradle | 24 +++++++++++++++++++ .../src/main/java/buf/test/v1/Foo.java | 21 ++++++++++++++++ .../buf.gen.yaml | 4 ++++ .../buf.work.yaml | 3 +++ .../build.gradle | 24 +++++++++++++++++++ .../src/main/java/buf/test/v1/Foo.java | 21 ++++++++++++++++ .../workspace/buf/test/v1/test.proto | 21 ++++++++++++++++ 11 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml create mode 100644 src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf/test/v1/test.proto create mode 100644 src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle create mode 100644 src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.work.yaml create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/workspace/buf/test/v1/test.proto diff --git a/src/main/kotlin/build/buf/gradle/GenerateTask.kt b/src/main/kotlin/build/buf/gradle/GenerateTask.kt index f55dde6f..7ef47092 100644 --- a/src/main/kotlin/build/buf/gradle/GenerateTask.kt +++ b/src/main/kotlin/build/buf/gradle/GenerateTask.kt @@ -50,8 +50,10 @@ abstract class GenerateTask : DefaultTask() { check(specifiedTemplateFile != null) { "Specified templateFileLocation does not exist." } - check(defaultTemplateFile == null) { - "Buf gen template file specified in the project directory as well as with templateFileLocation; pick one." + if (specifiedTemplateFile != defaultTemplateFile) { + check(defaultTemplateFile == null) { + "Buf gen template file specified in the project directory as well as with templateFileLocation; pick one." + } } specifiedTemplateFile } else { diff --git a/src/test/kotlin/build/buf/gradle/AbstractGenerateTest.kt b/src/test/kotlin/build/buf/gradle/AbstractGenerateTest.kt index cb70615e..223f33a5 100644 --- a/src/test/kotlin/build/buf/gradle/AbstractGenerateTest.kt +++ b/src/test/kotlin/build/buf/gradle/AbstractGenerateTest.kt @@ -43,6 +43,11 @@ abstract class AbstractGenerateTest : AbstractBufIntegrationTest() { gradleRunner().withArguments(BUILD_TASK_NAME).build() } + @Test + fun `buf generate with default template file path explicitly specifying`() { + gradleRunner().withArguments(BUILD_TASK_NAME).build() + } + @Test fun `buf generate fails with a nonexistent specified template file`() { val result = gradleRunner().withArguments(BUF_GENERATE_TASK_NAME).buildAndFail() diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml new file mode 100644 index 00000000..02e49114 --- /dev/null +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml @@ -0,0 +1,4 @@ +version: v1 +plugins: + - plugin: buf.build/protocolbuffers/java:v23.4 + out: java diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf/test/v1/test.proto b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf/test/v1/test.proto new file mode 100644 index 00000000..ae9bc374 --- /dev/null +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf/test/v1/test.proto @@ -0,0 +1,21 @@ +// Copyright 2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package buf.test.v1; + +option java_package = "buf.test.v1"; + +message BasicMessage {} diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle new file mode 100644 index 00000000..7d132fd6 --- /dev/null +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle @@ -0,0 +1,24 @@ +plugins { + id 'java' + id 'build.buf' +} + +repositories { + mavenCentral() +} + +buf { + generate { + templateFileLocation = project.file("./buf.gen.yaml") + } +} + +compileJava.dependsOn 'bufGenerate' + +sourceSets.main.java { + srcDir 'build/bufbuild/generated/java' +} + +dependencies { + implementation "com.google.protobuf:protobuf-java:$protobufVersion" +} diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java new file mode 100644 index 00000000..8a037af7 --- /dev/null +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java @@ -0,0 +1,21 @@ +// Copyright 2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package buf.test.v1; + +public class Foo { + public static void test() { + Test.BasicMessage.newBuilder().build(); + } +} diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml new file mode 100644 index 00000000..02e49114 --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.gen.yaml @@ -0,0 +1,4 @@ +version: v1 +plugins: + - plugin: buf.build/protocolbuffers/java:v23.4 + out: java diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.work.yaml b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.work.yaml new file mode 100644 index 00000000..a06f1623 --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.work.yaml @@ -0,0 +1,3 @@ +version: v1 +directories: + - workspace diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle new file mode 100644 index 00000000..7d132fd6 --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle @@ -0,0 +1,24 @@ +plugins { + id 'java' + id 'build.buf' +} + +repositories { + mavenCentral() +} + +buf { + generate { + templateFileLocation = project.file("./buf.gen.yaml") + } +} + +compileJava.dependsOn 'bufGenerate' + +sourceSets.main.java { + srcDir 'build/bufbuild/generated/java' +} + +dependencies { + implementation "com.google.protobuf:protobuf-java:$protobufVersion" +} diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java new file mode 100644 index 00000000..8a037af7 --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/src/main/java/buf/test/v1/Foo.java @@ -0,0 +1,21 @@ +// Copyright 2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package buf.test.v1; + +public class Foo { + public static void test() { + Test.BasicMessage.newBuilder().build(); + } +} diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/workspace/buf/test/v1/test.proto b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/workspace/buf/test/v1/test.proto new file mode 100644 index 00000000..ae9bc374 --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/workspace/buf/test/v1/test.proto @@ -0,0 +1,21 @@ +// Copyright 2023 Buf Technologies, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +package buf.test.v1; + +option java_package = "buf.test.v1"; + +message BasicMessage {} From 0b5d7d418f573e4db191824b8a6fa3e4626fa428 Mon Sep 17 00:00:00 2001 From: "K.Kawahara" Date: Tue, 23 Jul 2024 09:41:33 +0900 Subject: [PATCH 2/4] one line check --- src/main/kotlin/build/buf/gradle/GenerateTask.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/build/buf/gradle/GenerateTask.kt b/src/main/kotlin/build/buf/gradle/GenerateTask.kt index 7ef47092..0c2b825e 100644 --- a/src/main/kotlin/build/buf/gradle/GenerateTask.kt +++ b/src/main/kotlin/build/buf/gradle/GenerateTask.kt @@ -50,10 +50,8 @@ abstract class GenerateTask : DefaultTask() { check(specifiedTemplateFile != null) { "Specified templateFileLocation does not exist." } - if (specifiedTemplateFile != defaultTemplateFile) { - check(defaultTemplateFile == null) { - "Buf gen template file specified in the project directory as well as with templateFileLocation; pick one." - } + check(defaultTemplateFile == null || specifiedTemplateFile == defaultTemplateFile) { + "Buf gen template file specified in the project directory as well as with templateFileLocation; pick one." } specifiedTemplateFile } else { From 10658989033dc9c8a3c80b26d9cda2aef8ad19de Mon Sep 17 00:00:00 2001 From: "K.Kawahara" Date: Tue, 23 Jul 2024 09:42:22 +0900 Subject: [PATCH 3/4] add configration file / buf.yaml --- .../buf.yaml | 1 + .../build.gradle | 1 + .../buf.yaml | 1 + .../build.gradle | 1 + 4 files changed, 4 insertions(+) create mode 100644 src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml create mode 100644 src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml new file mode 100644 index 00000000..5830b70e --- /dev/null +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml @@ -0,0 +1 @@ +version: v1 \ No newline at end of file diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle index 7d132fd6..6667d840 100644 --- a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle @@ -9,6 +9,7 @@ repositories { buf { generate { + configFileLocation = rootProject.file("./buf.yaml") templateFileLocation = project.file("./buf.gen.yaml") } } diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml new file mode 100644 index 00000000..5830b70e --- /dev/null +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml @@ -0,0 +1 @@ +version: v1 \ No newline at end of file diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle index 7d132fd6..6667d840 100644 --- a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/build.gradle @@ -9,6 +9,7 @@ repositories { buf { generate { + configFileLocation = rootProject.file("./buf.yaml") templateFileLocation = project.file("./buf.gen.yaml") } } From f27b5ecceb59a278683da5ac586786ca87b2aa96 Mon Sep 17 00:00:00 2001 From: Dan Rice Date: Tue, 23 Jul 2024 10:53:32 -0400 Subject: [PATCH 4/4] Add newlines to buf.yaml files --- .../buf.yaml | 2 +- .../buf.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml index 5830b70e..c126332f 100644 --- a/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml +++ b/src/test/resources/GenerateTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml @@ -1 +1 @@ -version: v1 \ No newline at end of file +version: v1 diff --git a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml index 5830b70e..c126332f 100644 --- a/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml +++ b/src/test/resources/GenerateWithWorkspaceTest/buf_generate_with_default_template_file_path_explicitly_specifying/buf.yaml @@ -1 +1 @@ -version: v1 \ No newline at end of file +version: v1