Skip to content

Commit

Permalink
Review comments + added scenario's in testcase of migration/addition/…
Browse files Browse the repository at this point in the history
…removal...
  • Loading branch information
Jente Sondervorst committed Sep 19, 2024
1 parent 4a1ccd1 commit d94dc0c
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.ExecutionContext;
import org.openrewrite.FileAttributes;
import org.openrewrite.Option;
import org.openrewrite.PathUtils;
import org.openrewrite.Preconditions;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.SourceFile;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.Validated;
import org.openrewrite.gradle.search.FindGradleProject;
import org.openrewrite.gradle.util.DistributionInfos;
import org.openrewrite.gradle.util.GradleWrapper;
Expand All @@ -43,12 +52,25 @@

import java.net.URI;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Objects.requireNonNull;
import static org.openrewrite.PathUtils.equalIgnoringSeparators;
import static org.openrewrite.gradle.util.GradleWrapper.*;
import static org.openrewrite.internal.StringUtils.formatUriForPropertiesFile;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_BATCH_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_BATCH_LOCATION_RELATIVE_PATH;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_JAR_LOCATION_RELATIVE_PATH;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_PROPERTIES_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_PROPERTIES_LOCATION_RELATIVE_PATH;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_SCRIPT_LOCATION;
import static org.openrewrite.gradle.util.GradleWrapper.WRAPPER_SCRIPT_LOCATION_RELATIVE_PATH;
import static org.openrewrite.internal.StringUtils.isBlank;

