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

Provide platform name to self-updater #541

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 21 additions & 11 deletions src/main/java/org/terasology/launcher/LauncherInitTask.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 MovingBlocks
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,7 +90,7 @@ protected LauncherConfiguration call() {

final boolean serverAvailable = DownloadUtils.isJenkinsAvailable();
if (serverAvailable && launcherSettings.isSearchForLauncherUpdates()) {
final boolean selfUpdaterStarted = checkForLauncherUpdates(downloadDirectory, tempDirectory, launcherSettings.isKeepDownloadedFiles());
final boolean selfUpdaterStarted = checkForLauncherUpdates(os, downloadDirectory, tempDirectory, launcherSettings.isKeepDownloadedFiles());
if (selfUpdaterStarted) {
logger.info("Exit old TerasologyLauncher: {}", TerasologyLauncherVersionInfo.getInstance());
return NullLauncherConfiguration.getInstance();
Expand Down Expand Up @@ -192,7 +192,19 @@ private BaseLauncherSettings getLauncherSettings(Path launcherDirectory) throws
return launcherSettings;
}

private boolean checkForLauncherUpdates(Path downloadDirectory, Path tempDirectory, boolean saveDownloadedFiles) {
private boolean checkForLauncherUpdates(OperatingSystem os, Path downloadDirectory, Path tempDirectory, boolean saveDownloadedFiles) {
final String platform;
if (os.isWindows()) {
platform = "windows";
} else if (os.isMac()) {
platform = "mac";
} else if (os == OperatingSystem.LINUX) {
platform = "linux";
} else {
logger.warn("Current platform is unsupported: {}", System.getProperty("os.name"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.getProperty("os.name") - why not print out the os instead? Mabye update the toString to contain more information, if needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be a quick fix. 👍 Since we're planning to get rid of that enum or maybe refactor it at least, it should contain just four constants: windows, mac, linux, and unsupported. So we might need to get it refactored while solving #542 too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we need the unsupported constant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be the default if someone somehow tries to run the launcher on anything other than the three supported platforms. We can use null of course, but I'd suggest avoiding that.

Copy link
Member

@jdrueckert jdrueckert Apr 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get what "unsupported" means ;)
I just don't get why we'd need a specific value for that...
Consider you want somebody to slice apples, peel potatoes and throw everything else you find in your kitchen cabinet away, which of the following would you rather write down and why?

A)

- open kitchen cabinet and do for each
    - if "apple": slice
    - if "potato": peel
    - if "everything else": throw away

B)

- open kitchen cabinet and do for each
    - if "apple": slice
    - if "potato": peel
    - else: throw away

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(B) seems natural 👍 (and that's what the else keyword was invented for)

platform = null;
}

logger.trace("Check for launcher updates...");
boolean selfUpdaterStarted = false;
updateMessage(BundleUtils.getLabel("splash_launcherUpdateCheck"));
Expand All @@ -212,15 +224,13 @@ private boolean checkForLauncherUpdates(Path downloadDirectory, Path tempDirecto
GuiUtils.showErrorMessageDialog(owner, BundleUtils.getLabel("message_error_launcherInstallationDirectory"));
// Run launcher without an update. Don't throw a LauncherStartFailedException.
}
if (foundLauncherInstallationDirectory) {
final boolean update = updater.showUpdateDialog(owner, release);
if (update) {
if (foundLauncherInstallationDirectory && updater.confirmUpdate(owner, release)) {
if (platform != null) {
// TODO: start self-updater
final Path targetDirectory = saveDownloadedFiles ? downloadDirectory : tempDirectory;
selfUpdaterStarted = updater.update(targetDirectory, tempDirectory);
} else {
showDownloadPage();
// TODO: start self-updater instead
if (false) {
final Path targetDirectory = saveDownloadedFiles ? downloadDirectory : tempDirectory;
selfUpdaterStarted = updater.update(targetDirectory, tempDirectory);
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/terasology/launcher/github/GitHubRelease.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import com.google.common.base.Preconditions;

import javax.json.JsonObject;
import javax.json.JsonValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Simplified interface for GitHub releases.
Expand All @@ -46,4 +51,14 @@ public String getTagName() {
public String getBody() {
return json.getString("body");
}

public String getDownloadUrl(String platform) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of putting utility methods with logic in here, just expose getAssets which returns a List<GithubAsset> (introduce new class). That class will provide means to getName() and getBrowserDownloadUrl().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy to implement 👍. That's surely the most object-oriented approach, but let me ask if it's really necessary? Like if we are supposed to work with a single target asset (depending on the user's platform), why should we provide a list of assets or their names? 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to keep all the classes in the github package as generic as possible, so that we could exchange them easily.

The current way we use the github api library is not ideal, as we are dealing with the plain JSON objects. In principle, those libraries offer interfaces similar to what is implemented in that package, so I'm preparing an easy switch.

return json.getJsonArray("assets")
.stream()
.map(JsonValue::asJsonObject)
.filter(asset -> asset.getString("name").contains(platform))
.findFirst()
.map(asset -> asset.getString("browser_download_url"))
.orElseThrow(() -> new IllegalArgumentException("Invalid platform")); // Should not reach
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 MovingBlocks
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.terasology.launcher.github.GitHubRelease;
import org.terasology.launcher.util.BundleUtils;
import org.terasology.launcher.util.GuiUtils;
import org.terasology.launcher.util.OperatingSystem;
import org.terasology.launcher.version.TerasologyLauncherVersionInfo;

import java.io.IOException;
Expand Down Expand Up @@ -69,9 +70,8 @@ public GitHubRelease updateAvailable() {
//TODO: only check of both version are defined and valid semver?
try {
final GitHubRelease latestRelease = github.getLatestRelease("movingblocks", "terasologylauncher");
final Semver latestVersion = versionOf(latestRelease);

if (latestVersion.isGreaterThan(currentVersion)) {
if (versionOf(latestRelease).isGreaterThan(currentVersion)) {
return latestRelease;
}
} catch (IOException e) {
Expand All @@ -80,7 +80,7 @@ public GitHubRelease updateAvailable() {
return null;
}

public boolean showUpdateDialog(Stage parentStage, final GitHubRelease release) {
public boolean confirmUpdate(Stage parentStage, final GitHubRelease release) {
FutureTask<Boolean> dialog = getUpdateDialog(parentStage, release);

Platform.runLater(dialog);
Expand Down