Skip to content

Commit

Permalink
Merge pull request #32536 from ia3andy/use-update-props
Browse files Browse the repository at this point in the history
Various Quarkus Update fixes and enhancements
  • Loading branch information
gsmet authored Apr 12, 2023
2 parents aa39566 + b4bd1a1 commit 8c8720f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 27 deletions.
2 changes: 1 addition & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Update extends BaseBuildCommand implements Callable<Integer> {
@CommandLine.ArgGroup(order = 0, heading = "%nTarget Quarkus version:%n", multiplicity = "0..1")
TargetQuarkusVersionGroup targetQuarkusVersion = new TargetQuarkusVersionGroup();

@CommandLine.ArgGroup(order = 1, heading = "%nRewrite:%n")
@CommandLine.ArgGroup(order = 1, heading = "%nRewrite:%n", exclusive = false)
RewriteGroup rewrite = new RewriteGroup();

@CommandLine.Option(order = 2, names = { "--per-module" }, description = "Display information per project module.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, Rew
args.add(targetQuarkusVersion.platformVersion);
}
if (!StringUtil.isNullOrEmpty(targetQuarkusVersion.streamId)) {
args.add("--streamId");
args.add("--stream");
args.add(targetQuarkusVersion.streamId);
}
if (rewrite.pluginVersion != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import io.quarkus.devtools.commands.UpdateProject;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.update.QuarkusUpdateCommand;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
Expand All @@ -27,9 +26,9 @@ public abstract class QuarkusUpdate extends QuarkusPlatformTask {
private String targetStreamId;
private String targetPlatformVersion;

private String rewritePluginVersion = QuarkusUpdateCommand.DEFAULT_GRADLE_REWRITE_PLUGIN_VERSION;
private String rewritePluginVersion = null;

private String rewriteUpdateRecipesVersion;
private String rewriteUpdateRecipesVersion = null;

@Input
@Optional
Expand Down Expand Up @@ -66,6 +65,7 @@ public void setPerModule(boolean perModule) {
}

@Input
@Optional
public String getRewritePluginVersion() {
return rewritePluginVersion;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ public String getTargetStreamId() {
return targetStreamId;
}

@Option(description = "A target stream id, for example: 2.0", option = "streamId")
@Option(description = "A target stream, for example: 2.0", option = "stream")
public void setStreamId(String targetStreamId) {
this.targetStreamId = targetStreamId;
}
Expand Down Expand Up @@ -141,7 +141,12 @@ public void logUpdates() {

final UpdateProject invoker = new UpdateProject(quarkusProject);
invoker.latestCatalog(targetCatalog);
invoker.rewritePluginVersion(rewritePluginVersion);
if (rewriteUpdateRecipesVersion != null) {
invoker.rewriteUpdateRecipesVersion(rewriteUpdateRecipesVersion);
}
if (rewritePluginVersion != null) {
invoker.rewritePluginVersion(rewritePluginVersion);
}
invoker.targetPlatformVersion(targetPlatformVersion);
invoker.rewriteDryRun(rewriteDryRun);
invoker.noRewrite(noRewrite);
Expand Down
8 changes: 4 additions & 4 deletions devtools/maven/src/main/java/io/quarkus/maven/UpdateMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandException;
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.update.QuarkusUpdateCommand;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
Expand Down Expand Up @@ -49,7 +49,7 @@ public class UpdateMojo extends QuarkusProjectStateMojoBase {
/**
* The OpenRewrite plugin version
*/
@Parameter(property = "rewritePluginVersion", required = true, defaultValue = QuarkusUpdateCommand.DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION)
@Parameter(property = "rewritePluginVersion", required = false)
private String rewritePluginVersion;

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ protected void validateParameters() throws MojoExecutionException {

@Override
protected void processProjectState(QuarkusProject quarkusProject) throws MojoExecutionException {

QuarkusProjectHelper.setArtifactResolver(artifactResolver());
final ExtensionCatalog targetCatalog;
try {
if (platformVersion != null) {
Expand Down Expand Up @@ -120,7 +120,7 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
invoker.rewritePluginVersion(rewritePluginVersion);
}
if (rewriteUpdateRecipesVersion != null) {
invoker.rewritePluginVersion(rewriteUpdateRecipesVersion);
invoker.rewriteUpdateRecipesVersion(rewriteUpdateRecipesVersion);
}
invoker.rewriteDryRun(rewriteDryRun);
invoker.noRewrite(noRewrite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.quarkus.devtools.commands.data.QuarkusCommandOutcome;
import io.quarkus.devtools.commands.handlers.ProjectInfoCommandHandler.PlatformInfo;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.QuarkusProject;
import io.quarkus.devtools.project.QuarkusProjectHelper;
import io.quarkus.devtools.project.state.ExtensionProvider;
Expand Down Expand Up @@ -71,21 +72,26 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
final boolean noRewrite = invocation.getValue(UpdateProject.NO_REWRITE, false);

if (!noRewrite) {
final BuildTool buildTool = quarkusProject.getExtensionManager().getBuildTool();
QuarkusUpdates.ProjectUpdateRequest request = new QuarkusUpdates.ProjectUpdateRequest(
quarkusProject.getExtensionManager().getBuildTool(),
buildTool,
projectQuarkusPlatformBom.getVersion(), targetPlatformVersion);
Path recipe = null;
try {
recipe = Files.createTempFile("quarkus-project-recipe-", ".yaml");
final String updateRecipesVersion = invocation.getValue(UpdateProject.REWRITE_UPDATE_RECIPES_VERSION,
QuarkusUpdatesRepository.DEFAULT_UPDATE_RECIPES_VERSION);
QuarkusUpdates.createRecipe(recipe,
QuarkusProjectHelper.artifactResolver(), updateRecipesVersion, request);
final String rewritePluginVersion = invocation.getValue(UpdateProject.REWRITE_PLUGIN_VERSION);
final QuarkusUpdatesRepository.FetchResult fetchResult = QuarkusUpdates.createRecipe(invocation.log(),
recipe,
QuarkusProjectHelper.artifactResolver(), buildTool, updateRecipesVersion, request);
final String rewritePluginVersion = invocation.getValue(UpdateProject.REWRITE_PLUGIN_VERSION,
fetchResult.getRewritePluginVersion());
final boolean rewriteDryRun = invocation.getValue(UpdateProject.REWRITE_DRY_RUN, false);
invocation.log().warn(
"The update feature does not yet handle updates of the extension versions. If needed, update your extensions manually.");
QuarkusUpdateCommand.handle(
invocation.log(),
quarkusProject.getExtensionManager().getBuildTool(),
buildTool,
quarkusProject.getProjectDirPath(),
rewritePluginVersion,
recipe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class QuarkusUpdateCommand {

public static final String MAVEN_REWRITE_PLUGIN_GROUP = "org.openrewrite.maven";
public static final String MAVEN_REWRITE_PLUGIN_ARTIFACT = "rewrite-maven-plugin";
public static final String DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION = "4.41.0";
public static final String DEFAULT_GRADLE_REWRITE_PLUGIN_VERSION = "5.38.0";

public static Set<String> ADDITIONAL_SOURCE_FILES_SET = Set.of("**/META-INF/services/**",
"**/*.txt",
"**/*.adoc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.update.QuarkusUpdatesRepository.FetchResult;
import io.quarkus.devtools.project.update.operations.UpdatePropertyOperation;

public final class QuarkusUpdates {

private QuarkusUpdates() {
}

public static void createRecipe(Path target, MavenArtifactResolver artifactResolver, String updateRecipesVersion,
public static FetchResult createRecipe(MessageWriter log, Path target, MavenArtifactResolver artifactResolver,
BuildTool buildTool, String updateRecipesVersion,
ProjectUpdateRequest request)
throws IOException {
final List<String> recipes = QuarkusUpdatesRepository.fetchRecipes(artifactResolver, updateRecipesVersion,
final FetchResult result = QuarkusUpdatesRepository.fetchRecipes(log, artifactResolver, buildTool, updateRecipesVersion,
request.currentVersion,
request.targetVersion);
QuarkusUpdateRecipe recipe = new QuarkusUpdateRecipe()
Expand All @@ -34,10 +36,11 @@ public static void createRecipe(Path target, MavenArtifactResolver artifactResol
break;
}

for (String s : recipes) {
for (String s : result.getRecipes()) {
recipe.addRecipes(QuarkusUpdateRecipeIO.readRecipesYaml(s));
}
QuarkusUpdateRecipeIO.write(target, recipe);
return result;
}

public static class ProjectUpdateRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package io.quarkus.devtools.project.update;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.eclipse.aether.artifact.Artifact;

import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.bootstrap.util.DependencyUtils;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.platform.descriptor.loader.json.ResourceLoader;
import io.quarkus.platform.descriptor.loader.json.ResourceLoaders;

Expand All @@ -23,14 +29,20 @@ private QuarkusUpdatesRepository() {
private static final String QUARKUS_RECIPE_GA = "io.quarkus:quarkus-update-recipes";
public static final String DEFAULT_UPDATE_RECIPES_VERSION = "LATEST";

public static List<String> fetchRecipes(MavenArtifactResolver artifactResolver, String recipeVersion, String currentVersion,
public static final String DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION = "4.41.0";
public static final String DEFAULT_GRADLE_REWRITE_PLUGIN_VERSION = "5.38.0";
public static final String PROP_REWRITE_MAVEN_PLUGIN_VERSION = "rewrite-maven-plugin-version";
public static final String PROP_REWRITE_GRADLE_PLUGIN_VERSION = "rewrite-gradle-plugin-version";

public static FetchResult fetchRecipes(MessageWriter log, MavenArtifactResolver artifactResolver, BuildTool buildTool,
String recipeVersion, String currentVersion,
String targetVersion) {
final String gav = QUARKUS_RECIPE_GA + ":" + recipeVersion;
try {
final Artifact artifact = artifactResolver.resolve(DependencyUtils.toArtifact(gav)).getArtifact();
final ResourceLoader resourceLoader = ResourceLoaders.resolveFileResourceLoader(
artifactResolver.resolve(DependencyUtils.toArtifact(gav)).getArtifact().getFile());

return resourceLoader.loadResourceAsPath("quarkus-updates/core",
artifact.getFile());
final List<String> recipes = resourceLoader.loadResourceAsPath("quarkus-updates/core",
path -> {
try (final Stream<Path> pathStream = Files.walk(path)) {
return pathStream
Expand All @@ -45,13 +57,67 @@ public static List<String> fetchRecipes(MavenArtifactResolver artifactResolver,
}).collect(Collectors.toList());
}
});
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 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(recipes, 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);
}
}

private static String getPropRewritePluginVersion(Properties props, BuildTool buildTool) {
switch (buildTool) {
case MAVEN:
return Optional.ofNullable(props.getProperty(PROP_REWRITE_MAVEN_PLUGIN_VERSION))
.orElse(DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION);
case GRADLE:
case GRADLE_KOTLIN_DSL:
return Optional.ofNullable(props.getProperty(PROP_REWRITE_GRADLE_PLUGIN_VERSION))
.orElse(DEFAULT_GRADLE_REWRITE_PLUGIN_VERSION);
default:
throw new IllegalStateException("This build tool does not support update " + buildTool);
}
}

public static class FetchResult {

private final List<String> recipes;
private final String rewritePluginVersion;

public FetchResult(List<String> recipes, String rewritePluginVersion) {
this.rewritePluginVersion = rewritePluginVersion;
this.recipes = recipes;
}

public List<String> getRecipes() {
return recipes;
}

public String getRewritePluginVersion() {
return rewritePluginVersion;
}
}

static boolean shouldApplyRecipe(String recipeFileName, String currentVersion, String targetVersion) {
String recipeVersion = recipeFileName.replaceFirst("[.][^.]+$", "");
final DefaultArtifactVersion recipeAVersion = new DefaultArtifactVersion(recipeVersion);
Expand Down

0 comments on commit 8c8720f

Please sign in to comment.