Skip to content

Commit

Permalink
Merge pull request #204 from pmpowers-usgs/versioning-203
Browse files Browse the repository at this point in the history
Added version storage and reporting
  • Loading branch information
pmpowers-usgs committed Mar 27, 2017
2 parents 413157b + 7df0b78 commit 632d854
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
26 changes: 24 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
nshmp-haz is a platform for conducting seismic hazard calculations
</description>

<!-- PROPERTIES -->
<property name="src" location="src" />
<property name="test" location="test" />
<property name="lib" location="lib" />
Expand All @@ -14,6 +13,8 @@
<property name="docs" location="docs" />
<property name="javadoc" location="${docs}/javadoc" />

<property name="app.properties" location="${dist}/app.properties" />
<available file=".git" type="dir" property="git.present" />

<property name="reports" location="${docs}/reports" />
<property name="reports.xml" location="${reports}/junit-xml" />
Expand Down Expand Up @@ -56,14 +57,35 @@
and internal package; need to check dependencies
-->

<target name="jar" depends="compile.source">
<target name="jar" depends="compile.source,set.app.properties">
<jar destfile="${dist}/nshmp-haz.jar">
<fileset dir="${classes}" />
<fileset file="${app.properties}" />
<zipfileset src="${guava.jar}" excludes="**/META-INF/**" />
<zipfileset src="${gson.jar}" />
</jar>
<delete file="${app.properties}" />
</target>

<target name="set.app.properties" depends="check.git">
<condition property="app.version" value="${git.tag}" else="unknown">
<isset property="git.tag" />
</condition>
<echo>app.version=${app.version}</echo>
<echo file="${app.properties}">app.version=${app.version}</echo>
<echo file="${app.properties}" append="true">${line.separator}</echo>
</target>

<target name="check.git" if="git.present">
<exec executable="git"
outputproperty="git.tag"
failifexecutionfails="false">
<arg value="describe" />
<arg value="--tags" />
</exec>
</target>


<target name="compile">
<mkdir dir="${classes}" />
<javac srcdir="${param.srcdir}"
Expand Down
6 changes: 4 additions & 2 deletions src/org/opensha2/DeaggCalc.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static Optional<String> run(String[] args) {
fh.setFormatter(new Logging.ConsoleFormatter());
log.getParent().addHandler(fh);

log.info(PROGRAM + ": initializing...");
log.info(PROGRAM + ": " + HazardCalc.VERSION);
Path modelPath = Paths.get(args[0]);
HazardModel model = HazardModel.load(modelPath);

Expand Down Expand Up @@ -199,7 +199,9 @@ public static Deaggregation calc(

private static final String USAGE = new StringBuilder()
.append(NEWLINE)
.append(PROGRAM).append(" usage:").append(NEWLINE)
.append(PROGRAM).append(" [").append(HazardCalc.VERSION).append("]").append(NEWLINE)
.append(NEWLINE)
.append("Usage:").append(NEWLINE)
.append(" ").append(USAGE_COMMAND).append(NEWLINE)
.append(NEWLINE)
.append("Where:").append(NEWLINE)
Expand Down
42 changes: 39 additions & 3 deletions src/org/opensha2/HazardCalc.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
import com.google.common.base.Optional;
import com.google.common.base.Throwables;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -89,7 +93,7 @@ static Optional<String> run(String[] args) {
fh.setFormatter(new Logging.ConsoleFormatter());
log.getParent().addHandler(fh);

log.info(PROGRAM + ": initializing...");
log.info(PROGRAM + ": " + VERSION);
Path modelPath = Paths.get(args[0]);
HazardModel model = HazardModel.load(modelPath);

Expand Down Expand Up @@ -229,7 +233,12 @@ public static Hazard calc(
throw new RuntimeException(e);
}
}


/**
* The Git application version. This version string applies to all other
* nshnmp-haz applications.
*/
public static final String VERSION = version();

private static final String PROGRAM = HazardCalc.class.getSimpleName();
private static final String USAGE_COMMAND =
Expand All @@ -238,9 +247,36 @@ public static Hazard calc(
private static final String USAGE_URL2 = "https://github.com/usgs/nshmp-haz/tree/master/etc";
private static final String SITE_STRING = "name,lon,lat[,vs30,vsInf[,z1p0,z2p5]]";

private static String version() {
String version = "unknown";
/* Assume we're running from a jar. */
try {
InputStream is = HazardCalc.class.getResourceAsStream("/app.properties");
Properties props = new Properties();
props.load(is);
is.close();
version = props.getProperty("app.version");
} catch (Exception e1) {
/* Otherwise check for a repository. */
Path gitDir = Paths.get(".git");
if (Files.exists(gitDir)) {
try {
Process pr = Runtime.getRuntime().exec("git describe --tags");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
version = br.readLine();
br.close();
/* Detached from repository. */
} catch (Exception e2) {}
}
}
return version;
}

private static final String USAGE = new StringBuilder()
.append(NEWLINE)
.append(PROGRAM).append(" usage:").append(NEWLINE)
.append(PROGRAM).append(" [").append(VERSION).append("]").append(NEWLINE)
.append(NEWLINE)
.append("Usage:").append(NEWLINE)
.append(" ").append(USAGE_COMMAND).append(NEWLINE)
.append(NEWLINE)
.append("Where:").append(NEWLINE)
Expand Down

0 comments on commit 632d854

Please sign in to comment.