Skip to content

Commit

Permalink
feat: Autoupdater for non windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jagodevreede committed Nov 18, 2024
1 parent 67e0462 commit 0d66949
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void start(Stage stage) throws Exception {
}
}

/** Returns true if an argument was handled and leads to closing the application */
private boolean handleArguments(List<String> list) throws IOException {
if (!list.isEmpty()) {
if (list.size() == 3 && checkArgument(list.get(0), "u", "use")) {
Expand All @@ -87,7 +88,9 @@ private boolean handleArguments(List<String> list) throws IOException {
return true;
}
}
SERVICE_REGISTRY.getPopupView().showWarning("Invalid arguments: " + list);
if (!list.get(0).equals("--update-complete") && !list.get(0).equals("--no-console")) {
SERVICE_REGISTRY.getPopupView().showWarning("Invalid arguments: " + list);
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.util.List;
import java.util.Optional;

import static io.github.jagodevreede.sdkman.api.OsHelper.getOs;
import static io.github.jagodevreede.sdkman.api.OsHelper.isMac;

public abstract class AutoUpdater {
private static final Logger logger = LoggerFactory.getLogger(AutoUpdater.class);
protected final ServiceRegistry serviceRegistry = ServiceRegistry.INSTANCE;
Expand All @@ -25,6 +28,9 @@ public static Optional<AutoUpdater> getInstance() {
if (OsHelper.isWindows()) {
return Optional.of(new AutoUpdaterWindows());
}
if (OsHelper.hasShell()) {
return Optional.of(new AutoUpdaterShell());
}
return Optional.empty();
}

Expand Down Expand Up @@ -64,16 +70,19 @@ public void runUpdate() {

Optional<String> downloadUrl = getDownloadUrl(getLatestGitHubRelease().getLatestReleaseDownloads());
if (!downloadUrl.isPresent()) {
ServiceRegistry.INSTANCE.getPopupView().showError("Did not find the download url yet please visit the github page, and download the update manually");
ServiceRegistry.INSTANCE.getPopupView()
.showError("Did not find the download url yet please visit the github page, and download the update manually");
return;
}
DownloadTask downloadTask = new DownloadTask(downloadUrl.get(), tempFile, destFile, null);
PopupView.ProgressWindow progressWindow = ServiceRegistry.INSTANCE.getPopupView().showProgress("Download of update in progress", downloadTask);
PopupView.ProgressWindow progressWindow = ServiceRegistry.INSTANCE.getPopupView()
.showProgress("Download of update in progress", downloadTask);
ProgressInformation progressInformation = new ProgressInformation() {
@Override
public void publishProgress(int current) {
Platform.runLater(() -> progressWindow.progressBar().setProgress(current / 100.0));
}

@Override
public void publishState(String state) {
Platform.runLater(() -> progressWindow.alert().setHeaderText(state));
Expand All @@ -99,5 +108,21 @@ public void publishState(String state) {

abstract public File getUpdateFile();

abstract public String getPlatformIdentifier();
public String getPlatformIdentifier() {
String result;
if (getOs().contains("windows")) {
result = "windows";
} else if (isMac()) {
result = "osx";
} else {
result = "linux";
}

if (System.getProperty("os.arch").equals("arm64") || System.getProperty("os.arch").equals("aarch64")) {
result += "_aarch64";
} else {
result += "_x86_64";
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.jagodevreede.sdkmanui.updater;

import io.github.jagodevreede.sdkman.api.files.ZipExtractTask;
import javafx.application.Platform;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

class AutoUpdaterShell extends AutoUpdater {
AutoUpdaterShell() {

}

@Override
public Optional<String> getDownloadUrl(List<String> listOfUrls) {
return listOfUrls.stream().filter(url -> url.contains(getPlatformIdentifier()) && url.contains("zip")).findFirst();
}

@Override
public void finalizeUpdate(File tempFile) {
ZipExtractTask.extract(tempFile, getUpdateFile());
File installFolder = new File(serviceRegistry.getApi().getBaseFolder(), "ui");
ProcessBuilder builder = new ProcessBuilder("bash", "update.sh");
builder.directory(installFolder);
try {
builder.start();
Platform.exit();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public File getUpdateFile() {
return new File(serviceRegistry.getApi().getBaseFolder(), "ui/sdkman-ui-update");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,4 @@ public File getUpdateFile() {
return new File(serviceRegistry.getApi().getBaseFolder(), "ui/sdkman-ui-update.exe");
}

@Override
public String getPlatformIdentifier() {
return "windows_x86_64";
}
}
30 changes: 29 additions & 1 deletion sdkman-ui/src/main/resources/update.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
#!/usr/bin/env bash

# TODO
cd "$HOME/.sdkman/ui"

check_and_replace() {
if [[ -f "sdkman-ui-update" ]]; then
rm -f "sdkman-ui"

if [[ ! -f "sdkman-ui" ]]; then
mv "sdkman-ui-update" "sdkman-ui"

if [[ -f "sdkman-ui" ]]; then
start_program
return
else
sleep 1
check_and_replace
fi
else
sleep 1
check_and_replace
fi
fi
}

start_program() {
./sdkman-ui --update-complete
exit 0
}

check_and_replace

0 comments on commit 0d66949

Please sign in to comment.