Skip to content

Commit

Permalink
allow --include-imports in buf generate via plugin (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewparmet authored Sep 16, 2022
1 parent dda437a commit 8912a0c
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ publishing {

Projects with Buf workspaces must configure their workspaces as described in the Buf documentation; no configuration for linting will be overrideable. A `buf.yaml` in the project root or specified in the extension will be used for breakage checks only.

### Dependencies

If your `buf.yaml` declares any dependencies using the `deps` key, you must run `buf mod update` to create a `buf.lock` file manually. The `buf-gradle-plugin` does not (yet) support creating the dependency lock file.

### `bufFormatApply` and `bufFormatCheck`

`bufFormatApply` is run manually and has no configuration.
Expand Down Expand Up @@ -224,6 +228,22 @@ dependencies {
}
```

#### Generating Dependencies

If you'd like to generate code for your dependencies, configure the `bufGenerate` task:

``` kotlin
// build.gradle.kts

buf {
generate {
includeImports = true
}
}
```

Ensure you have an up-to-date `buf.lock` file generated by `buf mod update` or this generation will fail.

## Additional Configuration

The version of Buf used can be configured using the `toolVersion` property on the extension:
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/com/parmet/buf/gradle/BufExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,27 @@ open class BufExtension {
fun imageArtifact(configure: Action<ArtifactDetails>) {
imageArtifactDetails = (imageArtifactDetails ?: ArtifactDetails()).apply(configure::execute)
}

internal var generateOptions: GenerateOptions? = null

/**
* Generate code using `buf generate`. Configure any options with the provided [configure] closure.
*/
fun generate(configure: Action<GenerateOptions>) {
generateOptions = (generateOptions ?: GenerateOptions()).apply(configure::execute)
}
}

class ArtifactDetails(
var groupId: String? = null,
var artifactId: String? = null,
var version: String? = null
)

class GenerateOptions(
/**
* If you specify any dependencies in `buf.yaml`, you must create a `buf.lock` file using `buf mod update` for
* dependency resolution to succeed.
*/
var includeImports: Boolean? = null
)
11 changes: 10 additions & 1 deletion src/main/kotlin/com/parmet/buf/gradle/Generate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ internal fun Project.configureGenerate() {
description = "Generates code from a Protobuf schema."

createsOutput()
execBuf("generate", "--output", File(bufbuildDir, GENERATED_DIR))

val args = listOf("generate", "--output", File(bufbuildDir, GENERATED_DIR))
execBuf(args + additionalArgs())
}
}
}

private fun Project.hasGenerate() =
file("buf.gen.yaml").let { it.exists() && it.isFile }

private fun Project.additionalArgs() =
if (getExtension().generateOptions?.includeImports == true) {
listOf("--include-imports")
} else {
emptyList()
}
10 changes: 10 additions & 0 deletions src/test/kotlin/com/parmet/buf/gradle/AbstractGenerateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

package com.parmet.buf.gradle

import com.google.common.truth.Truth.assertThat
import org.gradle.language.base.plugins.LifecycleBasePlugin.BUILD_TASK_NAME
import org.junit.jupiter.api.Test
import java.nio.file.Paths

abstract class AbstractGenerateTest : AbstractBufIntegrationTest() {
@Test
Expand All @@ -28,4 +30,12 @@ abstract class AbstractGenerateTest : AbstractBufIntegrationTest() {
fun `generate java with kotlin dsl`() {
gradleRunner().withArguments(BUILD_TASK_NAME).build()
}

@Test
fun `generate java with --include-imports`() {
gradleRunner().withArguments(BUILD_TASK_NAME).build()
val generatedPathElements =
listOf("build", "bufbuild", "generated", "java", "com", "google", "type", "DateTime.java")
assertThat(Paths.get(projectDir.absolutePath, *generatedPathElements.toTypedArray()).toFile().exists()).isTrue()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.

version: v1
plugins:
- remote: buf.build/protocolbuffers/plugins/java:v3.20.0-1
out: java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 62f35d8aed1149c291d606d958a7ce32
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
deps:
- buf.build/googleapis/googleapis
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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'
}

repositories { mavenCentral() }

buf {
generate {
includeImports = true
}
}

compileJava.dependsOn 'bufGenerate'

sourceSets.main.java {
srcDir 'build/bufbuild/generated/java'
}

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

import "google/type/datetime.proto";

option java_package = "com.parmet.buf.test.v1";

message BasicMessage {
google.type.DateTime date_time = 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

package com.parmet.buf.test.v1;

import com.google.type.DateTime;

public class Foo {
public static void test() {
Test.BasicMessage.newBuilder().setDateTime(DateTime.newBuilder().build()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.

version: v1
plugins:
- remote: buf.build/protocolbuffers/plugins/java:v3.20.0-1
out: java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

version: v1
directories:
- workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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'
}

repositories { mavenCentral() }

buf {
generate {
includeImports = true
}
}

compileJava.dependsOn 'bufGenerate'

sourceSets.main.java {
srcDir 'build/bufbuild/generated/java'
}

dependencies {
implementation "com.google.protobuf:protobuf-java:$protobufVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

package com.parmet.buf.test.v1;

import com.google.type.DateTime;

public class Foo {
public static void test() {
Test.BasicMessage.newBuilder().setDateTime(DateTime.newBuilder().build()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 62f35d8aed1149c291d606d958a7ce32
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v1
deps:
- buf.build/googleapis/googleapis
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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;

import "google/type/datetime.proto";

option java_package = "com.parmet.buf.test.v1";

message BasicMessage {
google.type.DateTime date_time = 1;
}

0 comments on commit 8912a0c

Please sign in to comment.