Skip to content

Commit

Permalink
Fix issue loading snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Oct 12, 2024
1 parent 6ea86b6 commit 06840e2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 deletions.
48 changes: 43 additions & 5 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@

import org.gradle.internal.jvm.Jvm
import org.gradle.util.GradleVersion
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;

plugins {
id 'java-gradle-plugin'
Expand Down Expand Up @@ -278,25 +284,57 @@ if (project != rootProject) {
}
}

// Define this here because we need it early.
// Define this here because we need it early. This reads in a toml file, extracts the section with the header
// [versions] and converts to a Java Properties object
class VersionPropertiesLoader {
static Properties loadBuildSrcVersion(File input) throws IOException {
Properties props = new Properties();
InputStream is = new FileInputStream(input)
InputStream versionsStream = getSectionAsStream(is, "versions");
try {
props.load(is)
props.load(versionsStream)
} finally {
versionsStream.close()
is.close()
}
props.forEach((key, value) -> {
String newValue = value.toString().replace("\"", "")
props.setProperty(key.toString(), newValue)
if (value != null && value instanceof String) {
String newValue = value.toString().replace("\"", "")
props.setProperty(key.toString(), newValue)
}
});
loadBuildSrcVersion(props, System.getProperties())
return props
}

protected static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
static InputStream getSectionAsStream(InputStream is, String startSection) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder section = new StringBuilder();

String line;
boolean inSection = false;

while ((line = reader.readLine()) != null) {
line = line.trim();

if (line.equals("[" + startSection + "]")) {
inSection = true;
continue;
}

if (inSection && line.startsWith("[") && line.endsWith("]")) {
break;
}
if (inSection) {
section.append(line).append("\n");
}
}

reader.close();
return new ByteArrayInputStream(section.toString().getBytes());
}

static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
String opensearch = loadedProps.getProperty("opensearch")
if (opensearch == null) {
throw new IllegalStateException("OpenSearch version is missing from properties.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void tomlVersionsToProperties(TomlTable tomlTable, Properties pro
}
}

private static Properties getVersionProperties() {
public static Properties getVersionProperties() {
TomlParseResult toml = null;
try {
Path rootDir = Paths.get(System.getProperty("user.dir"));
Expand All @@ -156,9 +156,41 @@ private static Properties getVersionProperties() {

Properties properties = new Properties();
tomlVersionsToProperties(toml, properties);
loadBuildSrcVersion(properties, System.getProperties());
return properties;
}

private static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
String opensearch = loadedProps.getProperty("opensearch");
if (opensearch == null) {
throw new IllegalStateException("OpenSearch version is missing from properties.");
}
if (opensearch.matches("[0-9]+\\.[0-9]+\\.[0-9]+") == false) {
throw new IllegalStateException("Expected opensearch version to be numbers only of the form X.Y.Z but it was: " + opensearch);
}
String qualifier = systemProperties.getProperty("build.version_qualifier", "");
if (qualifier.isEmpty() == false) {
if (qualifier.matches("(alpha|beta|rc)\\d+") == false) {
throw new IllegalStateException("Invalid qualifier: " + qualifier);
}
opensearch += "-" + qualifier;
}
final String buildSnapshotSystemProperty = systemProperties.getProperty("build.snapshot", "true");
switch (buildSnapshotSystemProperty) {
case "true":
opensearch += "-SNAPSHOT";
break;
case "false":
// do nothing
break;
default:
throw new IllegalArgumentException(
"build.snapshot was set to [" + buildSnapshotSystemProperty + "] but can only be unset or [true|false]"
);
}
loadedProps.put("opensearch", opensearch);
}

public static boolean isOpenSearchSnapshot() {
return opensearch.endsWith("-SNAPSHOT");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,38 @@ public static Properties generateVersionProperties(File tomlFile) throws Excepti
String version = toml.getString("versions." + key);
properties.setProperty(key, version);
});

loadBuildSrcVersion(properties, System.getProperties());
return properties;
}

private static void loadBuildSrcVersion(Properties loadedProps, Properties systemProperties) {
String opensearch = loadedProps.getProperty("opensearch");
if (opensearch == null) {
throw new IllegalStateException("OpenSearch version is missing from properties.");
}
if (opensearch.matches("[0-9]+\\.[0-9]+\\.[0-9]+") == false) {
throw new IllegalStateException("Expected opensearch version to be numbers only of the form X.Y.Z but it was: " + opensearch);
}
String qualifier = systemProperties.getProperty("build.version_qualifier", "");
if (qualifier.isEmpty() == false) {
if (qualifier.matches("(alpha|beta|rc)\\d+") == false) {
throw new IllegalStateException("Invalid qualifier: " + qualifier);
}
opensearch += "-" + qualifier;
}
final String buildSnapshotSystemProperty = systemProperties.getProperty("build.snapshot", "true");
switch (buildSnapshotSystemProperty) {
case "true":
opensearch += "-SNAPSHOT";
break;
case "false":
// do nothing
break;
default:
throw new IllegalArgumentException(
"build.snapshot was set to [" + buildSnapshotSystemProperty + "] but can only be unset or [true|false]"
);
}
loadedProps.put("opensearch", opensearch);
}
}

0 comments on commit 06840e2

Please sign in to comment.