use of com.google.protobuf.gradle.ProtobufConfigurator in project curiostack by curioswitch.
the class GrpcApiPlugin method apply.
@Override
public void apply(Project project) {
project.getPluginManager().apply(JavaLibraryPlugin.class);
project.getExtensions().create(ImmutableGrpcExtension.NAME, GrpcExtension.class);
GRPC_DEPENDENCIES.forEach(dep -> project.getDependencies().add("api", "io.grpc:" + dep));
project.afterEvaluate(p -> {
ImmutableGrpcExtension config = project.getExtensions().getByType(GrpcExtension.class);
Map<String, String> managedVersions = project.getExtensions().getByType(DependencyManagementExtension.class).getManagedVersions();
ProtobufConfigurator protobuf = project.getConvention().getPlugin(ProtobufConvention.class).getProtobuf();
protobuf.protoc(LambdaClosure.of((ExecutableLocator locator) -> locator.setArtifact("com.google.protobuf:protoc:" + managedVersions.get("com.google.protobuf:protoc"))));
protobuf.plugins(LambdaClosure.of((NamedDomainObjectContainer<ExecutableLocator> locators) -> {
locators.create("grpc").setArtifact("io.grpc:protoc-gen-grpc-java:" + managedVersions.get("io.grpc:grpc-core"));
if (config.web()) {
locators.create("ts").setPath(project.file("node_modules/.bin/protoc-gen-ts-resolved" + (IS_WINDOWS ? ".cmd" : "")).getAbsolutePath());
}
}));
String archivesBaseName = project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName();
String descriptorSetOutputPath = project.getBuildDir() + "/resources/main/META-INF/armeria/grpc/" + project.getGroup() + "." + archivesBaseName + ".dsc";
protobuf.generateProtoTasks(LambdaClosure.of((JavaGenerateProtoTaskCollection tasks) -> {
tasks.all().forEach(task -> {
task.getBuiltins().getByName("java").setOutputSubDir("");
task.getPlugins().create("grpc").setOutputSubDir("");
if (config.web()) {
// We generate web protos into build/web to make it easier to
// reference from other projects.
task.getBuiltins().create("js").option("import_style=commonjs,binary").setOutputSubDir("../../../../web");
task.getPlugins().create("ts").option("service=true").setOutputSubDir("../../../../web");
}
});
tasks.ofSourceSet("main").forEach(task -> {
task.getOutputs().file(descriptorSetOutputPath);
task.setGenerateDescriptorSet(true);
DescriptorSetOptions options = task.getDescriptorSetOptions();
options.setIncludeSourceInfo(true);
options.setIncludeImports(true);
options.setPath(new GStringImpl(new Object[] {}, new String[] { descriptorSetOutputPath }));
});
}));
});
// Add the protobuf plugin last to make sure our afterEvaluate runs before it.
project.getPluginManager().apply(ProtobufPlugin.class);
// Additional configuration of tasks created by protobuf plugin.
project.afterEvaluate(p -> {
ImmutableGrpcExtension config = project.getExtensions().getByType(GrpcExtension.class);
if (config.web()) {
String currentProjectPath = project.getPath().replace(':', '_');
NpmTask installTsProtocGen = project.getRootProject().getTasks().create("installTsProtocGen_" + currentProjectPath, NpmTask.class);
installTsProtocGen.setWorkingDir(project.getProjectDir());
installTsProtocGen.setArgs(ImmutableList.of("install", "--no-save", "ts-protoc-gen@" + TS_PROTOC_GEN_VERSION));
installTsProtocGen.getInputs().property("ts-protoc-gen-version", TS_PROTOC_GEN_VERSION);
installTsProtocGen.getOutputs().dir("node_modules/ts-protoc-gen");
// gradle-protobuf-plugin does not allow manipulating PATH for protoc invocation, so
// there's no way
// to point it at our downloaded nodejs. We go ahead and create our own plugin
// executable with the
// nodejs path resolved.
Task addResolvedPluginScript = project.getTasks().create("addResolvedPluginScript").dependsOn(installTsProtocGen).doFirst(t -> {
String nodePath = project.getRootProject().getExtensions().getByType(NodeExtension.class).getVariant().getNodeExec();
writeResolvedScript(project, nodePath, "protoc-gen-ts-resolved", "ts-protoc-gen/lib/ts_index");
});
addResolvedPluginScript.getOutputs().files(ImmutableMap.of("protoc-gen-ts-resolved", "node_modules/.bin/protoc-gen-ts-resolved", "protoc-gen-ts-resolved-cmd", "node_modules/.bin/protoc-gen-ts-resolved.cmd"));
String packageName = config.webPackageName().isEmpty() ? project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName() : config.webPackageName();
Path packageJsonPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "package.json");
Path indexJsPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "index.ts");
Path tsConfigPath = Paths.get(project.getBuildDir().getAbsolutePath(), "web", "tsconfig.json");
Task addPackageJson = project.getTasks().create("packageJson").dependsOn("generateProto").doFirst(t -> {
try {
Files.write(packageJsonPath, PACKAGE_JSON_TEMPLATE.replaceFirst("\\|PACKAGE_NAME\\|", packageName).replaceFirst("\\|TYPES_GOOGLE_PROTOBUF_VERSION\\|", TYPES_GOOGLE_PROTOBUF_VERSION).replaceFirst("\\|GOOGLE_PROTOBUF_VERSION\\|", GOOGLE_PROTOBUF_VERSION).replaceFirst("\\|GRPC_WEB_CLIENT_VERSION\\|", GRPC_WEB_CLIENT_VERSION).replaceFirst("\\|CURIOSTACK_BASE_NODE_DEV_VERSION\\|", CURIOSTACK_BASE_NODE_DEV_VERSION).getBytes(StandardCharsets.UTF_8));
Files.write(tsConfigPath, TSCONFIG_TEMPLATE.getBytes(StandardCharsets.UTF_8));
Files.write(indexJsPath, new byte[0]);
} catch (IOException e) {
throw new UncheckedIOException("Could not write package.json.", e);
}
});
addPackageJson.getOutputs().files(ImmutableMap.of("PACKAGE_JSON", packageJsonPath.toFile(), "INDEX_TS", indexJsPath.toFile(), "TS_CONFIG", tsConfigPath.toFile()));
Task generateProto = project.getTasks().getByName("generateProto");
generateProto.dependsOn(addResolvedPluginScript).finalizedBy(addPackageJson);
project.getRootProject().getTasks().findByName("yarn").dependsOn(generateProto);
// Unclear why sometimes compileTestJava fails with "no source files" instead of being
// skipped (usually when activating web), but it's not that hard to at least check the
// source set directory.
SourceSetContainer sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
if (sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getAllJava().isEmpty()) {
project.getTasks().getByName("compileTestJava").setEnabled(false);
}
}
});
}
use of com.google.protobuf.gradle.ProtobufConfigurator in project curiostack by curioswitch.
the class GrpcApiPluginTest method normal.
@Test
public void normal() throws Exception {
Project project = ProjectBuilder.builder().withName("api").build();
project.setGroup("org.curioswitch.test");
project.getPluginManager().apply(DependencyManagementPlugin.class);
DependencyManagementExtension dependencyManagement = project.getExtensions().getByType(DependencyManagementExtension.class);
dependencyManagement.dependencies(handler -> {
handler.dependency("io.grpc:grpc-core:5.0.0");
handler.dependency("io.grpc:grpc-protobuf:5.0.0");
handler.dependency("io.grpc:grpc-stub:5.0.0");
handler.dependency("com.google.protobuf:protoc:6.0.0");
});
project.getPluginManager().apply("org.curioswitch.gradle-grpc-api-plugin");
project.setProperty("archivesBaseName", "curio-test-api");
((DefaultProject) project).evaluate();
Task task = project.getTasks().findByName("generateProto");
assertThat(task).isInstanceOf(GenerateProtoTask.class).isNotNull();
GenerateProtoTask generateProtoTask = (GenerateProtoTask) task;
assertThat(generateProtoTask.getGenerateDescriptorSet()).isTrue();
assertThat(generateProtoTask.getDescriptorSetOptions().getIncludeSourceInfo()).isTrue();
assertThat(generateProtoTask.getDescriptorSetOptions().getIncludeImports()).isTrue();
assertThat(generateProtoTask.getDescriptorSetOptions().getPath().toString()).isEqualTo(project.getBuildDir() + "/resources/main/META-INF/armeria/grpc/org.curioswitch.test.curio-test-api.dsc");
ProtobufConfigurator protobuf = project.getConvention().getPlugin(ProtobufConvention.class).getProtobuf();
protobuf.protoc(LambdaClosure.of((ExecutableLocator locator) -> assertThat(locator.getArtifact()).isEqualTo("com.google.protobuf:protoc:6.0.0")));
protobuf.plugins(LambdaClosure.of((NamedDomainObjectContainer<ExecutableLocator> locators) -> assertThat(locators.getByName("grpc").getArtifact()).isEqualTo("io.grpc:protoc-gen-grpc-java:5.0.0")));
}
Aggregations