-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added gradle recipe to remove enableFeaturePreview method #4191
Merged
timtebeek
merged 12 commits into
openrewrite:main
from
ryanwalker:add-gradle-recipe-remove-enable-feature-preview
May 19, 2024
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e1d2eca
Added gradle recipe to remove enableFeaturePreview method
ryanwalker 677a647
Update rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveEnab…
ryanwalker 39b96be
Update rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveEnab…
ryanwalker 68b37bb
Use methodMatcher and code review cleanup
ryanwalker 09f9885
Added license
ryanwalker a6a4261
Added license
ryanwalker 2bea390
Failing the build so it wont get merged yet, not passing my manual lo…
ryanwalker 2032bb4
Removed failing test
ryanwalker 6ea3d4a
Removed methodMatcher because it wasnt workign on one of my gradle pr…
ryanwalker 7279357
Minor polish
timtebeek 30c0af2
Merge branch 'main' into add-gradle-recipe-remove-enable-feature-preview
timtebeek d03428b
Update rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveEnab…
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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.Literal; | ||
import org.openrewrite.java.tree.J.MethodInvocation; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class RemoveEnableFeaturePreview extends Recipe { | ||
|
||
@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 an enabled Gradle preview feature"; | ||
} | ||
|
||
@Override | ||
public @NotNull String getDescription() { | ||
return "Remove an enabled Gradle preview feature from `settings.gradle`."; | ||
} | ||
|
||
@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 (!"enableFeaturePreview".equals(method.getSimpleName())) { | ||
return method; | ||
} | ||
|
||
List<Expression> arguments = method.getArguments(); | ||
if (arguments == null || arguments.size() != 1) { | ||
return method; | ||
} | ||
|
||
Expression argument = arguments.get(0); | ||
if (argument instanceof J.Literal) { | ||
String candidatePreviewFeatureName = (String) ((Literal) argument).getValue(); | ||
if (previewFeatureName.equals(candidatePreviewFeatureName)) { | ||
return null; | ||
} | ||
} | ||
|
||
return method; | ||
} | ||
} | ||
|
||
} |
160 changes: 160 additions & 0 deletions
160
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,160 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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_noChangeNoArgument() { | ||
//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")) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void testRemoveEnableFeaturePreviewMethodRecipe_noChangeNullArgument() { | ||
//language=gradle | ||
rewriteRun( | ||
spec -> spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")), | ||
Assertions.settingsGradle( | ||
""" | ||
pluginManagement { | ||
repositories { | ||
gradlePluginPortal() | ||
} | ||
} | ||
|
||
enableFeaturePreview(null) | ||
|
||
rootProject.name = 'merge-service' | ||
|
||
include 'service' | ||
""", | ||
spec -> spec.path(Paths.get("settings.gradle")) | ||
) | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using a method matcher, and it passed tests fine, but I manually ran this recipe against a few local gradle projects and it worked on 1 but on another it didn't match at all. The one it didn't work on, I debugged the LST and found that the method had a methodType of null for some reason. I dont' know if it's a bug in the parser or not, but I changed the logic here to just check the method name, check that there's exactly 1 argument, and check that the argument matches the
@Option
passed in.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It being flaky is a bit curious to me. My likely suspect though is that in the cases where it is not working, the type is missing from the Groovy AST -- coming out of the Groovy Compiler -- prior to it being mapped to the OpenRewrite Groovy LST.