Skip to content

Commit 4dcd9b7

Browse files
committed
jdk updater now supports graalvm
1 parent cb2912c commit 4dcd9b7

File tree

6 files changed

+324
-79
lines changed

6 files changed

+324
-79
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.fvarrui.javapackager.utils;
2+
3+
import java.io.File;
4+
import java.nio.file.NotDirectoryException;
5+
6+
public class GraalVM {
7+
public File dir;
8+
9+
public GraalVM(File dir) throws NotDirectoryException {
10+
if(!dir.isDirectory()) throw new NotDirectoryException(dir.toString());
11+
this.dir = dir;
12+
}
13+
14+
}

src/main/java/io/github/fvarrui/javapackager/utils/updater/AdoptV3API.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public enum OperatingSystemArchitectureType {
189189
// AARCHx64 with alternative names:
190190
ARM64("aarch64");
191191

192-
private final String name;
192+
public final String name;
193193

194194
OperatingSystemArchitectureType(String name) {
195195
this.name = name;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.github.fvarrui.javapackager.utils.updater;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import org.apache.commons.io.IOUtils;
6+
7+
import java.net.URL;
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
import java.util.function.Predicate;
13+
14+
public class Github {
15+
16+
/**
17+
* Searches the latest GitHub release and returns a {@link SearchResult} object with all the relevant information.
18+
* @param repoName GitHub repository name.
19+
* @param currentVersion current version of the installed software.
20+
* @param assetNamePredicate predicate that contains the asset name and ist used to determine the asset to download.
21+
*/
22+
public static SearchResult searchUpdate(String repoName, String currentVersion, Predicate<String> assetNamePredicate) {
23+
Exception exception = null;
24+
boolean updateAvailable = false;
25+
String downloadUrl = null;
26+
String latestVersion = null;
27+
String downloadFile = null;
28+
String sha256 = null;
29+
try {
30+
JsonObject latestRelease = Json.fromUrlAsObject("https://api.github.com/repos/" + repoName + "/releases/latest");
31+
latestVersion = latestRelease.get("tag_name").getAsString();
32+
if (latestVersion != null)
33+
latestVersion = latestVersion.replaceAll("[^0-9.]", ""); // Before passing over remove everything except numbers and dots
34+
if (new UtilsVersion().isLatestBigger(currentVersion, latestVersion)) {
35+
updateAvailable = true;
36+
// Contains JsonObjects sorted by their asset-names lengths, from smallest to longest.
37+
// The following does that sorting.
38+
List<JsonObject> sortedArtifactObjects = new ArrayList<>();
39+
for (JsonElement e :
40+
latestRelease.getAsJsonArray("assets")) {
41+
JsonObject obj = e.getAsJsonObject();
42+
String name = obj.get("name").getAsString();
43+
if (sortedArtifactObjects.size() == 0) sortedArtifactObjects.add(obj);
44+
else {
45+
int finalIndex = 0;
46+
boolean isSmaller = false;
47+
for (int i = 0; i < sortedArtifactObjects.size(); i++) {
48+
String n = sortedArtifactObjects.get(i).get("name").getAsString();
49+
if (name.length() < n.length()) {
50+
isSmaller = true;
51+
finalIndex = i;
52+
break;
53+
}
54+
}
55+
if (!isSmaller) sortedArtifactObjects.add(obj);
56+
else sortedArtifactObjects.add(finalIndex, obj);
57+
}
58+
}
59+
60+
// Find asset-name containing our provided asset-name
61+
for (JsonObject obj : sortedArtifactObjects) {
62+
String name = obj.get("name").getAsString();
63+
if (assetNamePredicate.test(name)) {
64+
downloadFile = name;
65+
downloadUrl = obj.get("browser_download_url").getAsString();
66+
break;
67+
}
68+
}
69+
70+
if (downloadUrl == null) {
71+
List<String> names = new ArrayList<>();
72+
for (JsonObject obj :
73+
sortedArtifactObjects) {
74+
String n = obj.get("name").getAsString();
75+
names.add(n);
76+
}
77+
throw new Exception("Failed to find an asset-name matching the assetNamePredicate inside of " + Arrays.toString(names.toArray()));
78+
}
79+
80+
// Determine sha256
81+
String expectedShaAssetName = downloadFile + ".sha256";
82+
for (JsonObject obj : sortedArtifactObjects) {
83+
String name = obj.get("name").getAsString();
84+
if (name.equals(expectedShaAssetName)) {
85+
sha256 = IOUtils.toString(new URL(obj.get("browser_download_url").getAsString()), StandardCharsets.UTF_8);
86+
break;
87+
}
88+
}
89+
90+
}
91+
} catch (Exception e) {
92+
exception = e;
93+
}
94+
95+
return new SearchResult(updateAvailable, exception, latestVersion, downloadUrl, downloadFile, sha256);
96+
}
97+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.fvarrui.javapackager.utils.updater;
2+
3+
public class SearchResult {
4+
public boolean isUpdateAvailable;
5+
public Exception exception;
6+
public String latestVersion;
7+
public String downloadUrl;
8+
public String downloadFileExtension;
9+
public String sha256;
10+
11+
public SearchResult(boolean isUpdateAvailable, Exception exception, String latestVersion, String downloadUrl, String downloadFileExtension, String sha256) {
12+
this.isUpdateAvailable = isUpdateAvailable;
13+
this.exception = exception;
14+
this.latestVersion = latestVersion;
15+
this.downloadUrl = downloadUrl;
16+
this.downloadFileExtension = downloadFileExtension;
17+
this.sha256 = sha256;
18+
}
19+
}

0 commit comments

Comments
 (0)