Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -302,7 +302,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
if (!StringUtils.isBlank(newArtifactId) && !updated.getArtifactId().equals(newArtifactId)) {
updated = updated.withGav(updated.getGav().withArtifactId(newArtifactId));
}
if (!StringUtils.isBlank(newVersion)) {
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) {
String resolvedVersion;
try {
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null)
Expand All @@ -315,10 +315,23 @@ 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));
if (Objects.equals(original.getVersion(), updated.getVersion())) {
// Version unchanged: 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))
));
} else {
// Version changed: collapse GString to a literal
String replacement = DependencyNotation.toStringNotation(updated);
J.Literal newLiteral = literal.withValue(replacement)
.withValueSource(gstring.getDelimiter() + replacement + gstring.getDelimiter());
m = m.withArguments(singletonList(newLiteral));
}
}
}
}
Expand Down Expand Up @@ -595,7 +608,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
if (!StringUtils.isBlank(newArtifactId) && !updated.getArtifactId().equals(newArtifactId)) {
updated = updated.withGav(updated.getGav().withArtifactId(newArtifactId));
}
if (!StringUtils.isBlank(newVersion)) {
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(original.getVersion()) || Boolean.TRUE.equals(overrideManagedVersion))) {
String resolvedVersion;
try {
resolvedVersion = new DependencyVersionSelector(metadataFailures, gradleProject, null)
Expand All @@ -608,10 +621,23 @@ 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));
if (Objects.equals(original.getVersion(), updated.getVersion())) {
// Version unchanged: 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))
));
} else {
// Version changed: collapse StringTemplate to a literal
String replacement = DependencyNotation.toStringNotation(updated);
J.Literal newLiteral = literal.withValue(replacement)
.withValueSource(template.getDelimiter() + replacement + template.getDelimiter());
m = m.withArguments(singletonList(newLiteral));
}
}
}
}
Expand Down
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,12 @@ 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.

""",
spec -> spec.path("gradle.properties")
),
buildGradle(
"""
plugins {
Expand All @@ -248,9 +255,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 +268,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 +553,12 @@ 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
""",
spec -> spec.path("gradle.properties")
),
buildGradleKts(
"""
plugins {
Expand All @@ -558,8 +569,8 @@ void kotlinDslStringInterpolation() {
mavenCentral()
}

val commonsLangVersion: String by project
dependencies {
val commonsLangVersion = "2.6"
implementation("commons-lang:commons-lang:${commonsLangVersion}")
}
""",
Expand All @@ -572,9 +583,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