Skip to content

Commit

Permalink
Serializable refactor legacy eclipse steps (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jan 24, 2024
2 parents 33093c7 + 6b3e692 commit 353736d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.JarState;
import com.diffplug.spotless.Provisioner;
import com.diffplug.spotless.ThrowingEx;
import com.diffplug.spotless.SerializedFunction;

/**
* Generic Eclipse based formatter step {@link State} builder.
*/
public class EclipseBasedStepBuilder {
private final String formatterName;
private final String formatterStepExt;
private final ThrowingEx.Function<State, FormatterFunc> stateToFormatter;
private final SerializedFunction<State, FormatterFunc> stateToFormatter;
private final Provisioner jarProvisioner;
private String formatterVersion;

Expand All @@ -63,12 +63,12 @@ public class EclipseBasedStepBuilder {
private Iterable<File> settingsFiles = new ArrayList<>();

/** Initialize valid default configuration, taking latest version */
public EclipseBasedStepBuilder(String formatterName, Provisioner jarProvisioner, ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
public EclipseBasedStepBuilder(String formatterName, Provisioner jarProvisioner, SerializedFunction<State, FormatterFunc> stateToFormatter) {
this(formatterName, "", jarProvisioner, stateToFormatter);
}

/** Initialize valid default configuration, taking latest version */
public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Provisioner jarProvisioner, ThrowingEx.Function<State, FormatterFunc> stateToFormatter) {
public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Provisioner jarProvisioner, SerializedFunction<State, FormatterFunc> stateToFormatter) {
this.formatterName = Objects.requireNonNull(formatterName, "formatterName");
this.formatterStepExt = Objects.requireNonNull(formatterStepExt, "formatterStepExt");
this.jarProvisioner = Objects.requireNonNull(jarProvisioner, "jarProvisioner");
Expand All @@ -78,7 +78,11 @@ public EclipseBasedStepBuilder(String formatterName, String formatterStepExt, Pr

/** Returns the FormatterStep (whose state will be calculated lazily). */
public FormatterStep build() {
return FormatterStep.createLazy(formatterName + formatterStepExt, this::get, stateToFormatter);
var roundtrippableState = new EclipseStep(formatterVersion, formatterStepExt, FileSignature.promise(settingsFiles), JarState.promise(() -> {
return JarState.withoutTransitives(dependencies, jarProvisioner);
}));
return FormatterStep.create(formatterName + formatterStepExt, roundtrippableState,
EclipseStep::state, stateToFormatter);
}

/** Set dependencies for the corresponding Eclipse version */
Expand Down Expand Up @@ -122,21 +126,23 @@ public void setPreferences(Iterable<File> settingsFiles) {
this.settingsFiles = settingsFiles;
}

/** Creates the state of the configuration. */
EclipseBasedStepBuilder.State get() throws IOException {
/*
* The current use case is tailored for Gradle.
* Gradle calls this method only once per execution
* and compares the State with the one of a previous run
* for incremental building.
* Hence a lazy construction is not required.
*/
return new State(
formatterVersion,
formatterStepExt,
jarProvisioner,
dependencies,
settingsFiles);
static class EclipseStep implements Serializable {
private static final long serialVersionUID = 1;
private final String semanticVersion;
private final String formatterStepExt;
private final FileSignature.Promised settingsPromise;
private final JarState.Promised jarPromise;

EclipseStep(String semanticVersion, String formatterStepExt, FileSignature.Promised settingsPromise, JarState.Promised jarPromise) {
this.semanticVersion = semanticVersion;
this.formatterStepExt = formatterStepExt;
this.settingsPromise = settingsPromise;
this.jarPromise = jarPromise;
}

private State state() {
return new State(semanticVersion, formatterStepExt, jarPromise.get(), settingsPromise.get());
}
}

/**
Expand All @@ -155,9 +161,9 @@ public static class State implements Serializable {
private final FileSignature settingsFiles;

/** State constructor expects that all passed items are not modified afterwards */
protected State(String formatterVersion, String formatterStepExt, Provisioner jarProvisioner, List<String> dependencies, Iterable<File> settingsFiles) throws IOException {
this.jarState = JarState.withoutTransitives(dependencies, jarProvisioner);
this.settingsFiles = FileSignature.signAsList(settingsFiles);
protected State(String formatterVersion, String formatterStepExt, JarState jarState, FileSignature settingsFiles) {
this.jarState = jarState;
this.settingsFiles = settingsFiles;
this.formatterStepExt = formatterStepExt;
semanticVersion = convertEclipseVersion(formatterVersion);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2024 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 @@ -21,8 +21,8 @@
import java.util.Arrays;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.StepHarnessWithFile;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;

/**
Expand All @@ -43,57 +43,19 @@
*/
public class EclipseResourceHarness extends ResourceHarness {
private final EclipseBasedStepBuilder stepBuilder;
private final String fileName;
private final String input;
private final String expected;

/**
* Create harness to be used for several versions of the formatter step
* @param builder Eclipse Formatter step builder
* @param unformatted Simple unformatted input
* @param formatted Expected formatted output
*/
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String unformatted, String formatted) {
this(builder, "someSourceFile", unformatted, formatted);
public EclipseResourceHarness(EclipseBasedStepBuilder builder) {
this.stepBuilder = builder;
}

/**
* Create harness to be used for several versions of the formatter step
* @param builder Eclipse Formatter step builder
* @param sourceFileName File name of the source file
* @param unformatted Simple unformatted input
* @param formatted Expected formatted output
*/
public EclipseResourceHarness(EclipseBasedStepBuilder builder, String sourceFileName, String unformatted, String formatted) {
stepBuilder = builder;
fileName = sourceFileName;
input = unformatted;
expected = formatted;
}

/**
* Assert that formatting input results in expected output
* @param formatterVersion Formatter version
* @param settingsFiles Formatter settings
* @return Formatted string
*/
protected String assertFormatted(String formatterVersion, File... settingsFiles) throws Exception {
String output = format(formatterVersion, settingsFiles);
assertThat(output).isEqualTo(expected);
return output;
}

/**
* Formatting input results and returns output
* @param formatterVersion Formatter version
* @param settingsFiles Formatter settings
* @return Formatted string
*/
protected String format(String formatterVersion, File... settingsFiles) throws Exception {
File inputFile = setFile(fileName).toContent(input);
protected StepHarnessWithFile harnessFor(String formatterVersion, File... settingsFiles) throws Exception {
stepBuilder.setVersion(formatterVersion);
stepBuilder.setPreferences(Arrays.asList(settingsFiles));
FormatterStep step = stepBuilder.build();
return LineEnding.toUnix(step.format(input, inputFile));
return StepHarnessWithFile.forStep(this, step);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2024 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 @@ -15,8 +15,6 @@
*/
package com.diffplug.spotless.extra.wtp;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -38,14 +36,18 @@ public class EclipseWtpFormatterStepTest {
private final static Jvm.Support<String> JVM_SUPPORT = Jvm.<String> support("Oldest Version").add(8, "4.8.0");

private static class NestedTests extends EclipseResourceHarness {
private final String unformatted, formatted;

public NestedTests(String unformatted, String formatted, EclipseWtpFormatterStep kind) {
super(kind.createBuilder(TestProvisioner.mavenCentral()), unformatted, formatted);
super(kind.createBuilder(TestProvisioner.mavenCentral()));
this.unformatted = unformatted;
this.formatted = formatted;
}

@ParameterizedTest
@MethodSource
void formatWithVersion(String version) throws Exception {
assertFormatted(version);
harnessFor(version).test("someFilename", unformatted, formatted);
}

private static Stream<String> formatWithVersion() {
Expand All @@ -67,8 +69,8 @@ void multipleConfigurations() throws Exception {
config.setProperty("indentationChar", "space");
config.setProperty("indentationSize", "5");
});
String defaultFormatted = assertFormatted(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile);
assertThat(format(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile)).as("Space formatting output unexpected").isEqualTo(defaultFormatted.replace("\t", " "));
harnessFor(EclipseWtpFormatterStep.defaultVersion(), tabPropertyFile).test("someFilename", unformatted, formatted);
harnessFor(EclipseWtpFormatterStep.defaultVersion(), spacePropertyFile).test("someFilename", unformatted, formatted.replace("\t", " "));
}

private File createPropertyFile(Consumer<Properties> config) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.diffplug.spotless;

import java.util.Locale;
import java.util.Objects;

import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -43,7 +44,7 @@ protected StepHarnessBase(Formatter formatter) {
supportsRoundTrip = true;
} else if (onlyStepName.equals("diktat")) {
supportsRoundTrip = true;
} else if (onlyStepName.equals("eclipse jdt formatter") || onlyStepName.equals("eclipse cdt formatter") || onlyStepName.equals("eclipse groovy formatter")) {
} else if (onlyStepName.toLowerCase(Locale.ROOT).contains("eclipse")) {
supportsRoundTrip = true;
}
}
Expand Down

0 comments on commit 353736d

Please sign in to comment.