Skip to content

Commit

Permalink
feat: Added feature to perform in-place update of Drifty (#562)
Browse files Browse the repository at this point in the history
* feat: Added ability to perform in-place update of drifty

* fix: fixed Linter issue

* fix: fixed Drifty GUI downloading wrong update file and also fixed some minor CLI issues

* fix: fixed CWE-248 - NumberFormatException raised by CodeQL

* feat: Added support for updating Drifty GUI and CLI based on architecture (`amd64`/`x86_64` or `aarch64`) for macOS

* style: format codebase

* fix(CI): Fixed linter issue

* debug: Added debug window to pop-up for windows msi if there's a lack in permission

* debug: Added debug window to pop-up for windows msi if there's a lack in permission

* fix: Fixed delay in updating Drifty GUI when the download queue is not empty

* fix: Fixed delay in updating Drifty GUI when the download queue is not empty

* debug: Added debug window to pop-up for windows msi if there's a lack in permission

* debug: Removed debug window as write permissions are apparently available

* fix: Fixed the usage of temporary drifty update directory for future downloads

* debug: Moved log file to a visible area for better monitoring of update of drifty in windows

* debug: Moved log file to a visible area for better monitoring of update of drifty in windows

* debug: Narrow down the update failure to the code for replacing executables

* debug: Narrow down the update failure to the code for replacing executables

* debug: Added log instructions to get an idea of the permissions available

* fix: Fixed permission detection for windows msi

* fix: Fixed permission detection for windows, linux and mac (for mac, only in case of CLI) to prevent failure in in-place update of Drifty

* fix(CI): Fixed linter issue

* style: format codebase

* fix: Fixed NullPointerException while creating log during initialization

* fix: Fixed unchecked admin privileges during initialization period of Drifty CLI

* fix: Added a copy instead of move for Windows update check

* fix: Added `deleteOnExit()` command to delete '.old' executable for windows

* fix: Added a fix for exe update in windows

* fix(CI): Fixed linter issues

* fix: the old exe will get deleted now and added check for update menu item

* chore: Removed the resizability of confirmation dialog

* chore: Removed unnecessary code lines previously added for debug purposes

* style: format codebase

* fix: Fixed CoderabbitAI's reviews

* fix: Fixed CoderabbitAI's reviews

* chore: Removed the version number modification previously made solely for testing purposes

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
SaptarshiSarkar12 and github-actions[bot] authored Sep 7, 2024
1 parent 772a996 commit e9c7e0e
Show file tree
Hide file tree
Showing 36 changed files with 967 additions and 167 deletions.
3 changes: 3 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions CLI/src/main/java/cli/support/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public class Constants extends support.Constants {
public static final String HELP_FLAG = "--help";
public static final String NAME_FLAG = "--name";
public static final String VERSION_FLAG = "--version";
public static final String UPDATE_FLAG = "--update";
public static final String EARLY_ACCESS_FLAG = "--early-access";
public static final String LOCATION_FLAG = "--location";
public static final String BATCH_FLAG = "--batch";
public static final String HELP_FLAG_SHORT = "-h";
public static final String NAME_FLAG_SHORT = "-n";
public static final String VERSION_FLAG_SHORT = "-v";
public static final String UPDATE_FLAG_SHORT = "-u";
public static final String EARLY_ACCESS_FLAG_SHORT = "-ea";
public static final String LOCATION_FLAG_SHORT = "-l";
public static final String BATCH_FLAG_SHORT = "-b";
public static final String REMOVE_ALL_URL_CONFIRMATION = "Are you sure you wish to delete ALL links? (Y/N): ";
Expand Down
28 changes: 28 additions & 0 deletions CLI/src/main/java/cli/updater/CLIUpdateExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cli.updater;

import java.io.File;

public class CLIUpdateExecutor extends updater.UpdateExecutor {
public CLIUpdateExecutor(File currentExecutableFile, File latestExecutableFile) {
super(currentExecutableFile, latestExecutableFile);
}

@Override
public boolean execute() {
M.msgLogInfo("Setting executable permission for the latest version of Drifty...");
if (setLatestExecutablePermissions()) {
M.msgLogInfo("Executable permission set! Executing update...");
} else {
M.msgUpdateError("Failed to set executable permission for the latest version of Drifty!");
return false;
}
cleanup(true); // This will delete the old executable created previously
if (renameCurrentExecutable()) {
if (replaceCurrentExecutable()) {
cleanup(false);
return true;
}
}
return false;
}
}
4 changes: 4 additions & 0 deletions CLI/src/main/java/cli/utils/MessageBroker.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public void msgHistoryWarning(String message, boolean endWithNewLine) {
this.endWithNewLine = endWithNewLine;
sendMessage(message, MessageType.WARN, MessageCategory.HISTORY);
}

public void msgUpdateWarning(String message) {
sendMessage(message, MessageType.WARN, MessageCategory.UPDATE);
}
}
28 changes: 16 additions & 12 deletions CLI/src/main/java/cli/utils/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,40 @@ public static String findFilenameInLink(String link) {
for (String json : Objects.requireNonNull(linkMetadataList)) {
filename = Utility.getFilenameFromJson(json);
}
if (filename.isEmpty()) {
msgBroker.msgFilenameError("Filename detection failed: No filename found in metadata!");
return null;
}
} else {
// Example: "example.com/file.txt" prints "Filename detected: file.txt"
// example.com/file.json -> file.json
String file = link.substring(link.lastIndexOf("/") + 1);
if (file.isEmpty()) {
M.msgFilenameError(FILENAME_DETECTION_ERROR);
msgBroker.msgFilenameError(FILENAME_DETECTION_ERROR);
return null;
}
int index = file.lastIndexOf(".");
if (index < 0) {
M.msgFilenameError(FILENAME_DETECTION_ERROR);
msgBroker.msgFilenameError(FILENAME_DETECTION_ERROR);
return null;
}
String extension = file.substring(index);
// edge case 1: "example.com/."
if (extension.length() == 1) {
M.msgFilenameError(FILENAME_DETECTION_ERROR);
msgBroker.msgFilenameError(FILENAME_DETECTION_ERROR);
return null;
}
// file.png?width=200 -> file.png
filename = file.split("([?])")[0];
M.msgFilenameInfo(FILENAME_DETECTED + "\"" + filename + "\"");
msgBroker.msgFilenameInfo(FILENAME_DETECTED + "\"" + filename + "\"");
}
return filename;
}

