1
0
mirror of https://github.com/RipMeApp/ripme.git synced 2025-08-26 15:24:51 +02:00

Updater for Linux/Mac now in pure Java

This commit is contained in:
buzzlightmonth
2019-01-19 18:23:53 +01:00
parent b76b930d62
commit b9cc962a16

View File

@@ -259,38 +259,20 @@ public class UpdateUtils {
logger.info("Hash is good"); logger.info("Hash is good");
} }
} }
if (shouldLaunch) {
// Setup updater script
final String batchFile, script;
final String[] batchExec;
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
// Windows
batchFile = "update_ripme.bat";
String batchPath = new File(batchFile).getAbsolutePath();
script = "@echo off\r\n"
+ "timeout 1" + "\r\n"
+ "copy " + updateFileName + " " + mainFileName + "\r\n"
+ "del " + updateFileName + "\r\n"
+ "ripme.jar" + "\r\n"
+ "del " + batchPath + "\r\n";
batchExec = new String[]{batchPath};
} else { if (System.getProperty("os.name").toLowerCase().contains("win")) {
// Mac / Linux // Windows
batchFile = "update_ripme.sh"; final String batchFile = "update_ripme.bat";
String batchPath = new File(batchFile).getAbsolutePath(); final String batchPath = new File(batchFile).getAbsolutePath();
script = "#!/bin/sh\n" String script = "@echo off\r\n"
+ "sleep 1" + "\n" + "timeout 1\r\n"
+ "cd " + new File(mainFileName).getAbsoluteFile().getParent() + "\n" + "copy " + updateFileName + " " + mainFileName + "\r\n"
+ "cp -f " + updateFileName + " " + mainFileName + "\n" + "del " + updateFileName + "\r\n";
+ "rm -f " + updateFileName + "\n" if (shouldLaunch) {
+ "java -jar \"" + new File(mainFileName).getAbsolutePath() + "\" &\n" script += mainFileName + "\r\n";
+ "sleep 1" + "\n"
+ "rm -f " + batchPath + "\n";
batchExec = new String[]{"sh", batchPath};
} }
script += "del " + batchPath + "\r\n";
final String[] batchExec = new String[]{batchPath};
// Create updater script // Create updater script
try (BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile))) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile))) {
bw.write(script); bw.write(script);
@@ -311,9 +293,17 @@ public class UpdateUtils {
logger.info("Exiting older version, should execute update script (" + batchFile + ") during exit"); logger.info("Exiting older version, should execute update script (" + batchFile + ") during exit");
System.exit(0); System.exit(0);
} else { } else {
// Mac / Linux
// Modifying file and launching it: *nix distributions don't have any issues with modifying/deleting files
// while they are being run
new File(mainFileName).delete(); new File(mainFileName).delete();
new File(updateFileName).renameTo(new File(mainFileName)); new File(updateFileName).renameTo(new File(mainFileName));
if (shouldLaunch) {
// No need to do it during shutdown: the file used will indeed be the new one
Runtime.getRuntime().exec("java -jar " + mainFileName);
}
logger.info("Update installed, newer version should be executed upon relaunch");
System.exit(0);
} }
} }
} }