Skip to content

Commit

Permalink
Allow lint task to be configured after Buf v1.31.0 while using protob…
Browse files Browse the repository at this point in the history
…uf-gradle-plugin (#256)

Gets custom linting working again with new versions of Buf.
  • Loading branch information
andrewparmet authored Nov 15, 2024
1 parent 6d322b4 commit a342ff2
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,20 @@ package build.buf.gradle

import java.io.File

internal fun AbstractBufExecTask.execBufInSpecificDirectory(
vararg bufCommand: String,
customErrorMessage: ((String) -> String)? = null,
) {
execBufInSpecificDirectory(bufCommand.asList(), emptyList(), customErrorMessage)
}

internal fun AbstractBufExecTask.execBufInSpecificDirectory(
bufCommand: String,
extraArgs: Iterable<String>,
customErrorMessage: (String) -> String,
) {
execBufInSpecificDirectory(listOf(bufCommand), extraArgs, customErrorMessage)
}

private fun AbstractBufExecTask.execBufInSpecificDirectory(
bufCommand: Iterable<String>,
extraArgs: Iterable<String>,
args: Iterable<String>,
customErrorMessage: ((String) -> String)? = null,
) {
fun runWithArgs(file: File? = null) = bufCommand + listOfNotNull(file?.let { makeMangledRelativizedPathStr(it) }) + extraArgs
fun runWithArgs(file: File? = null) = listOf(bufCommand) + listOfNotNull(file?.let { makeMangledRelativizedPathStr(it) }) + args

when {
hasProtobufGradlePlugin.get() ->
candidateProtoDirs
.filter { anyProtos(it) }
.forEach { execBuf(runWithArgs(it), customErrorMessage) }
hasWorkspace.get() ->
execBuf(bufCommand, customErrorMessage)
execBuf(listOf(bufCommand) + args, customErrorMessage)
else ->
execBuf(runWithArgs(), customErrorMessage)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/build/buf/gradle/FormatApplyTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ abstract class FormatApplyTask : AbstractBufExecTask() {

@TaskAction
fun bufFormatApply() {
execBufInSpecificDirectory("format", "-w")
execBufInSpecificDirectory("format", listOf("-w"))
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/build/buf/gradle/FormatCheckTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class FormatCheckTask : AbstractBufExecTask() {

@TaskAction
fun bufFormatCheck() {
execBufInSpecificDirectory("format", "-d", "--exit-code") {
execBufInSpecificDirectory("format", listOf("-d", "--exit-code")) {
"""
|Some Protobuf files had format violations:
|$it
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/build/buf/gradle/LintConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal fun Project.configureLint() {

bufConfigFile.set(project.bufConfigFile())
inputFiles.setFrom(obtainDefaultProtoFileSet())
v1SyntaxOnly.set(bufV1SyntaxOnly())
}

tasks.named(CHECK_TASK_NAME).dependsOn(BUF_LINT_TASK_NAME)
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/build/buf/gradle/LintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package build.buf.gradle

import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
Expand All @@ -34,11 +35,18 @@ abstract class LintTask : AbstractBufExecTask() {
@get:Optional
internal abstract val bufConfigFile: Property<File>

@get:Input
internal abstract val v1SyntaxOnly: Property<Boolean>

@TaskAction
fun bufLint() {
execBufInSpecificDirectory(
"lint",
bufConfigFile.orNull?.let { listOf("--config", it.readAndStripComments()) }.orEmpty(),
if (noWorkspaceAndV1Syntax() || noWorkspaceAndNoProtobufGradlePlugin()) {
bufConfigFile.orNull?.let { listOf("--config", it.readAndStripComments()) }
} else {
null
}.orEmpty(),
) {
"""
|Some Protobuf files had lint violations:
Expand All @@ -47,6 +55,10 @@ abstract class LintTask : AbstractBufExecTask() {
}
}

private fun noWorkspaceAndV1Syntax() = !hasWorkspace.get() && v1SyntaxOnly.get()

private fun noWorkspaceAndNoProtobufGradlePlugin() = !hasWorkspace.get() && !hasProtobufGradlePlugin.get()

private fun File.readAndStripComments() =
lines(toPath()).use { lines ->
lines.asSequence()
Expand Down
12 changes: 11 additions & 1 deletion src/test/kotlin/build/buf/gradle/LintWithProtobufGradleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@ class LintWithProtobufGradleTest : ConfigOverrideableLintTests, AbstractLintTest
}

@Test
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin`() {
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin pre 1_32_0`() {
assertSuccess()
}

@Test
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin v1`() {
assertSuccess()
}

@Test
fun `lint a file with an implementation dependency and a lint config with the protobuf-gradle-plugin v2`() {
assertSuccess()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
lint:
ignore:
- google
- protokt
use:
- STANDARD
except:
- ENUM_ZERO_VALUE_SUFFIX
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'java'
id 'com.google.protobuf' version "$protobufGradleVersion"
id 'build.buf'
}

repositories {
mavenCentral()
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
}

compileJava.enabled = false

dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
implementation "com.toasttab.protokt:protokt-runtime:0.6.5"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 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;

import "protokt/protokt.proto";

message BasicMessage {
protokt.ProtoktFileOptions protokt_file_options = 1;

enum BrokenEnum {
BROKEN_ENUM_NONSPECIFIED = 0; // should be _UNSPECIFIED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v2
lint:
ignore:
- google
- protokt
use:
- STANDARD
except:
- ENUM_ZERO_VALUE_SUFFIX
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'java'
id 'com.google.protobuf' version "$protobufGradleVersion"
id 'build.buf'
}

repositories {
mavenCentral()
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
}

compileJava.enabled = false

dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
implementation "com.toasttab.protokt:protokt-runtime:0.6.5"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 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;

import "protokt/protokt.proto";

message BasicMessage {
protokt.ProtoktFileOptions protokt_file_options = 1;

enum BrokenEnum {
BROKEN_ENUM_NONSPECIFIED = 0; // should be _UNSPECIFIED
}
}

0 comments on commit a342ff2

Please sign in to comment.