2121import org .gradle .api .GradleException ;
2222import org .gradle .api .Plugin ;
2323import org .gradle .api .Project ;
24+ import org .gradle .util .GradleVersion ;
2425
2526import com .diffplug .common .base .StringPrinter ;
2627
2728public class SpotlessPluginRedirect implements Plugin <Project > {
2829 private static final Pattern BAD_SEMVER = Pattern .compile ("(\\ d+)\\ .(\\ d+)" );
2930
30- static int badSemver (Project project ) {
31- return badSemver (project .getGradle ().getGradleVersion ());
32- }
33-
3431 static int badSemver (String input ) {
3532 Matcher matcher = BAD_SEMVER .matcher (input );
3633 if (!matcher .find () || matcher .start () != 0 ) {
@@ -41,18 +38,17 @@ static int badSemver(String input) {
4138 return badSemver (Integer .parseInt (major ), Integer .parseInt (minor ));
4239 }
4340
41+ static int badSemverOfGradle () {
42+ return badSemver (GradleVersion .current ().getVersion ());
43+ }
44+
4445 /** Ambiguous after 2147.483647.blah-blah */
4546 private static int badSemver (int major , int minor ) {
4647 return major * 1_000_000 + minor ;
4748 }
4849
49- static Boolean gradleIsTooOld ;
50-
51- static boolean gradleIsTooOld (Project project ) {
52- if (gradleIsTooOld == null ) {
53- gradleIsTooOld = badSemver (project ) < badSemver (SpotlessPlugin .VER_GRADLE_min );
54- }
55- return gradleIsTooOld .booleanValue ();
50+ static boolean gradleIsTooOld () {
51+ return badSemverOfGradle () < badSemver (SpotlessPlugin .VER_GRADLE_min );
5652 }
5753
5854 @ Override
@@ -76,8 +72,8 @@ public void apply(Project project) {
7672 "" ,
7773 "If you like the idea behind 'ratchetFrom', you should checkout spotless-changelog" ,
7874 "https://github.com/diffplug/spotless-changelog" );
79- if (gradleIsTooOld (project )) {
80- errorMsg = errorMsg .replace ("To migrate:\n " , "To migrate:\n - Upgrade Gradle to " + SpotlessPlugin .VER_GRADLE_min + " or newer (you're on " + project . getGradle ().getGradleVersion () + ")\n " );
75+ if (gradleIsTooOld ()) {
76+ errorMsg = errorMsg .replace ("To migrate:\n " , "To migrate:\n - Upgrade Gradle to " + SpotlessPlugin .VER_GRADLE_min + " or newer (you're on " + GradleVersion . current ().getVersion () + ")\n " );
8177 }
8278 throw new GradleException (errorMsg );
8379 }
0 commit comments