More update-button wackiness

This commit is contained in:
4pr0n 2014-04-05 02:11:04 -07:00
parent 048e53058a
commit 4279087605
4 changed files with 77 additions and 33 deletions

View File

@ -4,7 +4,7 @@
<groupId>com.rarchives.ripme</groupId>
<artifactId>ripme</artifactId>
<packaging>jar</packaging>
<version>1.0.2-beta1</version>
<version>1.0.2</version>
<name>ripme</name>
<url>http://rip.rarchives.com</url>
<properties>

View File

@ -14,6 +14,7 @@ import org.apache.log4j.Logger;
import com.rarchives.ripme.ripper.AbstractRipper;
import com.rarchives.ripme.ui.MainWindow;
import com.rarchives.ripme.ui.UpdateUtils;
import com.rarchives.ripme.utils.Utils;
/**
@ -24,7 +25,7 @@ public class App {
public static final Logger logger = Logger.getLogger(App.class);
public static void main(String[] args) throws MalformedURLException {
logger.debug("Initialized");
logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion());
if (args.length > 0) {
CommandLine cl = handleArguments(args);

View File

@ -83,6 +83,7 @@ public class MainWindow implements Runnable, RipStatusHandler {
// TODO Configuration components
public MainWindow() {
UpdateUtils.moveUpdatedJar();
mainFrame = new JFrame(WINDOW_TITLE);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//mainFrame.setPreferredSize(new Dimension(400, 180));
@ -196,14 +197,14 @@ public class MainWindow implements Runnable, RipStatusHandler {
configurationPanel.setVisible(false);
configurationPanel.setPreferredSize(new Dimension(300, 250));
// TODO Configuration components
JLabel configLabel = new JLabel("Version: " + Utils.getConfigInteger("version.major", 0) + "." + Utils.getConfigInteger("version.minor", 0) + "." + Utils.getConfigInteger("version.build", 0));
JLabel configLabel = new JLabel("Version: " + UpdateUtils.getThisJarVersion());
configurationPanel.add(configLabel);
configUpdateButton = new JButton("Check for updates");
configUpdateLabel = new JLabel("");
gbc.ipady = 0;
gbc.gridy = 1;
configurationPanel.add(configUpdateButton, gbc);
gbc.gridy = 2;
gbc.ipady = 10;
configurationPanel.add(configUpdateLabel, gbc);
gbc.gridy = 0; pane.add(ripPanel, gbc);

View File

@ -1,10 +1,12 @@
package com.rarchives.ripme.ui;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JLabel;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Response;
@ -13,9 +15,21 @@ import org.jsoup.nodes.Document;
public class UpdateUtils {
private static final String DEFAULT_VERSION = "1.0.0";
private static final Logger logger = Logger.getLogger(UpdateUtils.class);
private static final String DEFAULT_VERSION = "100.0.0";
private static final String updateJsonURL = "http://rarchives.com/ripme.json";
private static final String updateJarURL = "http://rarchives.com/ripme.jar";
private static final String mainFileName = "ripme.jar";
private static final String updateFileName = "ripme.jar.update";
public static String getThisJarVersion() {
String thisVersion = UpdateUtils.class.getPackage().getImplementationVersion();
if (thisVersion == null) {
// Version is null if we're not running from the JAR
thisVersion = DEFAULT_VERSION; ; // Super-high version number
}
return thisVersion;
}
public static void updateProgram(JLabel configUpdateLabel) {
configUpdateLabel.setText("Checking for update...:");
@ -27,7 +41,7 @@ public class UpdateUtils {
.get();
} catch (IOException e) {
configUpdateLabel.setText("Error while fetching update: " + e.getMessage());
e.printStackTrace();
logger.error("Error while fetching update: ", e);
return;
}
String jsonString = doc.body().html().replaceAll("&quot;", "\"");
@ -42,43 +56,20 @@ public class UpdateUtils {
String latestVersion = json.getString("latestVersion");
if (UpdateUtils.isNewerVersion(latestVersion)) {
configUpdateLabel.setText("<html>Newer version found! <br><br>" + configUpdateLabel.getText() + "</html>");
logger.info("New version found, downloading...");
try {
UpdateUtils.downloadJarAndReplace(updateJarURL);
} catch (IOException e) {
configUpdateLabel.setText("Error while updating: " + e.getMessage());
e.printStackTrace();
logger.error("Error while updating: ", e);
return;
}
} else {
configUpdateLabel.setText("Running latest version: " + UpdateUtils.getThisJarVersion());
logger.info("Running latest version: " + UpdateUtils.getThisJarVersion());
}
}
private static void downloadJarAndReplace(String updateJarURL)
throws IOException {
String newFile = "ripme.jar.update";
Response response;
response = Jsoup.connect(updateJarURL)
.ignoreContentType(true)
.timeout(60000)
.maxBodySize(1024 * 1024 * 100)
.execute();
FileOutputStream out = new FileOutputStream(newFile);
out.write(response.bodyAsBytes());
out.close();
Runtime.getRuntime().exec(new String[] {"java", "-jar", newFile});
System.exit(0);
}
private static String getThisJarVersion() {
String thisVersion = UpdateUtils.class.getPackage().getImplementationVersion();
if (thisVersion == null) {
// Version is null if we're not running from the JAR
thisVersion = DEFAULT_VERSION; ; // Super-high version number
}
return thisVersion;
}
private static boolean isNewerVersion(String latestVersion) {
int[] oldVersions = versionStringToInt(getThisJarVersion());
int[] newVersions = versionStringToInt(latestVersion);
@ -88,11 +79,12 @@ public class UpdateUtils {
}
for (int i = 0; i < oldVersions.length; i++) {
System.err.println("Calculating: " + newVersions[i] + " <> " + oldVersions[i]);
if (newVersions[i] > oldVersions[i]) {
logger.debug("oldVersion " + getThisJarVersion() + " < latestVersion" + latestVersion);
return true;
}
else if (newVersions[i] < oldVersions[i]) {
logger.debug("oldVersion " + getThisJarVersion() + " > latestVersion " + latestVersion);
return false;
}
else {
@ -114,4 +106,54 @@ public class UpdateUtils {
}
return intVersions;
}
private static void downloadJarAndReplace(String updateJarURL)
throws IOException {
Response response;
response = Jsoup.connect(updateJarURL)
.ignoreContentType(true)
.timeout(60000)
.maxBodySize(1024 * 1024 * 100)
.execute();
FileOutputStream out = new FileOutputStream(updateFileName);
out.write(response.bodyAsBytes());
out.close();
Runtime.getRuntime().exec(new String[] {"java", "-jar", updateFileName});
System.exit(0);
}
public static void moveUpdatedJar() {
File newFile = new File(updateFileName);
File oldFile = new File(mainFileName);
if (!newFile.exists()) {
// Can't update without .update file
return;
}
if (oldFile.exists()) {
logger.info("Deleting existing .jar file: " + oldFile);
try {
oldFile.delete();
} catch (Exception e) {
logger.error("Failed to delete old jar file: " + oldFile);
return;
}
}
boolean success = newFile.renameTo(oldFile);
if (!success) {
logger.error("Failed to rename file from " + newFile.getAbsolutePath() + " to " + oldFile.getAbsolutePath());
return;
}
try {
logger.debug("Executing jar " + oldFile.getName());
Runtime.getRuntime().exec(new String[] {"java", "-jar", oldFile.getName()});
logger.info("Started new version, quitting old version...");
System.exit(0);
} catch (IOException e) {
logger.error("Error while executing new jar " + newFile, e);
return;
}
}
}