Rework version discovery in wrapper

This commit is contained in:
Johannes Frohnmeyer 2021-12-08 20:38:28 +01:00
parent d98476a420
commit 7ac9cb1058
Signed by: Johannes
GPG Key ID: E76429612C2929F4
2 changed files with 58 additions and 39 deletions

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -8,6 +8,7 @@ import io.gitlab.jfronny.inceptum.util.MetaHolder;
import io.gitlab.jfronny.inceptum.util.OSCheck;
import io.gitlab.jfronny.inceptum.util.UpdateChecker;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@ -16,58 +17,76 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
public class Wrapper {
private static final Path INCEPTUM_LIB_DIR = MetaHolder.BASE_PATH.resolve("libraries/io/gitlab/jfronny/inceptum/Inceptum");
public static void main(String[] args) throws IOException {
System.out.println("Inceptum Wrapper v" + MetaHolder.VERSION.version + " (" + MetaHolder.VERSION.flavor + ")");
Path p = MetaHolder.BASE_PATH.resolve("libraries/io/gitlab/jfronny/inceptum/Inceptum");
if (!Files.exists(p)) {
System.err.println("No Inceptum jar was identified");
UpdateInfo ui = UpdateChecker.check(UpdateChannel.Stable, new ComparableVersion("0"), OSCheck.OS.getMojName(), c -> {}, System.out::println, System.err::println, (s, e) -> {
e.printStackTrace();
System.err.println(s);
});
if (ui == null) {
System.err.println("Couldn't download");
return;
} else {
System.err.println("Downloading " + ui.url());
Path dir = p.resolve(ui.newVersion().toString());
Files.createDirectories(dir);
try (InputStream is = new URL(ui.url()).openStream()) {
Files.write(dir.resolve("Inceptum-" + ui.newVersion() + '-' + OSCheck.OS.getMojName() + ".jar"), is.readAllBytes());
}
catch (Throwable t) {
Files.delete(dir);
Files.delete(p);
}
}
if (!Files.exists(INCEPTUM_LIB_DIR)) downloadLatest();
Optional<Path> localBinary = selectLocalBinary();
if (localBinary.isEmpty()) {
downloadLatest();
localBinary = selectLocalBinary();
}
Path pathChosen = null;
ComparableVersion chosenVer = new ComparableVersion("0");
try (Stream<Path> versionDirs = Files.list(p)) {
for (Path path1 : versionDirs.toList()) {
ComparableVersion cv = new ComparableVersion(path1.getFileName().toString());
if (cv.compareTo(chosenVer) > 0) {
chosenVer = cv;
pathChosen = path1;
}
}
}
if (pathChosen == null) {
Files.delete(p);
System.err.println("Something went wrong, please try again");
return;
if (localBinary.isEmpty()) {
throw new FileNotFoundException("Could not identify or download a valid binary");
}
List<String> newArgs = new ArrayList<>();
newArgs.add(JvmUtils.getJvm());
newArgs.add("-jar");
newArgs.add(pathChosen.resolve("Inceptum-" + chosenVer + '-' + OSCheck.OS.getMojName() + ".jar").toString());
newArgs.add(localBinary.get().toString());
newArgs.add("wrapper");
newArgs.addAll(Arrays.asList(args));
new ProcessBuilder(newArgs.toArray(new String[0]))
.inheritIO()
.start();
}
private static void downloadLatest() throws IOException {
System.err.println("No Inceptum jar was identified");
UpdateInfo ui = UpdateChecker.check(UpdateChannel.Stable, new ComparableVersion("0"), OSCheck.OS.getMojName(), c -> {}, System.out::println, System.err::println, (s, e) -> {
e.printStackTrace();
System.err.println(s);
});
if (ui == null) {
throw new FileNotFoundException("Could not identify a valid inceptum version. Are you connected to the internet?");
}
System.err.println("Downloading " + ui.url());
Path dir = INCEPTUM_LIB_DIR.resolve(ui.newVersion().toString());
Files.createDirectories(dir);
try (InputStream is = new URL(ui.url()).openStream()) {
Files.write(dir.resolve("Inceptum-" + ui.newVersion() + '-' + OSCheck.OS.getMojName() + ".jar"), is.readAllBytes());
}
catch (Throwable t) {
Files.delete(dir);
}
}
private static Optional<Path> selectLocalBinary() throws IOException {
Path pathChosen = null;
ComparableVersion versionChosen = new ComparableVersion("0");
try (Stream<Path> inceptumLibVersionDirStream = Files.list(INCEPTUM_LIB_DIR)) {
for (Path inceptumVersionPath : inceptumLibVersionDirStream.toList()) {
ComparableVersion versionCurrent = new ComparableVersion(inceptumVersionPath.getFileName().toString());
if (versionCurrent.compareTo(versionChosen) > 0) {
inceptumVersionPath = inceptumVersionPath.resolve("Inceptum-" + versionCurrent + '-' + OSCheck.OS.getMojName() + ".jar");
if (Files.exists(inceptumVersionPath)) {
versionChosen = versionCurrent;
pathChosen = inceptumVersionPath;
}
else {
System.out.println("Candidate " + inceptumVersionPath + " failed: doesn't exist");
}
}
}
}
if (pathChosen == null) {
System.err.println("Something went wrong, please try again");
return Optional.empty();
}
return Optional.of(pathChosen);
}
}