Merge 7470 from the branch

This commit is contained in:
jeb228 2011-02-11 21:55:15 +00:00
parent 82047bb17a
commit 22a2dc75d9
3 changed files with 89 additions and 36 deletions

View file

@ -6,14 +6,17 @@ import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import edu.cornell.mannlib.vitro.utilities.revisioninfo.ProcessRunner.ProcessException;
/**
* Get release and revision information to display on screen.
* Get release and revision information to display on screen. Put this
* information into a single line and append it to the specified file.
*
* Ask Subversion for the information. If Subversion is available, and if this
* is a working directory, then we can build the info from the responses we get
@ -27,6 +30,34 @@ import edu.cornell.mannlib.vitro.utilities.revisioninfo.ProcessRunner.ProcessExc
* "productName ~ unknown ~ unknown"
*/
public class RevisionInfoBuilder {
/**
* Indicates a problem with the command-line arguments.
*/
private static class UsageException extends Exception {
UsageException(String message) {
super(message);
}
}
/**
* An object that holds the revision information and a message about how we
* obtained it.
*/
private static class Results {
final String message;
final String infoLine;
Results(String message, String infoLine) {
this.message = message;
this.infoLine = infoLine;
}
public String toString() {
return message + ": " + infoLine;
}
}
private static final String SVN_DIRECTORY_NAME = ".svn";
private static final String[] SVNVERSION_COMMAND = { "svnversion", "." };
private static final String[] SVN_INFO_COMMAND = { "svn", "info" };
@ -35,51 +66,63 @@ public class RevisionInfoBuilder {
private final String productName;
private final File productDirectory;
private final File resultFile;
public RevisionInfoBuilder(String[] args) {
if (args.length != 2) {
throw new IllegalArgumentException(
"RevisionInfoBuilder requires 2 arguments, not "
private Results results;
public RevisionInfoBuilder(String[] args) throws UsageException {
if (args.length != 3) {
throw new UsageException(
"RevisionInfoBuilder requires 3 arguments, not "
+ args.length);
}
productName = args[0];
productDirectory = new File(args[1]);
resultFile = new File(args[2]);
if (!productDirectory.isDirectory()) {
throw new IllegalArgumentException("Directory '"
throw new UsageException("Directory '"
+ productDirectory.getAbsolutePath() + "' does not exist.");
}
if (!resultFile.getParentFile().exists()) {
throw new UsageException("Result file '"
+ resultFile.getAbsolutePath()
+ "' does not exist, and we can't create it "
+ "because it's parent directory doesn't exist either.");
}
}
private String buildInfo() {
String infoLine;
infoLine = buildInfoFromSubversion();
if (infoLine == null) {
infoLine = buildInfoFromFile();
private void buildInfo() {
results = buildInfoFromSubversion();
if (results == null) {
results = buildInfoFromFile();
}
if (infoLine == null) {
infoLine = buildDummyInfo();
if (results == null) {
results = buildDummyInfo();
}
return infoLine;
}
private String buildInfoFromSubversion() {
private Results buildInfoFromSubversion() {
if (!isThisASubversionWorkspace()) {
System.out.println("Not a Subversion workspace");
return null;
}
String release = assembleReleaseNameFromSubversion();
if (release == null) {
System.out.println("Couldn't get release name from Subversion");
return null;
}
String revision = obtainRevisionLevelFromSubversion();
if (revision == null) {
System.out.println("Couldn't get revision level from Subversion");
return null;
}
return buildLine(release, revision);
return new Results("Info from Subversion", buildLine(release, revision));
}
private boolean isThisASubversionWorkspace() {
@ -119,7 +162,7 @@ public class RevisionInfoBuilder {
}
}
private String buildInfoFromFile() {
private Results buildInfoFromFile() {
try {
File revisionInfoFile = new File(productDirectory,
REVISION_INFO_FILENAME);
@ -136,14 +179,16 @@ public class RevisionInfoBuilder {
throw new EOFException("No revision line in file.");
}
return buildLine(release, revision);
return new Results("Info from file", buildLine(release, revision));
} catch (IOException e) {
System.out.println("No information from file: " + e);
return null;
}
}
private String buildDummyInfo() {
return buildLine(null, null);
private Results buildDummyInfo() {
String line = buildLine(null, null);
return new Results("Using dummy info", line);
}
private String buildLine(String release, String revision) {
@ -157,13 +202,27 @@ public class RevisionInfoBuilder {
+ INFO_LINE_DELIMITER + revision.trim();
}
private void writeLine() throws IOException {
Writer writer = null;
writer = new FileWriter(resultFile, true);
writer.write(results.infoLine + "\n");
writer.close();
System.out.println(results);
}
public static void main(String[] args) {
try {
System.out.println(new RevisionInfoBuilder(args).buildInfo());
} catch (Exception e) {
e.printStackTrace(System.err);
RevisionInfoBuilder builder = new RevisionInfoBuilder(args);
builder.buildInfo();
builder.writeLine();
} catch (UsageException e) {
System.err.println(e);
System.err.println("usage: RevisionInfoBuilder [product_name] "
+ "[product_directory] [supplied_values]");
+ "[product_directory] [output_file]");
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

View file

@ -287,15 +287,12 @@ deploy - Deploy the application directly into the Tomcat webapps directory.
<java classname="edu.cornell.mannlib.vitro.utilities.revisioninfo.RevisionInfoBuilder"
fork="no"
failonerror="true"
logerror="true"
outputproperty="revisionInfo.outputLine">
failonerror="true">
<classpath refid="test.run.classpath" />
<arg value="${ant.project.name}" />
<arg file="${revisionInfo.product.dir}" />
<arg file="${revisionInfo.build.file}" />
</java>
<echo file="${revisionInfo.build.file}" append="true">${revisionInfo.outputLine}
</echo>
</target>
<!-- =================================

View file

@ -204,15 +204,12 @@
<java classname="edu.cornell.mannlib.vitro.utilities.revisioninfo.RevisionInfoBuilder"
fork="no"
failonerror="true"
logerror="true"
outputproperty="revisionInfo.outputLine">
failonerror="true">
<classpath refid="test.run.classpath" />
<arg value="${ant.project.name}" />
<arg file="${revisionInfo.product.dir}" />
<arg file="${revisionInfo.build.file}" />
</java>
<echo file="${revisionInfo.build.file}" append="true">${revisionInfo.outputLine}
</echo>
</target>
<!-- =================================