Search in sources :

Example 1 with GStringImpl

use of org.codehaus.groovy.runtime.GStringImpl 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);
            }
        }
    });
}
Also used : BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) Arrays(java.util.Arrays) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) ProtobufConvention(com.google.protobuf.gradle.ProtobufConvention) SourceSet(org.gradle.api.tasks.SourceSet) Task(org.gradle.api.Task) ImmutableList(com.google.common.collect.ImmutableList) DependencyManagementExtension(io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) Map(java.util.Map) Path(java.nio.file.Path) JavaGenerateProtoTaskCollection(com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection) NodeExtension(com.moowork.gradle.node.NodeExtension) GStringImpl(org.codehaus.groovy.runtime.GStringImpl) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) NpmTask(com.moowork.gradle.node.npm.NpmTask) Files(java.nio.file.Files) Project(org.gradle.api.Project) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) ProtobufPlugin(com.google.protobuf.gradle.ProtobufPlugin) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) JavaLibraryPlugin(org.gradle.api.plugins.JavaLibraryPlugin) List(java.util.List) DescriptorSetOptions(com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions) Os(org.apache.tools.ant.taskdefs.condition.Os) Paths(java.nio.file.Paths) BasePluginConvention(org.gradle.api.plugins.BasePluginConvention) ExecutableLocator(com.google.protobuf.gradle.ExecutableLocator) ProtobufConfigurator(com.google.protobuf.gradle.ProtobufConfigurator) Collections(java.util.Collections) LambdaClosure(org.curioswitch.gradle.common.LambdaClosure) Plugin(org.gradle.api.Plugin) Path(java.nio.file.Path) Task(org.gradle.api.Task) NpmTask(com.moowork.gradle.node.npm.NpmTask) DependencyManagementExtension(io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SourceSetContainer(org.gradle.api.tasks.SourceSetContainer) ProtobufConfigurator(com.google.protobuf.gradle.ProtobufConfigurator) GStringImpl(org.codehaus.groovy.runtime.GStringImpl) ProtobufConvention(com.google.protobuf.gradle.ProtobufConvention) ExecutableLocator(com.google.protobuf.gradle.ExecutableLocator) JavaPluginConvention(org.gradle.api.plugins.JavaPluginConvention) DescriptorSetOptions(com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions) JavaGenerateProtoTaskCollection(com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection) NpmTask(com.moowork.gradle.node.npm.NpmTask)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Resources (com.google.common.io.Resources)1 ExecutableLocator (com.google.protobuf.gradle.ExecutableLocator)1 DescriptorSetOptions (com.google.protobuf.gradle.GenerateProtoTask.DescriptorSetOptions)1 ProtobufConfigurator (com.google.protobuf.gradle.ProtobufConfigurator)1 JavaGenerateProtoTaskCollection (com.google.protobuf.gradle.ProtobufConfigurator.JavaGenerateProtoTaskCollection)1 ProtobufConvention (com.google.protobuf.gradle.ProtobufConvention)1 ProtobufPlugin (com.google.protobuf.gradle.ProtobufPlugin)1 NodeExtension (com.moowork.gradle.node.NodeExtension)1 NpmTask (com.moowork.gradle.node.npm.NpmTask)1 DependencyManagementExtension (io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1