snazy commented on code in PR #591:
URL: https://github.com/apache/polaris/pull/591#discussion_r1898062040


##########
tools/version/src/main/java/org/apache/polaris/version/PolarisVersion.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.version;
+
+import static java.lang.String.format;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.requireNonNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+/**
+ * Utility class to retrieve the current Polaris version and the contents of 
the {@code NOTICE} and
+ * {@code LICENSE*} files.
+ *
+ * <p>If built as a release using the Gradle {@code -Prelease} project 
property, more information
+ * like the Git tag, Git commit ID, build system and more is available as well.
+ */
+public final class PolarisVersion {
+  private PolarisVersion() {}
+
+  /** The version string as in the file {@code version.txt} in the project's 
root directory. */
+  public static String polarisVersionString() {
+    return PolarisVersionNumber.POLARIS_VERSION;
+  }
+
+  /**
+   * Flag whether the build is a released version, when Polaris has been built 
with the Gradle
+   * {@code -Prelease} project property. If {@code true}, the {@code 
getBuild*} functions return
+   * meaningful values.
+   */
+  public static boolean isReleaseBuild() {
+    return 
"true".equals(PolarisVersionJarInfo.buildInfo(MF_IS_RELEASE).orElse("false"));
+  }
+
+  /**
+   * Returns the version as in the jar manifest, if {@linkplain 
#isReleaseBuild() build-time Git
+   * information} is available, when Polaris has been built with the Gradle 
{@code -Prelease}
+   * project property..
+   *
+   * <p>Example value: {@code 1.0.0-incubating-SNAPSHOT}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildReleasedVersion() {
+    return PolarisVersionJarInfo.buildInfo(MF_VERSION);
+  }
+
+  /**
+   * Returns the commit ID as in the jar manifest, if {@linkplain 
#isReleaseBuild() build-time Git
+   * information} is available, when Polaris has been built with the Gradle 
{@code -Prelease}
+   * project property.
+   *
+   * <p>Example value: {@code d417725ec7c88c1ee8f940ffb2ce72aec7fb2a17}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildGitHead() {
+    return PolarisVersionJarInfo.buildInfo(MF_BUILD_GIT_HEAD);
+  }
+
+  /**
+   * Returns the output of {@code git describe --tags} as in the jar manifest, 
if {@linkplain
+   * #isReleaseBuild() build-time Git information} is available, when Polaris 
has been built with
+   * the Gradle {@code -Prelease} project property.
+   *
+   * <p>Example value: {@code apache-polaris-0.1.2-incubating}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildGitTag() {
+    return PolarisVersionJarInfo.buildInfo(MF_BUILD_GIT_DESCRIBE);
+  }
+
+  /**
+   * Returns the Java version used during the build as in the jar manifest, if 
{@linkplain
+   * #isReleaseBuild() build-time Git information} is available, when Polaris 
has been built with
+   * the Gradle {@code -Prelease} project property.
+   *
+   * <p>Example value: {@code 21.0.5}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildJavaVersion() {
+    return PolarisVersionJarInfo.buildInfo(MF_BUILD_JAVA_VERSION);
+  }
+
+  /**
+   * Returns information about the system that performed the build, if 
{@linkplain #isReleaseBuild()
+   * build-time Git information} is available, when Polaris has been built 
with the Gradle {@code
+   * -Prelease} project property.
+   *
+   * <p>Example value: {@code 21.0.5}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildSystem() {
+    return PolarisVersionJarInfo.buildInfo(MF_BUILD_SYSTEM);
+  }
+
+  /**
+   * Returns the build timestamp as in the jar manifest, if {@linkplain 
#isReleaseBuild() build-time
+   * Git information} is available, when Polaris has been built with the 
Gradle {@code -Prelease}
+   * project property.
+   *
+   * <p>Example value: {@code 2024-12-16-11:54:05+01:00}
+   *
+   * @see #isReleaseBuild()
+   */
+  public static Optional<String> getBuildTimestamp() {
+    return PolarisVersionJarInfo.buildInfo(MF_BUILD_TIMESTAMP);
+  }
+
+  public static String readNoticeFile() {
+    return readResource("NOTICE");
+  }
+
+  public static String readSourceLicenseFile() {
+    return readResource("LICENSE");
+  }
+
+  public static String readBinaryLicenseFile() {
+    return readResource("LICENSE-BINARY-DIST");
+  }
+
+  private static final String MF_VERSION = "Apache-Polaris-Version";
+  private static final String MF_IS_RELEASE = "Apache-Polaris-Is-Release";
+  private static final String MF_BUILD_GIT_HEAD = 
"Apache-Polaris-Build-Git-Head";
+  private static final String MF_BUILD_GIT_DESCRIBE = 
"Apache-Polaris-Build-Git-Describe";
+  private static final String MF_BUILD_TIMESTAMP = 
"Apache-Polaris-Build-Timestamp";
+  private static final String MF_BUILD_SYSTEM = "Apache-Polaris-Build-System";
+  private static final String MF_BUILD_JAVA_VERSION = 
"Apache-Polaris-Build-Java-Version";
+  private static final List<String> MF_ALL =
+      List.of(
+          MF_VERSION,
+          MF_IS_RELEASE,
+          MF_BUILD_GIT_HEAD,
+          MF_BUILD_GIT_DESCRIBE,
+          MF_BUILD_TIMESTAMP,
+          MF_BUILD_SYSTEM,
+          MF_BUILD_JAVA_VERSION);
+
+  static String readResource(String resource) {
+    var fullResource = format("/META-INF/resources/apache-polaris/%s.txt", 
resource);
+    var resourceUrl =
+        requireNonNull(
+            PolarisVersion.class.getResource(fullResource),
+            "Resource " + fullResource + " does not exist");
+    try (InputStream in = resourceUrl.openConnection().getInputStream()) {
+      return new String(in.readAllBytes(), UTF_8);
+    } catch (IOException e) {
+      throw new RuntimeException("Failed to load resource " + fullResource + " 
resource", e);
+    }
+  }
+
+  // Couple inner classes to leverage Java's class loading mechanism as 
singletons and for
+  // initialization. Package-protected for testing.
+
+  static final class PolarisVersionJarInfo {
+    static final Map<String, String> BUILD_INFO = new HashMap<>();
+
+    static Optional<String> buildInfo(String key) {
+      return Optional.ofNullable(BUILD_INFO.get(key));
+    }
+
+    static {
+      loadManifest("MANIFEST.MF");
+    }
+
+    static void loadManifest(String manifestFile) {

Review Comment:
   Sure, but that would break the fake-manifest test



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to