More update-button wackiness
This commit is contained in:
parent
048e53058a
commit
4279087605
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
|||||||
<groupId>com.rarchives.ripme</groupId>
|
<groupId>com.rarchives.ripme</groupId>
|
||||||
<artifactId>ripme</artifactId>
|
<artifactId>ripme</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>1.0.2-beta1</version>
|
<version>1.0.2</version>
|
||||||
<name>ripme</name>
|
<name>ripme</name>
|
||||||
<url>http://rip.rarchives.com</url>
|
<url>http://rip.rarchives.com</url>
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -14,6 +14,7 @@ import org.apache.log4j.Logger;
|
|||||||
|
|
||||||
import com.rarchives.ripme.ripper.AbstractRipper;
|
import com.rarchives.ripme.ripper.AbstractRipper;
|
||||||
import com.rarchives.ripme.ui.MainWindow;
|
import com.rarchives.ripme.ui.MainWindow;
|
||||||
|
import com.rarchives.ripme.ui.UpdateUtils;
|
||||||
import com.rarchives.ripme.utils.Utils;
|
import com.rarchives.ripme.utils.Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,7 +25,7 @@ public class App {
|
|||||||
public static final Logger logger = Logger.getLogger(App.class);
|
public static final Logger logger = Logger.getLogger(App.class);
|
||||||
|
|
||||||
public static void main(String[] args) throws MalformedURLException {
|
public static void main(String[] args) throws MalformedURLException {
|
||||||
logger.debug("Initialized");
|
logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion());
|
||||||
|
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
CommandLine cl = handleArguments(args);
|
CommandLine cl = handleArguments(args);
|
||||||
|
@ -83,6 +83,7 @@ public class MainWindow implements Runnable, RipStatusHandler {
|
|||||||
// TODO Configuration components
|
// TODO Configuration components
|
||||||
|
|
||||||
public MainWindow() {
|
public MainWindow() {
|
||||||
|
UpdateUtils.moveUpdatedJar();
|
||||||
mainFrame = new JFrame(WINDOW_TITLE);
|
mainFrame = new JFrame(WINDOW_TITLE);
|
||||||
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
//mainFrame.setPreferredSize(new Dimension(400, 180));
|
//mainFrame.setPreferredSize(new Dimension(400, 180));
|
||||||
@ -196,14 +197,14 @@ public class MainWindow implements Runnable, RipStatusHandler {
|
|||||||
configurationPanel.setVisible(false);
|
configurationPanel.setVisible(false);
|
||||||
configurationPanel.setPreferredSize(new Dimension(300, 250));
|
configurationPanel.setPreferredSize(new Dimension(300, 250));
|
||||||
// TODO Configuration components
|
// 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);
|
configurationPanel.add(configLabel);
|
||||||
configUpdateButton = new JButton("Check for updates");
|
configUpdateButton = new JButton("Check for updates");
|
||||||
configUpdateLabel = new JLabel("");
|
configUpdateLabel = new JLabel("");
|
||||||
gbc.ipady = 0;
|
|
||||||
gbc.gridy = 1;
|
gbc.gridy = 1;
|
||||||
configurationPanel.add(configUpdateButton, gbc);
|
configurationPanel.add(configUpdateButton, gbc);
|
||||||
gbc.gridy = 2;
|
gbc.gridy = 2;
|
||||||
|
gbc.ipady = 10;
|
||||||
configurationPanel.add(configUpdateLabel, gbc);
|
configurationPanel.add(configUpdateLabel, gbc);
|
||||||
|
|
||||||
gbc.gridy = 0; pane.add(ripPanel, gbc);
|
gbc.gridy = 0; pane.add(ripPanel, gbc);
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package com.rarchives.ripme.ui;
|
package com.rarchives.ripme.ui;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.jsoup.Connection.Response;
|
import org.jsoup.Connection.Response;
|
||||||
@ -13,9 +15,21 @@ import org.jsoup.nodes.Document;
|
|||||||
|
|
||||||
public class UpdateUtils {
|
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 updateJsonURL = "http://rarchives.com/ripme.json";
|
||||||
private static final String updateJarURL = "http://rarchives.com/ripme.jar";
|
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) {
|
public static void updateProgram(JLabel configUpdateLabel) {
|
||||||
configUpdateLabel.setText("Checking for update...:");
|
configUpdateLabel.setText("Checking for update...:");
|
||||||
@ -27,7 +41,7 @@ public class UpdateUtils {
|
|||||||
.get();
|
.get();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
configUpdateLabel.setText("Error while fetching update: " + e.getMessage());
|
configUpdateLabel.setText("Error while fetching update: " + e.getMessage());
|
||||||
e.printStackTrace();
|
logger.error("Error while fetching update: ", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String jsonString = doc.body().html().replaceAll(""", "\"");
|
String jsonString = doc.body().html().replaceAll(""", "\"");
|
||||||
@ -42,43 +56,20 @@ public class UpdateUtils {
|
|||||||
String latestVersion = json.getString("latestVersion");
|
String latestVersion = json.getString("latestVersion");
|
||||||
if (UpdateUtils.isNewerVersion(latestVersion)) {
|
if (UpdateUtils.isNewerVersion(latestVersion)) {
|
||||||
configUpdateLabel.setText("<html>Newer version found! <br><br>" + configUpdateLabel.getText() + "</html>");
|
configUpdateLabel.setText("<html>Newer version found! <br><br>" + configUpdateLabel.getText() + "</html>");
|
||||||
|
logger.info("New version found, downloading...");
|
||||||
try {
|
try {
|
||||||
UpdateUtils.downloadJarAndReplace(updateJarURL);
|
UpdateUtils.downloadJarAndReplace(updateJarURL);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
configUpdateLabel.setText("Error while updating: " + e.getMessage());
|
configUpdateLabel.setText("Error while updating: " + e.getMessage());
|
||||||
e.printStackTrace();
|
logger.error("Error while updating: ", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
configUpdateLabel.setText("Running latest version: " + UpdateUtils.getThisJarVersion());
|
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) {
|
private static boolean isNewerVersion(String latestVersion) {
|
||||||
int[] oldVersions = versionStringToInt(getThisJarVersion());
|
int[] oldVersions = versionStringToInt(getThisJarVersion());
|
||||||
int[] newVersions = versionStringToInt(latestVersion);
|
int[] newVersions = versionStringToInt(latestVersion);
|
||||||
@ -88,11 +79,12 @@ public class UpdateUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < oldVersions.length; i++) {
|
for (int i = 0; i < oldVersions.length; i++) {
|
||||||
System.err.println("Calculating: " + newVersions[i] + " <> " + oldVersions[i]);
|
|
||||||
if (newVersions[i] > oldVersions[i]) {
|
if (newVersions[i] > oldVersions[i]) {
|
||||||
|
logger.debug("oldVersion " + getThisJarVersion() + " < latestVersion" + latestVersion);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (newVersions[i] < oldVersions[i]) {
|
else if (newVersions[i] < oldVersions[i]) {
|
||||||
|
logger.debug("oldVersion " + getThisJarVersion() + " > latestVersion " + latestVersion);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -114,4 +106,54 @@ public class UpdateUtils {
|
|||||||
}
|
}
|
||||||
return intVersions;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user