11/*
2- * Copyright 2016-2021 DiffPlug
2+ * Copyright 2016-2023 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.
1616package com .diffplug .spotless .extra .groovy ;
1717
1818import java .lang .reflect .InvocationTargetException ;
19- import java .lang . reflect . Method ;
19+ import java .util . List ;
2020import java .util .Properties ;
2121
2222import com .diffplug .spotless .FormatterFunc ;
2323import com .diffplug .spotless .Jvm ;
2424import com .diffplug .spotless .Provisioner ;
25- import com .diffplug .spotless .extra .EclipseBasedStepBuilder ;
26- import com .diffplug .spotless .extra .EclipseBasedStepBuilder .State ;
25+ import com .diffplug .spotless .extra .EquoBasedStepBuilder ;
26+
27+ import dev .equo .solstice .p2 .P2Model ;
2728
2829/** Formatter step which calls out to the Groovy-Eclipse formatter. */
2930public final class GrEclipseFormatterStep {
3031 // prevent direct instantiation
3132 private GrEclipseFormatterStep () {}
3233
3334 private static final String NAME = "eclipse groovy formatter" ;
34- private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.groovy.GrEclipseFormatterStepImpl" ;
35- private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl" ;
36- private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-groovy" ;
37- private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (8 , "4.19.0" ).add (11 , "4.21.0" );
35+ private static final Jvm .Support <String > JVM_SUPPORT = Jvm .<String > support (NAME ).add (11 , "4.26" );
3836 private static final String FORMATTER_METHOD = "format" ;
3937
4038 public static String defaultVersion () {
4139 return JVM_SUPPORT .getRecommendedFormatterVersion ();
4240 }
4341
44- /** Provides default configuration */
45- public static EclipseBasedStepBuilder createBuilder (Provisioner provisioner ) {
46- return new EclipseBasedStepBuilder (NAME , provisioner , GrEclipseFormatterStep ::apply );
42+ public static EquoBasedStepBuilder createBuilder (Provisioner provisioner ) {
43+ return new EquoBasedStepBuilder (NAME , provisioner , GrEclipseFormatterStep ::apply ) {
44+ @ Override
45+ protected P2Model model (String version ) {
46+ if (!version .startsWith ("4." )) {
47+ throw new IllegalArgumentException ("Expected version 4.x" );
48+ }
49+ int eVersion = Integer .parseInt (version .substring ("4." .length ()));
50+ if (eVersion < 8 ) {
51+ throw new IllegalArgumentException ("4.8 is the oldest version we support, this was " + version );
52+ }
53+ String greclipseVersion ;
54+ if (eVersion >= 18 ) {
55+ greclipseVersion = "4." + (eVersion - 18 ) + ".0" ;
56+ } else {
57+ greclipseVersion = "3." + (eVersion - 8 ) + ".0" ;
58+ }
59+ var model = new P2Model ();
60+ model .addP2Repo ("https://download.eclipse.org/eclipse/updates/" + version + "/" );
61+ model .addP2Repo ("https://groovy.jfrog.io/artifactory/plugins-release/org/codehaus/groovy/groovy-eclipse-integration/" + greclipseVersion + "/e" + version + "/" );
62+ model .getInstall ().addAll (List .of (
63+ "org.codehaus.groovy.eclipse.refactoring" ,
64+ "org.codehaus.groovy.eclipse.core" ,
65+ "org.eclipse.jdt.groovy.core" ,
66+ "org.codehaus.groovy" ));
67+ return model ;
68+ }
69+
70+ @ Override
71+ public void setVersion (String version ) {
72+ if (version .endsWith (".0" )) {
73+ String newVersion = version .substring (0 , version .length () - 2 );
74+ System .err .println ("Recommend replacing '" + version + "' with '" + newVersion + "' for eclipse JDT" );
75+ version = newVersion ;
76+ }
77+ super .setVersion (version );
78+ }
79+ };
4780 }
4881
49- private static FormatterFunc apply (EclipseBasedStepBuilder .State state ) throws Exception {
82+ private static FormatterFunc apply (EquoBasedStepBuilder .State state ) throws Exception {
5083 JVM_SUPPORT .assertFormatterSupported (state .getSemanticVersion ());
51- Class <?> formatterClazz = getClass ( state );
52- Object formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
53- Method method = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
84+ Class <?> formatterClazz = state . getJarState (). getClassLoader (). loadClass ( "com.diffplug.spotless.extra.glue.groovy.GrEclipseFormatterStepImpl" );
85+ var formatter = formatterClazz .getConstructor (Properties .class ).newInstance (state .getPreferences ());
86+ var method = formatterClazz .getMethod (FORMATTER_METHOD , String .class );
5487 return JVM_SUPPORT .suggestLaterVersionOnError (state .getSemanticVersion (),
5588 input -> {
5689 try {
@@ -62,12 +95,4 @@ private static FormatterFunc apply(EclipseBasedStepBuilder.State state) throws E
6295 }
6396 });
6497 }
65-
66- private static Class <?> getClass (State state ) {
67- if (state .getMavenCoordinate (MAVEN_GROUP_ARTIFACT ).isPresent ()) {
68- return state .loadClass (FORMATTER_CLASS );
69- }
70- return state .loadClass (FORMATTER_CLASS_OLD );
71- }
72-
7398}
0 commit comments