public boolean yesNoValidation(String input, String printMessage) {
while (input.isEmpty()) {
Environment.getMessageBroker().msgInputError(ENTER_Y_OR_N, true);
M.msgLogError(ENTER_Y_OR_N);
msgBroker.msgLogError(ENTER_Y_OR_N);
Environment.getMessageBroker().msgInputInfo(printMessage, false);
input = SC.nextLine().toLowerCase();
}
Expand All @@ -62,7 +66,7 @@ public boolean yesNoValidation(String input, String printMessage) {
return false;
} else {
Environment.getMessageBroker().msgInputError("Invalid input!", true);
M.msgLogError("Invalid input!");
msgBroker.msgLogError("Invalid input!");
Environment.getMessageBroker().msgInputInfo(printMessage, false);
input = SC.nextLine().toLowerCase();
return yesNoValidation(input, printMessage);
Expand All @@ -82,26 +86,26 @@ public static String getSpotifyDownloadLink(String spotifyMetadataJson) {
ArrayList<HashMap<String, Object>> searchResults = getYoutubeSearchResults(query, true);
boolean searchedWithFilters = true;
if (searchResults == null) {
M.msgLogError("Failed to get search results for the song with filters! Trying without filters ...");
msgBroker.msgLogError("Failed to get search results for the song with filters! Trying without filters ...");
searchResults = getYoutubeSearchResults(query, false);
searchedWithFilters = false;
if (searchResults == null) {
M.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
msgBroker.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
return null;
}
}
String matchedId = getMatchingVideoID(Objects.requireNonNull(searchResults), duration, artistNames);
if (matchedId.isEmpty()) {
if (searchedWithFilters) {
M.msgLogError("Failed to get a matching video ID for the song with filters! Trying without filters ...");
msgBroker.msgLogError("Failed to get a matching video ID for the song with filters! Trying without filters ...");
searchResults = getYoutubeSearchResults(query, false);
matchedId = getMatchingVideoID(Objects.requireNonNull(searchResults), duration, artistNames);
if (matchedId.isEmpty()) {
M.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
msgBroker.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
return null;
}
} else {
M.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
msgBroker.msgDownloadError("Song is exclusive to Spotify and cannot be downloaded!");
return null;
}
}
Expand All @@ -114,7 +118,7 @@ public static Yaml getYamlParser() {
loaderOptions.setAllowRecursiveKeys(false);
loaderOptions.setProcessComments(false);
Yaml yamlParser = new Yaml(new SafeConstructor(loaderOptions));
M.msgLogInfo("YAML parser initialized successfully");
msgBroker.msgLogInfo("YAML parser initialized successfully");
return yamlParser;
}
}
Loading

0 comments on commit e9c7e0e

Please sign in to comment.