Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -29,6 +29,7 @@
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.maven.MavenDownloadingException;
Expand Down Expand Up @@ -153,7 +154,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
if (!(pluginArgs.get(0) instanceof J.Literal)) {
return m;
}
String pluginId = literalValue(pluginArgs.get(0));
String pluginId;
if (((J.MethodInvocation) m.getSelect()).getSimpleName().equals("kotlin")) {
pluginId = "kotlin";
} else {
pluginId = literalValue(pluginArgs.get(0));
}
if (pluginId == null || !StringUtils.matchesGlob(pluginId, pluginIdPattern)) {
return m;
}
Expand All @@ -162,8 +168,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
try {
String currentVersion = literalValue(versionArgs.get(0));
if (currentVersion != null) {
String resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
.select(new GroupArtifactVersion(pluginId, pluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
String resolvedVersion;
if (pluginId.equals("kotlin")) {
Comment thread
Laurens-W marked this conversation as resolved.
Outdated
String fullPluginId = String.format("org.jetbrains.%s.%s", pluginId, literalValue(pluginArgs.get(0)));
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
.select(new GroupArtifactVersion(fullPluginId, fullPluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
} else {
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, gradleSettings)
.select(new GroupArtifactVersion(pluginId, pluginId + ".gradle.plugin", currentVersion), "classpath", newVersion, versionPattern, ctx);
}
acc.pluginIdToNewVersion.put(pluginId, resolvedVersion);
} else if (versionArgs.get(0) instanceof G.GString) {
G.GString gString = (G.GString) versionArgs.get(0);
Expand Down Expand Up @@ -236,7 +249,12 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
}
assert m.getSelect() != null;
List<Expression> pluginArgs = ((J.MethodInvocation) m.getSelect()).getArguments();
String pluginId = literalValue(pluginArgs.get(0));
String pluginId;
if (((J.MethodInvocation) m.getSelect()).getSimpleName().equals("kotlin")) {
pluginId = "kotlin";
} else {
pluginId = literalValue(pluginArgs.get(0));
}
if (pluginId == null || !StringUtils.matchesGlob(pluginId, pluginIdPattern)) {
return m;
}
Expand Down Expand Up @@ -264,8 +282,15 @@ public J visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionC
String oldVersion = literalValue(initializer);
String newVersion = acc.pluginIdToNewVersion.get(acc.versionPropNameToPluginId.get(visited.getSimpleName()));
if (newVersion != null && !newVersion.equals(oldVersion)) {
String valueSource = initializer.getValueSource() == null || oldVersion == null ? initializer.getValueSource() : initializer.getValueSource().replace(oldVersion, newVersion);
return visited.withInitializer(initializer.withValueSource(valueSource).withValue(newVersion));
VersionComparator versionComparator = Semver.validate(newVersion, versionPattern).getValue();
if (versionComparator == null) {
return visited;
}
Optional<String> finalVersion = versionComparator.upgrade(oldVersion != null ? oldVersion : "", singletonList(newVersion));
if (finalVersion.isPresent()) {
String valueSource = initializer.getValueSource() == null || oldVersion == null ? initializer.getValueSource() : initializer.getValueSource().replace(oldVersion, newVersion);
return visited.withInitializer(initializer.withValueSource(valueSource).withValue(finalVersion.get()));
}
}
}
return visited;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,44 @@ void upgradePlugin() {
);
}

@Test
void upgradeKotlinPluginLiteralVersion() {
rewriteRun(
spec -> spec.recipe(new UpgradePluginVersion("kotlin", "2.3.0", null)),
buildGradleKts(
"""
plugins {
kotlin("jvm") version "2.0.0"
}
""",
"""
plugins {
kotlin("jvm") version "2.3.0"
}
"""
)
);
}

@Test
void upgradeKotlinPlugin() {
rewriteRun(
spec -> spec.recipe(new UpgradePluginVersion("kotlin", "latest.minor", null)),
buildGradleKts(
"""
plugins {
kotlin("jvm") version "2.0.0"
}
""",
"""
plugins {
kotlin("jvm") version "2.3.0"
}
"""
)
);
}

@Test
void upgradeGradleSettingsPlugin() {
rewriteRun(
Expand Down Expand Up @@ -127,6 +165,23 @@ void change() {
);
}

@Test
void dontDowngradeWhenExactVersionVariable() {
rewriteRun(
spec -> spec.recipe(new UpgradePluginVersion("org.openrewrite.rewrite", "5.39.9", null)),
settingsGradle(
"""
pluginManagement {
plugins {
String v = '5.40.0'
id 'org.openrewrite.rewrite' version v
}
}
"""
)
);
}

@Test
void pluginManagementPlugin() {
rewriteRun(
Expand Down
Loading