From 6ea86b6d0789910c19a9aeb8b7a5f23770e32038 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Fri, 11 Oct 2024 22:40:32 -0400 Subject: [PATCH 1/2] Get rootDir Signed-off-by: Craig Perkins --- .../main/java/org/opensearch/gradle/VersionProperties.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java b/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java index 74e5073862ede..dd73e41cbfdcc 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java @@ -35,6 +35,7 @@ import java.io.IOException; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -143,7 +144,11 @@ private static void tomlVersionsToProperties(TomlTable tomlTable, Properties pro private static Properties getVersionProperties() { TomlParseResult toml = null; try { - Path path = Path.of(System.getProperty("user.dir"), "gradle/libs.versions.toml"); + Path rootDir = Paths.get(System.getProperty("user.dir")); + if (rootDir.endsWith("buildSrc")) { + rootDir = rootDir.getParent(); + } + Path path = rootDir.resolve("gradle/libs.versions.toml"); toml = Toml.parse(path); } catch (IOException e) { throw new RuntimeException(e); From 06840e2366c48ebbf24276318548eb3b3e9feff6 Mon Sep 17 00:00:00 2001 From: Craig Perkins Date: Sat, 12 Oct 2024 12:34:23 -0400 Subject: [PATCH 2/2] Fix issue loading snapshot Signed-off-by: Craig Perkins --- buildSrc/build.gradle | 48 +++++++++++++++++-- .../opensearch/gradle/VersionProperties.java | 34 ++++++++++++- .../gradle/VersionPropertiesGenerator.java | 33 ++++++++++++- 3 files changed, 108 insertions(+), 7 deletions(-) diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 3ec10c3646cd1..9482bcb47f952 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -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' @@ -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.") diff --git a/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java b/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java index dd73e41cbfdcc..c5facac9e5b86 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/VersionProperties.java @@ -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")); @@ -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"); } diff --git a/buildSrc/src/main/java/org/opensearch/gradle/VersionPropertiesGenerator.java b/buildSrc/src/main/java/org/opensearch/gradle/VersionPropertiesGenerator.java index 2e68f4f4a5ac7..f127a067ca1cf 100644 --- a/buildSrc/src/main/java/org/opensearch/gradle/VersionPropertiesGenerator.java +++ b/buildSrc/src/main/java/org/opensearch/gradle/VersionPropertiesGenerator.java @@ -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); + } }