Skip to content

Commit

Permalink
Drop duplicate ChangeDependencyGroupIdAndArtifactId (#91)
Browse files Browse the repository at this point in the history
Replacement ChangeDependency achieves all the same, with one additional argument.
  • Loading branch information
timtebeek authored May 6, 2024
1 parent 0892471 commit 008f57c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,68 @@
*/
package org.openrewrite.java.dependencies;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;

@Getter
@RequiredArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class ChangeDependency extends Recipe {
// Gradle and Maven shared parameters
@Option(displayName = "Old group ID",
description = "The old group ID to replace. The group ID is the first part of a dependency coordinate 'com.google.guava:guava:VERSION'. Supports glob expressions.",
example = "org.openrewrite.recipe")
private final String oldGroupId;
String oldGroupId;

@Option(displayName = "Old artifact ID",
description = "The old artifact ID to replace. The artifact ID is the second part of a dependency coordinate 'com.google.guava:guava:VERSION'. Supports glob expressions.",
example = "rewrite-testing-frameworks")
private final String oldArtifactId;
String oldArtifactId;

@Option(displayName = "New group ID",
description = "The new group ID to use. Defaults to the existing group ID.",
example = "corp.internal.openrewrite.recipe",
required = false)
@Nullable
private final String newGroupId;
String newGroupId;

@Option(displayName = "New artifact ID",
description = "The new artifact ID to use. Defaults to the existing artifact ID.",
example = "rewrite-testing-frameworks",
required = false)
@Nullable
private final String newArtifactId;
String newArtifactId;

@Option(displayName = "New version",
description = "An exact version number or node-style semver selector used to select the version number.",
example = "29.X",
required = false)
@Nullable
private final String newVersion;
String newVersion;

@Option(displayName = "Version pattern",
description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," +
"Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre",
example = "-jre",
required = false)
@Nullable
private final String versionPattern;
String versionPattern;

@Option(displayName = "Override managed version",
description = "If the new dependency has a managed version, this flag can be used to explicitly set the version on the dependency. The default for this flag is `false`.",
required = false)
@Nullable
private final Boolean overrideManagedVersion;
Boolean overrideManagedVersion;

@Option(displayName = "Update dependency management",
description = "Also update the dependency management section. The default for this flag is `true`.",
required = false)
@Nullable
Boolean changeManagedDependency;

@Override
public String getDisplayName() {
Expand All @@ -84,15 +91,25 @@ public String getDescription() {
@Override
public Validated<Object> validate(ExecutionContext ctx) {
return super.validate(ctx)
.and(getChangeDependencyRecipeGradle().validate())
.and(getChangeDependencyRecipeMaven().validate());
.and(((Recipe) new org.openrewrite.gradle.ChangeDependency(
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion)).validate())
.and(((Recipe) new org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId(
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, changeManagedDependency)).validate());
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
private final TreeVisitor<?, ExecutionContext> gradleVisitor = getChangeDependencyRecipeGradle().getVisitor();
private final TreeVisitor<?, ExecutionContext> mavenVisitor = getChangeDependencyRecipeMaven().getVisitor();
TreeVisitor<?, ExecutionContext> mavenVisitor = new org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId(
oldGroupId, oldArtifactId,
newGroupId, newArtifactId,
newVersion, versionPattern,
overrideManagedVersion, changeManagedDependency).getVisitor();
TreeVisitor<?, ExecutionContext> gradleVisitor = new org.openrewrite.gradle.ChangeDependency(
oldGroupId, oldArtifactId,
newGroupId, newArtifactId,
newVersion, versionPattern,
overrideManagedVersion).getVisitor();

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
Expand All @@ -109,14 +126,4 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}
};
}

private Recipe getChangeDependencyRecipeGradle() {
return new org.openrewrite.gradle.ChangeDependency(
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion);
}

private Recipe getChangeDependencyRecipeMaven() {
return new org.openrewrite.maven.ChangeDependencyGroupIdAndArtifactId(
oldGroupId, oldArtifactId, newGroupId, newArtifactId, newVersion, versionPattern, overrideManagedVersion, null);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private <T extends Tree> T maybeUpdate(T tree, String groupId, @Nullable String
String newArtifactId = Optional.ofNullable(relocation.getTo().getArtifactId()).orElse(artifactId);
doAfterVisit(new ChangeDependency(
groupId, artifactId, newGroupId, newArtifactId,
"latest.release", null, null).getVisitor());
"latest.release", null, null, null).getVisitor());
} else {
return getSearchResultFound(tree, relocation);
}
Expand Down Expand Up @@ -266,7 +266,7 @@ private <T extends Tree> T maybeUpdate(T tree, String groupId, @Nullable String
String newArtifactId = Optional.ofNullable(relocation.getTo().getArtifactId()).orElse(artifactId);
doAfterVisit(new ChangeDependency(
groupId, artifactId, newGroupId, newArtifactId,
"latest.release", null, null).getVisitor());
"latest.release", null, null, null).getVisitor());
} else {
return getSearchResultFound(tree, relocation);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 008f57c

Please sign in to comment.