Put build number into version number

This commit is contained in:
jendave
2011-10-22 07:19:59 +00:00
parent 274fa95e92
commit 72e32e28ca

View File

@@ -1,11 +1,8 @@
package forge.model; package forge.model;
import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@@ -21,20 +18,24 @@ public class BuildInfo {
public static final Charset US_ASCII_CHARSET = Charset.forName("US-ASCII"); public static final Charset US_ASCII_CHARSET = Charset.forName("US-ASCII");
/** Convenience for file.separator. */ /** Convenience for file.separator. */
/*private static final String FILE_SEP = System.getProperty("file.separator");*/ /*
* private static final String FILE_SEP =
* System.getProperty("file.separator");
*/
/** Convenience for path.separator. */ /** Convenience for path.separator. */
/*private static final String PATH_SEP = System.getProperty("path.separator");*/ /*
* private static final String PATH_SEP =
* System.getProperty("path.separator");
*/
/* /*
private static final Pattern FORGE_JAR_REGEX_2G = // NOPMD by Braids on 8/12/11 10:18 AM * private static final Pattern FORGE_JAR_REGEX_2G = // NOPMD by Braids on
Pattern.compile("^(.*" + Pattern.quote(FILE_SEP) + ")?" * 8/12/11 10:18 AM Pattern.compile("^(.*" + Pattern.quote(FILE_SEP) + ")?"
+ Pattern.quote("forge-") * + Pattern.quote("forge-") + "([^" + Pattern.quote(FILE_SEP) +
+ "([^" + Pattern.quote(FILE_SEP) + Pattern.quote(PATH_SEP) + "]*)" * Pattern.quote(PATH_SEP) + "]*)" + Pattern.quote("-with-dependencies.jar")
+ Pattern.quote("-with-dependencies.jar") + "$", * + "$", Pattern.CASE_INSENSITIVE);
Pattern.CASE_INSENSITIVE); */
*/
private transient String pathToForgeJar; private transient String pathToForgeJar;
@@ -52,9 +53,10 @@ public class BuildInfo {
/** /**
* Unit-testable constructor which allows a specific jar file to be set. * Unit-testable constructor which allows a specific jar file to be set.
* *
* Dependency injection! Relax, this won't hurt a bit. * Dependency injection! Relax, this won't hurt a bit.
* *
* @param pathToMockJarFile where to find the mock Forge jar * @param pathToMockJarFile
* where to find the mock Forge jar
*/ */
public BuildInfo(final String pathToMockJarFile) { public BuildInfo(final String pathToMockJarFile) {
pathToForgeJar = pathToMockJarFile; pathToForgeJar = pathToMockJarFile;
@@ -64,53 +66,20 @@ public class BuildInfo {
* Get the current build ID for Forge. * Get the current build ID for Forge.
* *
* @return a String representing the build identifier, or null if we could * @return a String representing the build identifier, or null if we could
* not determine the value. * not determine the value.
*/ */
public final String getBuildID() { public final String getBuildID() {
String manifestResult; String manifestResult = "0000";
String result; String[] manifestResultArray;
try { String result = "0000";
manifestResult = getManifestAttribute("Implementation-Build");
} catch (IOException exn1) { String version = getVersion();
manifestResult = null; // NOPMD by Braids on 8/12/11 10:21 AM manifestResultArray = version.split("r");
} manifestResult = manifestResultArray[manifestResultArray.length - 1];
if (manifestResult == null) { if (manifestResult == null) {
// Try getting the SVN version number by running the svnversion result = "0000";
// command. This is a long shot, but it works on some developers' } else {
// systems.
Process proc = null;
BufferedReader reader = null;
try {
String cmd[] = {"svnversion", "src"};
proc = Runtime.getRuntime().exec(cmd, null, null);
final InputStream procStdoutStream = proc.getInputStream();
final Reader procReader = new InputStreamReader(procStdoutStream, US_ASCII_CHARSET);
reader = new BufferedReader(procReader);
result = reader.readLine(); // may be null
} catch (IOException exn2) {
System.out.println("BuildInfo - Runtime.exec_IOException - " + exn2.getMessage());
result = null; // NOPMD by Braids on 8/12/11 10:21 AM
} finally {
try {
reader.close();
} catch (Throwable exn3) { // NOPMD by Braids on 8/12/11 10:21 AM
// ignored
System.out.println("BuildInfo - reader.close_Throwable - " + exn3.getMessage());
}
try {
proc.destroy();
} catch (Throwable exn4) { // NOPMD by Braids on 8/12/11 10:21 AM
// ignored
System.out.println("BuildInfo - proc.destroy_Throwable - " + exn4.getMessage());
}
}
}
else {
result = manifestResult; result = manifestResult;
} }
@@ -120,24 +89,17 @@ public class BuildInfo {
/** /**
* Get the current version of Forge. * Get the current version of Forge.
* *
* @return a String representing the version specifier, or "SVN" if * @return a String representing the version specifier, or "SVN" if unknown.
* unknown.
*/ */
public final String getVersion() { public final String getVersion() {
String manifestResult; String manifestResult;
String result; String result;
//try { manifestResult = BuildInfo.class.getPackage().getImplementationVersion();
//manifestResult = getManifestAttribute("Implementation-Version");
manifestResult = BuildInfo.class.getPackage().getImplementationVersion();
//} catch (IOException exn) {
// manifestResult = null; // NOPMD by Braids on 8/12/11 10:21 AM
//}
if (manifestResult == null) { if (manifestResult == null) {
result = "SVN"; result = "SVN";
} } else {
else {
result = manifestResult; result = manifestResult;
} }
@@ -147,9 +109,11 @@ public class BuildInfo {
/** /**
* Fetch an attribute from the Forge main jar's manifest. * Fetch an attribute from the Forge main jar's manifest.
* *
* @param manifestAttrName the name of the attribute you want from the manifest * @param manifestAttrName
* the name of the attribute you want from the manifest
* @return the attribute's value, which may be empty or null * @return the attribute's value, which may be empty or null
* @throws IOException if a (unique) Forge jar could not be found * @throws IOException
* if a (unique) Forge jar could not be found
*/ */
protected final String getManifestAttribute(final String manifestAttrName) throws IOException { protected final String getManifestAttribute(final String manifestAttrName) throws IOException {
String result = null; String result = null;
@@ -168,31 +132,23 @@ public class BuildInfo {
} }
/* /*
if (result == null && pathToForgeJar == null) { * if (result == null && pathToForgeJar == null) {
*
// Try to find a unique Forge jar in the class path. * // Try to find a unique Forge jar in the class path.
*
final String classPath = System.getProperty("java.class.path"); * final String classPath = System.getProperty("java.class.path");
final String[] paths = classPath.split(PATH_SEP); * final String[] paths = classPath.split(PATH_SEP);
*
for (String path : paths) { * for (String path : paths) { final Matcher matcher =
final Matcher matcher = FORGE_JAR_REGEX_2G.matcher(path); * FORGE_JAR_REGEX_2G.matcher(path);
*
if (matcher.matches()) { * if (matcher.matches()) { if (pathToForgeJar == null) {
if (pathToForgeJar == null) { * pathToForgeJar = path; } else { // Error: we found more than one.
pathToForgeJar = path; * pathToForgeJar = null; // NOPMD by Braids on 8/12/11 10:21 AM
} *
else { * throw new MultipleForgeJarsFoundError( "Classpath = " +
// Error: we found more than one. * System.getProperty("java.class.path")); } } } }
pathToForgeJar = null; // NOPMD by Braids on 8/12/11 10:21 AM */
throw new MultipleForgeJarsFoundError(
"Classpath = " + System.getProperty("java.class.path"));
}
}
}
}
*/
if (result == null && pathToForgeJar == null) { if (result == null && pathToForgeJar == null) {
throw new FileNotFoundException( throw new FileNotFoundException(
@@ -209,8 +165,7 @@ public class BuildInfo {
result = getMainManifestAttribute(manifest, manifestAttrName); result = getMainManifestAttribute(manifest, manifestAttrName);
} }
} } finally {
finally {
try { try {
manifestStream.close(); manifestStream.close();
} catch (Throwable ignored) { // NOPMD by Braids on 8/12/11 10:21 AM } catch (Throwable ignored) { // NOPMD by Braids on 8/12/11 10:21 AM
@@ -231,8 +186,10 @@ public class BuildInfo {
* Convience method for fetching an attribute from the main section of a * Convience method for fetching an attribute from the main section of a
* jar's manifest. * jar's manifest.
* *
* @param manifest the manifest that provides attributes * @param manifest
* @param manifestAttrName the name of the attribute to fetch * the manifest that provides attributes
* @param manifestAttrName
* the name of the attribute to fetch
* @return the value of the attribute, or null if not set * @return the value of the attribute, or null if not set
*/ */
protected final String getMainManifestAttribute(final Manifest manifest, final String manifestAttrName) { protected final String getMainManifestAttribute(final Manifest manifest, final String manifestAttrName) {
@@ -254,16 +211,14 @@ public class BuildInfo {
String version; String version;
if (rawVersion == null) { if (rawVersion == null) {
version = "Unknown"; version = "Unknown";
} } else {
else {
version = rawVersion; version = rawVersion;
} }
String buildID; String buildID;
if (rawBuildID == null) { if (rawBuildID == null) {
buildID = "Unknown"; buildID = "Unknown";
} } else {
else {
buildID = rawBuildID; buildID = rawBuildID;
} }