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;
}
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));
});
}
Aggregations