Skip to content
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

Adding gradle wrapper through distribution is now possible #4491

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added checksum for custom distributionUri
Jente Sondervorst committed Sep 18, 2024
commit 0d63ee8c8ad0d220bc83097f72a93eeeb3acc1b0
Original file line number Diff line number Diff line change
@@ -108,12 +108,23 @@ public String getDescription() {
@Nullable
final String wrapperUri;

@Getter
@Option(example = "29e49b10984e585d8118b7d0bc452f944e386458df27371b49b4ac1dec4b7fda",
description = "The SHA-256 checksum of the Gradle distribution. " +
"If specified, the recipe will add the checksum along with the custom distribution URL.",
required = false)
@Nullable
final String distributionChecksum;

@Override
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");
}
if (wrapperUri == null && distributionChecksum != null) {
return Validated.invalid("distributionChecksum", distributionChecksum, "DistributionChecksum can only be used with wrapperUri");
}
if (version != null) {
validated = validated.and(Semver.validate(version, null));
}
@@ -279,12 +290,17 @@ public Collection<SourceFile> generate(GradleWrapperState acc, ExecutionContext
GradleWrapper gradleWrapper = getGradleWrapper(ctx);

if (acc.addGradleWrapperProperties) {
String checksum = gradleWrapper.getDistributionChecksum() == null ? null : gradleWrapper.getDistributionChecksum().getHexValue();
if (wrapperUri != null && distributionChecksum != null && checksum == null) {
checksum = distributionChecksum;
}

//noinspection UnusedProperty
Properties.File gradleWrapperProperties = new PropertiesParser().parse(
"distributionBase=GRADLE_USER_HOME\n" +
"distributionPath=wrapper/dists\n" +
"distributionUrl=" + gradleWrapper.getPropertiesFormattedUrl() + "\n" +
((gradleWrapper.getDistributionChecksum() == null) ? "" : "distributionSha256Sum=" + gradleWrapper.getDistributionChecksum().getHexValue() + "\n") +
(checksum == null ? "" : "distributionSha256Sum=" + checksum + "\n") +
"zipStoreBase=GRADLE_USER_HOME\n" +
"zipStorePath=wrapper/dists")
.findFirst()
Original file line number Diff line number Diff line change
@@ -66,10 +66,11 @@ class UpdateGradleWrapperTest implements RewriteTest {
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));
private final String wrapperJarChecksum = "29e49b10984e585d8118b7d0bc452f944e386458df27371b49b4ac1dec4b7fda";

@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, null))
.beforeRecipe(withToolingApi());
}

