Skip to content

Commit

Permalink
Enrich GradleDependency.Matcher to match on cases where a ResolvedDep…
Browse files Browse the repository at this point in the history
…endency is unavailable
  • Loading branch information
shanman190 committed Dec 4, 2024
1 parent b373d9f commit 9936a7a
Show file tree
Hide file tree
Showing 11 changed files with 688 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,16 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionCon
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);

GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher();
GradleDependency.Matcher gradleDependencyMatcher = new GradleDependency.Matcher()
.groupId(oldGroupId)
.artifactId(oldArtifactId);

if (!gradleDependencyMatcher.get(getCursor()).isPresent()) {
return m;
}

List<Expression> depArgs = m.getArguments();
if (depArgs.get(0) instanceof J.Literal || depArgs.get(0) instanceof G.GString || depArgs.get(0) instanceof G.MapEntry) {
if (depArgs.get(0) instanceof J.Literal || depArgs.get(0) instanceof G.GString || depArgs.get(0) instanceof G.MapEntry || depArgs.get(0) instanceof G.MapLiteral) {
m = updateDependency(m, ctx);
} else if (depArgs.get(0) instanceof J.MethodInvocation &&
(((J.MethodInvocation) depArgs.get(0)).getSimpleName().equals("platform") ||
Expand All @@ -204,7 +206,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
String gav = (String) ((J.Literal) depArgs.get(0)).getValue();
if (gav != null) {
Dependency original = DependencyStringNotationConverter.parse(gav);
if (original != null && depMatcher.matches(original.getGroupId(), original.getArtifactId())) {
if (original != null) {
Dependency updated = original;
if (!StringUtils.isBlank(newGroupId) && !updated.getGroupId().equals(newGroupId)) {
updated = updated.withGroupId(newGroupId);
Expand Down Expand Up @@ -238,7 +240,7 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte

J.Literal literal = (J.Literal) strings.get(0);
Dependency original = DependencyStringNotationConverter.parse((String) requireNonNull(literal.getValue()));
if (original != null && depMatcher.matches(original.getGroupId(), original.getArtifactId())) {
if (original != null) {
Dependency updated = original;
if (!StringUtils.isBlank(newGroupId) && !updated.getGroupId().equals(newGroupId)) {
updated = updated.withGroupId(newGroupId);
Expand Down Expand Up @@ -352,6 +354,92 @@ private J.MethodInvocation updateDependency(J.MethodInvocation m, ExecutionConte
return arg;
}));
}
} else if (m.getArguments().get(0) instanceof G.MapLiteral) {
G.MapLiteral map = (G.MapLiteral) depArgs.get(0);
G.MapEntry groupEntry = null;
G.MapEntry artifactEntry = null;
G.MapEntry versionEntry = null;
String groupId = null;
String artifactId = null;
String version = null;

for (G.MapEntry arg : map.getElements()) {
if (!(arg.getKey() instanceof J.Literal) || !(arg.getValue() instanceof J.Literal)) {
continue;
}
J.Literal key = (J.Literal) arg.getKey();
J.Literal value = (J.Literal) arg.getValue();
if (!(key.getValue() instanceof String) || !(value.getValue() instanceof String)) {
continue;
}
String keyValue = (String) key.getValue();
String valueValue = (String) value.getValue();
switch (keyValue) {
case "group":
groupEntry = arg;
groupId = valueValue;
break;
case "name":
artifactEntry = arg;
artifactId = valueValue;
break;
case "version":
versionEntry = arg;
version = valueValue;
break;
}
}
if (groupId == null || artifactId == null) {
return m;
}
if (!depMatcher.matches(groupId, artifactId)) {
return m;
}
String updatedGroupId = groupId;
if (!StringUtils.isBlank(newGroupId) && !updatedGroupId.equals(newGroupId)) {
updatedGroupId = newGroupId;
}
String updatedArtifactId = artifactId;
if (!StringUtils.isBlank(newArtifactId) && !updatedArtifactId.equals(newArtifactId)) {
updatedArtifactId = newArtifactId;
}
String updatedVersion = version;
if (!StringUtils.isBlank(newVersion) && (!StringUtils.isBlank(version) || Boolean.TRUE.equals(overrideManagedVersion))) {
String resolvedVersion;
try {
resolvedVersion = new DependencyVersionSelector(null, gradleProject, null)
.select(new GroupArtifact(updatedGroupId, updatedArtifactId), m.getSimpleName(), newVersion, versionPattern, ctx);
} catch (MavenDownloadingException e) {
return e.warn(m);
}
if (resolvedVersion != null && !resolvedVersion.equals(updatedVersion)) {
updatedVersion = resolvedVersion;
}
}

if (!updatedGroupId.equals(groupId) || !updatedArtifactId.equals(artifactId) || updatedVersion != null && !updatedVersion.equals(version)) {
G.MapEntry finalGroup = groupEntry;
String finalGroupIdValue = updatedGroupId;
G.MapEntry finalArtifact = artifactEntry;
String finalArtifactIdValue = updatedArtifactId;
G.MapEntry finalVersion = versionEntry;
String finalVersionValue = updatedVersion;
m = m.withArguments(ListUtils.mapFirst(m.getArguments(), arg -> {
G.MapLiteral mapLiteral = (G.MapLiteral) arg;
return mapLiteral.withElements(ListUtils.map(mapLiteral.getElements(), e -> {
if (e == finalGroup) {
return finalGroup.withValue(ChangeStringLiteral.withStringValue((J.Literal) finalGroup.getValue(), finalGroupIdValue));
}
if (e == finalArtifact) {
return finalArtifact.withValue(ChangeStringLiteral.withStringValue((J.Literal) finalArtifact.getValue(), finalArtifactIdValue));
}
if (e == finalVersion) {
return finalVersion.withValue(ChangeStringLiteral.withStringValue((J.Literal) finalVersion.getValue(), finalVersionValue));
}
return e;
}));
}));
}
}

return m;
Expand Down
Loading

0 comments on commit 9936a7a

Please sign in to comment.