From b2f82505d3230b9fe7db02095236b13822f406b2 Mon Sep 17 00:00:00 2001 From: nea Date: Wed, 19 Apr 2023 22:28:09 +0200 Subject: Add maven updater --- .../java/moe/nea/libautoupdate/CurrentVersion.java | 3 +- src/main/java/moe/nea/libautoupdate/Main.java | 4 +- .../java/moe/nea/libautoupdate/MavenSource.java | 64 ++++++++++++++++++++++ .../java/moe/nea/libautoupdate/UpdateSource.java | 7 +++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/main/java/moe/nea/libautoupdate/MavenSource.java diff --git a/src/main/java/moe/nea/libautoupdate/CurrentVersion.java b/src/main/java/moe/nea/libautoupdate/CurrentVersion.java index eb8fc8a..b684ffa 100644 --- a/src/main/java/moe/nea/libautoupdate/CurrentVersion.java +++ b/src/main/java/moe/nea/libautoupdate/CurrentVersion.java @@ -36,7 +36,8 @@ public interface CurrentVersion { } /** - * Create a {@link CurrentVersion} that uses git tag names. Any difference in tag name will be treated as newer. + * Create a {@link CurrentVersion} that uses string tag names. Any difference in tag name will treat the current + * version as outdated.. * * @param tagName the current tag name */ diff --git a/src/main/java/moe/nea/libautoupdate/Main.java b/src/main/java/moe/nea/libautoupdate/Main.java index 3043348..de91b08 100644 --- a/src/main/java/moe/nea/libautoupdate/Main.java +++ b/src/main/java/moe/nea/libautoupdate/Main.java @@ -4,9 +4,9 @@ public class Main { public static void main(String[] args) { UpdateContext updater = new UpdateContext( - UpdateSource.githubUpdateSource("NotEnoughUpdates", "NotEnoughUpdates"), + UpdateSource.mavenSource("https://repo.nea.moe/releases", "moe.nea", "neuhax"), UpdateTarget.deleteAndSaveInTheSameFolder(Main.class), - CurrentVersion.ofTag("v2.1.1-pre1"), + CurrentVersion.ofTag("1.1.0"), "test" ); updater.cleanup(); diff --git a/src/main/java/moe/nea/libautoupdate/MavenSource.java b/src/main/java/moe/nea/libautoupdate/MavenSource.java new file mode 100644 index 0000000..102e8c7 --- /dev/null +++ b/src/main/java/moe/nea/libautoupdate/MavenSource.java @@ -0,0 +1,64 @@ +package moe.nea.libautoupdate; + +import com.google.gson.JsonPrimitive; +import lombok.*; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilderFactory; +import java.net.URL; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +/** + * Download source for a maven artifact. Pulls from the maven-metadata versioning info and does not support SNAPSHOT + * versions. + */ +@Value +@EqualsAndHashCode(callSuper = false) +@NonNull +public class MavenSource implements UpdateSource { + String repoUrl; + String module; + String artifact; + String classifier; + String extension; + + protected String getMavenBaseUrl() { + return String.format("%s/%s/%s", repoUrl, module.replace('.', '/'), artifact); + } + + protected String getMavenMetadataUrl() { + return getMavenBaseUrl() + "/maven-metadata.xml"; + } + + protected String getMavenArtifactUrl(String version) { + return String.format("%s/%s/%s-%s%s%s.%s", getMavenBaseUrl(), version, artifact, version, classifier.isEmpty() ? "" : "-", classifier, extension); + } + + @SneakyThrows + @Override + public CompletableFuture checkUpdate(String updateStream) { + var dbf = DocumentBuilderFactory.newInstance(); + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + var db = dbf.newDocumentBuilder(); + return CompletableFuture.supplyAsync(() -> { + try (val is = new URL(getMavenMetadataUrl()).openStream()) { + var document = db.parse(new InputSource(is)); + var metadata = (Element) document.getDocumentElement(); + var versioning = (Element) metadata.getElementsByTagName("versioning").item(0); + var latest = (Element) versioning.getElementsByTagName("latest").item(0); + var latestVersion = latest.getTextContent(); + return new UpdateData( + latestVersion, + new JsonPrimitive(latestVersion), + null, + getMavenArtifactUrl(latestVersion) + ); + } catch (Exception e) { + throw new CompletionException(e); + } + }); + } +} diff --git a/src/main/java/moe/nea/libautoupdate/UpdateSource.java b/src/main/java/moe/nea/libautoupdate/UpdateSource.java index 9a0e7ae..924563e 100644 --- a/src/main/java/moe/nea/libautoupdate/UpdateSource.java +++ b/src/main/java/moe/nea/libautoupdate/UpdateSource.java @@ -6,6 +6,13 @@ import java.util.concurrent.CompletableFuture; * UpdateSource is an interface to check for updates in an update stream. */ public interface UpdateSource { + /** + * Create a {@link MavenSource}. Construct directly for more control + */ + static MavenSource mavenSource(String repository, String moduleId, String artifactId) { + return new MavenSource(repository, moduleId, artifactId, "", "jar"); + } + static UpdateSource gistSource(String owner, String gistId) { return new GistSource(owner, gistId); } -- cgit