Search in sources :

Example 1 with Executable

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

the class GenerateProtoTask method downloadTools.

private Map<String, File> downloadTools(List<String> artifacts) {
    RepositoryHandler repositories = getProject().getRepositories();
    List<ArtifactRepository> currentRepositories = ImmutableList.copyOf(repositories);
    // Make sure Maven Central is present as a repository since it's the usual place to
    // get protoc, even for non-Java projects. We restore to the previous state after the task.
    repositories.mavenCentral();
    List<Dependency> dependencies = artifacts.stream().map(artifact -> {
        checkArgument(!artifact.isEmpty(), "artifact must not be empty");
        List<String> coordinateParts = COORDINATE_SPLITTER.splitToList(artifact);
        List<String> artifactParts = ARTIFACT_SPLITTER.splitToList(coordinateParts.get(0));
        ImmutableMap.Builder<String, String> depParts = ImmutableMap.builderWithExpectedSize(5);
        // manipulation.
        if (artifactParts.size() > 0) {
            depParts.put("group", artifactParts.get(0));
        }
        if (artifactParts.size() > 1) {
            depParts.put("name", artifactParts.get(1));
        }
        if (artifactParts.size() > 2) {
            depParts.put("version", artifactParts.get(2));
        }
        if (artifactParts.size() > 3) {
            depParts.put("classifier", artifactParts.get(3));
        } else {
            depParts.put("classifier", getProject().getExtensions().getByType(OsDetector.class).getClassifier());
        }
        if (coordinateParts.size() > 1) {
            depParts.put("ext", coordinateParts.get(1));
        } else {
            depParts.put("ext", "exe");
        }
        return getProject().getDependencies().create(depParts.build());
    }).collect(toImmutableList());
    Configuration configuration = getProject().getConfigurations().getByName("protobufTools");
    configuration.getDependencies().addAll(dependencies);
    // Resolve once to download all tools in parallel.
    configuration.resolve();
    // This will not re-download.
    ResolvedConfiguration resolved = configuration.getResolvedConfiguration();
    Map<String, File> downloaded = Streams.zip(artifacts.stream(), dependencies.stream(), (artifact, dep) -> {
        Set<File> files = resolved.getFiles(d -> {
            // Dependency.contentEquals doesn't match for some reason...
            return Objects.equals(dep.getGroup(), d.getGroup()) && dep.getName().equals(d.getName()) && Objects.equals(dep.getVersion(), d.getVersion());
        });
        checkState(files.size() == 1);
        File file = Iterables.getOnlyElement(files);
        if (!file.canExecute()) {
            if (!file.setExecutable(true)) {
                throw new IllegalStateException("Could not set proto tool to executable: " + file.getAbsolutePath());
            }
        }
        return new SimpleImmutableEntry<>(artifact, file);
    }).collect(toImmutableMap(Entry::getKey, Entry::getValue));
    repositories.clear();
    repositories.addAll(currentRepositories);
    return downloaded;
}
Also used : Executable(org.curioswitch.gradle.protobuf.ProtobufExtension.Executable) ArtifactRepository(org.gradle.api.artifacts.repositories.ArtifactRepository) ListProperty(org.gradle.api.provider.ListProperty) InputFiles(org.gradle.api.tasks.InputFiles) TaskAction(org.gradle.api.tasks.TaskAction) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) DefaultTask(org.gradle.api.DefaultTask) Splitter(com.google.common.base.Splitter) ImmutableMap(com.google.common.collect.ImmutableMap) DescriptorSetOptions(org.curioswitch.gradle.protobuf.ProtobufExtension.DescriptorSetOptions) Project(org.gradle.api.Project) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet) ExternalExecUtil(org.curioswitch.gradle.helpers.exec.ExternalExecUtil) Set(java.util.Set) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) OutputDirectories(org.gradle.api.tasks.OutputDirectories) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) List(java.util.List) OutputFile(org.gradle.api.tasks.OutputFile) WorkerExecutor(org.gradle.workers.WorkerExecutor) Entry(java.util.Map.Entry) ExecSpec(org.gradle.process.ExecSpec) ProtobufExtension(org.curioswitch.gradle.protobuf.ProtobufExtension) Iterables(com.google.common.collect.Iterables) RepositoryHandler(org.gradle.api.artifacts.dsl.RepositoryHandler) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Configuration(org.gradle.api.artifacts.Configuration) ImmutableList(com.google.common.collect.ImmutableList) DirectoryProperty(org.gradle.api.file.DirectoryProperty) PathSensitivity(org.gradle.api.tasks.PathSensitivity) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) OsDetector(com.google.gradle.osdetector.OsDetector) Action(org.gradle.api.Action) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) File(java.io.File) LanguageSettings(org.curioswitch.gradle.protobuf.ProtobufExtension.LanguageSettings) ObjectFactory(org.gradle.api.model.ObjectFactory) SourceSetUtils(org.curioswitch.gradle.protobuf.utils.SourceSetUtils) Property(org.gradle.api.provider.Property) CacheableTask(org.gradle.api.tasks.CacheableTask) PathSensitive(org.gradle.api.tasks.PathSensitive) Dependency(org.gradle.api.artifacts.Dependency) SourceDirectorySet(org.gradle.api.file.SourceDirectorySet) Set(java.util.Set) Configuration(org.gradle.api.artifacts.Configuration) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) ArtifactRepository(org.gradle.api.artifacts.repositories.ArtifactRepository) Dependency(org.gradle.api.artifacts.Dependency) ResolvedConfiguration(org.gradle.api.artifacts.ResolvedConfiguration) OsDetector(com.google.gradle.osdetector.OsDetector) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) RepositoryHandler(org.gradle.api.artifacts.dsl.RepositoryHandler) OutputFile(org.gradle.api.tasks.OutputFile) File(java.io.File)

Example 2 with Executable

use of org.curioswitch.gradle.protobuf.ProtobufExtension.Executable 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)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 File (java.io.File)2 Executable (org.curioswitch.gradle.protobuf.ProtobufExtension.Executable)2 LanguageSettings (org.curioswitch.gradle.protobuf.ProtobufExtension.LanguageSettings)2 Project (org.gradle.api.Project)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Splitter (com.google.common.base.Splitter)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 Iterables (com.google.common.collect.Iterables)1 Streams (com.google.common.collect.Streams)1 OsDetector (com.google.gradle.osdetector.OsDetector)1 SimpleImmutableEntry (java.util.AbstractMap.SimpleImmutableEntry)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1