Skip to content

Commit 2673e91

Browse files
committed
We have a working GrEclipse!
1 parent 0591e49 commit 2673e91

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

lib-extra/src/groovy/java/com/diffplug/spotless/extra/glue/groovy/GrEclipseFormatterStepImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.Collections;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.Properties;
2526

2627
import org.codehaus.groovy.eclipse.core.GroovyCoreActivator;
@@ -36,6 +37,8 @@
3637
import org.eclipse.jface.text.TextSelection;
3738
import org.eclipse.text.edits.TextEdit;
3839

40+
import dev.equo.solstice.Solstice;
41+
3942
/** Spotless-Formatter step which calls out to the Groovy-Eclipse formatter. */
4043
public class GrEclipseFormatterStepImpl {
4144
/**
@@ -50,6 +53,11 @@ public class GrEclipseFormatterStepImpl {
5053
private final boolean ignoreFormatterProblems;
5154

5255
public GrEclipseFormatterStepImpl(final Properties properties) throws Exception {
56+
var solstice = Solstice.findBundlesOnClasspath();
57+
solstice.warnAndModifyManifestsToFix();
58+
solstice.openShim(Map.of());
59+
solstice.startAllWithLazy(false);
60+
solstice.startWithoutTransitives("org.codehaus.groovy.eclipse.core");
5361
PreferenceStore preferences = createPreferences(properties);
5462
preferencesStore = new FormatterPreferencesOnStore(preferences);
5563
ignoreFormatterProblems = Boolean.parseBoolean(properties.getProperty(IGNORE_FORMATTER_PROBLEMS, "false"));
Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -16,41 +16,74 @@
1616
package com.diffplug.spotless.extra.groovy;
1717

1818
import java.lang.reflect.InvocationTargetException;
19-
import java.lang.reflect.Method;
19+
import java.util.List;
2020
import java.util.Properties;
2121

2222
import com.diffplug.spotless.FormatterFunc;
2323
import com.diffplug.spotless.Jvm;
2424
import 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. */
2930
public 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
}

lib-extra/src/test/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStepTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
import org.junit.jupiter.params.ParameterizedTest;
2121
import org.junit.jupiter.params.provider.MethodSource;
2222

23-
import com.diffplug.spotless.Jvm;
2423
import com.diffplug.spotless.TestProvisioner;
2524
import com.diffplug.spotless.extra.eclipse.EquoResourceHarness;
2625

2726
class GrEclipseFormatterStepTest extends EquoResourceHarness {
28-
private final static Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support("Oldest Version").add(8, "4.8").add(11, "4.18");
2927
private final static String INPUT = "class F{ def m(){} }";
3028
private final static String EXPECTED = "class F{\n\tdef m(){}\n}";
3129

@@ -40,6 +38,6 @@ void formatWithVersion(String version) throws Exception {
4038
}
4139

4240
private static Stream<String> formatWithVersion() {
43-
return Stream.of(JVM_SUPPORT.getRecommendedFormatterVersion(), GrEclipseFormatterStep.defaultVersion());
41+
return Stream.of("4.25", GrEclipseFormatterStep.defaultVersion());
4442
}
4543
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/groovy/GrEclipseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-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.
@@ -60,7 +60,7 @@ private void writePomWithGrEclipse() throws IOException {
6060
writePomWithGroovySteps(
6161
"<greclipse>",
6262
" <file>${basedir}/greclipse.properties</file>",
63-
" <version>4.19.0</version>",
63+
" <version>4.25</version>",
6464
"</greclipse>");
6565
setFile("greclipse.properties").toResource("groovy/greclipse/format/greclipse.properties");
6666
}

0 commit comments

Comments
 (0)