Updater fixes

This commit is contained in:
crschnick 2024-11-03 21:44:46 +00:00
parent 77935c8761
commit 72618ac6b0
9 changed files with 28 additions and 94 deletions

View file

@ -3,6 +3,7 @@ package io.xpipe.app.issue;
import io.xpipe.app.core.*;
import io.xpipe.app.core.mode.OperationMode;
import io.xpipe.app.core.window.AppWindowHelper;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.update.XPipeDistributionType;
import io.xpipe.app.util.Hyperlinks;
import io.xpipe.app.util.ThreadHelper;
@ -73,7 +74,7 @@ public class TerminalErrorHandler extends GuiErrorHandlerBase implements ErrorHa
}
try {
var rel = XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheck();
var rel = XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheck(false, !AppPrefs.get().automaticallyUpdate().get());
if (rel != null && rel.isUpdate()) {
var update = AppWindowHelper.showBlockingAlert(alert -> {
alert.setAlertType(Alert.AlertType.INFORMATION);

View file

@ -29,13 +29,13 @@ public class UpdateCheckComp extends SimpleComp {
}
private void performUpdateAndRestart() {
XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheckSilent();
XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheckSilent(false, false);
UpdateAvailableAlert.showIfNeeded();
}
private void refresh() {
ThreadHelper.runFailableAsync(() -> {
XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheck();
XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheck(false, false);
XPipeDistributionType.get().getUpdateHandler().prepareUpdate();
});
}

View file

@ -4,7 +4,6 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.xpipe.app.core.AppProperties;
import io.xpipe.app.issue.ErrorEvent;
import io.xpipe.app.issue.TrackEvent;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.HttpHelper;
import io.xpipe.core.process.OsType;
import io.xpipe.core.util.JacksonMapper;
@ -118,14 +117,15 @@ public class AppDownloads {
}
}
private static String queryLatestVersion() throws Exception {
private static String queryLatestVersion(boolean first, boolean securityOnly) throws Exception {
var req = JsonNodeFactory.instance.objectNode();
req.put("securityOnly", !AppPrefs.get().automaticallyUpdate().get());
req.put("securityOnly", securityOnly);
req.put("ptb", AppProperties.get().isStaging());
req.put("os", OsType.getLocal().getId());
req.put("arch", AppProperties.get().getArch());
req.put("uuid", AppProperties.get().getUuid().toString());
req.put("version", AppProperties.get().getVersion());
req.put("first", first);
var url = URI.create("https://api.xpipe.io/version");
var builder = HttpRequest.newBuilder();
@ -143,16 +143,12 @@ public class AppDownloads {
return ver;
}
public static Optional<GHRelease> getLatestRelease() throws Exception {
var ver = queryLatestVersion();
var repo = getRepository();
var rel = repo.getReleaseByTagName(ver);
return Optional.ofNullable(rel);
}
public static Optional<GHRelease> getLatestSuitableRelease() throws Exception {
public static Optional<GHRelease> queryLatestRelease(boolean first, boolean securityOnly) throws Exception {
try {
return getLatestRelease();
var ver = queryLatestVersion(first, securityOnly);
var repo = getRepository();
var rel = repo.getReleaseByTagName(ver);
return Optional.ofNullable(rel);
} catch (Exception e) {
throw ErrorEvent.expected(e);
}

View file

@ -1,58 +0,0 @@
package io.xpipe.app.update;
import io.xpipe.app.core.AppProperties;
import io.xpipe.app.ext.LocalStore;
import io.xpipe.app.fxcomps.impl.CodeSnippet;
import io.xpipe.app.fxcomps.impl.CodeSnippetComp;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.layout.Region;
import java.time.Instant;
public class ChocoUpdater extends UpdateHandler {
public ChocoUpdater() {
super(true);
}
@Override
public Region createInterface() {
var snippet = CodeSnippet.builder()
.keyword("choco")
.space()
.identifier("install")
.space()
.string("xpipe")
.space()
.keyword("--version=" + getPreparedUpdate().getValue().getVersion())
.build();
return new CodeSnippetComp(false, new SimpleObjectProperty<>(snippet)).createRegion();
}
public AvailableRelease refreshUpdateCheckImpl() throws Exception {
try (var sc = new LocalStore().tempControl().start()) {
var latest = sc.executeSimpleStringCommand("choco outdated -r --nocolor")
.lines()
.filter(s -> s.startsWith("xpipe"))
.findAny()
.map(string -> string.split("\\|")[2]);
if (latest.isEmpty()) {
return null;
}
var isUpdate = isUpdate(latest.get());
var rel = new AvailableRelease(
AppProperties.get().getVersion(),
XPipeDistributionType.get().getId(),
latest.get(),
"https://community.chocolatey.org/packages/xpipe/" + latest,
null,
null,
Instant.now(),
isUpdate);
lastUpdateCheckResult.setValue(rel);
return lastUpdateCheckResult.getValue();
}
}
}

View file

@ -65,8 +65,8 @@ public class GitHubUpdater extends UpdateHandler {
}
}
public synchronized AvailableRelease refreshUpdateCheckImpl() throws Exception {
var rel = AppDownloads.getLatestSuitableRelease();
public synchronized AvailableRelease refreshUpdateCheckImpl(boolean first, boolean securityOnly) throws Exception {
var rel = AppDownloads.queryLatestRelease(first, securityOnly);
event("Determined latest suitable release "
+ rel.map(GHRelease::getName).orElse(null));

View file

@ -29,8 +29,8 @@ public class PortableUpdater extends UpdateHandler {
.createRegion();
}
public synchronized AvailableRelease refreshUpdateCheckImpl() throws Exception {
var rel = AppDownloads.getLatestSuitableRelease();
public synchronized AvailableRelease refreshUpdateCheckImpl(boolean first, boolean securityOnly) throws Exception {
var rel = AppDownloads.queryLatestRelease(first, securityOnly);
event("Determined latest suitable release "
+ rel.map(GHRelease::getName).orElse(null));

View file

@ -3,6 +3,7 @@ package io.xpipe.app.update;
import io.xpipe.app.comp.base.MarkdownComp;
import io.xpipe.app.core.AppI18n;
import io.xpipe.app.core.window.AppWindowHelper;
import io.xpipe.app.prefs.AppPrefs;
import io.xpipe.app.util.Hyperlinks;
import javafx.event.ActionEvent;
@ -22,7 +23,7 @@ public class UpdateAvailableAlert {
}
// Check whether we still have the latest version prepared
uh.refreshUpdateCheckSilent();
uh.refreshUpdateCheckSilent(false, !AppPrefs.get().automaticallyUpdate().get());
if (uh.getPreparedUpdate().getValue() == null) {
return;
}

View file

@ -99,12 +99,14 @@ public abstract class UpdateHandler {
private void startBackgroundUpdater() {
ThreadHelper.createPlatformThread("updater", true, () -> {
var checked = false;
ThreadHelper.sleep(Duration.ofMinutes(5).toMillis());
event("Starting background updater thread");
while (true) {
if (AppPrefs.get().automaticallyUpdate().get() || AppPrefs.get().checkForSecurityUpdates().get()) {
event("Performing background update");
refreshUpdateCheckSilent();
refreshUpdateCheckSilent(!checked, !AppPrefs.get().automaticallyUpdate().get());
checked = true;
prepareUpdate();
}
@ -134,17 +136,9 @@ public abstract class UpdateHandler {
return false;
}
public final void prepareUpdateAsync() {
ThreadHelper.runAsync(() -> prepareUpdate());
}
public final void refreshUpdateCheckAsync() {
ThreadHelper.runAsync(() -> refreshUpdateCheckSilent());
}
public final AvailableRelease refreshUpdateCheckSilent() {
public final AvailableRelease refreshUpdateCheckSilent(boolean first, boolean securityOnly) {
try {
return refreshUpdateCheck();
return refreshUpdateCheck(first, securityOnly);
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).discard().handle();
return null;
@ -214,7 +208,7 @@ public abstract class UpdateHandler {
// Check if prepared update is still the latest.
// We only do that here to minimize the sent requests by only executing when it's really necessary
var available = XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheckSilent();
var available = XPipeDistributionType.get().getUpdateHandler().refreshUpdateCheckSilent(false, !AppPrefs.get().automaticallyUpdate().get());
if (preparedUpdate.getValue() == null) {
return;
}
@ -233,17 +227,17 @@ public abstract class UpdateHandler {
throw new UnsupportedOperationException();
}
public final AvailableRelease refreshUpdateCheck() throws Exception {
public final AvailableRelease refreshUpdateCheck(boolean first, boolean securityOnly) throws Exception {
if (busy.getValue()) {
return lastUpdateCheckResult.getValue();
}
try (var ignored = new BooleanScope(busy).start()) {
return refreshUpdateCheckImpl();
return refreshUpdateCheckImpl(first, securityOnly);
}
}
public abstract AvailableRelease refreshUpdateCheckImpl() throws Exception;
public abstract AvailableRelease refreshUpdateCheckImpl(boolean first, boolean securityOnly) throws Exception;
@Value
@Builder

View file

@ -24,7 +24,7 @@ public enum XPipeDistributionType {
NATIVE_INSTALLATION("install", true, () -> new GitHubUpdater(true)),
HOMEBREW("homebrew", true, () -> new HomebrewUpdater()),
WEBTOP("webtop", true, () -> new PortableUpdater(false)),
CHOCO("choco", true, () -> new ChocoUpdater());
CHOCO("choco", true, () -> new PortableUpdater(true));
private static XPipeDistributionType type;