Skip to content

Commit

Permalink
Add implementation for more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shanman190 committed Sep 16, 2024
1 parent a0300be commit 9381432
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.groovy.tree.G;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.maven.MavenDownloadingException;
Expand Down Expand Up @@ -112,7 +113,6 @@ public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
new IsBuildGradle<>(),
new GroovyIsoVisitor<ExecutionContext>() {
final MethodMatcher platformMatcher = new MethodMatcher("org.gradle.api.artifacts.dsl.DependencyHandler platform(..)");
// TODO: how to `enforcedPlatform`s effect dependency versions
final MethodMatcher enforcedPlatformMatcher = new MethodMatcher("org.gradle.api.artifacts.dsl.DependencyHandler enforcedPlatform(..)");

GradleProject gradleProject;
Expand All @@ -130,7 +130,7 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (!platformMatcher.matches(m)) {
if (!platformMatcher.matches(m) && !enforcedPlatformMatcher.matches(m)) {
return m;
}

Expand All @@ -150,7 +150,51 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return m;
}
} else if (m.getArguments().get(0) instanceof G.MapEntry) {
// TODO: Implement me
String groupId = null;
String artifactId = null;
String version = null;

for (Expression arg : m.getArguments()) {
if (!(arg instanceof G.MapEntry)) {
continue;
}

G.MapEntry entry = (G.MapEntry) arg;
if (!(entry.getKey() instanceof J.Literal) || !(entry.getValue() instanceof J.Literal)) {
continue;
}

J.Literal key = (J.Literal) entry.getKey();
J.Literal value = (J.Literal) entry.getValue();
if (key.getType() != JavaType.Primitive.String || value.getType() != JavaType.Primitive.String) {
continue;
}

switch ((String) key.getValue()) {
case "group":
groupId = (String) value.getValue();
break;
case "name":
artifactId = (String) value.getValue();
break;
case "version":
version = (String) value.getValue();
break;
}
}

if (groupId == null || artifactId == null || version == null) {
return m;
}

MavenPomDownloader mpd = new MavenPomDownloader(ctx);
try {
ResolvedPom platformPom = mpd.download(new GroupArtifactVersion(groupId, artifactId, version), null, null, gradleProject.getMavenRepositories())
.resolve(Collections.emptyList(), mpd, ctx);
acc.platforms.add(platformPom);
} catch (MavenDownloadingException e) {
return m;
}
}
return m;
}
Expand Down Expand Up @@ -186,12 +230,27 @@ public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
}

return m.withArguments(ListUtils.mapFirst(m.getArguments(), arg -> ChangeStringLiteral.withStringValue(l, dep.toStringNotation())));
}/* else if (m.getArguments().get(0) instanceof G.MapEntry) {
} else if (m.getArguments().get(0) instanceof G.MapLiteral) {
return m.withArguments(ListUtils.mapFirst(m.getArguments(), arg -> {
G.MapLiteral mapLiteral = (G.MapLiteral) arg;
return mapLiteral.withElements(ListUtils.map(mapLiteral.getElements(), entry -> {
if (entry.getKey() instanceof J.Literal &&
"version".equals(((J.Literal) entry.getKey()).getValue())) {
return null;
}
return entry;
}));
}));
} else if (m.getArguments().get(0) instanceof G.MapEntry) {
return m.withArguments(ListUtils.map(m.getArguments(), arg -> {
G.MapEntry entry = (G.MapEntry) arg;
if (entry.getKey() instanceof J.Literal && ((J.Literal) entry.getKey()).getValue().equals("version"))
}))
}*/
if (entry.getKey() instanceof J.Literal &&
"version".equals(((J.Literal) entry.getKey()).getValue())) {
return null;
}
return entry;
}));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void defaults(RecipeSpec spec) {

@DocumentExample
@Test
void platform() {
void literal() {
rewriteRun(
buildGradle(
"""
Expand All @@ -47,7 +47,6 @@ void platform() {
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3:3.14.0")
implementation(group: "org.apache.commons", name: "commons-lang3", version: "3.14.0")
}
""",
"""
Expand All @@ -62,10 +61,191 @@ void platform() {
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3")
}
"""
)
);
}

@Test
void mapEntry() {
rewriteRun(
buildGradle(
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation(group: "org.apache.commons", name: "commons-lang3", version: "3.14.0")
}
""",
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation(group: "org.apache.commons", name: "commons-lang3")
}
"""
)
);
}

@Test
void mapLiteral() {
rewriteRun(
buildGradle(
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation([group: "org.apache.commons", name: "commons-lang3", version: "3.14.0"])
}
""",
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation([group: "org.apache.commons", name: "commons-lang3"])
}
"""
)
);
}

@Test
void enforcedPlatform() {
rewriteRun(
buildGradle(
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3:3.14.0")
}
""",
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3")
}
"""
)
);
}

@Test
void platformUsingMapEntry() {
rewriteRun(
buildGradle(
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(enforcedPlatform(group: "org.springframework.boot", name: "spring-boot-dependencies", version: "3.3.3"))
implementation("org.apache.commons:commons-lang3:3.14.0")
}
""",
"""
plugins {
id "java"
}
repositories {
mavenCentral()
}
dependencies {
implementation(enforcedPlatform(group: "org.springframework.boot", name: "spring-boot-dependencies", version: "3.3.3"))
implementation("org.apache.commons:commons-lang3")
}
"""
)
);
}

@Test
void freestandingScript() {
rewriteRun(
buildGradle(
"""
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3:3.14.0")
}
""",
"""
repositories {
mavenCentral()
}
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.3.3"))
implementation("org.apache.commons:commons-lang3")
}
""",
spec -> spec.path("dependencies.gradle")
),
buildGradle(
"""
plugins {
id("java")
}
apply from: 'dependencies.gradle'
"""
)
);
}
}

0 comments on commit 9381432

Please sign in to comment.