Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
trying to make the program update automatically on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
celedev97 committed Nov 16, 2023
1 parent e55e13f commit 14a3240
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/cele/asa_sm/enums/MapsEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum MapsEnum {
THE_ISLAND("TheIsland_WP"),
;

@Getter
private final String mapName;
}
33 changes: 32 additions & 1 deletion src/main/java/dev/cele/asa_sm/services/UpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
import dev.cele.asa_sm.feign.GithubClient;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.SystemUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.regex.Pattern;

@Service
Expand Down Expand Up @@ -94,7 +102,7 @@ public void checkForUpdates() {

if(response == 0){
try {
Desktop.getDesktop().browse(URI.create(latestRelease.getAssets()[0].getBrowser_download_url()));
downloadUpdate(latestRelease);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error while opening download link: " + e.getMessage());
log.error("Error while opening download link: ", e);
Expand All @@ -111,4 +119,27 @@ public void checkForUpdates() {
}
}

@SneakyThrows
private void downloadUpdate(Release latestRelease) {
if(SystemUtils.IS_OS_WINDOWS){
var asset = Arrays.stream(latestRelease.getAssets())
.filter(a -> a.getName().endsWith(".exe"))
.findFirst()
.orElseThrow(() -> new RuntimeException("No exe asset found"));

File file = new RestTemplate().execute(asset.getBrowser_download_url(), HttpMethod.GET, null, clientHttpResponse -> {
File ret = File.createTempFile("asa_sm_update", "tmp.exe");
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
return ret;
});

//run cmd /c start "" "path/to/file.exe" /SILENT
Runtime.getRuntime().exec("cmd /c start \"\" \"" + file.getAbsolutePath() + "\" /SILENT");
System.exit(0);
}else{
//open browser
Desktop.getDesktop().browse(new URI(latestRelease.getHtml_url()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public class TopPanel {
public JButton installVerifyButton;
public JLabel installedLocationLabel;

private final ObjectMapper objectMapper = SpringApplicationContext.autoWire(ObjectMapper.class);


private final AsaServerConfigDto configDto;

public TopPanel(AsaServerConfigDto configDto) {
Expand Down

0 comments on commit 14a3240

Please sign in to comment.