@@ -238,7 +239,7 @@ void updateChecksumAlreadySet() {
@Test
void dontAddMissingWrapper() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("7.x", null, Boolean.FALSE, null))
spec -> spec.recipe(new UpdateGradleWrapper("7.x", null, Boolean.FALSE, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.afterRecipe(run -> assertThat(run.getChangeset().getAllResults()).isEmpty())
);
@@ -248,7 +249,7 @@ void dontAddMissingWrapper() {
void updateMultipleWrappers() {
rewriteRun(
spec -> spec.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.recipe(new UpdateGradleWrapper("7.4.2", null, Boolean.FALSE, null)),
.recipe(new UpdateGradleWrapper("7.4.2", null, Boolean.FALSE, null, null)),
dir("example1",
properties(
"""
@@ -307,7 +308,7 @@ void updateMultipleWrappers() {
@Test
void olderThan6_6() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("5.6.4", null, null, null))
spec -> spec.recipe(new UpdateGradleWrapper("5.6.4", null, null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "4.0")))
.afterRecipe(run -> {
var gradleSh = result(run, PlainText.class, "gradlew");
@@ -368,7 +369,7 @@ void doNotDowngrade() {
@Test
void allowUpdatingDistributionTypeWhenSameVersion() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("5.6.x", "bin", null, null))
spec -> spec.recipe(new UpdateGradleWrapper("5.6.x", "bin", null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "5.6.4")))
.afterRecipe(run -> {
var gradleSh = result(run, PlainText.class, "gradlew");
@@ -409,7 +410,7 @@ void allowUpdatingDistributionTypeWhenSameVersion() {
@Test
void defaultsToLatestRelease() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper(null, null, null, null))
spec -> spec.recipe(new UpdateGradleWrapper(null, null, null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.afterRecipe(run -> {
var gradleSh = result(run, PlainText.class, "gradlew");
@@ -536,7 +537,7 @@ void skipWorkIfUpdatedEarlier() {
@Test
void preferExistingDistributionSource() {
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.0.x", null, null, null))
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(
@@ -588,7 +589,7 @@ void preferExistingDistributionSourceWhenServicesGradleOrgUnavailable() {
.setHttpSender(unhelpfulSender)
.setLargeFileHttpSender(unhelpfulSender);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.10", "bin", false, null))
spec -> spec.recipe(new UpdateGradleWrapper("8.10", "bin", false, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.executionContext(ctx),
properties(
@@ -627,7 +628,7 @@ void addWrapperWithCustomDistributionUri() {
.setLargeFileHttpSender(customDistributionHost);
rewriteRun(
spec -> spec
.recipe(new UpdateGradleWrapper(null, null, null, "https://company.com/repo/gradle-8.0.2-bin.zip"))
.recipe(new UpdateGradleWrapper(null, null, null, "https://company.com/repo/gradle-8.0.2-bin.zip", null))
.expectedCyclesThatMakeChanges(1)
.executionContext(ctx)
.afterRecipe(run -> {
@@ -660,6 +661,40 @@ void addWrapperWithCustomDistributionUri() {
);
}

@Test
void addWrapperWithCustomDistributionUriAndDistributionChecksum() {
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.0.2-bin.zip", wrapperJarChecksum))
.expectedCyclesThatMakeChanges(1)
.executionContext(ctx)
.afterRecipe(run -> {
var gradleWrapperProperties = result(run, Properties.File.class, "gradle-wrapper.properties");
assertThat(gradleWrapperProperties.getSourcePath()).isEqualTo(WRAPPER_PROPERTIES_LOCATION);
assertThat(gradleWrapperProperties.getContent().stream()
.filter(Properties.Entry.class::isInstance)
.map(Properties.Entry.class::cast)
.anyMatch(prop -> "distributionSha256Sum".equals(prop.getKey()) && wrapperJarChecksum.equals(prop.getValue().getText()))).isTrue();
}),
buildGradle(
"""
plugins {
id "java"
}
"""
)
);
}

@Test
void customDistributionUri() {
HttpSender customDistributionHost = request -> {
@@ -672,7 +707,7 @@ void customDistributionUri() {
.setHttpSender(customDistributionHost)
.setLargeFileHttpSender(customDistributionHost);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper(null, null, null, "https://company.com/repo/gradle-8.10-bin.zip"))
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(
@@ -710,7 +745,7 @@ void servicesGradleOrgUnavailable() {
.setHttpSender(unhelpfulSender)
.setLargeFileHttpSender(unhelpfulSender);
rewriteRun(
spec -> spec.recipe(new UpdateGradleWrapper("8.6", null, null, null))
spec -> spec.recipe(new UpdateGradleWrapper("8.6", null, null, null, null))
.allSources(source -> source.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Gradle, "7.4")))
.executionContext(ctx),
properties(
@@ -791,7 +826,7 @@ void updateWrapperInSubDirectory() {

@Test
void failRecipeIfBothVersionAndDistributionUriAreProvided() {
assertThat(new UpdateGradleWrapper("7.4.2", "bin", false, "https://company.com/repo/gradle-7.4.2-bin.zip").validate().isInvalid()).isTrue();
assertThat(new UpdateGradleWrapper("7.4.2", "bin", false, "https://company.com/repo/gradle-7.4.2-bin.zip", null).validate().isInvalid()).isTrue();
}

private <S extends SourceFile> S result(RecipeRun run, Class<S> clazz, String endsWith) {