-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the version catalog update task
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
Showing
13 changed files
with
640 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
445 changes: 279 additions & 166 deletions
445
src/main/java/io/micronaut/build/catalogs/tasks/VersionCatalogUpdate.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
src/main/java/io/micronaut/build/utils/ComparableVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.