Skip to content

Commit 54f72fc

Browse files
authored
lint only provided files (#47)
1 parent 9b468c8 commit 54f72fc

File tree

14 files changed

+123
-62
lines changed

14 files changed

+123
-62
lines changed

src/main/kotlin/com/parmet/buf/gradle/Breaking.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private fun Project.configureBreakingTask(ext: BufExtension, bufBreakingFile: La
9191

9292
group = CHECK_TASK_NAME
9393

94-
bufTask(
94+
buf(
9595
ext,
9696
"breaking",
9797
BUF_BUILD_PUBLICATION_FILENAME,

src/main/kotlin/com/parmet/buf/gradle/BufExtension.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ open class BufExtension {
4444
/**
4545
* Specify the version of Buf.
4646
*/
47-
var toolVersion: String = "1.1.0"
47+
var toolVersion: String = "1.3.1"
4848

4949
internal var imageArtifactDetails: ArtifactDetails? = null
5050

src/main/kotlin/com/parmet/buf/gradle/Build.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const val BUF_IMAGE_PUBLICATION_NAME = "bufImagePublication"
2929

3030
internal fun Project.configureBuild(ext: BufExtension) {
3131
tasks.register<Exec>(BUF_BUILD_TASK_NAME) {
32-
bufTask(ext, "build", "--output", BUF_BUILD_PUBLICATION_FILENAME)
32+
dependsOn(COPY_BUF_CONFIG_TASK_NAME)
33+
buf(ext, "build", "--output", BUF_BUILD_PUBLICATION_FILENAME)
3334
}
3435
}
3536

src/main/kotlin/com/parmet/buf/gradle/Config.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal fun Project.configureCopyBufConfig(ext: BufExtension) {
3030
}
3131
}
3232

33-
private fun Project.bufConfigFile(ext: BufExtension) =
33+
internal fun Project.bufConfigFile(ext: BufExtension) =
3434
project.resolveConfig(ext).let {
3535
if (it != null) {
3636
logger.info("Using buf config from $it")

src/main/kotlin/com/parmet/buf/gradle/DockerSupport.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ package com.parmet.buf.gradle
1717

1818
import org.gradle.api.Project
1919
import org.gradle.api.tasks.Exec
20+
import org.gradle.process.ExecSpec
2021

21-
internal fun Exec.bufTask(ext: BufExtension, vararg args: Any) {
22+
internal fun Exec.buf(ext: BufExtension, vararg args: Any) {
2223
dependsOn(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME)
2324
dependsOn(WRITE_WORKSPACE_YAML_TASK_NAME)
24-
dependsOn(COPY_BUF_CONFIG_TASK_NAME)
25+
buf(project, ext, args.asList())
26+
}
2527

28+
internal fun ExecSpec.buf(project: Project, ext: BufExtension, args: Iterable<Any>) {
2629
commandLine("docker")
27-
setArgs(project.baseDockerArgs(ext) + args)
30+
val dockerArgs = project.baseDockerArgs(ext) + args
31+
setArgs(dockerArgs)
32+
project.logger.debug("Running buf: `docker ${dockerArgs.joinToString(" ")}`")
2833
}
2934

3035
private fun Project.baseDockerArgs(ext: BufExtension) =

src/main/kotlin/com/parmet/buf/gradle/Lint.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ package com.parmet.buf.gradle
1717

1818
import org.gradle.api.Project
1919
import org.gradle.api.plugins.JavaBasePlugin.CHECK_TASK_NAME
20-
import org.gradle.api.tasks.Exec
21-
import org.gradle.kotlin.dsl.register
2220

2321
const val BUF_LINT_TASK_NAME = "bufLint"
2422

2523
internal fun Project.configureLint(ext: BufExtension) {
26-
tasks.register<Exec>(BUF_LINT_TASK_NAME) {
27-
dependsOn(BUF_BUILD_TASK_NAME)
24+
tasks.register(BUF_LINT_TASK_NAME) {
25+
dependsOn(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME)
26+
dependsOn(WRITE_WORKSPACE_YAML_TASK_NAME)
27+
2828
group = CHECK_TASK_NAME
29-
bufTask(ext, "lint", BUF_BUILD_PUBLICATION_FILENAME)
29+
30+
doLast {
31+
srcProtoDirs().forEach {
32+
exec {
33+
val configArgs = bufConfigFile(ext)?.let { listOf("--config", it.readText()) }.orEmpty()
34+
buf(this@configureLint, ext, listOf("lint", mangle(it)) + configArgs)
35+
}
36+
}
37+
}
3038
}
3139

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

src/main/kotlin/com/parmet/buf/gradle/Workspace.kt renamed to src/main/kotlin/com/parmet/buf/gradle/ProtobufGradlePluginSupport.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ const val CREATE_SYM_LINKS_TO_MODULES_TASK_NAME = "createSymLinksToModules"
3030
const val WRITE_WORKSPACE_YAML_TASK_NAME = "writeWorkspaceYaml"
3131

3232
private const val EXTRACT_INCLUDE_PROTO_TASK_NAME = "extractIncludeProto"
33-
private const val BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN = "build/extracted-include-protos/main"
34-
3533
private const val EXTRACT_PROTO_TASK_NAME = "extractProto"
34+
35+
private const val BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN = "build/extracted-include-protos/main"
3636
private const val BUILD_EXTRACTED_PROTOS_MAIN = "build/extracted-protos/main"
3737

3838
internal fun Project.configureCreateSymLinksToModules() {
3939
tasks.register(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME) {
4040
workspaceCommonConfig()
41-
doLast { protoDirs().forEach { createSymLink(it) } }
41+
doLast { allProtoDirs().forEach { createSymLink(it) } }
4242
}
4343
}
4444

@@ -77,20 +77,21 @@ private fun Task.workspaceCommonConfig() {
7777
}
7878

7979
private fun Project.workspaceSymLinkEntries() =
80-
protoDirs().joinToString("\n") { "| - ${mangle(it)}" }
80+
allProtoDirs().joinToString("\n") { "| - ${mangle(it)}" }
8181

82-
private fun Project.protoDirs(): List<Path> =
83-
(srcDirs() + extractDirs()).filter { anyProtos(it) }
82+
fun Project.allProtoDirs(): List<Path> =
83+
(srcProtoDirs() + extractProtoDirs()).filter { anyProtos(it) }
8484

85-
private fun Project.srcDirs() =
85+
fun Project.srcProtoDirs() =
8686
the<SourceSetContainer>()["main"]
8787
.extensions
8888
.getByName("proto")
8989
.let { it as SourceDirectorySet }
9090
.srcDirs
9191
.map { projectDir.toPath().relativize(it.toPath()) }
92+
.filter { anyProtos(it) }
9293

93-
private fun extractDirs() =
94+
private fun extractProtoDirs() =
9495
listOf(
9596
BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN,
9697
BUILD_EXTRACTED_PROTOS_MAIN
@@ -99,5 +100,5 @@ private fun extractDirs() =
99100
private fun Project.anyProtos(path: Path) =
100101
file(path).walkTopDown().any { it.extension == "proto" }
101102

102-
private fun mangle(name: Path) =
103+
fun mangle(name: Path) =
103104
name.toString().replace("-", "--").replace(File.separator, "-")

src/test/kotlin/com/parmet/buf/gradle/BreakingTest.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ class BreakingTest : AbstractBufIntegrationTest() {
3434
checkBreaking()
3535
}
3636

37+
@Test
38+
fun `normally breaking schema with an ignore`() {
39+
publishRunner().build()
40+
41+
breakSchema()
42+
43+
buildFile.replace("//", "")
44+
checkRunner().build()
45+
}
46+
3747
@Test
3848
fun `breaking schema fails with latest-release and previousVersion`() {
3949
val result = checkRunner().buildAndFail()
@@ -52,14 +62,18 @@ class BreakingTest : AbstractBufIntegrationTest() {
5262

5363
buildFile.replace("//", "")
5464

55-
val protoFile = Paths.get(protoDir.path, "parmet", "buf", "test", "v1", "test.proto").toFile()
56-
protoFile.replace("BasicMessage", "BasicMessage2")
65+
breakSchema()
5766

5867
val result = checkRunner().buildAndFail()
5968
assertThat(result.task(":bufBreaking")?.outcome).isEqualTo(FAILED)
6069
assertThat(result.output).contains("Previously present message \"BasicMessage\" was deleted from file.")
6170
}
6271

72+
private fun breakSchema() {
73+
val protoFile = Paths.get(protoDir.path, "parmet", "buf", "test", "v1", "test.proto").toFile()
74+
protoFile.replace("BasicMessage", "BasicMessage2")
75+
}
76+
6377
private fun File.replace(oldValue: String, newValue: String) {
6478
writeText(readText().replace(oldValue, newValue))
6579
}

src/test/resources/BreakingTest/schema with multi-directory workspace/buf.yaml renamed to src/test/resources/BreakingTest/normally breaking schema with an ignore/buf.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
# limitations under the License.
1313

1414
version: v1
15-
lint:
15+
breaking:
1616
ignore:
17-
- google
17+
- parmet
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2022 Andrew Parmet
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+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
plugins {
17+
id 'java'
18+
id 'com.parmet.buf'
19+
id 'com.google.protobuf' version "$protobufGradleVersion"
20+
id 'maven-publish'
21+
}
22+
23+
repositories {
24+
mavenCentral()
25+
maven { url 'build/repos/test' }
26+
}
27+
28+
protobuf {
29+
protoc {
30+
artifact = "com.google.protobuf:protoc:$protobufVersion"
31+
}
32+
}
33+
34+
compileJava.enabled = false
35+
36+
publishing {
37+
repositories {
38+
maven { url 'build/repos/test' }
39+
}
40+
}
41+
42+
buf {
43+
publishSchema = true
44+
//previousVersion = '2319'
45+
46+
imageArtifact {
47+
groupId = 'foo'
48+
artifactId = 'bar'
49+
version = '2319'
50+
}
51+
}

0 commit comments

Comments
 (0)