Skip to content

Commit

Permalink
Add rewrite support to update
Browse files Browse the repository at this point in the history
  • Loading branch information
ia3andy committed Mar 21, 2023
1 parent 8099f14 commit 21b9292
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 58 deletions.
13 changes: 9 additions & 4 deletions devtools/cli/src/main/java/io/quarkus/cli/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@
import io.quarkus.cli.build.BaseBuildCommand;
import io.quarkus.cli.build.BuildSystemRunner;
import io.quarkus.cli.common.TargetQuarkusVersionGroup;
import io.quarkus.cli.update.RewriteGroup;
import picocli.CommandLine;

@CommandLine.Command(name = "update", sortOptions = false, showDefaultValues = true, mixinStandardHelpOptions = false, header = "Suggest recommended project updates with the possibility to apply them.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", parameterListHeading = "%n", optionListHeading = "%nOptions:%n")
@CommandLine.Command(name = "update", aliases = { "up",
"upgrade" }, sortOptions = false, showDefaultValues = true, mixinStandardHelpOptions = false, header = "Suggest recommended project updates with the possibility to apply them.", headerHeading = "%n", commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", parameterListHeading = "%n", optionListHeading = "%nOptions:%n")
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.Option(order = 1, names = { "--per-module" }, description = "Display information per project module.")
@CommandLine.ArgGroup(order = 1, heading = "%nRewrite:%n")
RewriteGroup rewrite = new RewriteGroup();

@CommandLine.Option(order = 2, names = { "--per-module" }, description = "Display information per project module.")
public boolean perModule = false;

@Override
public Integer call() throws Exception {
try {
final BuildSystemRunner runner = getRunner();
return runner.updateProject(targetQuarkusVersion.platformVersion, targetQuarkusVersion.streamId, perModule);
return runner.updateProject(targetQuarkusVersion, rewrite, perModule);
} catch (Exception e) {
return output.handleCommandException(e, "Unable to collect Quarkus project information: " + e.getMessage());
return output.handleCommandException(e, "Unable to run Quarkus project update : " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.common.TargetQuarkusVersionGroup;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.cli.update.RewriteGroup;
import io.quarkus.devtools.project.BuildTool;
import picocli.CommandLine;

Expand Down Expand Up @@ -102,7 +104,8 @@ Integer listExtensions(RunModeOption runMode, ListFormatOptions format, boolean

Integer projectInfo(boolean perModule) throws Exception;

Integer updateProject(String targetPlatformVersion, String targetPlatformStream, boolean perModule) throws Exception;
Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGroup rewrite, boolean perModule)
throws Exception;

BuildCommandArgs prepareAction(String action, BuildOptions buildOptions, RunModeOption runMode, List<String> params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.common.TargetQuarkusVersionGroup;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.cli.update.RewriteGroup;
import io.quarkus.devtools.messagewriter.MessageWriter;
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.registry.config.RegistriesConfigLocator;
Expand Down Expand Up @@ -136,7 +138,7 @@ public Integer projectInfo(boolean perModule) {
}

@Override
public Integer updateProject(String targetPlatformVersion, String targetPlatformStream, boolean perModule)
public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGroup rewrite, boolean perModule)
throws Exception {
MessageWriter.info().error(
"quarkus update is not yet available for Gradle projects. See https://github.com/quarkusio/quarkus/issues/31658 for more information.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.common.TargetQuarkusVersionGroup;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.cli.update.RewriteGroup;
import io.quarkus.devtools.project.BuildTool;

public class JBangRunner implements BuildSystemRunner {
Expand Down Expand Up @@ -75,7 +77,7 @@ public Integer projectInfo(boolean perModule) {
}

@Override
public Integer updateProject(String targetPlatformVersion, String targetPlatformStream, boolean perModule)
public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGroup rewrite, boolean perModule)
throws Exception {
throw new UnsupportedOperationException("Not there yet. ;)");
}
Expand Down
26 changes: 21 additions & 5 deletions devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import io.quarkus.cli.common.OutputOptionMixin;
import io.quarkus.cli.common.PropertiesOptions;
import io.quarkus.cli.common.RunModeOption;
import io.quarkus.cli.common.TargetQuarkusVersionGroup;
import io.quarkus.cli.registry.RegistryClientMixin;
import io.quarkus.cli.update.RewriteGroup;
import io.quarkus.devtools.commands.AddExtensions;
import io.quarkus.devtools.commands.ListCategories;
import io.quarkus.devtools.commands.ListExtensions;
Expand Down Expand Up @@ -147,7 +149,7 @@ public Integer projectInfo(boolean perModule) throws Exception {
}

@Override
public Integer updateProject(String targetPlatformVersion, String targetPlatformStream, boolean perModule)
public Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGroup rewrite, boolean perModule)
throws Exception {
ArrayDeque<String> args = new ArrayDeque<>();
setMavenProperties(args, true);
Expand All @@ -157,11 +159,25 @@ public Integer updateProject(String targetPlatformVersion, String targetPlatform
QuarkusProjectHelper.artifactResolver(), MessageWriter.info());
final Properties props = ToolsUtils.readQuarkusProperties(extensionCatalog);
args.add(ToolsUtils.getPluginKey(props) + ":" + ToolsUtils.getMavenPluginVersion(props) + ":update");
if (targetPlatformVersion != null) {
args.add("-DplatformVersion=" + targetPlatformVersion);
args.add("-e");
args.add("-N");
if (targetQuarkusVersion.platformVersion != null) {
args.add("-DplatformVersion=" + targetQuarkusVersion.platformVersion);
}
if (targetPlatformStream != null) {
args.add("-Dstream=" + targetPlatformStream);
if (targetQuarkusVersion.streamId != null) {
args.add("-Dstream=" + targetQuarkusVersion.streamId);
}
if (rewrite.noRewrite) {
args.add("-DnoRewrite");
}
if (rewrite.pluginVersion != null) {
args.add("-DrewritePluginVersion=" + rewrite.pluginVersion);
}
if (rewrite.updateRecipesVersion != null) {
args.add("-DupdateRecipesVersion=" + rewrite.updateRecipesVersion);
}
if (rewrite.dryRun) {
args.add("-DrewriteDryRun");
}
if (perModule) {
args.add("-DperModule");
Expand Down
23 changes: 23 additions & 0 deletions devtools/cli/src/main/java/io/quarkus/cli/update/RewriteGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.cli.update;

import picocli.CommandLine;

public class RewriteGroup {

@CommandLine.Option(order = 0, names = {
"--no-rewrite" }, description = "Disable the rewrite feature.", defaultValue = "false")
public boolean noRewrite = false;

@CommandLine.Option(order = 1, names = {
"--dry-run" }, description = "Rewrite in dry-mode.", defaultValue = "false")
public boolean dryRun = false;

@CommandLine.Option(order = 2, names = {
"--update-recipes-version" }, description = "Use a custom io.quarkus:quarkus-update-recipes version. This artifact contains the base recipes used by this tool to update a project.")
public String updateRecipesVersion;

@CommandLine.Option(order = 3, names = {
"--rewrite-plugin-version" }, description = "Use a custom OpenRewrite plugin version.")
public String pluginVersion;

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void logUpdates() {
invoker.targetPlatformVersion(targetPlatformVersion);
invoker.perModule(perModule);
// This is currently not supported with gradle
invoker.generateRewriteConfig(false);
invoker.noRewrite(true);
invoker.appModel(extension().getApplicationModel());
try {
invoker.execute();
Expand Down
52 changes: 51 additions & 1 deletion devtools/maven/src/main/java/io/quarkus/maven/UpdateMojo.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package io.quarkus.maven;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

import java.util.List;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import io.quarkus.devtools.commands.UpdateProject;
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.maven.dependency.ArtifactCoords;
import io.quarkus.registry.RegistryResolutionException;
import io.quarkus.registry.catalog.ExtensionCatalog;
Expand Down Expand Up @@ -39,12 +46,43 @@ public class UpdateMojo extends QuarkusProjectStateMojoBase {
@Parameter(property = "platformVersion", required = false)
private String platformVersion;

/**
* The OpenRewrite plugin version
*/
@Parameter(property = "rewritePluginVersion", required = true, defaultValue = QuarkusUpdateCommand.DEFAULT_MAVEN_REWRITE_PLUGIN_VERSION)
private String rewritePluginVersion;

/**
* Disable the rewrite feature.
*/
@Parameter(property = "noRewrite", required = false, defaultValue = "false")
private Boolean noRewrite;

/**
* Rewrite in dry-mode.
*/
@Parameter(property = "rewriteDryRun", required = false, defaultValue = "false")
private Boolean rewriteDryRun;

/**
* "The io.quarkus:quarkus-update-recipes version. This artifact contains the base recipes used by this tool to update a
* project.
*/
@Parameter(property = "updateRecipesVersion", required = false)
private String rewriteUpdateRecipesVersion;

/**
* Target stream (e.g: 2.0)
*/
@Parameter(property = "stream", required = false)
private String stream;

@Component
private MavenSession mavenSession;

@Component
private BuildPluginManager pluginManager;

@Override
protected void validateParameters() throws MojoExecutionException {
getLog().warn("quarkus:update goal is experimental, its options and output might change in future versions");
Expand Down Expand Up @@ -78,9 +116,21 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
invoker.targetPlatformVersion(platformVersion);
invoker.perModule(perModule);
invoker.appModel(resolveApplicationModel());
if (rewritePluginVersion != null) {
invoker.rewritePluginVersion(rewritePluginVersion);
}
if (rewriteUpdateRecipesVersion != null) {
invoker.rewritePluginVersion(rewriteUpdateRecipesVersion);
}
invoker.rewriteDryRun(rewriteDryRun);
invoker.noRewrite(noRewrite);

try {
invoker.execute();
final QuarkusCommandOutcome result = invoker.execute();
if (!result.isSuccess()) {
throw new MojoExecutionException(
"The command did not succeed.");
}
} catch (QuarkusCommandException e) {
throw new MojoExecutionException("Failed to resolve the available updates", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public class UpdateProject {
public static final String APP_MODEL = "quarkus.update-project.app-model";
public static final String LATEST_CATALOG = "quarkus.update-project.latest-catalog";
public static final String PER_MODULE = "quarkus.update-project.per-module";

public static final String GENERATE_REWRITE_CONFIG = "quarkus.update-project.generate-update-config";
public static final String NO_REWRITE = "quarkus.update-project.rewrite.disabled";
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_DRY_RUN = "quarkus.update-project.rewrite.dry-run";

private final QuarkusCommandInvocation invocation;
private final UpdateProjectCommandHandler handler = new UpdateProjectCommandHandler();
Expand All @@ -46,8 +48,24 @@ public UpdateProject appModel(ApplicationModel applicationModel) {
return this;
}

public UpdateProject generateRewriteConfig(boolean generateUpdateConfig) {
invocation.setValue(GENERATE_REWRITE_CONFIG, generateUpdateConfig);
public UpdateProject noRewrite(boolean noRewrite) {
invocation.setValue(NO_REWRITE, noRewrite);
return this;
}

public UpdateProject rewritePluginVersion(String rewritePluginVersion) {
invocation.setValue(REWRITE_PLUGIN_VERSION, requireNonNull(rewritePluginVersion, "rewritePluginVersion is required"));
return this;
}

public UpdateProject rewriteUpdateRecipesVersion(String rewriteUpdateRecipesVersion) {
invocation.setValue(REWRITE_UPDATE_RECIPES_VERSION,
requireNonNull(rewriteUpdateRecipesVersion, "rewriteUpdateRecipesVersion is required"));
return this;
}

public UpdateProject rewriteDryRun(boolean rewriteDryRun) {
invocation.setValue(REWRITE_DRY_RUN, rewriteDryRun);
return this;
}

Expand Down
Loading

0 comments on commit 21b9292

Please sign in to comment.