diff options
author | Shevek <[email protected]> | 2015-01-01 10:44:27 -0800 |
---|---|---|
committer | Shevek <[email protected]> | 2015-01-01 10:44:27 -0800 |
commit | 3e0f34f77c1ea1f9b27b097efaa8bcf3dee19e4b (patch) | |
tree | eee67b9123714cdc54f45b3f4c509e483b60c6d7 /src/main/java/org/anarres/cpp | |
parent | 86f7ce7b960f504f299b0e43ffcd6a179566144a (diff) |
Fix #10 using build-time metadata.
Diffstat (limited to 'src/main/java/org/anarres/cpp')
-rw-r--r-- | src/main/java/org/anarres/cpp/BuildMetadata.java | 72 | ||||
-rw-r--r-- | src/main/java/org/anarres/cpp/Main.java | 6 |
2 files changed, 75 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."); |