Search in sources :

Example 1 with ProtobufExtension

use of org.curioswitch.gradle.protobuf.ProtobufExtension in project curiostack by curioswitch.

the class GrpcApiPlugin method apply.

@Override
public void apply(Project project) {
    project.getRootProject().getPlugins().apply(GrpcApiSetupPlugin.class);
    project.getPlugins().apply(JavaLibraryPlugin.class);
    project.getPlugins().apply(ProtobufPlugin.class);
    project.getPlugins().apply(NodePlugin.class);
    var config = GrpcExtension.createAndAdd(project);
    GRPC_DEPENDENCIES.forEach(dep -> project.getDependencies().add("api", "io.grpc:" + dep));
    ProtobufExtension protobuf = project.getExtensions().getByType(ProtobufExtension.class);
    protobuf.getProtoc().getArtifact().set("com.google.protobuf:protoc");
    protobuf.getLanguages().register("grpc", language -> language.getPlugin().getArtifact().set("io.grpc:protoc-gen-grpc-java"));
    project.afterEvaluate(p -> {
        String archivesBaseName = project.getConvention().getPlugin(BasePluginConvention.class).getArchivesBaseName();
        var descriptorOptions = protobuf.getDescriptorSetOptions();
        descriptorOptions.getPath().set(project.file("build/resources/main/META-INF/armeria/grpc/" + project.getGroup() + "." + archivesBaseName + ".dsc"));
        descriptorOptions.getEnabled().set(true);
        descriptorOptions.getIncludeSourceInfo().set(true);
        descriptorOptions.getIncludeImports().set(true);
        if (config.getWeb().get()) {
            protobuf.getLanguages().register("js", language -> {
                language.option("import_style=commonjs,binary");
                language.getOutputDir().set(project.file("build/webprotos"));
            });
            protobuf.getLanguages().register("grpc-web", language -> {
                language.getPlugin().getPath().set(project.file(DownloadedToolManager.get(project).getToolDir("protoc-gen-grpc-web").resolve("protoc-gen-grpc-web" + (IS_WINDOWS ? ".exe" : ""))));
                language.option("import_style=commonjs+dts");
                language.option("mode=grpcweb");
                language.getOutputDir().set(project.file("build/webprotos"));
            });
            project.getTasks().withType(GenerateProtoTask.class).configureEach(t -> t.execOverride(exec -> DownloadedToolManager.get(project).addAllToPath(exec)));
        }
    });
    // Additional configuration of tasks created by protobuf plugin.
    project.afterEvaluate(p -> {
        if (config.getWeb().get()) {
            var installProtocGenGrpcWeb = DownloadToolUtil.getSetupTask(project, "protoc-gen-grpc-web");
            var generateProto = project.getTasks().named("generateProto");
            var packageWeb = project.getTasks().register("packageWeb", PackageWebTask.class, t -> {
                t.dependsOn(generateProto);
                t.getWebPackageName().set(config.getWebPackageName());
                t.getPackageJsonTemplate().set(PACKAGE_JSON_TEMPLATE);
            });
            generateProto.configure(t -> t.dependsOn(installProtocGenGrpcWeb).finalizedBy(packageWeb));
            project.getRootProject().getTasks().named("yarn").configure(t -> t.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().named("compileTestJava").configure(t -> t.setEnabled(false));
                project.getTasks().named("test").configure(t -> t.setEnabled(false));
            }
        }
    });
}
Also used : BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) ProtobufExtension(org.curioswitch.gradle.protobuf.ProtobufExtension) Arrays(java.util.Arrays) PackageWebTask(org.curioswitch.gradle.plugins.grpcapi.tasks.PackageWebTask) Resources(com.google.common.io.Resources) Project(org.gradle.api.Project) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) IOException(java.io.IOException) DownloadToolUtil(org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil) StandardCharsets(java.nio.charset.StandardCharsets) SourceSet(org.gradle.api.tasks.SourceSet) UncheckedIOException(java.io.UncheckedIOException) ProtobufPlugin(org.curioswitch.gradle.protobuf.ProtobufPlugin) JavaLibraryPlugin(org.gradle.api.plugins.JavaLibraryPlugin) List(java.util.List) NodePlugin(org.curioswitch.gradle.plugins.nodejs.NodePlugin) Os(org.apache.tools.ant.taskdefs.condition.Os) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) DownloadedToolManager(org.curioswitch.gradle.tooldownloader.DownloadedToolManager) BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) Collections(java.util.Collections) GenerateProtoTask(org.curioswitch.gradle.protobuf.tasks.GenerateProtoTask) Plugin(org.gradle.api.Plugin) ProtobufExtension(org.curioswitch.gradle.protobuf.ProtobufExtension) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) GenerateProtoTask(org.curioswitch.gradle.protobuf.tasks.GenerateProtoTask)

Aggregations

Resources (com.google.common.io.Resources)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Os (org.apache.tools.ant.taskdefs.condition.Os)1 PackageWebTask (org.curioswitch.gradle.plugins.grpcapi.tasks.PackageWebTask)1 NodePlugin (org.curioswitch.gradle.plugins.nodejs.NodePlugin)1 ProtobufExtension (org.curioswitch.gradle.protobuf.ProtobufExtension)1 ProtobufPlugin (org.curioswitch.gradle.protobuf.ProtobufPlugin)1 GenerateProtoTask (org.curioswitch.gradle.protobuf.tasks.GenerateProtoTask)1 DownloadedToolManager (org.curioswitch.gradle.tooldownloader.DownloadedToolManager)1 DownloadToolUtil (org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil)1 Plugin (org.gradle.api.Plugin)1 Project (org.gradle.api.Project)1 BasePluginConvention (org.gradle.api.plugins.BasePluginConvention)1 JavaLibraryPlugin (org.gradle.api.plugins.JavaLibraryPlugin)1 JavaPluginConvention (org.gradle.api.plugins.JavaPluginConvention)1