Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for updating Drifty #316

Closed
wants to merge 84 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
0792c2c
Added checkUpdate() for Drifty_CLI.
kmr-rohit Oct 22, 2023
ace9e17
Added checkUpdate() for Drifty_CLI.
kmr-rohit Oct 23, 2023
7555e8c
Added checkUpdate() for Drifty_CLI.
kmr-rohit Oct 23, 2023
2eb1def
Added checkUpdate() for Drifty_CLI.
kmr-rohit Oct 23, 2023
f169723
Merge remote-tracking branch 'origin/kmrrohit' into kmrrohit
kmr-rohit Oct 23, 2023
9fc3c11
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 23, 2023
d5b1754
fix: Merge conflicts along with missing symbols fixed
SaptarshiSarkar12 Oct 23, 2023
791f523
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 24, 2023
dfa36ce
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 24, 2023
2501187
Updated Implementation as Suggested.
kmr-rohit Oct 25, 2023
bfdcfd1
Updated Implementation as Suggested.
kmr-rohit Oct 25, 2023
8d0a476
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 25, 2023
aa81c94
fix: Fixed merge conflicts and removed redundancy from Updater class …
SaptarshiSarkar12 Oct 25, 2023
4e30a4c
fix(ci): Fixed wrong directory name for updater package
SaptarshiSarkar12 Oct 26, 2023
80c106a
fix: Fixed linter and build ci issues
SaptarshiSarkar12 Oct 26, 2023
0f813ca
Merge branch 'kmrrohit' of https://github.com/kmr-rohit/Drifty into k…
SaptarshiSarkar12 Oct 26, 2023
06f63d6
fix: Fixed CI build issues and renamed some method parameters
SaptarshiSarkar12 Oct 26, 2023
4211d08
Merge branch 'master' into kmrrohit
kmr-rohit Oct 26, 2023
77399b0
fix: Fixed null pointer and IO exception in Updater class for log fil…
SaptarshiSarkar12 Oct 26, 2023
4de0370
Merge branch 'kmrrohit' of https://github.com/kmr-rohit/Drifty into k…
SaptarshiSarkar12 Oct 26, 2023
e0c6b07
fix: Removed test parameter from updater class
SaptarshiSarkar12 Oct 26, 2023
6accde5
fix: Added old executable delete call
SaptarshiSarkar12 Oct 26, 2023
61ffe93
fix: Removed `java.awt` and added `process.waitFor()` to wait for the…
SaptarshiSarkar12 Oct 26, 2023
9c83ea6
fix: Fixed duplicate log lines
SaptarshiSarkar12 Oct 26, 2023
b8b94ce
fix: Update for Drifty CLI and GUI working now
SaptarshiSarkar12 Oct 27, 2023
caf91e7
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 27, 2023
35fa35d
fix: Fixed linter issues
SaptarshiSarkar12 Oct 27, 2023
21cc2ff
fix: Fixed linter issues
SaptarshiSarkar12 Oct 27, 2023
3d3710e
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 30, 2023
6325fc1
fix: Fixed maven build error
SaptarshiSarkar12 Oct 30, 2023
a4b6453
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 30, 2023
61f2273
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 30, 2023
b944038
Merge branch 'SaptarshiSarkar12:master' into kmrrohit
kmr-rohit Oct 31, 2023
7cea908
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 31, 2023
5f20535
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 31, 2023
0ea3d8e
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Oct 31, 2023
e7d9df9
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 10, 2023
65d40ec
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 18, 2023
f9281a5
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 18, 2023
c3df06a
fix: Fixed build issues
SaptarshiSarkar12 Nov 18, 2023
857057a
fix: Fixed build issues
SaptarshiSarkar12 Nov 18, 2023
3160303
chore: Updated comments in Mode enum
SaptarshiSarkar12 Nov 18, 2023
4fb45bc
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 19, 2023
fdaacba
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
dfee770
fix: Fixed compilation failure: "Final variable can not be re-assigned"
SaptarshiSarkar12 Nov 20, 2023
f12640e
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
3dee47f
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
27695ff
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
0148bbc
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
08d7a54
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
3322c02
Commit message
github-actions[bot] Nov 20, 2023
a41e997
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
4f17fb2
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
e143312
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
82dc774
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
c3fa99f
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
a754e19
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
c9890cc
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
5370a6a
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
f77d1d5
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
c829e02
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 20, 2023
c8615ea
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 21, 2023
eb9be77
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 21, 2023
29fb6aa
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 21, 2023
537ff4e
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 21, 2023
33916d2
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
a0f00cc
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
28ddff9
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
d233778
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
44797bc
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
d48b5a5
chore: Added some spaces in Utility class to test if formatter works
SaptarshiSarkar12 Nov 23, 2023
2e2eb48
style: Formatted Java files
github-actions[bot] Nov 23, 2023
62f5ec8
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 23, 2023
beee02d
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 24, 2023
5bf83d4
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 24, 2023
71101f7
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 24, 2023
13d1aa5
style: Formatted Java files
github-actions[bot] Nov 24, 2023
2024e90
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 27, 2023
d1e5557
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 28, 2023
03a896f
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 28, 2023
3e504e1
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Nov 29, 2023
76ec1c6
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Dec 2, 2023
03f2a28
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Dec 2, 2023
2a0d597
Merge branch 'master' into kmrrohit
SaptarshiSarkar12 Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

