Skip to content

Commit 406a351

Browse files
feat: add javaPackageSuffix gradle plugin parameter (#442)
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com> Signed-off-by: anthony-swirldslabs <152534762+anthony-swirldslabs@users.noreply.github.com> Co-authored-by: Joseph S. <121976561+jsync-swirlds@users.noreply.github.com>
1 parent 828c522 commit 406a351

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/PbjCompilerPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public abstract class PbjCompilerPlugin implements Plugin<Project> {
2929
*/
3030
@Override
3131
public void apply(Project project) {
32+
// Register the PbjExtension to fetch optional parameters from Gradle files
33+
final PbjExtension pbj = project.getExtensions().create("pbj", PbjExtension.class);
34+
3235
// get reference to java plugin
3336
final var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class);
3437
// get java src sets
@@ -61,6 +64,7 @@ public void apply(Project project) {
6164
pbjTask.setSource(pbjSourceSet);
6265
pbjTask.getJavaMainOutputDirectory().set(outputDirectoryMain);
6366
pbjTask.getJavaTestOutputDirectory().set(outputDirectoryTest);
67+
pbjTask.getJavaPackageSuffix().set(pbj.getJavaPackageSuffix());
6468
});
6569

6670
// 5) register fact that pbj should be run before compiling by informing the 'java' part

pbj-core/pbj-compiler/src/main/java/com/hedera/pbj/compiler/PbjCompilerTask.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import javax.inject.Inject;
55
import org.gradle.api.file.DirectoryProperty;
66
import org.gradle.api.internal.file.FileOperations;
7+
import org.gradle.api.provider.Property;
8+
import org.gradle.api.tasks.Input;
9+
import org.gradle.api.tasks.Optional;
710
import org.gradle.api.tasks.OutputDirectory;
811
import org.gradle.api.tasks.SourceTask;
912
import org.gradle.api.tasks.TaskAction;
@@ -33,6 +36,11 @@ public abstract class PbjCompilerTask extends SourceTask {
3336
@Inject
3437
protected abstract FileOperations getFileOperations();
3538

39+
/** An optional Java package suffix for PBJ-generated classes when `pbj.java_package` is missing. */
40+
@Optional
41+
@Input
42+
public abstract Property<String> getJavaPackageSuffix();
43+
3644
/**
3745
* Perform task action - Generates all the PBJ java source files
3846
*
@@ -46,6 +54,6 @@ public void perform() throws Exception {
4654
getSource(),
4755
getJavaMainOutputDirectory().get().getAsFile(),
4856
getJavaTestOutputDirectory().get().getAsFile(),
49-
null);
57+
getJavaPackageSuffix().getOrNull());
5058
}
5159
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.pbj.compiler;
3+
4+
import org.gradle.api.provider.Property;
5+
6+
/** A Gradle extension to pass parameters to the PbjCompilerPlugin. */
7+
public interface PbjExtension {
8+
/**
9+
* An optional suffix to append to Java package names of PBJ-generated classes
10+
* when the Protobuf model is missing an explicit `pbj.java_package` option and PBJ has to
11+
* derive the Java package name from the standard `java_package` option or otherwise.
12+
* @return the suffix property
13+
*/
14+
Property<String> getJavaPackageSuffix();
15+
}

pbj-core/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.0-SNAPSHOT
1+
0.10.1-SNAPSHOT

pbj-integration-tests/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ dependencyAnalysis { issues { all { onAny { exclude("com.hedera.pbj:pbj-compiler
6161
// IMPROVE: JMH code should not depend on test code
6262
jmh { includeTests = true }
6363

64+
// Avoid a clash with Google protoc models when .proto files don't specify `pbj.java_package`:
65+
pbj { javaPackageSuffix = ".pbj.integration.tests" }
66+
6467
// Add downloaded HAPI repo protobuf files into build directory and add to sources to build them
6568
val cloneHederaProtobufs =
6669
tasks.register<GitClone>("cloneHederaProtobufs") {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
syntax = "proto3";
3+
4+
package proto;
5+
6+
// Test when only the java_package is specified. Do not specify the pbj.java_package option.
7+
option java_package = "com.hedera.pbj.test.proto";
8+
option java_multiple_files = true;
9+
10+
/**
11+
* Sample protobuf.
12+
*/
13+
message MessageInAPackage {
14+
bytes bytesField = 1;
15+
}

0 commit comments

Comments
 (0)