Skip to content

Commit

Permalink
lint only provided files (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet authored Apr 9, 2022
1 parent 9b468c8 commit 54f72fc
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/parmet/buf/gradle/Breaking.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private fun Project.configureBreakingTask(ext: BufExtension, bufBreakingFile: La

group = CHECK_TASK_NAME

bufTask(
buf(
ext,
"breaking",
BUF_BUILD_PUBLICATION_FILENAME,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/parmet/buf/gradle/BufExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class BufExtension {
/**
* Specify the version of Buf.
*/
var toolVersion: String = "1.1.0"
var toolVersion: String = "1.3.1"

internal var imageArtifactDetails: ArtifactDetails? = null

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/parmet/buf/gradle/Build.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const val BUF_IMAGE_PUBLICATION_NAME = "bufImagePublication"

internal fun Project.configureBuild(ext: BufExtension) {
tasks.register<Exec>(BUF_BUILD_TASK_NAME) {
bufTask(ext, "build", "--output", BUF_BUILD_PUBLICATION_FILENAME)
dependsOn(COPY_BUF_CONFIG_TASK_NAME)
buf(ext, "build", "--output", BUF_BUILD_PUBLICATION_FILENAME)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/parmet/buf/gradle/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal fun Project.configureCopyBufConfig(ext: BufExtension) {
}
}

private fun Project.bufConfigFile(ext: BufExtension) =
internal fun Project.bufConfigFile(ext: BufExtension) =
project.resolveConfig(ext).let {
if (it != null) {
logger.info("Using buf config from $it")
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/com/parmet/buf/gradle/DockerSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ package com.parmet.buf.gradle

import org.gradle.api.Project
import org.gradle.api.tasks.Exec
import org.gradle.process.ExecSpec

internal fun Exec.bufTask(ext: BufExtension, vararg args: Any) {
internal fun Exec.buf(ext: BufExtension, vararg args: Any) {
dependsOn(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME)
dependsOn(WRITE_WORKSPACE_YAML_TASK_NAME)
dependsOn(COPY_BUF_CONFIG_TASK_NAME)
buf(project, ext, args.asList())
}

internal fun ExecSpec.buf(project: Project, ext: BufExtension, args: Iterable<Any>) {
commandLine("docker")
setArgs(project.baseDockerArgs(ext) + args)
val dockerArgs = project.baseDockerArgs(ext) + args
setArgs(dockerArgs)
project.logger.debug("Running buf: `docker ${dockerArgs.joinToString(" ")}`")
}

private fun Project.baseDockerArgs(ext: BufExtension) =
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/com/parmet/buf/gradle/Lint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ package com.parmet.buf.gradle

import org.gradle.api.Project
import org.gradle.api.plugins.JavaBasePlugin.CHECK_TASK_NAME
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.register

const val BUF_LINT_TASK_NAME = "bufLint"

internal fun Project.configureLint(ext: BufExtension) {
tasks.register<Exec>(BUF_LINT_TASK_NAME) {
dependsOn(BUF_BUILD_TASK_NAME)
tasks.register(BUF_LINT_TASK_NAME) {
dependsOn(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME)
dependsOn(WRITE_WORKSPACE_YAML_TASK_NAME)

group = CHECK_TASK_NAME
bufTask(ext, "lint", BUF_BUILD_PUBLICATION_FILENAME)

doLast {
srcProtoDirs().forEach {
exec {
val configArgs = bufConfigFile(ext)?.let { listOf("--config", it.readText()) }.orEmpty()
buf(this@configureLint, ext, listOf("lint", mangle(it)) + configArgs)
}
}
}
}

tasks.named(CHECK_TASK_NAME).dependsOn(BUF_LINT_TASK_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const val CREATE_SYM_LINKS_TO_MODULES_TASK_NAME = "createSymLinksToModules"
const val WRITE_WORKSPACE_YAML_TASK_NAME = "writeWorkspaceYaml"

private const val EXTRACT_INCLUDE_PROTO_TASK_NAME = "extractIncludeProto"
private const val BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN = "build/extracted-include-protos/main"

private const val EXTRACT_PROTO_TASK_NAME = "extractProto"

private const val BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN = "build/extracted-include-protos/main"
private const val BUILD_EXTRACTED_PROTOS_MAIN = "build/extracted-protos/main"

internal fun Project.configureCreateSymLinksToModules() {
tasks.register(CREATE_SYM_LINKS_TO_MODULES_TASK_NAME) {
workspaceCommonConfig()
doLast { protoDirs().forEach { createSymLink(it) } }
doLast { allProtoDirs().forEach { createSymLink(it) } }
}
}

Expand Down Expand Up @@ -77,20 +77,21 @@ private fun Task.workspaceCommonConfig() {
}

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

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

private fun Project.srcDirs() =
fun Project.srcProtoDirs() =
the<SourceSetContainer>()["main"]
.extensions
.getByName("proto")
.let { it as SourceDirectorySet }
.srcDirs
.map { projectDir.toPath().relativize(it.toPath()) }
.filter { anyProtos(it) }

private fun extractDirs() =
private fun extractProtoDirs() =
listOf(
BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN,
BUILD_EXTRACTED_PROTOS_MAIN
Expand All @@ -99,5 +100,5 @@ private fun extractDirs() =
private fun Project.anyProtos(path: Path) =
file(path).walkTopDown().any { it.extension == "proto" }

private fun mangle(name: Path) =
fun mangle(name: Path) =
name.toString().replace("-", "--").replace(File.separator, "-")
18 changes: 16 additions & 2 deletions src/test/kotlin/com/parmet/buf/gradle/BreakingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class BreakingTest : AbstractBufIntegrationTest() {
checkBreaking()
}

@Test
fun `normally breaking schema with an ignore`() {
publishRunner().build()

breakSchema()

buildFile.replace("//", "")
checkRunner().build()
}

@Test
fun `breaking schema fails with latest-release and previousVersion`() {
val result = checkRunner().buildAndFail()
Expand All @@ -52,14 +62,18 @@ class BreakingTest : AbstractBufIntegrationTest() {

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

val protoFile = Paths.get(protoDir.path, "parmet", "buf", "test", "v1", "test.proto").toFile()
protoFile.replace("BasicMessage", "BasicMessage2")
breakSchema()

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

private fun breakSchema() {
val protoFile = Paths.get(protoDir.path, "parmet", "buf", "test", "v1", "test.proto").toFile()
protoFile.replace("BasicMessage", "BasicMessage2")
}

private fun File.replace(oldValue: String, newValue: String) {
writeText(readText().replace(oldValue, newValue))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# limitations under the License.

version: v1
lint:
breaking:
ignore:
- google
- parmet
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Andrew Parmet
*
* 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.
*/

plugins {
id 'java'
id 'com.parmet.buf'
id 'com.google.protobuf' version "$protobufGradleVersion"
id 'maven-publish'
}

repositories {
mavenCentral()
maven { url 'build/repos/test' }
}

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

compileJava.enabled = false

publishing {
repositories {
maven { url 'build/repos/test' }
}
}

buf {
publishSchema = true
//previousVersion = '2319'

imageArtifact {
groupId = 'foo'
artifactId = 'bar'
version = '2319'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 Andrew Parmet
//
// 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 parmet.buf.test.v1;

message BasicMessage {}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

version: v1
lint:
ignore:
- google
use:
- DEFAULT
except:
Expand Down

0 comments on commit 54f72fc

Please sign in to comment.