Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional recipe artifacts in quarkus update #41663

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, Rew
if (rewrite.updateRecipesVersion != null) {
args.add("--updateRecipesVersion=" + rewrite.updateRecipesVersion);
}
if (rewrite.additionalUpdateRecipeCoords != null) {
args.add("--additionalUpdateRecipeCoords=" + rewrite.additionalUpdateRecipeCoords);
}
if (rewrite.noRewrite) {
args.add("--noRewrite");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, Rew
if (rewrite.updateRecipesVersion != null) {
args.add("-DupdateRecipesVersion=" + rewrite.updateRecipesVersion);
}
if (rewrite.additionalUpdateRecipeCoords != null) {
args.add("-DadditionalUpdateRecipeCoords" + rewrite.additionalUpdateRecipeCoords);
}
if (rewrite.dryRun) {
args.add("-DrewriteDryRun");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public class RewriteGroup {
"--rewrite-plugin-version" }, description = "Use a custom OpenRewrite plugin version.")
public String pluginVersion;

@CommandLine.Option(order = 4, names = {
"--additional-update-recipe-coords" }, description = "Specify an additional list of artifacts to retrieve recipes from.")
public String additionalUpdateRecipeCoords;

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public abstract class QuarkusUpdate extends QuarkusPlatformTask {
private String rewritePluginVersion = null;

private String rewriteUpdateRecipesVersion = null;
private String rewriteAdditionalUpdateRecipeCoords = null;

@Input
@Optional
Expand Down Expand Up @@ -87,6 +88,18 @@ public QuarkusUpdate setRewriteUpdateRecipesVersion(String rewriteUpdateRecipesV
return this;
}

@Input
@Optional
public String getRewriteAdditionalUpdateRecipeCoords() {
return rewriteAdditionalUpdateRecipeCoords;
}

@Option(description = " The additional artifacts to retrieve recipes from.", option = "additionalUpdateRecipeCoords")
public QuarkusUpdate setRewriteAdditionalUpdateRecipeCoords(String rewriteAdditionalUpdateRecipeCoords) {
this.rewriteAdditionalUpdateRecipeCoords = rewriteAdditionalUpdateRecipeCoords;
return this;
}

@Input
@Optional
public String getTargetStreamId() {
Expand Down Expand Up @@ -143,6 +156,9 @@ public void logUpdates() {
if (rewriteUpdateRecipesVersion != null) {
invoker.rewriteUpdateRecipesVersion(rewriteUpdateRecipesVersion);
}
if (rewriteAdditionalUpdateRecipeCoords != null) {
invoker.rewriteAdditionalUpdateRecipeCoords(rewriteAdditionalUpdateRecipeCoords);
}
if (rewritePluginVersion != null) {
invoker.rewritePluginVersion(rewritePluginVersion);
}
Expand Down
9 changes: 9 additions & 0 deletions devtools/maven/src/main/java/io/quarkus/maven/UpdateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public class UpdateMojo extends QuarkusProjectStateMojoBase {
@Parameter(property = "updateRecipesVersion", required = false)
private String rewriteUpdateRecipesVersion;

/**
* The list of artifacts containing rewrite recipes
*/
@Parameter(property = "additionalUpdateRecipeCoords", required = false)
private String rewriteAdditionalUpdateRecipeCoords;

/**
* Target stream (e.g: 2.0)
*/
Expand Down Expand Up @@ -116,6 +122,9 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
if (rewriteUpdateRecipesVersion != null) {
invoker.rewriteUpdateRecipesVersion(rewriteUpdateRecipesVersion);
}
if (rewriteAdditionalUpdateRecipeCoords != null) {
invoker.rewriteAdditionalUpdateRecipeCoords(rewriteAdditionalUpdateRecipeCoords);
}
invoker.rewriteDryRun(rewriteDryRun);
invoker.noRewrite(noRewrite);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class UpdateProject {
public static final String TARGET_PLATFORM_VERSION = "quarkus.update-project.target-platform-version";
public static final String REWRITE_PLUGIN_VERSION = "quarkus.update-project.rewrite.plugin-version";
public static final String REWRITE_UPDATE_RECIPES_VERSION = "quarkus.update-project.rewrite.update-recipes-version";
public static final String REWRITE_ADDITIONAL_UPDATE_RECIPE_COORDS = "quarkus.update-project.rewrite.additional-update-recipe-coords";
public static final String REWRITE_DRY_RUN = "quarkus.update-project.rewrite.dry-run";

private final QuarkusCommandInvocation invocation;
Expand Down Expand Up @@ -64,6 +65,11 @@ public UpdateProject rewriteUpdateRecipesVersion(String rewriteUpdateRecipesVers
return this;
}

public UpdateProject rewriteAdditionalUpdateRecipeCoords(String rewriteAdditionalUpdateRecipeCoords) {
invocation.setValue(REWRITE_ADDITIONAL_UPDATE_RECIPE_COORDS, rewriteAdditionalUpdateRecipeCoords);
return this;
}

public UpdateProject rewriteDryRun(boolean rewriteDryRun) {
invocation.setValue(REWRITE_DRY_RUN, rewriteDryRun);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
final String updateRecipesVersion = invocation.getValue(
UpdateProject.REWRITE_UPDATE_RECIPES_VERSION,
QuarkusUpdatesRepository.DEFAULT_UPDATE_RECIPES_VERSION);
final String additionalUpdateRecipeCoords = invocation.getValue(
UpdateProject.REWRITE_ADDITIONAL_UPDATE_RECIPE_COORDS,
null);
final FetchResult fetchResult = QuarkusUpdates.createRecipe(invocation.log(),
recipe,
QuarkusProjectHelper.artifactResolver(), buildTool, updateRecipesVersion, request);
QuarkusProjectHelper.artifactResolver(), buildTool, updateRecipesVersion,
additionalUpdateRecipeCoords, request);
invocation.log().info("OpenRewrite recipe generated: %s", recipe);

String rewritePluginVersion = invocation.getValue(UpdateProject.REWRITE_PLUGIN_VERSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ private static void runMavenUpdate(MessageWriter log, Path baseDir, String rewri
executeCommand(baseDir, getMavenUpdateCommand(mvnBinary, rewritePluginVersion, recipesGAV, recipe, dryRun), log);

// format the sources
executeCommand(baseDir, getMavenProcessSourcesCommand(mvnBinary), log);
if (!dryRun) {
executeCommand(baseDir, getMavenProcessSourcesCommand(mvnBinary), log);
}
}

private static void runGradleUpdate(MessageWriter log, Path baseDir, String rewritePluginVersion, String recipesGAV,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ private QuarkusUpdates() {
}

public static FetchResult createRecipe(MessageWriter log, Path target, MavenArtifactResolver artifactResolver,
BuildTool buildTool, String updateRecipesVersion,
BuildTool buildTool, String updateRecipesVersion, String additionalUpdateRecipeCoords,
ProjectUpdateRequest request)
throws IOException {
final FetchResult result = QuarkusUpdatesRepository.fetchRecipes(log, artifactResolver, buildTool,
updateRecipesVersion,
additionalUpdateRecipeCoords,
request.currentVersion,
request.targetVersion,
request.projectExtensionsUpdateInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -41,50 +43,75 @@ private QuarkusUpdatesRepository() {

public static FetchResult fetchRecipes(MessageWriter log, MavenArtifactResolver artifactResolver,
BuildTool buildTool,
String recipeVersion, String currentVersion,
String recipeVersion, String additionalUpdateRecipeCoords, String currentVersion,
String targetVersion, List<ExtensionUpdateInfo> topExtensionDependency) {
final String gav = QUARKUS_RECIPE_GA + ":" + recipeVersion;

Map<String, String[]> recipeDirectoryNames = new LinkedHashMap<>();
recipeDirectoryNames.put("core", new String[] { currentVersion, targetVersion });
for (ExtensionUpdateInfo dep : topExtensionDependency) {
recipeDirectoryNames.put(
toKey(dep),
new String[] { dep.getCurrentDep().getVersion(), dep.getRecommendedDependency().getVersion() });

List<String> gavs = new ArrayList<>();
gavs.add(QUARKUS_RECIPE_GA + ":" + recipeVersion);
if (additionalUpdateRecipeCoords != null) {
gavs.addAll(Arrays.stream(additionalUpdateRecipeCoords.split(",")).map(String::strip).toList());
}

try {
final Artifact artifact = artifactResolver.resolve(DependencyUtils.toArtifact(gav)).getArtifact();
final ResourceLoader resourceLoader = ResourceLoaders.resolveFileResourceLoader(
artifact.getFile());
final Map<String, String> recipes = fetchUpdateRecipes(resourceLoader, "quarkus-updates", recipeDirectoryNames);
final Properties props = resourceLoader.loadResourceAsPath("quarkus-updates/", p -> {
final Properties properties = new Properties();
final Path propPath = p.resolve("recipes.properties");
if (Files.isRegularFile(propPath)) {
try (final InputStream inStream = Files.newInputStream(propPath)) {
properties.load(inStream);
List<String> artifacts = new ArrayList<>();
Map<String, String> recipes = new HashMap<>();
String propRewritePluginVersion = null;

for (String gav : gavs) {

Map<String, String[]> recipeDirectoryNames = new LinkedHashMap<>();
recipeDirectoryNames.put("core", new String[] { currentVersion, targetVersion });
for (ExtensionUpdateInfo dep : topExtensionDependency) {
recipeDirectoryNames.put(
toKey(dep),
new String[] { dep.getCurrentDep().getVersion(), dep.getRecommendedDependency().getVersion() });
}

try {
final Artifact artifact = artifactResolver.resolve(DependencyUtils.toArtifact(gav)).getArtifact();
String resolvedGAV = artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion();
artifacts.add(resolvedGAV);
final ResourceLoader resourceLoader = ResourceLoaders.resolveFileResourceLoader(
artifact.getFile());
Map<String, String> newRecipes = fetchUpdateRecipes(resourceLoader, "quarkus-updates", recipeDirectoryNames);
recipes.putAll(newRecipes);
final Properties props = resourceLoader.loadResourceAsPath("quarkus-updates/", p -> {
final Properties properties = new Properties();
final Path propPath = p.resolve("recipes.properties");
if (Files.isRegularFile(propPath)) {
try (final InputStream inStream = Files.newInputStream(propPath)) {
properties.load(inStream);
}
}
return properties;
});

final String pluginVersion = getPropRewritePluginVersion(props, buildTool);
if (propRewritePluginVersion == null) {
propRewritePluginVersion = pluginVersion;
} else if (!propRewritePluginVersion.equals(pluginVersion)) {
throw new RuntimeException(
"quarkus update artifacts require multiple rewrite plugin versions: " + propRewritePluginVersion
+ " and " + pluginVersion);
}
return properties;
});
final String propRewritePluginVersion = getPropRewritePluginVersion(props, buildTool);

log.info(String.format(
"Resolved io.quarkus:quarkus-updates-recipes:%s with %s recipe(s) to update from %s to %s (initially made for OpenRewrite %s plugin version: %s) ",
artifact.getVersion(),
recipes.size(),
currentVersion,
targetVersion,
buildTool,
propRewritePluginVersion));
return new FetchResult(artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion(),
new ArrayList<>(recipes.values()), propRewritePluginVersion);
} catch (BootstrapMavenException e) {
throw new RuntimeException("Failed to resolve artifact: " + gav, e);
} catch (IOException e) {
throw new RuntimeException("Failed to load recipes in artifact: " + gav, e);

log.info(String.format(
"Resolved %s with %s recipe(s) to update from %s to %s (initially made for OpenRewrite %s plugin version: %s) ",
gav,
recipes.size(),
currentVersion,
targetVersion,
buildTool,
propRewritePluginVersion));

} catch (BootstrapMavenException e) {
throw new RuntimeException("Failed to resolve artifact: " + gav, e);
} catch (IOException e) {
throw new RuntimeException("Failed to load recipes in artifact: " + gav, e);
}
}

return new FetchResult(String.join(",", artifacts),
new ArrayList<>(recipes.values()), propRewritePluginVersion);
}

private static String getPropRewritePluginVersion(Properties props, BuildTool buildTool) {
Expand Down
Loading