Skip to content

Commit

Permalink
Added gradle recipe to remove enableFeaturePreview method (#4191)
Browse files Browse the repository at this point in the history
* Added gradle recipe to remove enableFeaturePreview method

* Update rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveEnableFeaturePreview.java

Co-authored-by: Shannon Pamperl <[email protected]>

* Update rewrite-gradle/src/main/java/org/openrewrite/gradle/RemoveEnableFeaturePreview.java

Co-authored-by: Shannon Pamperl <[email protected]>

* Use methodMatcher and code review cleanup

* Added license

* Added license

* Failing the build so it wont get merged yet, not passing my manual local testing

* Removed failing test

* Removed methodMatcher because it wasnt workign on one of my gradle projects i tested against

* Minor polish

* Update rewrite-gradle/src/test/java/org/openrewrite/gradle/RemoveEnableFeaturePreviewTest.java

Co-authored-by: Shannon Pamperl <[email protected]>

---------

Co-authored-by: Shannon Pamperl <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored May 19, 2024
1 parent 46d21d6 commit 2ae80bc
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.groovy.GroovyIsoVisitor;
import org.openrewrite.java.tree.J;

@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")
String previewFeatureName;

@Override
public String getDisplayName() {
return "Remove an enabled Gradle preview feature";
}

@Override
public String getDescription() {
return "Remove an enabled Gradle preview feature from `settings.gradle`.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new IsSettingsGradle<>(), new GroovyIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if ("enableFeaturePreview".equals(method.getSimpleName()) &&
method.getArguments().size() == 1 &&
J.Literal.isLiteralValue(method.getArguments().get(0), previewFeatureName)) {
return null;
}
return method;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.gradle.Assertions.settingsGradle;

class RemoveEnableFeaturePreviewTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RemoveEnableFeaturePreview("ONE_LOCKFILE_PER_PROJECT"));
}

@Test
void singleQuotes() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'merge-service'
enableFeaturePreview('ONE_LOCKFILE_PER_PROJECT')
""",
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'merge-service'
"""
)
);
}

@Test
void doubleQuotes() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'merge-service'
enableFeaturePreview("ONE_LOCKFILE_PER_PROJECT")
include 'service'
""",
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'merge-service'
include 'service'
"""
)
);
}

@Nested
class NoChange {
@Test
void differentFeature() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
enableFeaturePreview("DIFFERENT_FEATURE")
rootProject.name = 'merge-service'
include 'service'
"""
)
);
}

@Test
void nullArgument() {
//language=gradle
rewriteRun(
settingsGradle(
"""
pluginManagement {
repositories {
gradlePluginPortal()
}
}
enableFeaturePreview(null)
rootProject.name = 'merge-service'
include 'service'
"""
)
);
}
}
}

0 comments on commit 2ae80bc

Please sign in to comment.