aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorShevek <[email protected]>2015-01-01 10:44:27 -0800
committerShevek <[email protected]>2015-01-01 10:44:27 -0800
commit3e0f34f77c1ea1f9b27b097efaa8bcf3dee19e4b (patch)
treeeee67b9123714cdc54f45b3f4c509e483b60c6d7 /src
parent86f7ce7b960f504f299b0e43ffcd6a179566144a (diff)
Fix #10 using build-time metadata.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/anarres/cpp/BuildMetadata.java72
-rw-r--r--src/main/java/org/anarres/cpp/Main.java6
-rw-r--r--src/main/velocity/org/anarres/cpp/Version.java12
-rw-r--r--src/test/java/org/anarres/cpp/BuildMetadataTest.java33
4 files changed, 120 insertions, 3 deletions
diff --git a/src/main/java/org/anarres/cpp/BuildMetadata.java b/src/main/java/org/anarres/cpp/BuildMetadata.java
new file mode 100644
index 0000000..79de407
--- /dev/null
+++ b/src/main/java/org/anarres/cpp/BuildMetadata.java
@@ -0,0 +1,72 @@
+package org.anarres.cpp;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import javax.annotation.Nonnull;
+
+/**
+ * Returns information about the build.
+ *
+ * @author shevek
+ */
+public class BuildMetadata {
+
+ public static final String RESOURCE = "/META-INF/jcpp.properties";
+ private static BuildMetadata INSTANCE;
+
+ /** @throws RuntimeException if the properties file cannot be found on the classpath. */
+ @Nonnull
+ public static synchronized BuildMetadata getInstance() {
+ try {
+ if (INSTANCE == null)
+ INSTANCE = new BuildMetadata();
+ return INSTANCE;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private final Properties properties = new Properties();
+
+ private BuildMetadata() throws IOException {
+ URL url = BuildMetadata.class.getResource(RESOURCE);
+ InputStream in = url.openStream();
+ try {
+ properties.load(in);
+ } finally {
+ in.close();
+ }
+ }
+
+ @Nonnull
+ public Map<? extends String, ? extends String> asMap() {
+ Map<String, String> out = new HashMap<String, String>();
+ for (Map.Entry<Object, Object> e : properties.entrySet())
+ out.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
+ return out;
+ }
+
+ @Nonnull
+ public com.github.zafarkhaja.semver.Version getVersion() {
+ return com.github.zafarkhaja.semver.Version.valueOf(properties.getProperty("Implementation-Version"));
+ }
+
+ @Nonnull
+ public Date getBuildDate() throws ParseException {
+ // Build-Date=2015-01-01_10:09:09
+ String text = properties.getProperty("Build-Date");
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
+ return format.parse(text);
+ }
+
+ public String getChangeId() {
+ return properties.getProperty("Change");
+ }
+}
diff --git a/src/main/java/org/anarres/cpp/Main.java b/src/main/java/org/anarres/cpp/Main.java
index 5a3e113..acf2436 100644
--- a/src/main/java/org/anarres/cpp/Main.java
+++ b/src/main/java/org/anarres/cpp/Main.java
@@ -25,7 +25,6 @@ import javax.annotation.Nonnull;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
-import joptsimple.ValueConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,7 +58,7 @@ public class Main {
"Displays command-line help.")
.forHelp();
OptionSpec<?> versionOption = parser.acceptsAll(Arrays.asList("version"),
- "Displays the product version (" + Version.getVersion() + ") and exits.")
+ "Displays the product version (" + BuildMetadata.getInstance().getVersion() + ") and exits.")
.forHelp();
OptionSpec<?> debugOption = parser.acceptsAll(Arrays.asList("debug"),
@@ -187,7 +186,8 @@ public class Main {
}
private static void version(@Nonnull PrintStream out) {
- out.println("Anarres Java C Preprocessor version " + Version.getVersion());
+ BuildMetadata metadata = BuildMetadata.getInstance();
+ out.println("Anarres Java C Preprocessor version " + metadata.getVersion() + " change-id " + metadata.getChangeId());
out.println("Copyright (C) 2008-2014 Shevek (http://www.anarres.org/).");
out.println("This is free software; see the source for copying conditions. There is NO");
out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
diff --git a/src/main/velocity/org/anarres/cpp/Version.java b/src/main/velocity/org/anarres/cpp/Version.java
index 62494b5..cedd6bb 100644
--- a/src/main/velocity/org/anarres/cpp/Version.java
+++ b/src/main/velocity/org/anarres/cpp/Version.java
@@ -17,6 +17,7 @@
package org.anarres.cpp;
+import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
@@ -35,12 +36,14 @@ public class Version {
private static final int major;
private static final int minor;
private static final int patch;
+ private static final String modifier;
static {
String[] tmp = VERSION.split("[\\.-]");
major = Integer.parseInt(tmp[0]);
minor = Integer.parseInt(tmp[1]);
patch = Integer.parseInt(tmp[2]);
+ modifier = (tmp.length > 3) ? tmp[3] : null;
}
@Nonnull
@@ -60,6 +63,15 @@ public class Version {
return patch;
}
+ @CheckForNull
+ public static String getModifier() {
+ return modifier;
+ }
+
+ public static boolean isSnapshot() {
+ return "SNAPSHOT".equalsIgnoreCase(getModifier());
+ }
+
public static void main(String[] args) {
System.out.println("Version " + VERSION);
System.out.println("getVersion() returns " + getVersion());
diff --git a/src/test/java/org/anarres/cpp/BuildMetadataTest.java b/src/test/java/org/anarres/cpp/BuildMetadataTest.java
new file mode 100644
index 0000000..42dc071
--- /dev/null
+++ b/src/test/java/org/anarres/cpp/BuildMetadataTest.java
@@ -0,0 +1,33 @@
+package org.anarres.cpp;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.Resources;
+import java.net.URL;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * @author shevek
+ */
+public class BuildMetadataTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(BuildMetadataTest.class);
+
+ @Test
+ public void testProperties() throws Exception {
+ URL url = Resources.getResource("META-INF/jcpp.properties");
+ String text = Resources.asCharSource(url, Charsets.ISO_8859_1).read();
+ LOG.info("Metadata is " + text);
+ }
+
+ @Test
+ public void testMetadata() throws Exception {
+ BuildMetadata metadata = BuildMetadata.getInstance();
+ LOG.info("Version is " + metadata.getVersion());
+ LOG.info("BuildDate is " + metadata.getBuildDate());
+ LOG.info("ChangeId is " + metadata.getChangeId());
+ }
+
+}