Skip to content

Commit a342ff2

Browse files
authored
Allow lint task to be configured after Buf v1.31.0 while using protobuf-gradle-plugin (#256)
Gets custom linting working again with new versions of Buf.
1 parent 6d322b4 commit a342ff2

File tree

15 files changed

+146
-22
lines changed

15 files changed

+146
-22
lines changed

src/main/kotlin/build/buf/gradle/DirectorySpecificBufExecutionSupport.kt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,20 @@ package build.buf.gradle
1616

1717
import java.io.File
1818

19-
internal fun AbstractBufExecTask.execBufInSpecificDirectory(
20-
vararg bufCommand: String,
21-
customErrorMessage: ((String) -> String)? = null,
22-
) {
23-
execBufInSpecificDirectory(bufCommand.asList(), emptyList(), customErrorMessage)
24-
}
25-
2619
internal fun AbstractBufExecTask.execBufInSpecificDirectory(
2720
bufCommand: String,
28-
extraArgs: Iterable<String>,
29-
customErrorMessage: (String) -> String,
30-
) {
31-
execBufInSpecificDirectory(listOf(bufCommand), extraArgs, customErrorMessage)
32-
}
33-
34-
private fun AbstractBufExecTask.execBufInSpecificDirectory(
35-
bufCommand: Iterable<String>,
36-
extraArgs: Iterable<String>,
21+
args: Iterable<String>,
3722
customErrorMessage: ((String) -> String)? = null,
3823
) {
39-
fun runWithArgs(file: File? = null) = bufCommand + listOfNotNull(file?.let { makeMangledRelativizedPathStr(it) }) + extraArgs
24+
fun runWithArgs(file: File? = null) = listOf(bufCommand) + listOfNotNull(file?.let { makeMangledRelativizedPathStr(it) }) + args
4025

4126
when {
4227
hasProtobufGradlePlugin.get() ->
4328
candidateProtoDirs
4429
.filter { anyProtos(it) }
4530
.forEach { execBuf(runWithArgs(it), customErrorMessage) }
4631
hasWorkspace.get() ->
47-
execBuf(bufCommand, customErrorMessage)
32+
execBuf(listOf(bufCommand) + args, customErrorMessage)
4833
else ->
4934
execBuf(runWithArgs(), customErrorMessage)
5035
}

src/main/kotlin/build/buf/gradle/FormatApplyTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ abstract class FormatApplyTask : AbstractBufExecTask() {
3030

3131
@TaskAction
3232
fun bufFormatApply() {
33-
execBufInSpecificDirectory("format", "-w")
33+
execBufInSpecificDirectory("format", listOf("-w"))
3434
}
3535
}

src/main/kotlin/build/buf/gradle/FormatCheckTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class FormatCheckTask : AbstractBufExecTask() {
2525

2626
@TaskAction
2727
fun bufFormatCheck() {
28-
execBufInSpecificDirectory("format", "-d", "--exit-code") {
28+
execBufInSpecificDirectory("format", listOf("-d", "--exit-code")) {
2929
"""
3030
|Some Protobuf files had format violations:
3131
|$it

src/main/kotlin/build/buf/gradle/LintConfiguration.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal fun Project.configureLint() {
2727

2828
bufConfigFile.set(project.bufConfigFile())
2929
inputFiles.setFrom(obtainDefaultProtoFileSet())
30+
v1SyntaxOnly.set(bufV1SyntaxOnly())
3031
}
3132

3233
tasks.named(CHECK_TASK_NAME).dependsOn(BUF_LINT_TASK_NAME)

src/main/kotlin/build/buf/gradle/LintTask.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package build.buf.gradle
1616

1717
import org.gradle.api.file.ConfigurableFileCollection
1818
import org.gradle.api.provider.Property
19+
import org.gradle.api.tasks.Input
1920
import org.gradle.api.tasks.InputFile
2021
import org.gradle.api.tasks.InputFiles
2122
import org.gradle.api.tasks.Optional
@@ -34,11 +35,18 @@ abstract class LintTask : AbstractBufExecTask() {
3435
@get:Optional
3536
internal abstract val bufConfigFile: Property<File>
3637

38+
@get:Input
39+
internal abstract val v1SyntaxOnly: Property<Boolean>
40+
3741
@TaskAction
3842
fun bufLint() {
3943
execBufInSpecificDirectory(
4044
"lint",
41-
bufConfigFile.orNull?.let { listOf("--config", it.readAndStripComments()) }.orEmpty(),
45+
if (noWorkspaceAndV1Syntax() || noWorkspaceAndNoProtobufGradlePlugin()) {
46+
bufConfigFile.orNull?.let { listOf("--config", it.readAndStripComments()) }
47+
} else {
48+
null
49+
}.orEmpty(),
4250
) {
4351
"""
4452
|Some Protobuf files had lint violations:
@@ -47,6 +55,10 @@ abstract class LintTask : AbstractBufExecTask() {
4755
}
4856
}
4957

58+
private fun noWorkspaceAndV1Syntax() = !hasWorkspace.get() && v1SyntaxOnly.get()
59+
60+
private fun noWorkspaceAndNoProtobufGradlePlugin() = !hasWorkspace.get() && !hasProtobufGradlePlugin.get()
61+
5062
private fun File.readAndStripComments() =
5163
lines(toPath()).use { lines ->
5264
lines.asSequence()

src/test/kotlin/build/buf/gradle/LintWithProtobufGradleTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ class LintWithProtobufGradleTest : ConfigOverrideableLintTests, AbstractLintTest
4343
}
4444

4545
@Test
46-
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin`() {
46+
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin pre 1_32_0`() {
47+
assertSuccess()
48+
}
49+
50+
@Test
51+
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin v1`() {
52+
assertSuccess()
53+
}
54+
55+
@Test
56+
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin v2`() {
4757
assertSuccess()
4858
}
4959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: v1
2+
lint:
3+
ignore:
4+
- google
5+
- protokt
6+
use:
7+
- STANDARD
8+
except:
9+
- ENUM_ZERO_VALUE_SUFFIX

0 commit comments

Comments
 (0)