Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

package org.elasticsearch.gradle.internal.transport;

import java.nio.file.Path;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.nio.file.Path;
import java.nio.file.Path;

import java.util.ArrayList;
import java.util.List;

record TransportVersionDefinition(String name, List<TransportVersionId> ids) {
public static TransportVersionDefinition fromString(String filename, String contents) {
public static TransportVersionDefinition fromString(Path file, String contents) {
String filename = file.getFileName().toString();
assert filename.endsWith(".csv");
String name = filename.substring(0, filename.length() - 4);
List<TransportVersionId> ids = new ArrayList<>();
Expand All @@ -23,7 +25,7 @@ public static TransportVersionDefinition fromString(String filename, String cont
try {
ids.add(TransportVersionId.fromString(rawId));
} catch (NumberFormatException e) {
throw new IllegalStateException("Failed to parse id " + rawId + " in " + filename, e);
throw new IllegalStateException("Failed to parse id " + rawId + " in " + file, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

package org.elasticsearch.gradle.internal.transport;

import java.nio.file.Path;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [ ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


record TransportVersionLatest(String branch, String name, TransportVersionId id) {
public static TransportVersionLatest fromString(String filename, String contents) {
public static TransportVersionLatest fromString(Path file, String contents) {
String filename = file.getFileName().toString();
assert filename.endsWith(".csv");
String branch = filename.substring(0, filename.length() - 4);

String[] parts = contents.split(",");
if (parts.length != 2) {
throw new IllegalStateException("Invalid transport version latest file [" + filename + "]: " + contents);
throw new IllegalStateException("Invalid transport version latest file [" + file + "]: " + contents);
}

return new TransportVersionLatest(branch, parts[0], TransportVersionId.fromString(parts[1]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.gradle.process.ExecResult;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -104,7 +103,7 @@ Map<String, TransportVersionDefinition> getNamedDefinitions() throws IOException

/** Get a named definition from main if it exists there, or null otherwise */
TransportVersionDefinition getNamedDefinitionFromMain(String name) {
String resourcePath = getNamedDefinitionRelativePath(name).toString();
Path resourcePath = getNamedDefinitionRelativePath(name);
return getMainFile(resourcePath, TransportVersionDefinition::fromString);
}

Expand All @@ -130,7 +129,7 @@ Map<String, TransportVersionDefinition> getUnreferencedDefinitions() throws IOEx

/** Get a named definition from main if it exists there, or null otherwise */
TransportVersionDefinition getUnreferencedDefinitionFromMain(String name) {
String resourcePath = getUnreferencedDefinitionRelativePath(name).toString();
Path resourcePath = getUnreferencedDefinitionRelativePath(name);
return getMainFile(resourcePath, TransportVersionDefinition::fromString);
}

Expand All @@ -145,7 +144,7 @@ Map<String, TransportVersionLatest> getLatestByReleaseBranch() throws IOExceptio
try (var stream = Files.list(transportResourcesDir.resolve(LATEST_DIR))) {
for (var latestFile : stream.toList()) {
String contents = Files.readString(latestFile, StandardCharsets.UTF_8).strip();
var latest = TransportVersionLatest.fromString(latestFile.getFileName().toString(), contents);
var latest = TransportVersionLatest.fromString(latestFile, contents);
latests.put(latest.name(), latest);
}
}
Expand All @@ -154,7 +153,7 @@ Map<String, TransportVersionLatest> getLatestByReleaseBranch() throws IOExceptio

/** Retrieve the latest transport version for the given release branch on main */
TransportVersionLatest getLatestFromMain(String releaseBranch) {
String resourcePath = getLatestRelativePath(releaseBranch).toString();
Path resourcePath = getLatestRelativePath(releaseBranch);
return getMainFile(resourcePath, TransportVersionLatest::fromString);
}

Expand All @@ -174,7 +173,7 @@ private Set<String> getMainResources() {
String output = gitCommand("ls-tree", "--name-only", "-r", "main", ".");

HashSet<String> resources = new HashSet<>();
Collections.addAll(resources, output.split(System.lineSeparator()));
Collections.addAll(resources, output.split("\n")); // git always outputs LF
mainResources.set(resources);
}
}
Expand All @@ -188,20 +187,21 @@ private Set<String> getChangedResources() {
String output = gitCommand("diff", "--name-only", "main", ".");

HashSet<String> resources = new HashSet<>();
Collections.addAll(resources, output.split(System.lineSeparator()));
Collections.addAll(resources, output.split("\n")); // git always outputs LF
changedResources.set(resources);
}
}
return changedResources.get();
}

// Read a transport version resource from the main branch, or return null if it doesn't exist on main
private <T> T getMainFile(String resourcePath, BiFunction<String, String, T> parser) {
if (getMainResources().contains(resourcePath) == false) {
private <T> T getMainFile(Path resourcePath, BiFunction<Path, String, T> parser) {
String pathString = resourcePath.toString().replace('\\', '/'); // normalize to forward slash that git uses
if (getMainResources().contains(pathString) == false) {
return null;
}

String content = gitCommand("show", "main:." + File.separator + resourcePath).strip();
String content = gitCommand("show", "main:./" + pathString).strip();
return parser.apply(resourcePath, content);
}

Expand All @@ -213,7 +213,7 @@ private static Map<String, TransportVersionDefinition> readDefinitions(Path dir)
try (var definitionsStream = Files.list(dir)) {
for (var definitionFile : definitionsStream.toList()) {
String contents = Files.readString(definitionFile, StandardCharsets.UTF_8).strip();
var definition = TransportVersionDefinition.fromString(definitionFile.getFileName().toString(), contents);
var definition = TransportVersionDefinition.fromString(definitionFile, contents);
definitions.put(definition.name(), definition);
}
}
Expand Down