From bcf895e26d56bb9861d8f4a2c41c813e6d9842b7 Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 20 Mar 2018 22:51:42 +0200 Subject: [PATCH] SCL-functionality for build information querying refs #7828 Change-Id: Ic4b02ce9a205309a7f9a3fd38b0842c6b0597087 --- bundles/org.simantics/build.properties | 4 +- .../org.simantics/scl/Simantics/BuildInfo.scl | 11 +++ .../src/org/simantics/BuildInfo.java | 85 +++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 bundles/org.simantics/scl/Simantics/BuildInfo.scl create mode 100644 bundles/org.simantics/src/org/simantics/BuildInfo.java diff --git a/bundles/org.simantics/build.properties b/bundles/org.simantics/build.properties index b476c1fb3..3c7701d37 100644 --- a/bundles/org.simantics/build.properties +++ b/bundles/org.simantics/build.properties @@ -2,4 +2,6 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ .,\ - plugin.xml \ No newline at end of file + plugin.xml,\ + scl/ + diff --git a/bundles/org.simantics/scl/Simantics/BuildInfo.scl b/bundles/org.simantics/scl/Simantics/BuildInfo.scl new file mode 100644 index 000000000..22e90d7cd --- /dev/null +++ b/bundles/org.simantics/scl/Simantics/BuildInfo.scl @@ -0,0 +1,11 @@ +import "Map" as Map + +importJava "org.simantics.BuildInfo" where + @JavaName get + getBuildInfo :: String -> String + + @JavaName all + getAllBuildInfo :: String + + @JavaName asMap + getBuildInfoAsMap :: Map.T String String \ No newline at end of file diff --git a/bundles/org.simantics/src/org/simantics/BuildInfo.java b/bundles/org.simantics/src/org/simantics/BuildInfo.java new file mode 100644 index 000000000..d3585642b --- /dev/null +++ b/bundles/org.simantics/src/org/simantics/BuildInfo.java @@ -0,0 +1,85 @@ +package org.simantics; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; +import java.util.Properties; +import java.util.stream.Collectors; + +import org.eclipse.core.runtime.IProduct; +import org.eclipse.core.runtime.Platform; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Jani Simomaa + * + */ +public final class BuildInfo { + + private static final Logger LOGGER = LoggerFactory.getLogger(BuildInfo.class); + private static final Properties PROPS; + + public static final String DEFAULT_BUILDINFO_PROPERTIES_FILE = "buildinfo.properties"; + public static final String BUILDINFO_PROPERTIES_PROP = "buildinfo.properties.location"; + + public static final String BUILD_INFO_PROPERTIES_FILE = System.getProperty(BUILDINFO_PROPERTIES_PROP, + DEFAULT_BUILDINFO_PROPERTIES_FILE); + + static { + if (LOGGER.isDebugEnabled()) + LOGGER.debug("Trying to load build information from {}", BUILD_INFO_PROPERTIES_FILE); + Properties buildinfo = new Properties(); + Path propertiesFile = Paths.get(BUILD_INFO_PROPERTIES_FILE); + if (Files.exists(propertiesFile)) { + try { + buildinfo.load(Files.newInputStream(propertiesFile)); + if (LOGGER.isDebugEnabled()) + LOGGER.debug(buildinfo.toString()); + } catch (IOException e) { + LOGGER.error("Could not load build information from {}", propertiesFile.toAbsolutePath(), e); + } + } else { + if (LOGGER.isDebugEnabled()) + LOGGER.debug("Build information not loaded as {} does not exist", propertiesFile.toAbsolutePath()); + } + // add information from .product if possible + collectInformationFromActiveProduct(buildinfo); + + PROPS = buildinfo; + } + + private static void collectInformationFromActiveProduct(Properties buildinfo) { + IProduct product = Platform.getProduct(); + if (product != null) { + buildinfo.setProperty("product.name", product.getName()); + buildinfo.setProperty("product.application", product.getApplication()); + buildinfo.setProperty("product.description", product.getDescription()); + buildinfo.setProperty("product.id", product.getId()); + } + } + + /** + * @param key + * @return + */ + public static String get(String key) { + return PROPS.getProperty(key, ""); + } + + /** + * @return + */ + public static String all() { + return PROPS.toString(); + } + + /** + * @return + */ + public static Map asMap() { + return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v))); + } +} -- 2.47.1