Skip to content

Commit

Permalink
Skip updating Gradle when already updated (#3794)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanman190 authored Dec 8, 2023
1 parent 30c0df1 commit 3770f85
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {

SourceFile sourceFile = (SourceFile) tree;
if (acc.updatedMarker != null) {
sourceFile = sourceFile.getMarkers().findFirst(BuildTool.class)
.map(buildTool -> (SourceFile) tree.withMarkers(tree.getMarkers().computeByType(buildTool, (b, a) -> acc.updatedMarker)))
.orElse(sourceFile);
Optional<BuildTool> maybeCurrentMarker = sourceFile.getMarkers().findFirst(BuildTool.class);
if (maybeCurrentMarker.isPresent()) {
BuildTool currentMarker = maybeCurrentMarker.get();
VersionComparator versionComparator = requireNonNull(Semver.validate(isBlank(version) ? "latest.release" : version, null).getValue());
int compare = versionComparator.compare(null, currentMarker.getVersion(), acc.updatedMarker.getVersion());
if (compare < 0) {
sourceFile = sourceFile.withMarkers(sourceFile.getMarkers().computeByType(currentMarker, (b, a) -> acc.updatedMarker));
} else {
return sourceFile;
}
}
}

if (sourceFile instanceof PlainText && PathUtils.matchesGlob(sourceFile.getSourcePath(), "**/" + WRAPPER_SCRIPT_LOCATION_RELATIVE_PATH)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,70 @@ void defaultsToLatestRelease() {
);
}

@Test
void skipWorkIfUpdatedEarlier() {
rewriteRun(
spec -> spec.recipeFromYaml(
"""
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.gradle.MultipleWrapperUpdates
displayName: Multiple wrapper updates
description: Multiple wrapper updates.
recipeList:
- org.openrewrite.gradle.UpdateGradleWrapper:
version: 7.6.3
addIfMissing: false
- org.openrewrite.gradle.UpdateGradleWrapper:
version: 6.9.4
addIfMissing: false
""",
"org.openrewrite.gradle.MultipleWrapperUpdates")
.cycles(1)
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "5.6.4")))
.afterRecipe(run -> {
var gradleSh = result(run, PlainText.class, "gradlew");
assertThat(gradleSh.getSourcePath()).isEqualTo(WRAPPER_SCRIPT_LOCATION);
assertThat(gradleSh.getText()).isNotBlank();
assertThat(gradleSh.getFileAttributes()).isNotNull();
assertThat(gradleSh.getFileAttributes().isReadable()).isTrue();
assertThat(gradleSh.getFileAttributes().isExecutable()).isTrue();

var gradleBat = result(run, PlainText.class, "gradlew.bat");
assertThat(gradleBat.getSourcePath()).isEqualTo(WRAPPER_BATCH_LOCATION);
assertThat(gradleBat.getText()).isNotBlank();

var gradleWrapperJar = result(run, Remote.class, "gradle-wrapper.jar");
assertThat(gradleWrapperJar.getSourcePath()).isEqualTo(WRAPPER_JAR_LOCATION);
//noinspection OptionalGetWithoutIsPresent
BuildTool buildTool = gradleWrapperJar.getMarkers().findFirst(BuildTool.class).get();
assertThat(buildTool.getVersion()).isEqualTo("7.6.3");
assertThat(gradleWrapperJar.getUri()).isEqualTo(URI.create("https://services.gradle.org/distributions/gradle-" + buildTool.getVersion() + "-bin.zip"));
assertThat(isValidWrapperJar(gradleWrapperJar)).as("Wrapper jar is not valid").isTrue();
}),
properties(
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-7.6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=740c2e472ee4326c33bf75a5c9f5cd1e69ecf3f9b580f6e236c86d1f3d98cfac
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

private <S extends SourceFile> S result(RecipeRun run, Class<S> clazz, String endsWith) {
return run.getChangeset().getAllResults().stream()
.map(Result::getAfter)
Expand Down

0 comments on commit 3770f85

Please sign in to comment.