Search in sources :

Example 1 with LanguageSettings

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

the class GenerateProtoTask method exec.

@TaskAction
public void exec() {
    Project project = getProject();
    List<LanguageSettings> languages = this.languages.get();
    ImmutableList.Builder<String> artifacts = ImmutableList.builder();
    if (protocArtifact.isPresent()) {
        artifacts.add(protocArtifact.get());
    }
    for (LanguageSettings language : languages) {
        if (language.getPlugin().getArtifact().isPresent()) {
            artifacts.add(language.getPlugin().getArtifact().get());
        }
    }
    Map<String, File> downloadedTools = downloadTools(artifacts.build());
    File protocPath = protocArtifact.isPresent() ? checkNotNull(downloadedTools.get(protocArtifact.get())) : this.protocPath.get();
    ImmutableList.Builder<String> protocCommand = ImmutableList.builder();
    protocCommand.add(protocPath.getAbsolutePath());
    for (LanguageSettings language : languages) {
        String optionsPrefix = optionsPrefix(language.getOptions().getOrElse(ImmutableList.of()));
        String outputDir = getLanguageOutputDir(language).getAbsolutePath();
        project.delete(outputDir);
        project.mkdir(outputDir);
        protocCommand.add("--" + language.getName() + "_out=" + optionsPrefix + outputDir);
        Executable plugin = language.getPlugin();
        if (plugin.isPresent()) {
            String pluginPath = Objects.requireNonNullElseGet(plugin.getPath().getOrNull(), () -> downloadedTools.get(plugin.getArtifact().get())).getAbsolutePath();
            protocCommand.add("--plugin=protoc-gen-" + language.getName() + "=" + pluginPath);
        }
    }
    Streams.concat(sources.getSrcDirs().stream(), includeDirs.getSrcDirs().stream()).distinct().filter(File::exists).forEach(dir -> protocCommand.add("-I" + dir.getAbsolutePath()));
    if (descriptorSetOptions.getEnabled().get()) {
        File descriptorSetPath = descriptorSetOptions.getPath().getOrElse(project.file("build/descriptors/" + sourceSetName + ".dsc"));
        project.mkdir(descriptorSetPath.getParent());
        protocCommand.add("--descriptor_set_out=" + descriptorSetPath.getAbsolutePath());
        if (descriptorSetOptions.getIncludeSourceInfo().get()) {
            protocCommand.add("--include_source_info");
        }
        if (descriptorSetOptions.getIncludeImports().get()) {
            protocCommand.add("--include_imports");
        }
    }
    // Sort to ensure generated descriptors have a canonical representation
    // to avoid triggering unnecessary rebuilds downstream
    sources.getFiles().stream().map(File::getAbsolutePath).sorted().forEach(protocCommand::add);
    ExternalExecUtil.exec(getProject(), workerExecutor, exec -> {
        exec.commandLine(protocCommand.build());
        execOverrides.forEach(a -> a.execute(exec));
    });
}
Also used : Project(org.gradle.api.Project) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) LanguageSettings(org.curioswitch.gradle.protobuf.ProtobufExtension.LanguageSettings) Executable(org.curioswitch.gradle.protobuf.ProtobufExtension.Executable) OutputFile(org.gradle.api.tasks.OutputFile) File(java.io.File) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 File (java.io.File)1 Executable (org.curioswitch.gradle.protobuf.ProtobufExtension.Executable)1 LanguageSettings (org.curioswitch.gradle.protobuf.ProtobufExtension.LanguageSettings)1 Project (org.gradle.api.Project)1 OutputFile (org.gradle.api.tasks.OutputFile)1 TaskAction (org.gradle.api.tasks.TaskAction)1