101 changes: 100 additions & 1 deletion src/main/java/CLI/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import GUI.Support.Job;
import GUI.Support.JobHistory;
import Preferences.AppSettings;
import Updater.Updater;
import Utils.*;
import org.yaml.snakeyaml.Yaml;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -48,6 +53,13 @@ public static void main(String[] args) {
LOGGER.log(MessageType.INFO, CLI_APPLICATION_STARTED);
messageBroker = new MessageBroker(System.out);
Environment.setMessageBroker(messageBroker);
try {
if (checkUpdate()){
return;
}
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
messageBroker.msgInitInfo("Initializing environment...");
Environment.initializeEnvironment();
messageBroker.msgInitInfo("Environment initialized successfully!");
Expand Down Expand Up @@ -375,4 +387,91 @@ private static void checkHistoryAddJobsAndDownload(Job job, boolean removeInputB
downloader.run();
}
}
}

public static boolean checkUpdate() throws URISyntaxException {
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
String latestVersion = getLatestVersion();
if (isNewerVersion(latestVersion , VERSION_NUMBER)) {
String Link;
String oldFilePath = String.valueOf(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
String newFilePath = "";
String OS_NAME = OS.getOSName();
if (OS_NAME.contains("win")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-CLI.exe";
String fileName = "Drifty_CLI.exe";
String dirPath = String.valueOf(Paths.get(System.getenv("LOCALAPPDATA"), "Drifty", "updates"));
FileDownloader downloader = new FileDownloader(Link , fileName , dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
} else if (OS_NAME.contains("mac")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-CLI_macos";
String fileName = "Drifty-CLI_macos";
String dirPath = ".drifty/updates";
FileDownloader downloader = new FileDownloader(Link , fileName , dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
} else if (OS_NAME.contains("linux")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-CLI_linux";
String fileName = "Drifty-CLI_linux";
String dirPath = ".drifty/updates";
FileDownloader downloader = new FileDownloader(Link, fileName, dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
Updater.replaceUpdate(oldFilePath , newFilePath);
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
}

private static String getLatestVersion() {
try {
URL url = new URI("https://api.github.com/repos/SaptarshiSarkar12/Drifty/releases/latest").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
reader.close();

// Parse JSON response to get the "tag_name"
// For simplicity, we're assuming the tag_name is a string enclosed in double quotes
int start = response.indexOf("\"tag_name\":\"") + 12;
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
int end = response.indexOf("\"", start);
System.out.println( response.substring(start, end));
return response.substring(start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
}

private static boolean isNewerVersion(String newVersion, String currentVersion) {
// Split version strings into arrays of integers
String[] newVersionParts = newVersion.replaceAll("[^\\d.]", "").split("\\.");
String[] currentVersionParts = currentVersion.replaceAll("[^\\d.]", "").split("\\.");

// Convert parts to integers
int[] newVersionNumbers = new int[newVersionParts.length];
int[] currentVersionNumbers = new int[currentVersionParts.length];

for (int i = 0; i < newVersionParts.length; i++) {
newVersionNumbers[i] = Integer.parseInt(newVersionParts[i]);
}

for (int i = 0; i < currentVersionParts.length; i++) {
currentVersionNumbers[i] = Integer.parseInt(currentVersionParts[i]);
}

// Compare version numbers
for (int i = 0; i < Math.min(newVersionNumbers.length, currentVersionNumbers.length); i++) {
if (newVersionNumbers[i] > currentVersionNumbers[i]) {
return true; // New version is greater
} else if (newVersionNumbers[i] < currentVersionNumbers[i]) {
return false; // Current version is greater
}
}

// If all compared parts are equal, consider the longer version as newer
return newVersionNumbers.length > currentVersionNumbers.length;
}
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
160 changes: 112 additions & 48 deletions src/main/java/GUI/Forms/Main.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package GUI.Forms;

import Backend.FileDownloader;
import Enums.Mode;
import Enums.OS;
import Updater.Updater;
import Utils.DriftyConstants;
import Utils.Environment;
import Utils.MessageBroker;
import Utils.Utility;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.input.Clipboard;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.LinearGradient;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

import static Utils.DriftyConstants.DRIFTY_WEBSITE_URL;
import static Utils.DriftyConstants.VERSION_NUMBER;
import static javafx.scene.layout.AnchorPane.*;

public class Main extends Application {
Expand All @@ -30,13 +38,16 @@ public class Main extends Application {
private Scene scene;
private boolean firstRun = true;

public static void main(String[] args) {
public static void main(String[] args) throws URISyntaxException {
System.setProperty("apple.laf.useScreenMenuBar", "true");
Mode.setGUIMode();
msgBroker = new MessageBroker();
Environment.setMessageBroker(msgBroker);
msgBroker.msgLogInfo(DriftyConstants.GUI_APPLICATION_STARTED);
Environment.initializeEnvironment();
if (checkUpdate()) {
return;
}
launch(args);
}

Expand Down Expand Up @@ -125,47 +136,12 @@ private Menu getHelpMenu() {
MenuItem bug = new MenuItem("Report a Bug");
MenuItem securityVulnerability = new MenuItem("Report a Security Vulnerability");
MenuItem feature = new MenuItem("Suggest a Feature");
MenuItem about = new MenuItem("About Drifty");
contactUs.setOnAction(e -> openWebsite("https://saptarshisarkar12.github.io/Drifty/contact"));
contactUs.setOnAction(e -> openWebsite("https://saptarshisarkar12.github.io/Drifty/contact.html"));
contribute.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty"));
bug.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty/issues/new?assignees=&labels=bug+%F0%9F%90%9B%2CApp+%F0%9F%92%BB&projects=&template=Bug-for-application.yaml&title=%5BBUG%5D+"));
bug.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty/issues/new?assignees=&labels=bug%2CApp&template=Bug-for-application.yaml&title=%5BBUG%5D+"));
securityVulnerability.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty/security/advisories/new"));
feature.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty/issues/new?assignees=&labels=feature+%E2%9C%A8%2CApp+%F0%9F%92%BB&projects=&template=feature-request-application.yaml&title=%5BFEATURE%5D+"));
about.setOnAction(event -> {
Stage stage = Constants.getStage("About Drifty", false);
VBox root = new VBox(10);
root.setPadding(new Insets(10));
root.setAlignment(Pos.TOP_CENTER);
ImageView appIcon = new ImageView(String.valueOf(Constants.class.getResource("/GUI/Splash.png")));
appIcon.setFitWidth(Constants.SCREEN_WIDTH * .2);
appIcon.setFitHeight(Constants.SCREEN_HEIGHT * .2);
appIcon.setPreserveRatio(true);
Label lblDescription = new Label("An Open-Source Interactive File Downloader System");
lblDescription.setFont(Font.font("Arial", FontWeight.BOLD, 24));
lblDescription.setTextFill(LinearGradient.valueOf("linear-gradient(to right, #8e2de2, #4a00e0)"));
Label lblVersion = new Label(DriftyConstants.VERSION_NUMBER);
lblVersion.setFont(Font.font("Arial", FontWeight.BOLD, 20));
lblVersion.setTextFill(LinearGradient.valueOf("linear-gradient(to right, #0f0c29, #302b63, #24243e)"));
Hyperlink websiteLink = new Hyperlink("Website");
websiteLink.setFont(Font.font("Arial", FontWeight.BOLD, 18));
websiteLink.setTextFill(LinearGradient.valueOf("linear-gradient(to right, #fc466b, #3f5efb)"));
websiteLink.setOnAction(e -> openWebsite("https://saptarshisarkar12.github.io/Drifty"));
Hyperlink discordLink = new Hyperlink("Join Discord");
discordLink.setFont(Font.font("Arial", FontWeight.BOLD, 18));
discordLink.setTextFill(LinearGradient.valueOf("linear-gradient(to right, #00d956, #0575e6)"));
discordLink.setOnAction(e -> openWebsite("https://discord.gg/DeT4jXPfkG"));
Hyperlink githubLink = new Hyperlink("Contribute to Drifty");
githubLink.setFont(Font.font("Arial", FontWeight.BOLD, 18));
githubLink.setTextFill(LinearGradient.valueOf("linear-gradient(to right, #009fff, #ec2f4b)"));
githubLink.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty"));
root.getChildren().addAll(appIcon, lblDescription, lblVersion, websiteLink, discordLink, githubLink);
Scene aboutScene = Constants.getScene(root);
stage.setMinHeight(Constants.SCREEN_HEIGHT * .5);
stage.setMinWidth(Constants.SCREEN_WIDTH * .5);
stage.setScene(aboutScene);
stage.show();
});
menu.getItems().setAll(contactUs, contribute, bug, securityVulnerability, feature, about);
feature.setOnAction(e -> openWebsite("https://github.com/SaptarshiSarkar12/Drifty/issues/new?assignees=&labels=enhancement%2CApp&template=feature-request-application.yaml&title=%5BFEATURE%5D+"));
menu.getItems().setAll(contactUs, contribute, bug, securityVulnerability, feature);
return menu;
}

Expand Down Expand Up @@ -203,4 +179,92 @@ protected static void openWebsite(String websiteURL) {
public static void toggleFullScreen() {
guiInstance.primaryStage.setFullScreen(!guiInstance.primaryStage.isFullScreen());
}

public static boolean checkUpdate() throws URISyntaxException {
String latestVersion = getLatestVersion();
if(isNewerVersion(latestVersion , VERSION_NUMBER)){
String Link;
String oldFilePath = String.valueOf(CLI.Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
String newFilePath = "";
String OS_NAME = OS.getOSName();
if (OS_NAME.contains("win")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-GUI.exe";
String fileName = "Drifty_GUI.exe";
String dirPath = String.valueOf(Paths.get(System.getenv("LOCALAPPDATA"), "Drifty", "updates"));
FileDownloader downloader = new FileDownloader(Link , fileName , dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
} else if (OS_NAME.contains("mac")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-GUI.pkg";
String fileName = "Drifty-GUI.pkg";
String dirPath = ".drifty/updates";
FileDownloader downloader = new FileDownloader(Link , fileName , dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
} else if (OS_NAME.contains("linux")) {
Link = "https://github.com/SaptarshiSarkar12/Drifty/releases/latest/download/Drifty-GUI_linux";
String fileName = "Drifty-GUI_linux";
String dirPath = ".drifty/updates";
FileDownloader downloader = new FileDownloader(Link, fileName, dirPath);
downloader.run();
newFilePath = dirPath +'\\' + fileName;
}
Updater.replaceUpdate(oldFilePath , newFilePath);
return true;
}
return false;
}

private static String getLatestVersion() {
try {
URL url = new URI("https://api.github.com/repos/SaptarshiSarkar12/Drifty/releases/latest").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
reader.close();

// Parse JSON response to get the "tag_name"
// For simplicity, we're assuming the tag_name is a string enclosed in double quotes
int start = response.indexOf("\"tag_name\":\"") + 12;
int end = response.indexOf("\"", start);
System.out.println( response.substring(start, end));
return response.substring(start, end);

} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static boolean isNewerVersion(String newVersion, String currentVersion) {
// Split version strings into arrays of integers
String[] newVersionParts = newVersion.replaceAll("[^\\d.]", "").split("\\.");
String[] currentVersionParts = currentVersion.replaceAll("[^\\d.]", "").split("\\.");

// Convert parts to integers
int[] newVersionNumbers = new int[newVersionParts.length];
int[] currentVersionNumbers = new int[currentVersionParts.length];

for (int i = 0; i < newVersionParts.length; i++) {
newVersionNumbers[i] = Integer.parseInt(newVersionParts[i]);
}

for (int i = 0; i < currentVersionParts.length; i++) {
currentVersionNumbers[i] = Integer.parseInt(currentVersionParts[i]);
}

// Compare version numbers
for (int i = 0; i < Math.min(newVersionNumbers.length, currentVersionNumbers.length); i++) {
if (newVersionNumbers[i] > currentVersionNumbers[i]) {
return true; // New version is greater
} else if (newVersionNumbers[i] < currentVersionNumbers[i]) {
return false; // Current version is greater
}
}

// If all compared parts are equal, consider the longer version as newer
return newVersionNumbers.length > currentVersionNumbers.length;
}

}
43 changes: 43 additions & 0 deletions src/main/java/Updater/Updater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Updater;

import java.io.IOException;
import java.nio.file.*;
import java.awt.Desktop;

public class Updater {

public static void main(String[] args) {
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
String oldFilePath = "";
String newFilePath = "";
replaceUpdate(oldFilePath , newFilePath);
}

public static void replaceUpdate(String oldFilePath ,String newFilePath){
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
try {
//Replacing new files
Path oldPath = Path.of(oldFilePath);
Path newPath = Path.of(newFilePath);
Path copy = Files.copy(newPath, oldPath, StandardCopyOption.REPLACE_EXISTING);

if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.OPEN)) {
// Open the new file with the default application
desktop.open(oldPath.toFile());
} else {
System.out.println("Desktop doesn't support OPEN action.");
}
} else {
System.out.println("Desktop is not supported.");
}
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved

} catch (IOException e) {
throw new RuntimeException(e);
SaptarshiSarkar12 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}





Loading