Skip to content

Commit

Permalink
Rework the version catalog update task
Browse files Browse the repository at this point in the history
This commit completely reworks the version catalog update task.
The previous implementation had issues because there was no shared
state between passes, so it was impossible to make smarter decisions
based on something seen previously.

It also appears that Gradle may, or may not, send all candidates,
and the reasons are unclear. This commit changes the algorithm
to be stateful instead. It also makes the resolution of metadata
parallel, so that the builds are faster.

The algorithm makes sure that candidate versions are passed
in order, from the latest release to the oldest one. Subclasses
may decide to reject a candidate, in which case the next one
will be tested, or they may accept it as a fallback, in which
case all versions can potentially be tested. If a version is
accepted, then the loop is interrupted and no other candidate
is tested.
  • Loading branch information
melix committed Jul 16, 2024
1 parent 7acaa1c commit 26f951c
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 355 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
projectVersion=7.1.5-SNAPSHOT
projectVersion=7.2.0-SNAPSHOT
title=Micronaut Build Plugins
projectDesc=Micronaut internal Gradle plugins. Not intended to be used in user's projects
projectUrl=https://micronaut.io
Expand Down
445 changes: 279 additions & 166 deletions src/main/java/io/micronaut/build/catalogs/tasks/VersionCatalogUpdate.java

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/main/java/io/micronaut/build/compat/FindBaselineTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.micronaut.build.compat;

