Skip to content

Commit

Permalink
Properly apply the update recipes in version order
Browse files Browse the repository at this point in the history
This is extremely important as some recipes are additive and changing
the order will affect the result.
I noticed that because a project updated from 3.5 to 3.15 was updated to
use quarkus-resteasy-client-jackson instead of
quarkus-rest-client-jackson due to the recipes not applied in order.
  • Loading branch information
gsmet committed Oct 30, 2024
1 parent 0e4dc19 commit c211c89
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -26,6 +35,8 @@ private QuarkusUpdatesRepository() {
}

private static final String QUARKUS_UPDATE_RECIPES_GA = "io.quarkus:quarkus-update-recipes";
private static final Pattern VERSION_EXTRACTION_PATTERN = Pattern.compile("[.][^.]+$");

public static final String DEFAULT_UPDATE_RECIPES_VERSION = "LATEST";

public static final String DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION = "4.46.0";
Expand All @@ -46,7 +57,7 @@ public static FetchResult fetchRecipes(MessageWriter log, MavenArtifactResolver
}

List<String> artifacts = new ArrayList<>();
Map<String, String> recipes = new HashMap<>();
Map<String, String> recipes = new LinkedHashMap<>();
String propRewritePluginVersion = null;

for (String gav : gavs) {
Expand Down Expand Up @@ -147,7 +158,7 @@ public String getRewritePluginVersion() {
}

static boolean shouldApplyRecipe(String recipeFileName, String currentVersion, String targetVersion) {
String recipeVersion = recipeFileName.replaceFirst("[.][^.]+$", "");
String recipeVersion = VERSION_EXTRACTION_PATTERN.matcher(recipeFileName).replaceFirst("");
final DefaultArtifactVersion recipeAVersion = new DefaultArtifactVersion(recipeVersion);
final DefaultArtifactVersion currentAVersion = new DefaultArtifactVersion(currentVersion);
final DefaultArtifactVersion targetAVersion = new DefaultArtifactVersion(targetVersion);
Expand All @@ -172,6 +183,7 @@ static Map<String, String> fetchUpdateRecipes(ResourceLoader resourceLoader, Str
.matches("^\\d\\H+.ya?ml$"))
.filter(p -> shouldApplyRecipe(p.getFileName().toString(),
versions[0], versions[1]))
.sorted(RecipeVersionComparator.INSTANCE)
.map(p -> {
try {
return new String[] { p.toString(),
Expand Down Expand Up @@ -231,4 +243,18 @@ static List<String> applyStartsWith(String key, Map<String, String[]> recipeDire
.collect(Collectors.toList());
}

private static class RecipeVersionComparator implements Comparator<Path> {

private static final RecipeVersionComparator INSTANCE = new RecipeVersionComparator();

@Override
public int compare(Path recipePath1, Path recipePath2) {
DefaultArtifactVersion recipeVersion1 = new DefaultArtifactVersion(
VERSION_EXTRACTION_PATTERN.matcher(recipePath1.getFileName().toString()).replaceFirst(""));
DefaultArtifactVersion recipeVersion2 = new DefaultArtifactVersion(
VERSION_EXTRACTION_PATTERN.matcher(recipePath2.getFileName().toString()).replaceFirst(""));

return recipeVersion1.compareTo(recipeVersion2);
}
}
}

0 comments on commit c211c89

Please sign in to comment.