Search in sources :

Example 1 with AOV3Exception

use of net.adoptopenjdk.v3.api.AOV3Exception in project curiostack by curioswitch.

the class UpdateGradleWrapperTask method exec.

@TaskAction
public void exec() throws IOException {
    var rootProject = getProject().getRootProject();
    String releaseName = null;
    String urlLinux = null;
    String urlMac = null;
    String urlWindows = null;
    var clients = new AOV3Clients();
    final List<AOV3ListBinaryAssetView> releases;
    try (var client = clients.createClient()) {
        var errors = new ArrayList<AOV3Error>();
        releases = client.assetsForLatest(errors::add, new BigInteger(ToolDependencies.getOpenJdkVersion(rootProject)), AOV3JVMImplementation.HOTSPOT).execute();
    } catch (AOV3Exception | InterruptedException e) {
        throw new GradleException("Could not query for latest AdoptOpenJDK release.", e);
    }
    for (var release : releases) {
        releaseName = release.releaseName();
        var binary = release.binary();
        if (binary.architecture() != AOV3Architecture.X64 || binary.imageType() != AOV3ImageKind.JDK) {
            continue;
        }
        switch(binary.operatingSystem()) {
            case LINUX:
                {
                    urlLinux = binary.package_().link().toASCIIString();
                    break;
                }
            case MAC:
                {
                    urlMac = binary.package_().link().toASCIIString();
                    break;
                }
            case WINDOWS:
                {
                    urlWindows = binary.package_().link().toASCIIString();
                    break;
                }
            default:
        }
    }
    if (releaseName == null || urlLinux == null || urlMac == null || urlWindows == null) {
        throw new GradleException("Could not find OpenJDK packages: " + releases);
    }
    Map<String, String> templateVars = ImmutableMap.<String, String>builder().put("dest_folder", releaseName).put("url_linux", urlLinux).put("url_mac", urlMac).put("url_windows", urlWindows).put("dest_archive_name", releaseName + ".tar.gz.or.zip").put("version", releaseName).build();
    Jinjava jinjava = new Jinjava(JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build());
    String rendered = jinjava.render(Resources.toString(Resources.getResource("curiostack/get-jdk.sh.tmpl"), StandardCharsets.UTF_8), templateVars);
    Files.writeString(rootProject.file("gradle/get-jdk.sh").toPath(), rendered, StandardCharsets.UTF_8);
    Files.writeString(rootProject.file(".gradle/jdk-release-name.txt").toPath(), releaseName);
    var gradlew = rootProject.file("gradlew").toPath();
    var gradleWrapperLines = Files.readAllLines(gradlew);
    if (gradleWrapperLines.stream().anyMatch(line -> line.contains(". ./gradle/get-jdk.sh"))) {
        return;
    }
    // First line is always shebang, skip it.
    int lineIndexAfterCopyright = 1;
    // Skip empty lines
    for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
        if (!gradleWrapperLines.get(lineIndexAfterCopyright).isEmpty()) {
            break;
        }
    }
    // Skip comment lines, they are all the license
    for (; lineIndexAfterCopyright < gradleWrapperLines.size(); lineIndexAfterCopyright++) {
        if (!gradleWrapperLines.get(lineIndexAfterCopyright).startsWith("#")) {
            break;
        }
    }
    // Found first empty line after license, insert our JDK download script and write it out.
    var linesWithGetJdk = ImmutableList.<String>builderWithExpectedSize(gradleWrapperLines.size() + 2);
    linesWithGetJdk.addAll(gradleWrapperLines.subList(0, lineIndexAfterCopyright));
    linesWithGetJdk.add("").add(". ./gradle/get-jdk.sh");
    linesWithGetJdk.addAll(gradleWrapperLines.subList(lineIndexAfterCopyright, gradleWrapperLines.size()));
    Files.writeString(gradlew, String.join("\n", linesWithGetJdk.build()));
}
Also used : AOV3Clients(net.adoptopenjdk.v3.vanilla.AOV3Clients) ArrayList(java.util.ArrayList) Jinjava(com.hubspot.jinjava.Jinjava) AOV3Exception(net.adoptopenjdk.v3.api.AOV3Exception) AOV3ListBinaryAssetView(net.adoptopenjdk.v3.api.AOV3ListBinaryAssetView) GradleException(org.gradle.api.GradleException) BigInteger(java.math.BigInteger) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

Jinjava (com.hubspot.jinjava.Jinjava)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 AOV3Exception (net.adoptopenjdk.v3.api.AOV3Exception)1 AOV3ListBinaryAssetView (net.adoptopenjdk.v3.api.AOV3ListBinaryAssetView)1 AOV3Clients (net.adoptopenjdk.v3.vanilla.AOV3Clients)1 GradleException (org.gradle.api.GradleException)1 TaskAction (org.gradle.api.tasks.TaskAction)1