21
21
import org .gradle .api .GradleException ;
22
22
import org .gradle .api .Plugin ;
23
23
import org .gradle .api .Project ;
24
+ import org .gradle .util .GradleVersion ;
24
25
25
26
import com .diffplug .common .base .StringPrinter ;
26
27
27
28
public class SpotlessPluginRedirect implements Plugin <Project > {
28
29
private static final Pattern BAD_SEMVER = Pattern .compile ("(\\ d+)\\ .(\\ d+)" );
29
30
30
- static int badSemver (Project project ) {
31
- return badSemver (project .getGradle ().getGradleVersion ());
32
- }
33
-
34
31
static int badSemver (String input ) {
35
32
Matcher matcher = BAD_SEMVER .matcher (input );
36
33
if (!matcher .find () || matcher .start () != 0 ) {
@@ -41,18 +38,17 @@ static int badSemver(String input) {
41
38
return badSemver (Integer .parseInt (major ), Integer .parseInt (minor ));
42
39
}
43
40
41
+ static int badSemverOfGradle () {
42
+ return badSemver (GradleVersion .current ().getVersion ());
43
+ }
44
+
44
45
/** Ambiguous after 2147.483647.blah-blah */
45
46
private static int badSemver (int major , int minor ) {
46
47
return major * 1_000_000 + minor ;
47
48
}
48
49
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 );
56
52
}
57
53
58
54
@ Override
@@ -76,8 +72,8 @@ public void apply(Project project) {
76
72
"" ,
77
73
"If you like the idea behind 'ratchetFrom', you should checkout spotless-changelog" ,
78
74
"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 " );
81
77
}
82
78
throw new GradleException (errorMsg );
83
79
}
0 commit comments