import io.micronaut.build.utils.ExternalURLService;
import io.micronaut.build.utils.ComparableVersion;
import io.micronaut.build.utils.VersionParser;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.RegularFileProperty;
Expand Down Expand Up @@ -87,9 +89,9 @@ protected Provider<byte[]> getMavenMetadata() {
@TaskAction
public void execute() throws IOException {
byte[] metadata = getMavenMetadata().get();
List<VersionModel> releases = MavenMetadataVersionHelper.findReleasesFrom(metadata);
VersionModel current = VersionModel.of(trimVersion());
Optional<VersionModel> previous = MavenMetadataVersionHelper.findPreviousReleaseFor(current, releases);
List<ComparableVersion> releases = MavenMetadataVersionHelper.findReleasesFrom(metadata);
ComparableVersion current = VersionParser.parse(trimVersion());
Optional<ComparableVersion> previous = MavenMetadataVersionHelper.findPreviousReleaseFor(current, releases);
if (!previous.isPresent()) {
throw new IllegalStateException("Could not find a previous version for " + current);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@
*/
package io.micronaut.build.compat;

import org.gradle.api.GradleException;
import io.micronaut.build.utils.ComparableVersion;
import io.micronaut.build.utils.VersionParser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public abstract class MavenMetadataVersionHelper {
private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+\\.\\d+\\.\\d+)([.-]\\w+)?$");
private static final String VERSION_OPEN_TAG = "<version>";
private static final String VERSION_CLOSE_TAG = "</version>";

private MavenMetadataVersionHelper() {

}

public static List<VersionModel> findReleasesFrom(byte[] mavenMetadata) {
public static List<ComparableVersion> findReleasesFrom(byte[] mavenMetadata) {
List<String> allVersions = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new StringReader(new String(mavenMetadata, StandardCharsets.UTF_8)))) {
String line;
Expand All @@ -49,28 +48,16 @@ public static List<VersionModel> findReleasesFrom(byte[] mavenMetadata) {
}
}
return allVersions.stream()
.map(version -> {
Matcher m = VERSION_PATTERN.matcher(version);
if (m.find()) {
if (m.group(2) != null) {
// discard non release versions like M2, RC1, etc
return null;
}
return m.group(1);
}
return null;
})
.filter(Objects::nonNull)
.map(VersionModel::of)
.map(VersionParser::parse)
.sorted()
.collect(Collectors.toList());

} catch (IOException e) {
throw new GradleException("Error parsing maven-metadata.xml", e);
return List.of();
}
}

public static Optional<VersionModel> findPreviousReleaseFor(VersionModel version, List<VersionModel> releases) {
public static Optional<ComparableVersion> findPreviousReleaseFor(ComparableVersion version, List<ComparableVersion> releases) {
return releases.stream()
.filter(v -> v.compareTo(version) < 0)
.reduce((a, b) -> b);
Expand Down
82 changes: 0 additions & 82 deletions src/main/java/io/micronaut/build/compat/VersionModel.java

This file was deleted.

153 changes: 153 additions & 0 deletions src/main/java/io/micronaut/build/utils/ComparableVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package io.micronaut.build.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

/**
* A version model, used for comparing versions when performing automatic updates.
* The version model is primarily aimed at supporting semantic versioning, but in
* order to take into account more real world use cases, it also has support for
* qualifiers and "extra" version components.
*/
public final class ComparableVersion implements Comparable<ComparableVersion> {
private static final List<String> PARTIAL_QUALIFIER_ORDER = List.of("snapshot", "alpha", "beta", "rc", "final");

private final String fullVersion;
private final Integer major;
private final Integer minor;
private final Integer patch;
private final String qualifier;
private final Integer qualifierVersion;
private final List<Integer> extraVersions;

ComparableVersion(
String fullVersion,
Integer major,
Integer minor,
Integer patch,
String qualifier,
Integer qualifierVersion, List<Integer> extraVersions
) {
this.fullVersion = fullVersion;
this.major = major;
this.minor = minor;
this.patch = patch;
this.qualifier = qualifier;
this.qualifierVersion = qualifierVersion;
this.extraVersions = extraVersions;
}

public String fullVersion() {
return fullVersion;
}

public Optional<Integer> major() {
return Optional.ofNullable(major);
}

public Optional<Integer> minor() {
return Optional.ofNullable(minor);
}

public Optional<Integer> patch() {
return Optional.ofNullable(patch);
}

public Optional<String> qualifier() {
return Optional.ofNullable(qualifier);
}

public Optional<Integer> qualifierVersion() {
return Optional.ofNullable(qualifierVersion);
}

public List<Integer> getExtraVersions() {
return extraVersions;
}

public List<Integer> getVersionComponents() {
var all = new ArrayList<Integer>(3 + extraVersions.size());
all.add(Objects.requireNonNullElse(major, 0));
all.add(Objects.requireNonNullElse(minor, 0));
all.add(Objects.requireNonNullElse(patch, 0));
all.addAll(extraVersions);
return all;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ComparableVersion that = (ComparableVersion) o;
return Objects.equals(fullVersion, that.fullVersion);
}

@Override
public int hashCode() {
return Objects.hashCode(fullVersion);
}

@Override
public String toString() {
return fullVersion;
}

@Override
public int compareTo(ComparableVersion o) {
var components = getVersionComponents();
var otherComponents = o.getVersionComponents();

if (components.size() != otherComponents.size()) {
if (components.size() < otherComponents.size()) {
components = new ArrayList<>(components);
while (components.size() < otherComponents.size()) {
components.add(0);
}
} else {
otherComponents = new ArrayList<>(otherComponents);
while (otherComponents.size() < components.size()) {
otherComponents.add(0);
}
}
}

for (int i = 0; i < components.size(); i++) {
int result = components.get(i).compareTo(otherComponents.get(i));
if (result != 0) {
return result;
}
}

if (qualifier == null && o.qualifier != null) {
return 1;
} else if (qualifier != null && o.qualifier == null) {
return -1;
} else if (qualifier != null) {
int thisQualifierIndex = PARTIAL_QUALIFIER_ORDER.indexOf(qualifier.toLowerCase(Locale.US));
int otherQualifierIndex = PARTIAL_QUALIFIER_ORDER.indexOf(o.qualifier.toLowerCase(Locale.US));

if (thisQualifierIndex != otherQualifierIndex) {
return Integer.compare(thisQualifierIndex, otherQualifierIndex);
}
}

if (qualifierVersion != null && o.qualifierVersion != null) {
return qualifierVersion.compareTo(o.qualifierVersion);
} else if (qualifierVersion != null) {
return 1;
} else if (o.qualifierVersion != null) {
return -1;
}

return 0;
}

}
38 changes: 38 additions & 0 deletions src/main/java/io/micronaut/build/utils/Downloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.build.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class Downloader {
public static byte[] doDownload(URI uri) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try (InputStream stream = uri.toURL().openStream()) {
byte[] buffer = new byte[4096];
int read;
while ((read = stream.read(buffer)) > 0) {
outputStream.write(buffer, 0, read);
}
} catch (IOException e) {
return null;
}
return outputStream.toByteArray();
}
}
Loading

0 comments on commit 26f951c

Please sign in to comment.