Skip to content

Commit

Permalink
configure properly when protobuf plugin is applied after buf plugin (#63
Browse files Browse the repository at this point in the history
)
  • Loading branch information
andrewparmet authored May 6, 2022
1 parent 635bea0 commit 57f00af
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
54 changes: 25 additions & 29 deletions src/main/kotlin/com/parmet/buf/gradle/BufPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,11 @@ class BufPlugin : Plugin<Project> {
with(project) {
createExtension()
configurations.create(BUF_CONFIGURATION_NAME)

if (hasProtobufGradlePlugin()) {
failForWorkspaceAndPlugin()
afterEvaluate { configureBufWithProtobufGradle() }
} else {
withProtobufGradlePlugin { failForWorkspaceAndPlugin() }
configureBuf()
}
configureBuf()
withProtobufGradlePlugin { configureBufWithProtobufGradle() }
}
}

private fun Project.failForWorkspaceAndPlugin() {
check(!hasWorkspace()) {
"""
A project cannot use both the protobuf-gradle-plugin and a Buf workspace.
If you have multiple protobuf source directories and you would like to
use the protobuf-gradle-plugin, configure the protobuf-gradle-plugin to use
those directories as source directories in the appropriate source set. If you
would like to use a Buf workspace, you must configure dependency resolution and
code generation using Buf. There is no (easy) way to reconcile the two
configurations for linting, breakage, and code generation steps.
""".trimIndent().replace('\n', ' ')
}
}

private fun Project.configureBufWithProtobufGradle() {
configureCreateSymLinksToModules()
configureCopyBufConfig()
configureWriteWorkspaceYaml()
configureBuf()
}

private fun Project.configureBuf() {
configureBufDependency()
configureLint()
Expand All @@ -74,4 +47,27 @@ class BufPlugin : Plugin<Project> {
}
}
}

private fun Project.configureBufWithProtobufGradle() {
failForWorkspaceAndPlugin()
afterEvaluate {
configureCreateSymLinksToModules()
configureCopyBufConfig()
configureWriteWorkspaceYaml()
}
}

private fun Project.failForWorkspaceAndPlugin() {
check(!hasWorkspace()) {
"""
A project cannot use both the protobuf-gradle-plugin and a Buf workspace.
If you have multiple protobuf source directories and you would like to
use the protobuf-gradle-plugin, configure the protobuf-gradle-plugin to use
those directories as source directories in the appropriate source set. If you
would like to use a Buf workspace, you must configure dependency resolution and
code generation using Buf. There is no (easy) way to reconcile the two
configurations for linting, breakage, and code generation steps.
""".trimIndent().replace('\n', ' ')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package com.parmet.buf.gradle
import org.junit.jupiter.api.Test

class LintWithProtobufGradleTest : ConfigOverrideableLintTests, AbstractLintTest() {
@Test
fun `lint with protobuf plugin applied after buf plugin`() {
assertSuccess()
}

@Test
fun `lint a separate protobuf source directory through the protobuf-gradle-plugin`() {
assertSuccess()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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"
}

repositories { mavenCentral() }

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

compileJava.enabled = false
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 {}

0 comments on commit 57f00af

Please sign in to comment.