Check if matching version is already downloaded

This commit is contained in:
Andres Almiray 2023-05-24 20:16:06 +02:00
parent f2226e009e
commit ab17acedd9
No known key found for this signature in database
GPG key ID: CCC55C5167419ADB

View file

@ -1,10 +1,15 @@
//JAVA 11+ //JAVA 11+
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
class get_jreleaser { class get_jreleaser {
public static void main(String... args) throws Exception { public static void main(String... args) throws Exception {
@ -27,15 +32,27 @@ class get_jreleaser {
System.exit(1); System.exit(1);
} }
var versionFile = Path.of("JRELEASER_VERSION");
var cachedVersion = "";
if (Files.exists(versionFile)) {
try {
cachedVersion = Files.readString(versionFile).trim();
} catch (IOException e) {
System.out.printf("☠️ JReleaser %s could not be resolved%n", version);
e.printStackTrace();
System.exit(1);
}
}
// resolve latest to a tagged release // resolve latest to a tagged release
if ("latest".equalsIgnoreCase(version)) { if ("latest".equalsIgnoreCase(version)) {
var url = "https://jreleaser.org/releases/latest/download/VERSION"; var url = "https://jreleaser.org/releases/latest/download/VERSION";
var file = Path.of("JRELEASER_VERSION");
try (var stream = new URL(url).openStream()) { try (var stream = new URL(url).openStream()) {
System.out.printf("✅ Located version marker%n"); System.out.printf("✅ Located version marker%n");
Files.copy(stream, file, StandardCopyOption.REPLACE_EXISTING); Files.copy(stream, versionFile, REPLACE_EXISTING);
version = new String(Files.readAllBytes(file)).trim(); version = new String(Files.readAllBytes(versionFile)).trim();
System.out.printf("✅ JReleaser latest resolves to %s%n", version); System.out.printf("✅ JReleaser latest resolves to %s%n", version);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.printf("❌ JReleaser %s not found%n", version); System.out.printf("❌ JReleaser %s not found%n", version);
@ -46,18 +63,32 @@ class get_jreleaser {
e.printStackTrace(); e.printStackTrace();
System.exit(1); System.exit(1);
} }
} else {
try {
Files.writeString(versionFile, version, CREATE, TRUNCATE_EXISTING, WRITE);
} catch (IOException e) {
System.out.printf("☠️ JReleaser %s could not be resolved%n", version);
e.printStackTrace();
System.exit(1);
}
}
var jreleaserJar = Path.of("jreleaser-cli.jar");
if (version.equals(cachedVersion) && Files.exists(jreleaserJar)) {
// assume jreleaserJar has the expected version for now
System.out.printf("✅ JReleaser %s already downloaded%n", version);
System.exit(0);
} }
// setup the actual download // setup the actual download
var tag = !"early-access".equals(version) ? "v" + version : version; var tag = !"early-access".equals(version) ? "v" + version : version;
var url = "https://github.com/jreleaser/jreleaser/releases/download/" + tag + "/jreleaser-tool-provider-" + version + ".jar"; var url = "https://github.com/jreleaser/jreleaser/releases/download/" + tag + "/jreleaser-tool-provider-" + version + ".jar";
var file = Path.of("jreleaser-cli.jar");
try (var stream = new URL(url).openStream()) { try (var stream = new URL(url).openStream()) {
System.out.printf("✅ Located JReleaser %s%n", version); System.out.printf("✅ Located JReleaser %s%n", version);
System.out.printf("⬇️ Downloading %s%n", url); System.out.printf("⬇️ Downloading %s%n", url);
var size = Files.copy(stream, file, StandardCopyOption.REPLACE_EXISTING); var size = Files.copy(stream, jreleaserJar, REPLACE_EXISTING);
System.out.printf("%s << copied %d bytes%n", file, size); System.out.printf("%s << copied %d bytes%n", jreleaserJar, size);
System.out.printf("✅ JReleaser installed successfully%n"); System.out.printf("✅ JReleaser installed successfully%n");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.printf("❌ JReleaser %s not found%n", version); System.out.printf("❌ JReleaser %s not found%n", version);