Skip to content

Commit

Permalink
We have a working GrEclipse!
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Mar 11, 2023
1 parent 0591e49 commit 2673e91
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.codehaus.groovy.eclipse.core.GroovyCoreActivator;
Expand All @@ -36,6 +37,8 @@
import org.eclipse.jface.text.TextSelection;
import org.eclipse.text.edits.TextEdit;

import dev.equo.solstice.Solstice;

/** Spotless-Formatter step which calls out to the Groovy-Eclipse formatter. */
public class GrEclipseFormatterStepImpl {
/**
Expand All @@ -50,6 +53,11 @@ public class GrEclipseFormatterStepImpl {
private final boolean ignoreFormatterProblems;

public GrEclipseFormatterStepImpl(final Properties properties) throws Exception {
var solstice = Solstice.findBundlesOnClasspath();
solstice.warnAndModifyManifestsToFix();
solstice.openShim(Map.of());
solstice.startAllWithLazy(false);
solstice.startWithoutTransitives("org.codehaus.groovy.eclipse.core");
PreferenceStore preferences = createPreferences(properties);
preferencesStore = new FormatterPreferencesOnStore(preferences);
ignoreFormatterProblems = Boolean.parseBoolean(properties.getProperty(IGNORE_FORMATTER_PROBLEMS, "false"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,41 +16,74 @@
package com.diffplug.spotless.extra.groovy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;

import com.diffplug.spotless.FormatterFunc;
import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder.State;
import com.diffplug.spotless.extra.EquoBasedStepBuilder;

import dev.equo.solstice.p2.P2Model;

/** Formatter step which calls out to the Groovy-Eclipse formatter. */
public final class GrEclipseFormatterStep {
// prevent direct instantiation
private GrEclipseFormatterStep() {}

private static final String NAME = "eclipse groovy formatter";
private static final String FORMATTER_CLASS = "com.diffplug.spotless.extra.eclipse.groovy.GrEclipseFormatterStepImpl";
private static final String FORMATTER_CLASS_OLD = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
private static final String MAVEN_GROUP_ARTIFACT = "com.diffplug.spotless:spotless-eclipse-groovy";
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(8, "4.19.0").add(11, "4.21.0");
private static final Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support(NAME).add(11, "4.26");
private static final String FORMATTER_METHOD = "format";

public static String defaultVersion() {
return JVM_SUPPORT.getRecommendedFormatterVersion();
}

/** Provides default configuration */
public static EclipseBasedStepBuilder createBuilder(Provisioner provisioner) {
return new EclipseBasedStepBuilder(NAME, provisioner, GrEclipseFormatterStep::apply);
public static EquoBasedStepBuilder createBuilder(Provisioner provisioner) {
return new EquoBasedStepBuilder(NAME, provisioner, GrEclipseFormatterStep::apply) {
@Override
protected P2Model model(String version) {
if (!version.startsWith("4.")) {
throw new IllegalArgumentException("Expected version 4.x");
}
int eVersion = Integer.parseInt(version.substring("4.".length()));
if (eVersion < 8) {
throw new IllegalArgumentException("4.8 is the oldest version we support, this was " + version);
}
String greclipseVersion;
if (eVersion >= 18) {
greclipseVersion = "4." + (eVersion - 18) + ".0";
} else {
greclipseVersion = "3." + (eVersion - 8) + ".0";
}
var model = new P2Model();
model.addP2Repo("https://download.eclipse.org/eclipse/updates/" + version + "/");
model.addP2Repo("https://groovy.jfrog.io/artifactory/plugins-release/org/codehaus/groovy/groovy-eclipse-integration/" + greclipseVersion + "/e" + version + "/");
model.getInstall().addAll(List.of(
"org.codehaus.groovy.eclipse.refactoring",
"org.codehaus.groovy.eclipse.core",
"org.eclipse.jdt.groovy.core",
"org.codehaus.groovy"));
return model;
}

@Override
public void setVersion(String version) {
if (version.endsWith(".0")) {
String newVersion = version.substring(0, version.length() - 2);
System.err.println("Recommend replacing '" + version + "' with '" + newVersion + "' for eclipse JDT");
version = newVersion;
}
super.setVersion(version);
}
};
}

private static FormatterFunc apply(EclipseBasedStepBuilder.State state) throws Exception {
private static FormatterFunc apply(EquoBasedStepBuilder.State state) throws Exception {
JVM_SUPPORT.assertFormatterSupported(state.getSemanticVersion());
Class<?> formatterClazz = getClass(state);
Object formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
Method method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
Class<?> formatterClazz = state.getJarState().getClassLoader().loadClass("com.diffplug.spotless.extra.glue.groovy.GrEclipseFormatterStepImpl");
var formatter = formatterClazz.getConstructor(Properties.class).newInstance(state.getPreferences());
var method = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return JVM_SUPPORT.suggestLaterVersionOnError(state.getSemanticVersion(),
input -> {
try {
Expand All @@ -62,12 +95,4 @@ private static FormatterFunc apply(EclipseBasedStepBuilder.State state) throws E
}
});
}

private static Class<?> getClass(State state) {
if (state.getMavenCoordinate(MAVEN_GROUP_ARTIFACT).isPresent()) {
return state.loadClass(FORMATTER_CLASS);
}
return state.loadClass(FORMATTER_CLASS_OLD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import com.diffplug.spotless.Jvm;
import com.diffplug.spotless.TestProvisioner;
import com.diffplug.spotless.extra.eclipse.EquoResourceHarness;

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

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

private static Stream<String> formatWithVersion() {
return Stream.of(JVM_SUPPORT.getRecommendedFormatterVersion(), GrEclipseFormatterStep.defaultVersion());
return Stream.of("4.25", GrEclipseFormatterStep.defaultVersion());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 DiffPlug
* Copyright 2020-2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,7 +60,7 @@ private void writePomWithGrEclipse() throws IOException {
writePomWithGroovySteps(
"<greclipse>",
" <file>${basedir}/greclipse.properties</file>",
" <version>4.19.0</version>",
" <version>4.25</version>",
"</greclipse>");
setFile("greclipse.properties").toResource("groovy/greclipse/format/greclipse.properties");
}
Expand Down

0 comments on commit 2673e91

Please sign in to comment.