Skip to content

Commit

Permalink
Support upgrading versions of dependencies that include configuration…
Browse files Browse the repository at this point in the history
… closures
  • Loading branch information
sambsnyd committed Apr 26, 2023
1 parent d0f2b78 commit 4f73bd9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 54 deletions.
53 changes: 27 additions & 26 deletions rewrite-gradle/src/main/groovy/RewriteGradleProject.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ExternalDependency
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
import org.gradle.api.file.FileCollection
Expand All @@ -37,57 +38,57 @@ import org.gradle.process.ProcessForkOptions

interface DependencyHandlerSpec extends DependencyHandler {
Dependency annotationProcessor(String dependencyNotation)
Dependency annotationProcessor(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency annotationProcessor(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency annotationProcessor(Map<String, String> dependencyNotation)
Dependency annotationProcessor(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency annotationProcessor(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency api(String dependencyNotation)
Dependency api(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency api(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency api(Map<String, String> dependencyNotation)
Dependency api(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency api(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency classpath(String dependencyNotation)
Dependency classpath(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency classpath(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency classpath(Map<String, String> dependencyNotation)
Dependency classpath(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency classpath(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency compile(String dependencyNotation)
Dependency compile(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency compile(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency compile(Map<String, String> dependencyNotation)
Dependency compile(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency compile(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency compileOnly(String dependencyNotation)
Dependency compileOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency compileOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency compileOnly(Map<String, String> dependencyNotation)
Dependency compileOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency compileOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency implementation(String dependencyNotation)
Dependency implementation(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency implementation(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency implementation(Map<String, String> dependencyNotation)
Dependency implementation(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency implementation(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtime(String dependencyNotation)
Dependency runtime(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtime(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtime(Map<String, String> dependencyNotation)
Dependency runtime(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtime(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtimeOnly(String dependencyNotation)
Dependency runtimeOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtimeOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtimeOnly(Map<String, String> dependencyNotation)
Dependency runtimeOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtimeOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtimeClasspath(String dependencyNotation)
Dependency runtimeClasspath(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtimeClasspath(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency runtimeClasspath(Map<String, String> dependencyNotation)
Dependency runtimeClasspath(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency runtimeClasspath(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testCompile(String dependencyNotation)
Dependency testCompile(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testCompile(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testCompile(Map<String, String> dependencyNotation)
Dependency testCompile(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testCompile(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testImplementation(String dependencyNotation)
Dependency testImplementation(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testImplementation(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testImplementation(Map<String, String> dependencyNotation)
Dependency testImplementation(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testImplementation(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testRuntime(String dependencyNotation)
Dependency testRuntime(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testRuntime(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testRuntime(Map<String, String> dependencyNotation)
Dependency testRuntime(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testRuntime(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testRuntimeOnly(String dependencyNotation)
Dependency testRuntimeOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testRuntimeOnly(String dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
Dependency testRuntimeOnly(Map<String, String> dependencyNotation)
Dependency testRuntimeOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ModuleDependency) Closure closure)
Dependency testRuntimeOnly(Map<String, String> dependencyNotation, @DelegatesTo(strategy=Closure.DELEGATE_ONLY, value= ExternalDependency) Closure closure)
}

interface RewriteTestSpec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return e.warn(m);
}
}
} else if (depArgs.size() == 3 && depArgs.get(0) instanceof G.MapEntry
} else if (depArgs.size() >= 3 && depArgs.get(0) instanceof G.MapEntry
&& depArgs.get(1) instanceof G.MapEntry
&& depArgs.get(2) instanceof G.MapEntry) {
Expression groupValue = ((G.MapEntry) depArgs.get(0)).getValue();
Expand Down Expand Up @@ -248,6 +248,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
versionLiteral
.withValueSource(requireNonNull(versionLiteral.getValueSource()).replace(version, newVersion))
.withValue(newVersion)));
newArgs.addAll(depArgs.subList(3, depArgs.size()));

return m.withArguments(newArgs);
} else if(versionExp instanceof J.Identifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ void guava() {
}
dependencies {
implementation 'com.google.guava:guava:29.0-jre'
compileOnly 'com.google.guava:guava:29.0-jre'
runtimeOnly ('com.google.guava:guava:29.0-jre') {
force = true
}
}
""",
"""
Expand All @@ -62,30 +65,31 @@ void guava() {
}
dependencies {
implementation 'com.google.guava:guava:30.1.1-jre'
compileOnly 'com.google.guava:guava:30.1.1-jre'
runtimeOnly ('com.google.guava:guava:30.1.1-jre') {
force = true
}
}
""",
spec -> {
spec.afterRecipe(after -> {
Optional<GradleProject> maybeGp = after.getMarkers().findFirst(GradleProject.class);
assertThat(maybeGp).isPresent();
GradleProject gp = maybeGp.get();
GradleDependencyConfiguration compileClasspath = gp.getConfiguration("compileClasspath");
assertThat(compileClasspath).isNotNull();
assertThat(
compileClasspath.getRequested().stream()
.filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion()))
.findAny())
.as("GradleProject requested dependencies should have been updated with the new version of guava")
.isPresent();
assertThat(
compileClasspath.getResolved().stream()
.filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion()))
.findAny())
.as("GradleProject requested dependencies should have been updated with the new version of guava")
.isPresent();
});
}
spec -> spec.afterRecipe(after -> {
Optional<GradleProject> maybeGp = after.getMarkers().findFirst(GradleProject.class);
assertThat(maybeGp).isPresent();
GradleProject gp = maybeGp.get();
GradleDependencyConfiguration compileClasspath = gp.getConfiguration("compileClasspath");
assertThat(compileClasspath).isNotNull();
assertThat(
compileClasspath.getRequested().stream()
.filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion()))
.findAny())
.as("GradleProject requested dependencies should have been updated with the new version of guava")
.isPresent();
assertThat(
compileClasspath.getResolved().stream()
.filter(dep -> "com.google.guava".equals(dep.getGroupId()) && "guava".equals(dep.getArtifactId()) && "30.1.1-jre".equals(dep.getVersion()))
.findAny())
.as("GradleProject requested dependencies should have been updated with the new version of guava")
.isPresent();
})
)
);
}
Expand All @@ -106,7 +110,9 @@ void updateVersionInVariable() {
}
dependencies {
implementation "com.google.guava:guava:$guavaVersion"
implementation ("com.google.guava:guava:$guavaVersion") {
force = true
}
implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion"
}
""",
Expand All @@ -122,7 +128,9 @@ void updateVersionInVariable() {
}
dependencies {
implementation "com.google.guava:guava:$guavaVersion"
implementation ("com.google.guava:guava:$guavaVersion") {
force = true
}
implementation "com.fasterxml.jackson.core:jackson-databind:$otherVersion"
}
"""
Expand Down Expand Up @@ -180,7 +188,9 @@ void mapNotationLiteral() {
}
dependencies {
implementation group: "com.google.guava", name: "guava", version: '29.0-jre'
implementation (group: "com.google.guava", name: "guava", version: '29.0-jre') {
force = true
}
}
""",
"""
Expand All @@ -193,7 +203,9 @@ void mapNotationLiteral() {
}
dependencies {
implementation group: "com.google.guava", name: "guava", version: '30.1.1-jre'
implementation (group: "com.google.guava", name: "guava", version: '30.1.1-jre') {
force = true
}
}
"""
)
Expand Down

0 comments on commit 4f73bd9

Please sign in to comment.