@RequiredArgsConstructor
Expand Down Expand Up @@ -120,7 +142,7 @@ public String getDescription() {
public Validated<Object> validate() {
Validated<Object> validated = super.validate();
if (wrapperUri != null && (version != null || distribution != null)) {
return Validated.invalid("wrapperUri", wrapperUri, "WrapperUri cannot be used with other parameters");
return Validated.invalid("wrapperUri", wrapperUri, "WrapperUri cannot be used with version and/or distribution parameter");
}
if (wrapperUri == null && distributionChecksum != null) {
return Validated.invalid("distributionChecksum", distributionChecksum, "DistributionChecksum can only be used with wrapperUri");
Expand Down Expand Up @@ -220,16 +242,26 @@ public Properties visitEntry(Properties.Entry entry, ExecutionContext ctx) {
return entry;
}

// Typical example: https://services.gradle.org/distributions/gradle-7.4-all.zip
// Typical example: https://services.gradle.org/distributions/gradle-7.4-all.zip or https://company.com/repo/gradle-8.2-bin.zip
String currentDistributionUrl = entry.getValue().getText();

GradleWrapper gradleWrpr = getGradleWrapper(ctx);
if (StringUtils.isBlank(gradleWrpr.getDistributionUrl()) &&
!StringUtils.isBlank(version) && Semver.validate(version, null).getValue() instanceof ExactVersion) {
if (StringUtils.isBlank(gradleWrpr.getDistributionUrl()) && !StringUtils.isBlank(version) && Semver.validate(version, null)
.getValue() instanceof ExactVersion) {
String newDownloadUrl = currentDistributionUrl.replace("\\", "")
.replaceAll("(.*gradle-)(\\d+\\.\\d+(?:\\.\\d+)?)(.*-(?:bin|all).zip)", "$1" + gradleWrapper.getVersion() + "$3");
.replaceAll("(.*gradle-)(\\d+\\.\\d+(?:\\.\\d+)?)(.*-(?:bin|all).zip)",
"$1" + gradleWrapper.getVersion() + "$3");
gradleWrapper = new GradleWrapper(version, new DistributionInfos(newDownloadUrl, null, null));
}
String wrapperHost = currentDistributionUrl.substring(0, currentDistributionUrl.lastIndexOf("/")) + "/gradle-";
if (StringUtils.isBlank(wrapperUri) && !StringUtils.isBlank(gradleWrpr.getDistributionUrl()) && !gradleWrpr.getPropertiesFormattedUrl().startsWith(wrapperHost)) {

String newDownloadUrl = gradleWrpr.getDistributionUrl()
.replace("\\", "")
.replaceAll("(.*gradle-)(\\d+\\.\\d+(?:\\.\\d+)?)(.*-(?:bin|all).zip)",
wrapperHost + gradleWrapper.getVersion() + "$3");
gradleWrapper = new GradleWrapper(gradleWrpr.getVersion(), new DistributionInfos(newDownloadUrl, null, null));
}

if (!gradleWrapper.getPropertiesFormattedUrl().equals(currentDistributionUrl)) {
acc.needsWrapperUpdate = true;
Expand Down Expand Up @@ -483,6 +515,14 @@ public Properties visitFile(Properties.File file, ExecutionContext ctx) {
Properties.Entry entry = new Properties.Entry(Tree.randomId(), "\n", Markers.EMPTY, DISTRIBUTION_SHA_256_SUM_KEY, "", Properties.Entry.Delimiter.EQUALS, propertyValue);
List<Properties.Content> contentList = ListUtils.concat(((Properties.File) p).getContent(), entry);
p = ((Properties.File) p).withContent(contentList);
} else if (!checksumKey.isEmpty() && gradleWrapper.getDistributionChecksum() == null) {
List<Properties.Content> contentList = ListUtils.map(((Properties.File) p).getContent(), c -> {
if (c instanceof Properties.Entry && DISTRIBUTION_SHA_256_SUM_KEY.equals(((Properties.Entry) c).getKey())) {
return null;
}
return c;
});
p = ((Properties.File) p).withContent(contentList);
}
return p;
}
Expand All @@ -491,22 +531,7 @@ public Properties visitFile(Properties.File file, ExecutionContext ctx) {
public Properties visitEntry(Properties.Entry entry, ExecutionContext ctx) {
if ("distributionUrl".equals(entry.getKey())) {
Properties.Value value = entry.getValue();
String currentUrl = value.getText();
// Prefer wrapperUri specified directly in the recipe over other options
// If that isn't set, prefer the existing artifact repository URL over changing to services.gradle.org
if (!StringUtils.isBlank(wrapperUri)) {
String effectiveWrapperUri = formatUriForPropertiesFile(wrapperUri
.replace("${version}", gradleWrapper.getVersion())
.replace("${distribution}", distribution == null ? "bin" : distribution));
return entry.withValue(value.withText(effectiveWrapperUri));
} else if (currentUrl.startsWith("https\\://services.gradle.org/distributions/")) {
return entry.withValue(value.withText(gradleWrapper.getPropertiesFormattedUrl()));
} else {
String gradleServicesDistributionUrl = gradleWrapper.getDistributionUrl();
String newDistributionFile = gradleServicesDistributionUrl.substring(gradleServicesDistributionUrl.lastIndexOf('/') + 1);
String repositoryUrlPrefix = currentUrl.substring(0, currentUrl.lastIndexOf('/'));
return entry.withValue(value.withText(repositoryUrlPrefix + "/" + newDistributionFile));
}
return entry.withValue(value.withText(gradleWrapper.getPropertiesFormattedUrl()));
}
if (DISTRIBUTION_SHA_256_SUM_KEY.equals(entry.getKey()) && gradleWrapper.getDistributionChecksum() != null) {
return entry.withValue(entry.getValue().withText(gradleWrapper.getDistributionChecksum().getHexValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.openrewrite.ExecutionContext;
import org.openrewrite.HttpSenderExecutionContextView;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.PathUtils;
import org.openrewrite.RecipeRun;
import org.openrewrite.Result;
Expand Down Expand Up @@ -77,6 +76,7 @@ class UpdateGradleWrapperTest implements RewriteTest {
private static final String GRADLEW_TEXT = StringUtils.readFully(UpdateGradleWrapperTest.class.getResourceAsStream("gradlew-7.4.2"));
private static final String GRADLEW_BAT_TEXT = StringUtils.readFully(UpdateGradleWrapperTest.class.getResourceAsStream("gradlew-7.4.2.bat"));


private final SourceSpecs gradlew = text("", spec -> spec.path(WRAPPER_SCRIPT_LOCATION).after(notEmpty));
private final SourceSpecs gradlewBat = text("", spec -> spec.path(WRAPPER_BATCH_LOCATION).after(notEmpty));
private final SourceSpecs gradleWrapperJarQuark = other("", spec -> spec.path(WRAPPER_JAR_LOCATION));
Expand Down Expand Up @@ -547,18 +547,16 @@ void skipWorkIfUpdatedEarlier() {
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/2651")
@Test
void preferExistingDistributionSource() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.0.x", null, null, null, null))
.expectedCyclesThatMakeChanges(2)
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4"))),
properties(
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-7.4-bin.zip
distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
Expand All @@ -573,7 +571,7 @@ void preferExistingDistributionSource() {
return """
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.0.2-bin.zip
distributionUrl=https\\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=%s
Expand Down Expand Up @@ -710,7 +708,7 @@ void addWrapperWithCustomDistributionUriAndDistributionChecksum() {
}

@Test
void customDistributionUri() {
void migrateToCustomDistributionUri() {
HttpSender customDistributionHost = request -> {
if (request.getUrl().toString().contains("company.com")) {
return new HttpSender.Response(200, UpdateGradleWrapperTest.class.getClassLoader().getResourceAsStream("gradle-8.10-bin.zip"), () -> {});
Expand Down Expand Up @@ -747,6 +745,83 @@ void customDistributionUri() {
);
}

@Test
void removeShaDuringMigrationToCustomDistributionUri() {
HttpSender customDistributionHost = request -> {
if (request.getUrl().toString().contains("company.com")) {
return new HttpSender.Response(200, UpdateGradleWrapperTest.class.getClassLoader().getResourceAsStream("gradle-8.10-bin.zip"), () -> {});
}
return new HttpUrlConnectionSender().send(request);
};
HttpSenderExecutionContextView ctx = HttpSenderExecutionContextView.view(new InMemoryExecutionContext())
.setHttpSender(customDistributionHost)
.setLargeFileHttpSender(customDistributionHost);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper(null, null, null, "https://company.com/repo/gradle-8.10-bin.zip", null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.executionContext(ctx),
properties(
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=29e49b10984e585d8118b7d0bc452f944e386458df27371b49b4ac1dec4b7fda
""",
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

@Test
void updateWithCustomDistributionUri() {
HttpSender customDistributionHost = request -> {
if (request.getUrl().toString().contains("company.com")) {
return new HttpSender.Response(200, UpdateGradleWrapperTest.class.getClassLoader().getResourceAsStream("gradle-8.10-bin.zip"), () -> {});
}
return new HttpUrlConnectionSender().send(request);
};
HttpSenderExecutionContextView ctx = HttpSenderExecutionContextView.view(new InMemoryExecutionContext())
.setHttpSender(customDistributionHost)
.setLargeFileHttpSender(customDistributionHost);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.10", null, null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.executionContext(ctx),
properties(
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

@Test
void servicesGradleOrgUnavailable() {
HttpSender unhelpfulSender = request -> {
Expand Down Expand Up @@ -779,8 +854,49 @@ void servicesGradleOrgUnavailable() {
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
),
text(GRADLEW_TEXT, spec -> spec.path(WRAPPER_SCRIPT_LOCATION)),
text(GRADLEW_BAT_TEXT, spec -> spec.path(WRAPPER_BATCH_LOCATION)),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}

@Test
void servicesGradleOrgUnavailableForCustomDistributionUri() {
HttpSender unhelpfulSender = request -> {
if (request.getUrl().toString().contains("services.gradle.org")) {
throw new RuntimeException("I'm sorry Dave, I'm afraid I can't do that.");
}
if (request.getUrl().toString().contains("company.com")) {
return new HttpSender.Response(200, UpdateGradleWrapperTest.class.getClassLoader().getResourceAsStream("gradle-8.10-bin.zip"), () -> {});
}
return new HttpUrlConnectionSender().send(request);
};
HttpSenderExecutionContextView ctx = HttpSenderExecutionContextView.view(new InMemoryExecutionContext())
.setHttpSender(unhelpfulSender)
.setLargeFileHttpSender(unhelpfulSender);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.10", null, null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.executionContext(ctx),
properties(
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
"""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\\://company.com/repo/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""",
spec -> spec.path("gradle/wrapper/gradle-wrapper.properties")
),
gradlew,
gradlewBat,
gradleWrapperJarQuark
);
}
Expand Down

0 comments on commit d94dc0c

Please sign in to comment.