-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gradle recipe to remove enableFeaturePreview method
- Loading branch information
1 parent
9f634a3
commit e1d2eca
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveEnableFeaturePreview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.openrewrite.gradle; | ||
|
||
import java.util.List; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Option; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.groovy.GroovyIsoVisitor; | ||
import org.openrewrite.java.tree.Expression; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.J.MethodInvocation; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class RemoveEnableFeaturePreview extends Recipe { | ||
|
||
private static final String ENABLE_FEATURE_PREVIEW_METHOD_NAME = "enableFeaturePreview"; | ||
|
||
@Option(displayName = "The feature preview name", | ||
description = "The name of the feature preview to remove.", | ||
example = "ONE_LOCKFILE_PER_PROJECT" | ||
) | ||
public String previewFeatureName; | ||
|
||
@Override | ||
public @NotNull String getDisplayName() { | ||
return "Remove a Gradle enableFeaturePreview method"; | ||
} | ||
|
||
@Override | ||
public @NotNull String getDescription() { | ||
return "Remove a Gradle enableFeaturePreview method from settings.gradle / settings.gradle.kts."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
new IsSettingsGradle<>(), | ||
new RemoveEnableFeaturePreviewVisitor()); | ||
} | ||
|
||
public class RemoveEnableFeaturePreviewVisitor extends GroovyIsoVisitor<ExecutionContext> { | ||
|
||
@Override | ||
public MethodInvocation visitMethodInvocation(MethodInvocation method, | ||
@NotNull ExecutionContext executionContext) { | ||
if (ENABLE_FEATURE_PREVIEW_METHOD_NAME.equals(method.getSimpleName())) { | ||
List<Expression> arguments = method.getArguments(); | ||
for (Expression argument : arguments) { | ||
if (argument instanceof J.Literal) { | ||
String candidatePreviewFeatureName = ((J.Literal) argument).getValue().toString(); | ||
// Remove leading and trailing single or double quotes. | ||
candidatePreviewFeatureName = candidatePreviewFeatureName.substring(0, | ||
candidatePreviewFeatureName.length()); | ||
if (candidatePreviewFeatureName.equals(previewFeatureName)) { | ||
return null; | ||
} | ||
} | ||
} | ||
} | ||
return method; | ||
} | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveEnableFeaturePreviewTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package org.openrewrite.gradle; | ||
|
||
import java.nio.file.Paths; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
public class RemoveEnableFeaturePreviewTest implements RewriteTest { | ||
|
||
@Test | ||
void testRemoveEnableFeaturePreviewMethodRecipe_singleQuotes() { | ||
//language=gradle | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")), | ||
Assertions.settingsGradle( | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
rootProject.name = 'merge-service' | ||
enableFeaturePreview('ONE_LOCKFILE_PER_PROJECT') | ||
""", | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
rootProject.name = 'merge-service' | ||
""", | ||
spec -> spec.path(Paths.get("settings.gradle")) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testRemoveEnableFeaturePreviewMethodRecipe_doubleQuotes() { | ||
//language=gradle | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")), | ||
Assertions.settingsGradle( | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
rootProject.name = 'merge-service' | ||
enableFeaturePreview("ONE_LOCKFILE_PER_PROJECT") | ||
include 'service' | ||
""", | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
rootProject.name = 'merge-service' | ||
include 'service' | ||
""", | ||
spec -> spec.path(Paths.get("settings.gradle")) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testRemoveEnableFeaturePreviewMethodRecipe_noChange() { | ||
//language=gradle | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")), | ||
Assertions.settingsGradle( | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
enableFeaturePreview("DIFFERENT_FEATURE") | ||
rootProject.name = 'merge-service' | ||
include 'service' | ||
""", | ||
spec -> spec.path(Paths.get("settings.gradle")) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testRemoveEnableFeaturePreviewMethodRecipe_noChangeNullArgument() { | ||
//language=gradle | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")), | ||
Assertions.settingsGradle( | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
enableFeaturePreview() | ||
rootProject.name = 'merge-service' | ||
include 'service' | ||
""", | ||
spec -> spec.path(Paths.get("settings.gradle")) | ||
) | ||
); | ||
} | ||
|
||
} |