Skip to content

Commit

Permalink
Only add a Gradle wrapper if there is a Gradle project (#4379)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanman190 authored Aug 3, 2024
1 parent 43ac3be commit af4f5e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.openrewrite.*;
import org.openrewrite.gradle.search.FindGradleProject;
import org.openrewrite.gradle.util.GradleWrapper;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.StringUtils;
Expand Down Expand Up @@ -150,6 +151,7 @@ private GradleWrapper getGradleWrapper(ExecutionContext ctx) {
}

public static class GradleWrapperState {
boolean gradleProject = false;
boolean needsWrapperUpdate = false;
BuildTool updatedMarker;
boolean addGradleWrapperProperties = true;
Expand Down Expand Up @@ -225,6 +227,10 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
return false;
}

if (new FindGradleProject(FindGradleProject.SearchCriteria.Marker).getVisitor().visitNonNull(sourceFile, ctx) != sourceFile) {
acc.gradleProject = true;
}

if ((sourceFile instanceof Quark || sourceFile instanceof Remote) &&
equalIgnoringSeparators(sourceFile.getSourcePath(), WRAPPER_JAR_LOCATION)) {
acc.addGradleWrapperJar = false;
Expand Down Expand Up @@ -253,6 +259,10 @@ public Collection<SourceFile> generate(GradleWrapperState acc, ExecutionContext
return Collections.emptyList();
}

if (!acc.gradleProject) {
return Collections.emptyList();
}

if (!(acc.addGradleWrapperJar || acc.addGradleWrapperProperties || acc.addGradleBatchScript || acc.addGradleShellScript)) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_BATCH_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_PROPERTIES_LOCATION;
Expand All @@ -67,7 +69,8 @@ class UpdateGradleWrapperTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new UpdateGradleWrapper("7.4.2", null, null, null));
spec.recipe(new UpdateGradleWrapper("7.4.2", null, null, null))
.beforeRecipe(withToolingApi());
}

@Test
Expand Down Expand Up @@ -95,7 +98,14 @@ void addGradleWrapper() {
assertThat(gradleWrapperJar.getSourcePath()).isEqualTo(WRAPPER_JAR_LOCATION);
assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-7.4.2-bin.zip"));
assertThat(isValidWrapperJar(gradleWrapperJar)).as("Wrapper jar is not valid").isTrue();
})
}),
buildGradle(
"""
plugins {
id "java"
}
"""
)
);
}

Expand Down Expand Up @@ -218,7 +228,10 @@ void updateChecksumAlreadySet() {
zipStorePath=wrapper/dists
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
)
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

Expand Down Expand Up @@ -325,7 +338,10 @@ void olderThan6_6() {
distributionSha256Sum=1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
)
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

Expand Down Expand Up @@ -383,7 +399,10 @@ void allowUpdatingDistributionTypeWhenSameVersion() {
distributionSha256Sum=1f3067073041bc44554d0efe5d402a33bc3d3c93cc39ab684f308586d732a80d
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
)
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

Expand Down

0 comments on commit af4f5e6

Please sign in to comment.