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

Spotless now checks to make sure that Gradle is modern enough. #684

Merged
merged 3 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Fixed
* We did not proactively check to ensure that the Gradle version was modern enough, now we do (fixes [#684](https://github.com/diffplug/spotless/pull/684)).

## [5.3.0] - 2020-08-29
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.diffplug.gradle.spotless;

import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
Expand All @@ -27,6 +28,9 @@ public class SpotlessPlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
if (SpotlessPluginRedirect.gradleIsTooOld(project)) {
throw new GradleException("Spotless requires Gradle " + MINIMUM_GRADLE + " or newer, this was " + project.getGradle().getGradleVersion());
}
// if -PspotlessModern=true, then use the modern stuff instead of the legacy stuff
if (project.hasProperty(SPOTLESS_MODERN)) {
project.getLogger().warn("'spotlessModern' has no effect as of Spotless 5.0, recommend removing it.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,45 @@
*/
package com.diffplug.gradle.spotless;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

import com.diffplug.common.base.StringPrinter;

public class SpotlessPluginRedirect implements Plugin<Project> {
private static final Pattern BAD_SEMVER = Pattern.compile("(\\d+)\\.(\\d+)");

private static int badSemver(String input) {
Matcher matcher = BAD_SEMVER.matcher(input);
if (!matcher.find() || matcher.start() != 0) {
throw new IllegalArgumentException("Version must start with " + BAD_SEMVER.pattern());
}
String major = matcher.group(1);
String minor = matcher.group(2);
return badSemver(Integer.parseInt(major), Integer.parseInt(minor));
}

/** Ambiguous after 2147.483647.blah-blah */
private static int badSemver(int major, int minor) {
return major * 1_000_000 + minor;
}

static Boolean gradleIsTooOld;

static boolean gradleIsTooOld(Project project) {
if (gradleIsTooOld == null) {
gradleIsTooOld = badSemver(project.getGradle().getGradleVersion()) < badSemver(SpotlessPlugin.MINIMUM_GRADLE);
}
return gradleIsTooOld.booleanValue();
}

@Override
public void apply(Project project) {
throw new GradleException(StringPrinter.buildStringFromLines(
String errorMsg = StringPrinter.buildStringFromLines(
"We have moved from 'com.diffplug.gradle.spotless'",
" to 'com.diffplug.spotless'",
"To migrate:",
Expand All @@ -42,6 +71,10 @@ public void apply(Project project) {
"to be useful, hope you do too.",
"",
"If you like the idea behind 'ratchetFrom', you should checkout spotless-changelog",
"https://github.com/diffplug/spotless-changelog"));
"https://github.com/diffplug/spotless-changelog");
if (gradleIsTooOld(project)) {
errorMsg = errorMsg.replace("To migrate:\n", "To migrate:\n- Upgrade gradle to " + SpotlessPlugin.MINIMUM_GRADLE + " or newer (you're on " + project.getGradle().getGradleVersion() + ")\n");
}
throw new GradleException(errorMsg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

public class GradleIntegrationHarness extends ResourceHarness {
public enum GradleVersionSupport {
MINIMUM(SpotlessPlugin.MINIMUM_GRADLE), SETTINGS_PLUGINS("6.0");
JRE_11("5.0"), MINIMUM(SpotlessPlugin.MINIMUM_GRADLE), SETTINGS_PLUGINS("6.0");

final String version;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public class SpotlessPluginRedirectTest extends GradleIntegrationHarness {
@Test
public void test() throws IOException {
public void redirectPluginModernGradle() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
Expand All @@ -34,6 +34,36 @@ public void test() throws IOException {
"> Failed to apply plugin [id 'com.diffplug.gradle.spotless']",
" > We have moved from 'com.diffplug.gradle.spotless'",
" to 'com.diffplug.spotless'",
" To migrate:"));
" To migrate:",
" - Test your build with: id 'com.diffplug.gradle.spotless' version '4.5.1'"));
}

@Test
public void redirectPluginOldGradle() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}");
Assertions.assertThat(gradleRunner().withGradleVersion(GradleVersionSupport.JRE_11.version)
.buildAndFail().getOutput().replace("\r", ""))
.contains(StringPrinter.buildStringFromLines(
"> Failed to apply plugin [id 'com.diffplug.gradle.spotless']",
" > We have moved from 'com.diffplug.gradle.spotless'",
" to 'com.diffplug.spotless'",
" To migrate:",
" - Upgrade gradle to 5.4 or newer (you're on 5.0)",
" - Test your build with: id 'com.diffplug.gradle.spotless' version '4.5.1'"));
}

@Test
public void realPluginOldGradle() throws IOException {
setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}");
Assertions.assertThat(gradleRunner().withGradleVersion(GradleVersionSupport.JRE_11.version)
.buildAndFail().getOutput().replace("\r", ""))
.contains(StringPrinter.buildStringFromLines(
"Spotless requires Gradle 5.4 or newer, this was 5.0"));
}
}