Skip to content

Commit

Permalink
Upgrade Kotlin with quarkus update
Browse files Browse the repository at this point in the history
Fixes #32768
  • Loading branch information
gsmet committed May 4, 2023
1 parent 998ff4c commit 5552e70
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void logUpdates() {
}

final UpdateProject invoker = new UpdateProject(quarkusProject);
invoker.latestCatalog(targetCatalog);
invoker.targetCatalog(targetCatalog);
if (rewriteUpdateRecipesVersion != null) {
invoker.rewriteUpdateRecipesVersion(rewriteUpdateRecipesVersion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected void processProjectState(QuarkusProject quarkusProject) throws MojoExe
"Failed to resolve the recommended Quarkus extension catalog from the configured extension registries", e);
}
final UpdateProject invoker = new UpdateProject(quarkusProject);
invoker.latestCatalog(targetCatalog);
invoker.targetCatalog(targetCatalog);
invoker.targetPlatformVersion(platformVersion);
invoker.perModule(perModule);
invoker.appModel(resolveApplicationModel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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 TARGET_CATALOG = "quarkus.update-project.target-catalog";
public static final String PER_MODULE = "quarkus.update-project.per-module";
public static final String NO_REWRITE = "quarkus.update-project.rewrite.disabled";
public static final String TARGET_PLATFORM_VERSION = "quarkus.update-project.target-platform-version";
Expand All @@ -38,8 +38,8 @@ public UpdateProject(final QuarkusProject quarkusProject, final MessageWriter me
this.invocation = new QuarkusCommandInvocation(quarkusProject, new HashMap<>(), messageWriter);
}

public UpdateProject latestCatalog(ExtensionCatalog latestCatalog) {
invocation.setValue(LATEST_CATALOG, requireNonNull(latestCatalog, "latestCatalog is required"));
public UpdateProject targetCatalog(ExtensionCatalog latestCatalog) {
invocation.setValue(TARGET_CATALOG, requireNonNull(latestCatalog, "targetCatalog is required"));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class UpdateProjectCommandHandler implements QuarkusCommandHandler {
@Override
public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws QuarkusCommandException {
final ApplicationModel appModel = invocation.getValue(UpdateProject.APP_MODEL);
final ExtensionCatalog latestCatalog = invocation.getValue(UpdateProject.LATEST_CATALOG);
final ExtensionCatalog targetCatalog = invocation.getValue(UpdateProject.TARGET_CATALOG);
final String targetPlatformVersion = invocation.getValue(UpdateProject.TARGET_PLATFORM_VERSION);

final boolean perModule = invocation.getValue(UpdateProject.PER_MODULE, false);
Expand All @@ -68,14 +68,18 @@ public QuarkusCommandOutcome execute(QuarkusCommandInvocation invocation) throws
invocation.log().info("Instructions to update this project from '%s' to '%s':",
projectQuarkusPlatformBom.getVersion(), targetPlatformVersion);
final QuarkusProject quarkusProject = invocation.getQuarkusProject();
logUpdates(currentState, latestCatalog, false, perModule, quarkusProject.log());
logUpdates(currentState, targetCatalog, false, perModule, quarkusProject.log());
final boolean noRewrite = invocation.getValue(UpdateProject.NO_REWRITE, false);

if (!noRewrite) {
final BuildTool buildTool = quarkusProject.getExtensionManager().getBuildTool();
String kotlinVersion = getMetadata(targetCatalog, "project", "properties", "kotlin-version");

QuarkusUpdates.ProjectUpdateRequest request = new QuarkusUpdates.ProjectUpdateRequest(
buildTool,
projectQuarkusPlatformBom.getVersion(), targetPlatformVersion);
projectQuarkusPlatformBom.getVersion(),
targetPlatformVersion,
kotlinVersion);
Path recipe = null;
try {
recipe = Files.createTempFile("quarkus-project-recipe-", ".yaml");
Expand Down Expand Up @@ -534,4 +538,18 @@ boolean isEmpty() {
return extensionInfo.isEmpty();
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> T getMetadata(ExtensionCatalog catalog, String... path) {
Object currentValue = catalog.getMetadata();
for (String pathElement : path) {
if (!(currentValue instanceof Map)) {
return null;
}

currentValue = ((Map) currentValue).get(pathElement);
}

return (T) currentValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.update.QuarkusUpdatesRepository.FetchResult;
import io.quarkus.devtools.project.update.operations.UpdatePropertyOperation;
import io.quarkus.devtools.project.update.operations.UpgradeGradlePluginOperation;

public final class QuarkusUpdates {

Expand All @@ -28,11 +29,17 @@ public static FetchResult createRecipe(MessageWriter log, Path target, MavenArti
case MAVEN:
recipe.addOperation(new UpdatePropertyOperation("quarkus.platform.version", request.targetVersion))
.addOperation(new UpdatePropertyOperation("quarkus.version", request.targetVersion));
if (request.kotlinVersion != null) {
recipe.addOperation(new UpdatePropertyOperation("kotlin.version", request.kotlinVersion));
}
break;
case GRADLE:
case GRADLE_KOTLIN_DSL:
recipe.addOperation(new UpdatePropertyOperation("quarkusPlatformVersion", request.targetVersion))
.addOperation(new UpdatePropertyOperation("quarkusPluginVersion", request.targetVersion));
if (request.kotlinVersion != null) {
recipe.addOperation(new UpgradeGradlePluginOperation("org.jetbrains.kotlin.*", request.kotlinVersion));
}
break;
}

Expand All @@ -48,15 +55,17 @@ public static class ProjectUpdateRequest {
public BuildTool buildTool;
public String currentVersion;
public String targetVersion;
public String kotlinVersion;

public ProjectUpdateRequest(String currentVersion, String targetVersion) {
this(BuildTool.MAVEN, currentVersion, targetVersion);
public ProjectUpdateRequest(String currentVersion, String targetVersion, String kotlinVersion) {
this(BuildTool.MAVEN, currentVersion, targetVersion, kotlinVersion);
}

public ProjectUpdateRequest(BuildTool buildTool, String currentVersion, String targetVersion) {
public ProjectUpdateRequest(BuildTool buildTool, String currentVersion, String targetVersion, String kotlinVersion) {
this.buildTool = buildTool;
this.currentVersion = currentVersion;
this.targetVersion = targetVersion;
this.kotlinVersion = kotlinVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.devtools.project.update.operations;

import java.util.Map;

import io.quarkus.devtools.project.BuildTool;
import io.quarkus.devtools.project.update.RewriteOperation;

public class UpgradeGradlePluginOperation implements RewriteOperation {

public String pluginIdPattern;
public String newVersion;

public UpgradeGradlePluginOperation(String pluginIdPattern, String newVersion) {
this.pluginIdPattern = pluginIdPattern;
this.newVersion = newVersion;
}

@Override
public Map<String, Object> toMap(BuildTool buildTool) {
switch (buildTool) {
case GRADLE:
return Map.of(
"org.openrewrite.gradle.plugins.UpgradePluginVersion",
Map.of("pluginIdPattern", pluginIdPattern, "newVersion", newVersion));
default:
throw new UnsupportedOperationException("This operation is only supported for Gradle projects");
}
}
}

0 comments on commit 5552e70

Please sign in to comment.