]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
SCL-functionality for build information querying 09/1609/2
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 20 Mar 2018 20:51:42 +0000 (22:51 +0200)
committerjsimomaa <jani.simomaa@gmail.com>
Tue, 20 Mar 2018 21:04:47 +0000 (23:04 +0200)
refs #7828

Change-Id: Ic4b02ce9a205309a7f9a3fd38b0842c6b0597087

bundles/org.simantics/build.properties
bundles/org.simantics/scl/Simantics/BuildInfo.scl [new file with mode: 0644]
bundles/org.simantics/src/org/simantics/BuildInfo.java [new file with mode: 0644]

index b476c1fb379a6d62bc7a0ff842d7efa4503570ea..3c7701d37c42bd480e6121f17a7656f0bd7940b8 100644 (file)
@@ -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 (file)
index 0000000..22e90d7
--- /dev/null
@@ -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 (file)
index 0000000..d358564
--- /dev/null
@@ -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, "<not-set>");
+    }
+
+    /**
+     * @return
+     */
+    public static String all() {
+        return PROPS.toString();
+    }
+
+    /**
+     * @return
+     */
+    public static Map<String, String> asMap() {
+        return PROPS.entrySet().stream().collect(Collectors.toMap(k -> String.valueOf(k), v -> String.valueOf(v)));
+    }
+}