Skip to content

Commit

Permalink
fix: Fixed macOS GUI update by simplifying the code
Browse files Browse the repository at this point in the history
  • Loading branch information
SaptarshiSarkar12 committed Feb 17, 2024
1 parent e341519 commit e5b9a3a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 75 deletions.
56 changes: 33 additions & 23 deletions GUI/src/main/java/gui/updater/ExecuteUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@

import gui.init.Environment;
import gui.utils.MessageBroker;
import main.Drifty_GUI;
import org.buildobjects.process.ProcBuilder;
import org.buildobjects.process.ProcResult;
import properties.OS;
import ui.ConfirmationDialog;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class ExecuteUpdate {
private static final MessageBroker M = Environment.getMessageBroker();
private final File currentExecutable;
private final File latestExecutable;
private final File currentExecutableFile;
private final File latestExecutableFile;

public ExecuteUpdate(File currentExecutable, File latestExecutable) {
this.currentExecutable = currentExecutable;
this.latestExecutable = latestExecutable;
public ExecuteUpdate(File currentExecutableFile, File latestExecutableFile) {
this.currentExecutableFile = currentExecutableFile;
this.latestExecutableFile = latestExecutableFile;
}

public boolean setExecutablePermission() {
boolean isExecutablePermissionGranted = latestExecutable.setExecutable(true);
boolean isExecutablePermissionGranted = latestExecutableFile.setExecutable(true);
if (!isExecutablePermissionGranted) {
M.msgUpdateError("Failed to set executable permission for the latest version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to set executable permission for the latest version of Drifty!", true, true).getResponse();
return false;
}
boolean isWritePermissionGranted = latestExecutable.setWritable(true);
boolean isWritePermissionGranted = latestExecutableFile.setWritable(true);
if (!isWritePermissionGranted) {
M.msgUpdateError("Failed to set write permission for the latest version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to set write permission for the latest version of Drifty!", true, true).getResponse();
return false;
}
boolean isReadPermissionGranted = latestExecutable.setReadable(true);
boolean isReadPermissionGranted = latestExecutableFile.setReadable(true);
if (!isReadPermissionGranted) {
M.msgUpdateError("Failed to set read permission for the latest version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to set read permission for the latest version of Drifty!", true, true).getResponse();
Expand All @@ -46,18 +46,28 @@ public boolean setExecutablePermission() {
}

public void executeUpdate() throws IOException {
String currentExecutablePathString = currentExecutable.toPath().toString();
boolean isCurrentExecutableRenamed = currentExecutable.renameTo(new File(currentExecutable.getName() + ".old"));
if (!isCurrentExecutableRenamed) {
M.msgUpdateError("Failed to replace the current version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to replace the current version of Drifty!", true, true).getResponse();
return;
if (OS.isMac()) {
ProcResult executionResult = new ProcBuilder("open").withArgs(latestExecutableFile.getAbsolutePath()).withNoTimeout().ignoreExitStatus().run();
if (executionResult.getExitValue() != 0) {
M.msgUpdateError("Failed to open the installer for the latest version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to open the installer for the latest version of Drifty!", true, true).getResponse();
} else {
System.exit(0);
}
} else {
String currentExecutablePathString = currentExecutableFile.toPath().toString();
ProcessBuilder runCurrentExecutable = new ProcessBuilder(currentExecutableFile.getAbsolutePath());
boolean isCurrentExecutableRenamed = currentExecutableFile.renameTo(new File(Paths.get(currentExecutableFile.getParent()).resolve(currentExecutableFile.getName() + ".old").toString()));
if (!isCurrentExecutableRenamed) {
M.msgUpdateError("Failed to replace the current version of Drifty!");
new ConfirmationDialog("Update Failed", "Failed to replace the current version of Drifty!", true, true).getResponse();
return;
}
Files.move(latestExecutableFile.toPath(), Paths.get(currentExecutablePathString), StandardCopyOption.REPLACE_EXISTING);
M.msgUpdateInfo("Update successful!");
runCurrentExecutable.start();
new ConfirmationDialog("Update Successful", "Update was successfully installed!" + System.lineSeparator().repeat(2) + "Restarting Drifty...").getResponse();
Files.deleteIfExists(Paths.get(currentExecutablePathString + ".old"));
}
Files.move(latestExecutable.toPath(), Paths.get(currentExecutablePathString), StandardCopyOption.REPLACE_EXISTING);
M.msgUpdateInfo("Update successful!");
ProcessBuilder processBuilder = new ProcessBuilder(Paths.get(URLDecoder.decode(Drifty_GUI.class.getProtectionDomain().getCodeSource().getLocation().getPath(), StandardCharsets.UTF_8)).toAbsolutePath().toString());
processBuilder.start();
new ConfirmationDialog("Update Successful", "Update was successfully installed!" + System.lineSeparator().repeat(2) + "Restarting Drifty...").getResponse();
Files.deleteIfExists(Paths.get(currentExecutablePathString + ".old"));
}
}
69 changes: 17 additions & 52 deletions GUI/src/main/java/ui/UIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import main.Drifty_GUI;
import org.buildobjects.process.ProcBuilder;
import properties.OS;
import support.Job;
import support.JobHistory;
import utils.Utility;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
Expand Down Expand Up @@ -97,63 +96,29 @@ private void start(MainGridPane pane) {
}

private void downloadUpdate() {
String previouslySelectedDir = getDir(); // Save the download folder selected before the update was initiated.
try {
Path currentExecutablePath = Paths.get(URLDecoder.decode(Drifty_GUI.class.getProtectionDomain().getCodeSource().getLocation().getPath(), StandardCharsets.UTF_8)).toAbsolutePath();
File currentExecutable = currentExecutablePath.toFile();
String customDirectory = getDir();
Path tmpFolder = Files.createTempDirectory("Drifty").toAbsolutePath();
String latestExecutableName = OS.isMac() ? "Drifty_GUI.pkg" : currentExecutable.getName();
Job updateJob = new Job(Constants.updateURL.toString(), tmpFolder.toString(), latestExecutableName, false);
// "Current executable" means the executable currently running i.e., the one that is outdated.
URI currentExecutableURI = Paths.get(URLDecoder.decode(Drifty_GUI.class.getProtectionDomain().getCodeSource().getLocation().getPath(), StandardCharsets.UTF_8)).toUri();
File currentExecutableFile = new File(currentExecutableURI);
// "Latest executable" means the executable that is to be downloaded and installed i.e., the latest version.
// "tmpFolder" is the temporary folder where the latest executable will be downloaded to.
URI tmpFolderURI = Files.createTempDirectory("Drifty").toUri();
String latestExecutableName = OS.isMac() ? "Drifty_GUI.pkg" : currentExecutableFile.getName();
URI latestExecutableURI = Paths.get(tmpFolderURI).resolve(latestExecutableName).toUri();
File latestExecutableFile = new File(latestExecutableURI);
// Download the latest executable
Job updateJob = new Job(Constants.updateURL.toString(), latestExecutableFile.getParent(), latestExecutableName, false);
addJob(updateJob);
Thread downloadUpdate = new Thread(batchDownloader());
downloadUpdate.start();
Path downloadedFilePath = tmpFolder.resolve(latestExecutableName);
while (!downloadUpdate.getState().equals(Thread.State.TERMINATED)) {
sleep(500);
}
setDir(customDirectory);
if (OS.isMac()) {
try {
File pkg = new File(tmpFolder.toString(), latestExecutableName);
boolean isExecutable = pkg.setExecutable(true);
if (!isExecutable) {
M.msgUpdateError("Failed to set the pkg file as executable!");
new ConfirmationDialog("Update Failed", "Failed to set the pkg file as executable!", true, true).getResponse();
}
String argument = downloadedFilePath.toAbsolutePath().toString();
// new ConfirmationDialog("PKG start command", "The command is " + nl.repeat(2) + "open" + argument).getResponse();
// M.msgUpdateInfo("The command is " + nl.repeat(2) + "open" + argument);
ProcBuilder pb = new ProcBuilder("open").withArgs(argument).withNoTimeout().ignoreExitStatus();
pb.run();
// ProcessBuilder startPkg = new ProcessBuilder(Paths.get(tmpFolder.toString(), currentExecutablePath.getFileName().toString()).toAbsolutePath().toString());
// startPkg.start().waitFor();
System.exit(0);
// ProcessBuilder extractPkg = new ProcessBuilder("pkgutil", "--expand", Paths.get(tmpFolder.toString(), currentExecutablePath.getFileName().toString()).toAbsolutePath().toString(), Paths.get(tmpFolder.toString(), "Drifty GUI").toAbsolutePath().toString());
// Process pkgExtractProcess = extractPkg.start();
// pkgExtractProcess.waitFor();
// ProcessBuilder extractPayload = new ProcessBuilder("tar", "-xvf", Paths.get(tmpFolder.toString(), "Drifty GUI", "GUI-app.pkg", "Payload").toAbsolutePath().toString(), "-C", Paths.get(tmpFolder.toString(), "Drifty GUI", "Payload_Contents").toAbsolutePath().toString());
// Process payloadExtractProcess = extractPayload.start();
// payloadExtractProcess.waitFor();
// File latestExecutable = Paths.get(tmpFolder.toString(), "Drifty GUI", "Payload_Contents", "GUI.app").toAbsolutePath().toFile();
// ExecuteUpdate updateExecutor = new ExecuteUpdate(currentExecutable, latestExecutable);
// if (!Mode.isGUI()) {
// updateExecutor.setExecutablePermission();
// }
// updateExecutor.executeUpdate();
} catch (SecurityException e) {
M.msgUpdateError("Failed to extract the latest executable due to security restrictions! " + e.getMessage());
} catch (UnsupportedOperationException e) {
M.msgUpdateError("Failed to extract the latest executable! Extract operation is not supported on this platform! " + e.getMessage());
}
// catch (IOException e) {
// M.msgUpdateError("Failed to extract the latest executable! " + e.getMessage());
// }
} else {
File latestExecutable = new File(tmpFolder.toString(), currentExecutablePath.getFileName().toString());
ExecuteUpdate updateExecutor = new ExecuteUpdate(currentExecutable, latestExecutable);
if (updateExecutor.setExecutablePermission()) {
updateExecutor.executeUpdate();
}
setDir(previouslySelectedDir); // Reset the download folder to the one that was selected before the update was initiated.
ExecuteUpdate updateExecutor = new ExecuteUpdate(currentExecutableFile, latestExecutableFile);
if (updateExecutor.setExecutablePermission()) {
updateExecutor.executeUpdate();
}
} catch (IOException e) {
M.msgUpdateError("Failed to download update! " + e.getMessage());
Expand Down

0 comments on commit e5b9a3a

Please sign in to comment.