forked from public-mirrors/DesertWell
Use builders for constructing, refactor, add new version check endpoints
parent
c7316c0767
commit
067860e159
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
@ -1,80 +0,0 @@
|
|||||||
package net.william278.desertwell;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility for comparing a {@link Version} against the latest version on SpigotMC
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public class UpdateChecker {
|
|
||||||
// The SpigotMC.org website API endpoint
|
|
||||||
private static final String SPIGOT_API_ENDPOINT = "https://api.spigotmc.org/legacy/update.php?resource=";
|
|
||||||
@NotNull
|
|
||||||
private final Version currentVersion;
|
|
||||||
private int resourceId;
|
|
||||||
|
|
||||||
private UpdateChecker() {
|
|
||||||
this.currentVersion = new Version();
|
|
||||||
}
|
|
||||||
|
|
||||||
private UpdateChecker(@NotNull Version currentVersion, final int resourceId) {
|
|
||||||
this.currentVersion = currentVersion;
|
|
||||||
this.resourceId = resourceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new UpdateChecker for a plugin
|
|
||||||
*
|
|
||||||
* @param currentVersion The current version of the plugin
|
|
||||||
* @param resourceId The resource ID of the plugin on SpigotMC
|
|
||||||
* @return The {@link UpdateChecker}
|
|
||||||
*/
|
|
||||||
public static UpdateChecker create(@NotNull Version currentVersion, final int resourceId) {
|
|
||||||
return new UpdateChecker(currentVersion, resourceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the current plugin {@link Version}
|
|
||||||
*
|
|
||||||
* @return The current plugin {@link Version}
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public Version getCurrentVersion() {
|
|
||||||
return currentVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Query SpigotMC for the latest {@link Version} of the plugin
|
|
||||||
*
|
|
||||||
* @return A {@link CompletableFuture} containing the latest {@link Version} of the plugin
|
|
||||||
*/
|
|
||||||
public CompletableFuture<Version> getLatestVersion() {
|
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
|
||||||
try (final InputStreamReader inputStreamReader = new InputStreamReader(
|
|
||||||
new URL(SPIGOT_API_ENDPOINT + resourceId).openConnection().getInputStream())) {
|
|
||||||
return Version.fromString(new BufferedReader(inputStreamReader).readLine());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new IllegalStateException("Unable to fetch latest version", e);
|
|
||||||
}
|
|
||||||
}).exceptionally(throwable -> {
|
|
||||||
throwable.printStackTrace();
|
|
||||||
return new Version();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the current plugin {@link Version} is outdated compared to {@link #getLatestVersion()}
|
|
||||||
*
|
|
||||||
* @return A {@link CompletableFuture} containing true if the current plugin {@link Version} is outdated
|
|
||||||
*/
|
|
||||||
public CompletableFuture<Boolean> isUpToDate() {
|
|
||||||
return getLatestVersion().thenApply(latestVersion -> currentVersion.compareTo(latestVersion) >= 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,19 @@
|
|||||||
|
package net.william278.desertwell.util;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface ThrowingConsumer<T> extends Consumer<T> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void accept(final T elem) {
|
||||||
|
try {
|
||||||
|
acceptThrows(elem);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void acceptThrows(T elem) throws Exception;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,181 @@
|
|||||||
|
package net.william278.desertwell.util;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility for comparing a {@link Version} against the latest version on various {@link Endpoint}s
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class UpdateChecker {
|
||||||
|
private final Endpoint endpoint;
|
||||||
|
private final Version currentVersion;
|
||||||
|
private final String versionMetaDelimiter;
|
||||||
|
private final String resource;
|
||||||
|
|
||||||
|
private UpdateChecker(@NotNull Endpoint endpoint, @NotNull Version currentVersion,
|
||||||
|
@NotNull String versionMetaDelimiter, @NotNull String resource) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
this.currentVersion = currentVersion;
|
||||||
|
this.versionMetaDelimiter = versionMetaDelimiter;
|
||||||
|
this.resource = resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Query SpigotMC for the latest {@link Version} of the plugin
|
||||||
|
*
|
||||||
|
* @return A {@link CompletableFuture} containing the latest {@link Version} of the plugin
|
||||||
|
*/
|
||||||
|
public CompletableFuture<Completed> check() {
|
||||||
|
return CompletableFuture
|
||||||
|
.supplyAsync(() -> new Completed(this, Version.fromString(endpoint.query(resource), versionMetaDelimiter)))
|
||||||
|
.exceptionally(throwable -> new Completed(this, currentVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
private Endpoint endpoint = Endpoint.SPIGOT;
|
||||||
|
private Version currentVersion;
|
||||||
|
private String versionMetaDelimiter = Version.META_DELIMITER;
|
||||||
|
private String resource;
|
||||||
|
|
||||||
|
private Builder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder endpoint(@NotNull Endpoint endpoint) {
|
||||||
|
this.endpoint = endpoint;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder currentVersion(@NotNull Version currentVersion) {
|
||||||
|
this.currentVersion = currentVersion;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder versionMetaDelimiter(@NotNull String versionMetaDelimiter) {
|
||||||
|
this.versionMetaDelimiter = versionMetaDelimiter;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Builder resource(@NotNull String resource) {
|
||||||
|
this.resource = resource;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public UpdateChecker build() {
|
||||||
|
if (currentVersion == null) {
|
||||||
|
throw new IllegalStateException("Current version is not set");
|
||||||
|
}
|
||||||
|
if (resource == null) {
|
||||||
|
throw new IllegalStateException("Resource is not set");
|
||||||
|
}
|
||||||
|
return new UpdateChecker(endpoint, currentVersion, versionMetaDelimiter, resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents endpoints from which the latest version can be queried
|
||||||
|
*/
|
||||||
|
public enum Endpoint {
|
||||||
|
SPIGOT((resource -> {
|
||||||
|
final String url = formatId("https://api.spigotmc.org/legacy/update.php?resource={id}", resource);
|
||||||
|
try (final InputStreamReader reader = new InputStreamReader(new URL(url).openConnection().getInputStream())) {
|
||||||
|
return new BufferedReader(reader).readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to fetch latest version", e);
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
POLYMART((resource -> {
|
||||||
|
final String url = formatId("https://api.polymart.org/v1/getResourceInfoSimple/?resource_id={id}&key=version", resource);
|
||||||
|
try (final InputStreamReader reader = new InputStreamReader(new URL(url).openConnection().getInputStream())) {
|
||||||
|
return new BufferedReader(reader).readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to fetch latest version", e);
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
MODRINTH((resource -> {
|
||||||
|
final String url = formatId("https://api.modrinth.com/v2/project/{id}/version", resource);
|
||||||
|
try (final InputStreamReader reader = new InputStreamReader(new URL(url).openConnection().getInputStream())) {
|
||||||
|
final JSONArray array = new JSONArray(new BufferedReader(reader).readLine());
|
||||||
|
for (int i = 0; i < array.length(); i++) {
|
||||||
|
final JSONObject object = array.getJSONObject(i);
|
||||||
|
if (object.getString("version_type").equals("release")) {
|
||||||
|
return object.getString("version_number");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("No versions found");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to fetch latest version", e);
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
GITHUB((resource -> {
|
||||||
|
final String url = formatId("https://api.github.com/repos/{id}/releases/latest", resource);
|
||||||
|
try (final InputStreamReader reader = new InputStreamReader(new URL(url).openConnection().getInputStream())) {
|
||||||
|
return new JSONObject(new BufferedReader(reader).readLine()).getString("tag_name");
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to fetch latest version", e);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
private final Function<String, String> queryFunction;
|
||||||
|
|
||||||
|
Endpoint(@NotNull Function<String, String> queryFunction) {
|
||||||
|
this.queryFunction = queryFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String query(@NotNull String resource) {
|
||||||
|
return queryFunction.apply(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static String formatId(@NotNull String endpoint, @NotNull String resource) {
|
||||||
|
return endpoint.replaceAll(Pattern.quote("{id}"), resource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Completed {
|
||||||
|
private final UpdateChecker checker;
|
||||||
|
private final Version latestVersion;
|
||||||
|
|
||||||
|
private Completed(@NotNull UpdateChecker checker, @NotNull Version latestVersion) {
|
||||||
|
this.checker = checker;
|
||||||
|
this.latestVersion = latestVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Version getLatestVersion() {
|
||||||
|
return latestVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public Version getCurrentVersion() {
|
||||||
|
return checker.currentVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isUpToDate() {
|
||||||
|
return checker.currentVersion.compareTo(latestVersion) >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,15 +1,53 @@
|
|||||||
package net.william278.desertwell;
|
package net.william278.desertwell;
|
||||||
|
|
||||||
|
import net.william278.desertwell.util.UpdateChecker;
|
||||||
|
import net.william278.desertwell.util.Version;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class UpdateCheckerTests {
|
public class UpdateCheckerTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUpdateChecker() {
|
public void testSpigotEndpoint() {
|
||||||
// Tests against the HuskSync resource ID
|
final UpdateChecker updateChecker = UpdateChecker.builder()
|
||||||
final UpdateChecker updateChecker = UpdateChecker.create(Version.fromString("1.0.0"), 97144);
|
.currentVersion(Version.fromString("1.0.0"))
|
||||||
Assertions.assertFalse(updateChecker.isUpToDate().join());
|
.resource("97144")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Assertions.assertFalse(updateChecker.check().join().isUpToDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGitHubEndpoint() {
|
||||||
|
final UpdateChecker updateChecker = UpdateChecker.builder()
|
||||||
|
.currentVersion(Version.fromString("1.0.0"))
|
||||||
|
.endpoint(UpdateChecker.Endpoint.GITHUB)
|
||||||
|
.resource("WiIIiam278/HuskHomes2")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Assertions.assertFalse(updateChecker.check().join().isUpToDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testModrinthEndpoint() {
|
||||||
|
final UpdateChecker updateChecker = UpdateChecker.builder()
|
||||||
|
.currentVersion(Version.fromString("1.0.0"))
|
||||||
|
.endpoint(UpdateChecker.Endpoint.MODRINTH)
|
||||||
|
.resource("huskhomes")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Assertions.assertFalse(updateChecker.check().join().isUpToDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPolymartEndpoint() {
|
||||||
|
final UpdateChecker updateChecker = UpdateChecker.builder()
|
||||||
|
.currentVersion(Version.fromString("1.0.0"))
|
||||||
|
.endpoint(UpdateChecker.Endpoint.POLYMART)
|
||||||
|
.resource("284")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Assertions.assertFalse(updateChecker.check().join().isUpToDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue