Skip to content
Closed
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 @@ -36,10 +36,13 @@
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.maven.MavenDownloadingException;
import org.openrewrite.maven.table.MavenMetadataFailures;
import org.openrewrite.properties.PropertiesVisitor;
import org.openrewrite.properties.tree.Properties;
import org.openrewrite.semver.DependencyMatcher;
import org.openrewrite.semver.Semver;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
Expand All @@ -59,6 +62,9 @@ public class ChangeDependency extends Recipe {
@EqualsAndHashCode.Exclude
transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this);

@EqualsAndHashCode.Exclude
transient Map<String, String> pendingPropertyUpdates = new ConcurrentHashMap<>();

@Option(displayName = "Old groupId",
description = "The old groupId to replace. The groupId is the first part of a dependency coordinate 'com.google.guava:guava:VERSION'. Supports glob expressions.",
example = "org.openrewrite.recipe")
Expand Down Expand Up @@ -161,7 +167,7 @@ public Validated<Object> validate() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindGradleProject(FindGradleProject.SearchCriteria.Marker).getVisitor(), new JavaIsoVisitor<ExecutionContext>() {
TreeVisitor<?, ExecutionContext> gradleVisitor = Preconditions.check(new FindGradleProject(FindGradleProject.SearchCriteria.Marker).getVisitor(), new JavaIsoVisitor<ExecutionContext>() {
final DependencyMatcher depMatcher = requireNonNull(DependencyMatcher.build(oldGroupId + ":" + oldArtifactId).getValue());
final DependencyMatcher existingMatcher = requireNonNull(DependencyMatcher.build(newGroupId + ":" + newArtifactId + (newVersion == null ? "" : ":" + newVersion)).getValue());

Expand Down Expand Up @@ -315,10 +321,22 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
}
}
if (original != updated) {
String replacement = DependencyNotation.toStringNotation(updated);
J.Literal newLiteral = literal.withValue(replacement)
.withValueSource(gstring.getDelimiter() + replacement + gstring.getDelimiter());
m = m.withArguments(singletonList(newLiteral));
// Always preserve GString structure, only update the literal prefix
String oldGav = original.getGroupId() + ":" + original.getArtifactId();
String newGav = updated.getGroupId() + ":" + updated.getArtifactId();
String oldValue = (String) literal.getValue();
String updatedValue = oldValue.replace(oldGav, newGav);
J.Literal updatedLiteral = literal.withValue(updatedValue).withValueSource(updatedValue);
m = m.withArguments(singletonList(
gstring.withStrings(ListUtils.mapFirst(strings, s -> updatedLiteral))
));
// If version was resolved, schedule property update
if (updated.getVersion() != null && !Objects.equals(original.getVersion(), updated.getVersion())) {
String varName = extractVersionVariableName(strings);
if (varName != null) {
pendingPropertyUpdates.put(varName, updated.getVersion());
}
}
}
}
}
Expand Down Expand Up @@ -608,10 +626,22 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
}
}
if (original != updated) {
String replacement = DependencyNotation.toStringNotation(updated);
J.Literal newLiteral = literal.withValue(replacement)
.withValueSource(template.getDelimiter() + replacement + template.getDelimiter());
m = m.withArguments(singletonList(newLiteral));
// Always preserve StringTemplate structure, only update the literal prefix
String oldGav = original.getGroupId() + ":" + original.getArtifactId();
String newGav = updated.getGroupId() + ":" + updated.getArtifactId();
String oldValue = (String) literal.getValue();
String updatedValue = oldValue.replace(oldGav, newGav);
J.Literal updatedLiteral = literal.withValue(updatedValue).withValueSource(updatedValue);
m = m.withArguments(singletonList(
template.withStrings(ListUtils.mapFirst(strings, s -> updatedLiteral))
));
// If version was resolved, schedule property update
if (updated.getVersion() != null && !Objects.equals(original.getVersion(), updated.getVersion())) {
String varName = extractVersionVariableName(strings);
if (varName != null) {
pendingPropertyUpdates.put(varName, updated.getVersion());
}
}
}
}
}
Expand Down Expand Up @@ -694,5 +724,46 @@ private GradleProject updateGradleModel(GradleProject gp, ExecutionContext ctx)
return gp;
}
});

return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof Properties.File && !pendingPropertyUpdates.isEmpty()) {
return new PropertiesVisitor<ExecutionContext>() {
@Override
public Properties.Entry visitEntry(Properties.Entry entry, ExecutionContext ctx) {
String version = pendingPropertyUpdates.get(entry.getKey());
if (version != null && !version.equals(entry.getValue().getText())) {
return entry.withValue(entry.getValue().withText(version));
}
return entry;
}
}.visit(tree, ctx);
}
return gradleVisitor.visit(tree, ctx);
}
};
}

private static @Nullable String extractVersionVariableName(List<J> strings) {
if (strings.size() >= 2) {
J versionPart = strings.get(strings.size() - 1);
// Groovy GString: strings[1] is G.GString.Value wrapping J.Identifier
if (versionPart instanceof G.GString.Value) {
J tree = ((G.GString.Value) versionPart).getTree();
if (tree instanceof J.Identifier) {
return ((J.Identifier) tree).getSimpleName();
}
}
// Kotlin StringTemplate: strings[1] is K.StringTemplate.Expression wrapping J.Identifier
if (versionPart instanceof K.StringTemplate.Expression) {
J tree = ((K.StringTemplate.Expression) versionPart).getTree();
if (tree instanceof J.Identifier) {
return ((J.Identifier) tree).getSimpleName();
}
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.Assertions.buildGradleKts;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.properties.Assertions.properties;

class ChangeDependencyTest implements RewriteTest {
@Override
Expand Down Expand Up @@ -238,6 +239,15 @@ implementation platform("org.apache.commons:commons-lang3:3.11")
void worksWithGString() {
rewriteRun(
spec -> spec.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null, true)),
properties(
"""
commonsLangVersion=2.6
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version should be changed to a specific version in the 3.11 range. Without it projects would break.

""",
"""
commonsLangVersion=3.11
""",
spec -> spec.path("gradle.properties")
),
buildGradle(
"""
plugins {
Expand All @@ -248,9 +258,8 @@ void worksWithGString() {
mavenCentral()
}

def version = '2.6'
dependencies {
implementation platform("commons-lang:commons-lang:${version}")
implementation platform("commons-lang:commons-lang:${commonsLangVersion}")
}
""",
"""
Expand All @@ -262,9 +271,8 @@ implementation platform("commons-lang:commons-lang:${version}")
mavenCentral()
}

def version = '2.6'
dependencies {
implementation platform("org.apache.commons:commons-lang3:3.11")
implementation platform("org.apache.commons:commons-lang3:${commonsLangVersion}")
}
"""
)
Expand Down Expand Up @@ -548,6 +556,15 @@ void kotlinDsl() {
void kotlinDslStringInterpolation() {
rewriteRun(
spec -> spec.recipe(new ChangeDependency("commons-lang", "commons-lang", "org.apache.commons", "commons-lang3", "3.11.x", null, null, true)),
properties(
"""
commonsLangVersion=2.6
""",
"""
commonsLangVersion=3.11
""",
spec -> spec.path("gradle.properties")
),
buildGradleKts(
"""
plugins {
Expand All @@ -558,8 +575,8 @@ void kotlinDslStringInterpolation() {
mavenCentral()
}

val commonsLangVersion: String by project
dependencies {
val commonsLangVersion = "2.6"
implementation("commons-lang:commons-lang:${commonsLangVersion}")
}
""",
Expand All @@ -572,9 +589,9 @@ void kotlinDslStringInterpolation() {
mavenCentral()
}

val commonsLangVersion: String by project
dependencies {
val commonsLangVersion = "2.6"
implementation("org.apache.commons:commons-lang3:3.11")
implementation("org.apache.commons:commons-lang3:${commonsLangVersion}")
}
"""
)
Expand Down
Loading