Skip to content

Commit

Permalink
Options for generating gRPC descriptor set
Browse files Browse the repository at this point in the history
  • Loading branch information
edeandrea committed Nov 7, 2023
1 parent 0741b56 commit 1fb9e23
Show file tree
Hide file tree
Showing 50 changed files with 994 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class CodeGenContext {
private final Path outDir;
private final Path workDir;
private final Path inputDir;
private final Path projectDir;
private final boolean redirectIO;
private final Config config;
private final boolean test;
Expand All @@ -25,16 +26,18 @@ public class CodeGenContext {
* @param outDir target directory for the generated output
* @param workDir working directory, typically the main build directory of the project
* @param inputDir directory containing input content for a code generator
* @param projectDir the base project directory
* @param redirectIO whether the code generating process should redirect its IO
* @param config application build time configuration
* @param test indicates whether the code generation is being triggered for tests
*/
public CodeGenContext(ApplicationModel model, Path outDir, Path workDir, Path inputDir, boolean redirectIO,
public CodeGenContext(ApplicationModel model, Path outDir, Path workDir, Path inputDir, Path projectDir, boolean redirectIO,
Config config, boolean test) {
this.model = model;
this.outDir = outDir;
this.workDir = workDir;
this.inputDir = inputDir;
this.projectDir = projectDir;
this.redirectIO = redirectIO;
this.config = config;
this.test = test;
Expand Down Expand Up @@ -62,6 +65,15 @@ public Path outDir() {
return outDir;
}

/**
* The base project directory
*
* @return The base project directory
*/
public Path projectDir() {
return projectDir;
}

/**
* Working directory, typically the main build directory of the project.
* For a typical Maven project it would be the {@code target} directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public class CodeGenerator {
// used by Gradle and Maven
public static void initAndRun(QuarkusClassLoader classLoader,
PathCollection sourceParentDirs, Path generatedSourcesDir, Path buildDir,
Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties,
Path projectDir, Consumer<Path> sourceRegistrar, ApplicationModel appModel, Properties properties,
String launchMode, boolean test) throws CodeGenException {

Map<String, String> props = new HashMap<>();
properties.entrySet().stream().forEach(e -> props.put((String) e.getKey(), (String) e.getValue()));
final List<CodeGenData> generators = init(appModel, props, classLoader, sourceParentDirs, generatedSourcesDir, buildDir,
sourceRegistrar);
projectDir, sourceRegistrar);
if (generators.isEmpty()) {
return;
}
Expand All @@ -86,6 +87,7 @@ private static List<CodeGenData> init(
PathCollection sourceParentDirs,
Path generatedSourcesDir,
Path buildDir,
Path projectDir,
Consumer<Path> sourceRegistrar) throws CodeGenException {
return callWithClassloader(deploymentClassLoader, () -> {
final List<CodeGenProvider> codeGenProviders = loadCodeGenProviders(deploymentClassLoader);
Expand All @@ -102,7 +104,7 @@ private static List<CodeGenData> init(
in = sourceParentDir.resolve(provider.inputDirectory());
}
result.add(
new CodeGenData(provider, outputDir, in, buildDir));
new CodeGenData(provider, outputDir, in, buildDir, projectDir));
}
}
return result;
Expand Down Expand Up @@ -138,7 +140,7 @@ public static List<CodeGenData> init(ApplicationModel model, Map<String, String>
}
codeGens.add(
new CodeGenData(provider, outputDir, in,
Path.of(module.getTargetDir())));
Path.of(module.getTargetDir()), Path.of(module.getProjectDirectory())));
}

}
Expand Down Expand Up @@ -202,7 +204,8 @@ public static boolean trigger(ClassLoader deploymentClassLoader,
CodeGenProvider provider = data.provider;
return provider.shouldRun(data.sourceDir, config)
&& provider.trigger(
new CodeGenContext(appModel, data.outPath, data.buildDir, data.sourceDir, data.redirectIO, config,
new CodeGenContext(appModel, data.outPath, data.buildDir, data.sourceDir, data.projectDir,
data.redirectIO, config,
test));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,35 @@ public class CodeGenData {
public final Path outPath;
public final Path sourceDir;
public final Path buildDir;
public final Path projectDir;
public boolean redirectIO;

/**
* @param provider code gen provider
* @param outPath where the generated output should be stored
* @param sourceDir where the input sources are
* @param buildDir base project output directory
* @param projectDir project root directory
*/
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir) {
this(provider, outPath, sourceDir, buildDir, true);
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, Path projectDir) {
this(provider, outPath, sourceDir, buildDir, projectDir, true);
}

/**
* @param provider code gen provider
* @param outPath where the generated output should be stored
* @param sourceDir where the input sources are
* @param buildDir base project output directory
* @param projectDir project root directory
* @param redirectIO whether to redirect IO, in case a provider is logging something
*/
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, boolean redirectIO) {
public CodeGenData(CodeGenProvider provider, Path outPath, Path sourceDir, Path buildDir, Path projectDir,
boolean redirectIO) {
this.provider = provider;
this.outPath = outPath;
this.sourceDir = sourceDir;
this.buildDir = buildDir.normalize();
this.projectDir = projectDir.normalize();
this.redirectIO = redirectIO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void generateCode() {
params.getBuildSystemProperties().putAll(configMap);
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(buildDir);
params.getProjectDirectory().set(projectDir);
params.getAppModel().set(appModel);
params
.getSourceDirectories()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void execute() {

ResolvedDependency appArtifact = params.getAppModel().get().getAppArtifact();
Path buildDir = params.getTargetDirectory().getAsFile().get().toPath();
Path projectDir = params.getProjectDirectory().getAsFile().get().toPath();
Path generatedSourceDir = params.getOutputPath().get().getAsFile().toPath();

String gav = appArtifact.getGroupId() + ":" + appArtifact.getArtifactId() + ":" + appArtifact.getVersion();
Expand All @@ -42,6 +43,7 @@ public void execute() {
LOGGER.info(" base name: {}", params.getBaseName().get());
LOGGER.info(" generated source directory: {}", generatedSourceDir);
LOGGER.info(" build directory: {}", buildDir);
LOGGER.info(" project directory: {}", projectDir);

try (CuratedApplication appCreationContext = createAppCreationContext()) {

Expand All @@ -51,7 +53,7 @@ public void execute() {
Method initAndRun;
try {
initAndRun = codeGenerator.getMethod(INIT_AND_RUN, QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class,
Path.class, Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
} catch (Exception e) {
Expand All @@ -73,6 +75,8 @@ public void execute() {
generatedSourceDir,
// Path buildDir,
buildDir,
// Path projectDir
projectDir,
// Consumer<Path> sourceRegistrar,
sourceRegistrar,
// ApplicationModel appModel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface CodeGenWorkerParams extends QuarkusParams {
DirectoryProperty getOutputPath();

Property<LaunchMode> getLaunchMode();

DirectoryProperty getProjectDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ void generateCode(PathCollection sourceParents,

final Class<?> codeGenerator = deploymentClassLoader.loadClass("io.quarkus.deployment.CodeGenerator");
final Method initAndRun = codeGenerator.getMethod("initAndRun", QuarkusClassLoader.class, PathCollection.class,
Path.class, Path.class,
Path.class, Path.class, Path.class,
Consumer.class, ApplicationModel.class, Properties.class, String.class,
boolean.class);
initAndRun.invoke(null, deploymentClassLoader, sourceParents,
generatedSourcesDir(test), buildDir().toPath(),
generatedSourcesDir(test), buildDir().toPath(), baseDir().toPath(),
sourceRegistrar, curatedApplication.getApplicationModel(), getBuildSystemProperties(false),
launchMode.name(),
test);
Expand Down
15 changes: 15 additions & 0 deletions docs/src/main/asciidoc/grpc-generation-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ quarkus {
}
----

== Generating Descriptor Set

Check warning on line 102 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Generating Descriptor Set'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Generating Descriptor Set'.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 102, "column": 4}}}, "severity": "INFO"}
Protocol Buffers do not contain descriptions of their own types. Thus, given only a raw message without the corresponding .proto file defining its type, it is difficult to extract any useful data. However, the contents of a .proto file can itself be https://protobuf.dev/programming-guides/techniques/#self-description[represented using protocol buffers].

Check warning on line 103 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'therefore' rather than 'Thus' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'therefore' rather than 'Thus' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 103, "column": 66}}}, "severity": "WARNING"}

By default, Quarkus does not generate these descriptors. Quarkus does provide several configuration options for generating them. These would be added to your `application.properties` or `application.yml` file:

* `quarkus.generate-code.grpc.descriptor-set.generate`
** Set to `true` to enable generation
* `quarkus.generate-code.grpc.descriptor-set.output-dir`
** Set this to a value relative to the project root directory
** Maven default value: `target/generated-sources/grpc`
** Gradle default value: `$buildDir/classes/java/quarkus-generated-sources/grpc`
* `quarkus.generate-code.grpc.descriptor-set.name`
** Name of the descriptor set file to generate
** Default value: `descriptor_set.dsc`

== Configuring gRPC code generation for dependencies

Check warning on line 117 in docs/src/main/asciidoc/grpc-generation-reference.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term. Raw Output: {"message": "[Quarkus.TermsWarnings] Consider using 'might (for possiblity)' or 'can (for ability)' rather than 'may' unless updating existing content that uses the term.", "location": {"path": "docs/src/main/asciidoc/grpc-generation-reference.adoc", "range": {"start": {"line": 117, "column": 33}}}, "severity": "WARNING"}

You may have dependencies that contain `\*.proto` files you want to compile to Java sources.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.grpc.deployment;

import static java.lang.Boolean.TRUE;
import static java.lang.Boolean.*;
import static java.util.Arrays.asList;

import java.io.BufferedWriter;
Expand Down Expand Up @@ -56,6 +56,9 @@ public class GrpcCodeGen implements CodeGenProvider {
private static final String SCAN_FOR_IMPORTS = "quarkus.generate-code.grpc.scan-for-imports";

private static final String POST_PROCESS_SKIP = "quarkus.generate.code.grpc-post-processing.skip";
private static final String GENERATE_DESCRIPTOR_SET = "quarkus.generate-code.grpc.descriptor-set.generate";
private static final String DESCRIPTOR_SET_OUTPUT_DIR = "quarkus.generate-code.grpc.descriptor-set.output-dir";
private static final String DESCRIPTOR_SET_FILENAME = "quarkus.generate-code.grpc.descriptor-set.name";

private Executables executables;
private String input;
Expand Down Expand Up @@ -149,6 +152,11 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
"--q-grpc_out=" + outDir,
"--grpc_out=" + outDir,
"--java_out=" + outDir));

if (shouldGenerateDescriptorSet(context.config())) {
command.add(String.format("--descriptor_set_out=%s", getDescriptorSetOutputFile(context)));
}

command.addAll(protoFiles);

ProcessBuilder processBuilder = new ProcessBuilder(command);
Expand Down Expand Up @@ -262,6 +270,25 @@ private boolean isGeneratingFromAppDependenciesEnabled(Config config) {
.filter(value -> !"none".equals(value)).isPresent();
}

private boolean shouldGenerateDescriptorSet(Config config) {
return config.getOptionalValue(GENERATE_DESCRIPTOR_SET, Boolean.class).orElse(FALSE);
}

private Path getDescriptorSetOutputFile(CodeGenContext context) throws IOException {
var dscOutputDir = context.config().getOptionalValue(DESCRIPTOR_SET_OUTPUT_DIR, String.class)
.map(context.projectDir()::resolve)
.orElseGet(context::outDir);

if (Files.notExists(dscOutputDir)) {
Files.createDirectories(dscOutputDir);
}

var dscFilename = context.config().getOptionalValue(DESCRIPTOR_SET_FILENAME, String.class)
.orElse("descriptor_set.dsc");

return dscOutputDir.resolve(dscFilename).normalize();
}

private Collection<String> gatherDirectoriesWithImports(Path workDir, CodeGenContext context) throws CodeGenException {
Config properties = context.config();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'
implementation 'io.quarkus:quarkus-grpc'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
includeGroup 'org.hibernate.orm'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id 'io.quarkus' version "${quarkusPluginVersion}"
}
}
rootProject.name = 'grpc-descriptor-set-alternate-output-dir'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.acme.quarkus.sample;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import io.quarkus.example.HelloMsg;

@Path("/hello")
public class HelloResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Integer number = HelloMsg.Status.TEST_ONE.getNumber();
// return a thing from proto file (for devmode test)
return "hello " + number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";
package io.quarkus.example;

option java_multiple_files = true;
option java_package = "io.quarkus.example";

import "google/protobuf/timestamp.proto";


message HelloMsg {
enum Status {
UNKNOWN = 0;
NOT_SERVING = 1;
TEST_ONE = 2;
}
string message = 1;
google.protobuf.Timestamp date_time = 2;
Status status = 3;
}

service DevModeService {
rpc Check(HelloMsg) returns (HelloMsg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkus.generate-code.grpc.descriptor-set.generate=true
quarkus.generate-code.grpc.descriptor-set.name=hello.dsc
quarkus.generate-code.grpc.descriptor-set.output-dir=build/resources
Loading

0 comments on commit 1fb9e23

Please sign in to comment.