Skip to content

Commit

Permalink
Show notice when new version of Nanobench is available
Browse files Browse the repository at this point in the history
  • Loading branch information
tkuhn committed Mar 11, 2021
1 parent 12f9a13 commit 42cf185
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -105,7 +105,7 @@
<build>
<resources>
<resource>
<filtering>false</filtering>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/petapico/nanobench/HomePage.html
Expand Up @@ -16,6 +16,8 @@

<h2>Welcome to Nanobench!</h2>

<p><span wicket:id="warning" class="negative"></span></p>

<p><span wicket:id="text"></span></p>

</div>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/petapico/nanobench/HomePage.java
Expand Up @@ -11,6 +11,17 @@ public class HomePage extends WebPage {

public HomePage(final PageParameters parameters) {
add(new TitleBar("titlebar"));
String v = WicketApplication.getThisVersion();
String lv = WicketApplication.getLatestVersion();
if (v.endsWith("-SNAPSHOT")) {
add(new Label("warning", "You are running a temporary snapshot version of Nanobench (" + v + "). The latest public version is " + lv + "."));
} else if (lv != null && !v.equals(lv)) {
add(new Label("warning", "There is a new version available: " + lv + ". You are currently using " + v + ". " +
"Run 'update' (Unix/Mac) or 'update-under-windows.bat' (Windows) to update to the latest version, or manually download it " +
"<a href=\"" + WicketApplication.LATEST_RELEASE_URL + "\">here</a>.").setEscapeModelStrings(false));
} else {
add(new Label("warning", ""));
}
if (ProfilePage.isComplete()) {
add(new Label("text", "Click on the menu items above to explore or publish nanopublications."));
} else {
Expand Down
103 changes: 68 additions & 35 deletions src/main/java/org/petapico/nanobench/WicketApplication.java
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -18,7 +19,7 @@

public class WicketApplication extends WebApplication {

private static final String LATEST_RELEASE_URL = "https://github.com/peta-pico/nanobench/releases/latest";
protected static final String LATEST_RELEASE_URL = "https://github.com/peta-pico/nanobench/releases/latest";

public WicketApplication() {
String runParam = System.getProperty("nanobench.run");
Expand All @@ -33,42 +34,29 @@ public WicketApplication() {
System.err.println("----------------------------------------");
System.err.println("");
try {
HttpResponse resp = HttpClientBuilder.create().build().execute(new HttpGet(LATEST_RELEASE_URL));
int c = resp.getStatusLine().getStatusCode();
if (c < 200 || c >= 300) throw new RuntimeException("HTTP error: " + c);
BufferedReader reader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
while(reader.ready()) {
String line = reader.readLine();
if (line.matches(".*/download/nanobench-[0-9]+\\.[0-9]+/nanobench-[0-9]+\\.[0-9]+\\.zip.*")) {
System.err.println(line);
String version = line.replaceFirst(".*/download/nanobench-[0-9]+\\.[0-9]+/nanobench-([0-9]+\\.[0-9]+)\\.zip.*", "$1");
System.err.println("Found latest version: nanobench-" + version);
String url = "https://github.com/peta-pico/nanobench/releases/download/nanobench-" + version + "/nanobench-" + version + ".zip";
System.err.println("Downloading " + url);
HttpResponse resp2 = HttpClientBuilder.create().build().execute(new HttpGet(url));
int c2 = resp2.getStatusLine().getStatusCode();
if (c2 < 200 || c2 >= 300) throw new RuntimeException("HTTP error: " + c2);
File zipFile = new File(System.getProperty("user.dir") + "/nanobench.zip");
FileOutputStream fileOut = new FileOutputStream(zipFile);
resp2.getEntity().writeTo(fileOut);
fileOut.close();
System.err.println("Unzipping " + zipFile);
ZipUtil.extract(zipFile, new File(System.getProperty("user.dir")));
zipFile.delete();
reader.close();
System.err.println("");
System.err.println("----------------------------------------");
System.err.println(" Nanobench updated successfully");
System.err.println("----------------------------------------");
System.err.println("");
System.exit(0);
}
}
reader.close();
System.err.println("Failed to update Nanobench");
System.exit(1);
String version = getLatestVersion();
System.err.println("Found latest version: nanobench-" + version);
String url = "https://github.com/peta-pico/nanobench/releases/download/nanobench-" + version + "/nanobench-" + version + ".zip";
System.err.println("Downloading " + url);
HttpResponse resp2 = HttpClientBuilder.create().build().execute(new HttpGet(url));
int c2 = resp2.getStatusLine().getStatusCode();
if (c2 < 200 || c2 >= 300) throw new RuntimeException("HTTP error: " + c2);
File zipFile = new File(System.getProperty("user.dir") + "/nanobench.zip");
FileOutputStream fileOut = new FileOutputStream(zipFile);
resp2.getEntity().writeTo(fileOut);
fileOut.close();
System.err.println("Unzipping " + zipFile);
ZipUtil.extract(zipFile, new File(System.getProperty("user.dir")));
zipFile.delete();
System.err.println("");
System.err.println("----------------------------------------");
System.err.println(" Nanobench updated successfully");
System.err.println("----------------------------------------");
System.err.println("");
System.exit(0);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println("Failed to update Nanobench");
System.exit(1);
}
}
Expand All @@ -83,10 +71,15 @@ public WicketApplication() {
ex.printStackTrace();
}
}
String v = getThisVersion();
String lv = getLatestVersion();
System.err.println("");
System.err.println("----------------------------------------");
System.err.println(" Nanobench");
System.err.println("----------------------------------------");
System.err.println(" You are using version: " + v);
System.err.println(" Latest public version: " + lv);
System.err.println("----------------------------------------");
System.err.println(" Your browser should show the Nanobench");
System.err.println(" interface in a few seconds.");
System.err.println("");
Expand Down Expand Up @@ -123,4 +116,44 @@ public RuntimeConfigurationType getConfigurationType() {
return RuntimeConfigurationType.DEPLOYMENT;
}


private static String latestVersion = null;

protected static String getLatestVersion() {
if (latestVersion != null) return latestVersion;
BufferedReader reader = null;
try {
HttpResponse resp = HttpClientBuilder.create().build().execute(new HttpGet(LATEST_RELEASE_URL));
int c = resp.getStatusLine().getStatusCode();
if (c < 200 || c >= 300) throw new RuntimeException("HTTP error: " + c);
reader = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
while(reader.ready()) {
String line = reader.readLine();
if (line.matches(".*/download/nanobench-[0-9]+\\.[0-9]+/nanobench-[0-9]+\\.[0-9]+\\.zip.*")) {
System.err.println(line);
latestVersion = line.replaceFirst(".*/download/nanobench-[0-9]+\\.[0-9]+/nanobench-([0-9]+\\.[0-9]+)\\.zip.*", "$1");
break;
}
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return latestVersion;
}

protected final static Properties properties = new Properties();

static {
try {
properties.load(WicketApplication.class.getClassLoader().getResourceAsStream("nanobench.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

protected static String getThisVersion() {
return properties.getProperty("nanobench.version");
}

}
1 change: 1 addition & 0 deletions src/main/resources/nanobench.properties
@@ -0,0 +1 @@
nanobench.version=${project.version}

0 comments on commit 42cf185

Please sign in to comment.