Skip to content

Commit 83850bf

Browse files
committed
Resolved TODO: custom steps only allowed on Gradle 8+
1 parent 8bef1ad commit 83850bf

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

Diff for: plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Added
7+
* Full, no-asterisk support of Gradle configuration cache. ([#1274](https://github.com/diffplug/spotless/issues/1274), giving up on [#987](https://github.com/diffplug/spotless/issues/987))
8+
* In order to use `custom`, you must now use Gradle 8.0+.
79
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
810
* Add support for formatting and sorting Maven POMs ([#2082](https://github.com/diffplug/spotless/issues/2082))
911
### Fixed

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.gradle.spotless;
1717

1818
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;
19+
import static com.diffplug.gradle.spotless.SpotlessPluginRedirect.badSemver;
1920
import static java.util.Objects.requireNonNull;
2021

2122
import java.io.File;
@@ -451,6 +452,12 @@ public String apply(String unixNewlines) {
451452
*/
452453
public void custom(String name, FormatterFunc formatter) {
453454
requireNonNull(formatter, "formatter");
455+
if (badSemver(getProject()) < badSemver(SpotlessPlugin.VER_GRADLE_minVersionForCustom)) {
456+
throw new GradleException("The 'custom' method is only available if you are using Gradle "
457+
+ SpotlessPlugin.VER_GRADLE_minVersionForCustom
458+
+ " or newer, this is "
459+
+ getProject().getGradle().getGradleVersion());
460+
}
454461
addStep(FormatterStep.createLazy(name, () -> globalState, SerializedFunction.alwaysReturns(formatter)));
455462
}
456463

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ public class SpotlessPlugin implements Plugin<Project> {
2828
static final String SPOTLESS_MODERN = "spotlessModern";
2929
static final String VER_GRADLE_min = "6.1.1";
3030
static final String VER_GRADLE_javaPluginExtension = "7.1";
31+
static final String VER_GRADLE_minVersionForCustom = "8.0";
3132
private static final int MINIMUM_JRE = 11;
3233

3334
@Override

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPluginRedirect.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 DiffPlug
2+
* Copyright 2020-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,11 @@
2727
public class SpotlessPluginRedirect implements Plugin<Project> {
2828
private static final Pattern BAD_SEMVER = Pattern.compile("(\\d+)\\.(\\d+)");
2929

30-
private static int badSemver(String input) {
30+
static int badSemver(Project project) {
31+
return badSemver(project.getGradle().getGradleVersion());
32+
}
33+
34+
static int badSemver(String input) {
3135
Matcher matcher = BAD_SEMVER.matcher(input);
3236
if (!matcher.find() || matcher.start() != 0) {
3337
throw new IllegalArgumentException("Version must start with " + BAD_SEMVER.pattern());
@@ -46,7 +50,7 @@ private static int badSemver(int major, int minor) {
4650

4751
static boolean gradleIsTooOld(Project project) {
4852
if (gradleIsTooOld == null) {
49-
gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.VER_GRADLE_min);
53+
gradleIsTooOld = badSemver(project) < badSemver(SpotlessPlugin.VER_GRADLE_min);
5054
}
5155
return gradleIsTooOld.booleanValue();
5256
}

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/BumpThisNumberIfACustomStepChangesTest.java

-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,9 @@
1717

1818
import java.io.IOException;
1919

20-
import org.gradle.testkit.runner.GradleRunner;
2120
import org.junit.jupiter.api.Test;
2221

2322
class BumpThisNumberIfACustomStepChangesTest extends GradleIntegrationHarness {
24-
@Override
25-
protected GradleRunner gradleRunner() throws IOException {
26-
return super.gradleRunner().withGradleVersion("8.0");
27-
}
28-
2923
private void writeBuildFile(String toInsert) throws IOException {
3024
setFile("build.gradle").toLines(
3125
"plugins {",

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GitRatchetGradleTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private Git initRepo() throws IllegalStateException, GitAPIException, IOExceptio
5353

5454
@Override
5555
protected GradleRunner gradleRunner() throws IOException {
56-
return super.gradleRunner().withGradleVersion(GradleVersionSupport.CONFIGURATION_CACHE.version);
56+
return super.gradleRunner().withGradleVersion(GradleVersionSupport.CUSTOM_STEPS.version);
5757
}
5858

5959
@ParameterizedTest

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@
4343

4444
public class GradleIntegrationHarness extends ResourceHarness {
4545
public enum GradleVersionSupport {
46-
JRE_11("5.0"), MINIMUM(SpotlessPlugin.VER_GRADLE_min),
46+
JRE_11("5.0"), MINIMUM(SpotlessPlugin.VER_GRADLE_min), CUSTOM_STEPS(SpotlessPlugin.VER_GRADLE_minVersionForCustom),
4747
// technically, this API exists in 6.5, but the flags for it change in 6.6, so we build to that
4848
CONFIGURATION_CACHE("6.6"),
4949
// https://docs.gradle.org/7.5/userguide/configuration_cache.html#config_cache:stable
@@ -116,8 +116,14 @@ void gitAttributes() throws IOException {
116116
}
117117

118118
protected GradleRunner gradleRunner() throws IOException {
119+
GradleVersionSupport version;
120+
if (newFile("build.gradle").exists() && read("build.gradle").contains("custom")) {
121+
version = GradleVersionSupport.CUSTOM_STEPS;
122+
} else {
123+
version = GradleVersionSupport.MINIMUM;
124+
}
119125
return GradleRunner.create()
120-
.withGradleVersion(GradleVersionSupport.MINIMUM.version)
126+
.withGradleVersion(version.version)
121127
.withProjectDir(rootFolder())
122128
.withTestKitDir(getTestKitDir())
123129
.withPluginClasspath();

0 commit comments

Comments
 (0)