Skip to content

Commit

Permalink
Merge branch 'main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Aug 29, 2023
2 parents 14e4e62 + 4b1a75b commit 130402a
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 50 deletions.
7 changes: 4 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
### Fixed
* Use latest versions of popular style guides for `eslint` tests to fix failing `useEslintXoStandardRules` test. ([#1761](https://github.com/diffplug/spotless/pull/1761), [#1756](https://github.com/diffplug/spotless/issues/1756))
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))
* Add support for `prettier` version `3.0.0` and newer. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
* Fix npm install calls when npm cache is not up-to-date. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))
### Changes
* Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761))
* Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))

## [2.40.0] - 2023-07-17
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import com.diffplug.spotless.FormatterFunc;

// Used via reflection by the Gradle plugin.
@SuppressWarnings("unused")
public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

@Nonnull
Expand All @@ -42,10 +44,13 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {

private final boolean reflowStrings;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
private final boolean reorderImports;

public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
this.version = Objects.requireNonNull(version);
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
this.reflowStrings = reflowStrings;
this.reorderImports = reorderImports;

this.formatter = new Formatter(JavaFormatterOptions.builder()
.style(formatterStyle)
Expand All @@ -57,10 +62,7 @@ public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String st
public String apply(@Nonnull String input) throws Exception {
String formatted = formatter.formatSource(input);
String removedUnused = RemoveUnusedImports.removeUnusedImports(formatted);
// Issue #1679: we used to call ImportOrderer.reorderImports(String) here, but that is deprecated.
// Replacing the call with (the correct) reorderImports(String, Style) causes issues for Style.AOSP,
// so we force the style to GOOGLE for now (which is what the deprecated method did)
String sortedImports = ImportOrderer.reorderImports(removedUnused, Style.GOOGLE);
String sortedImports = ImportOrderer.reorderImports(removedUnused, reorderImports ? formatterStyle : Style.GOOGLE);
return reflowLongStrings(sortedImports);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ private GoogleJavaFormatStep() {}

private static final String DEFAULT_STYLE = "GOOGLE";
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
private static final boolean DEFAULT_REORDER_IMPORTS = false;
static final String NAME = "google-java-format";
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";

Expand All @@ -55,8 +56,12 @@ public static FormatterStep create(String version, String style, Provisioner pro
return create(MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
}

/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
Objects.requireNonNull(groupArtifact, "groupArtifact");
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
Expand All @@ -65,7 +70,7 @@ public static FormatterStep create(String groupArtifact, String version, String
Objects.requireNonNull(style, "style");
Objects.requireNonNull(provisioner, "provisioner");
return FormatterStep.createLazy(NAME,
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
State::createFormat);
}

Expand All @@ -92,6 +97,10 @@ public static boolean defaultReflowLongStrings() {
return DEFAULT_REFLOW_LONG_STRINGS;
}

public static boolean defaultReorderImports() {
return DEFAULT_REORDER_IMPORTS;
}

static final class State implements Serializable {
private static final long serialVersionUID = 1L;

Expand All @@ -101,6 +110,7 @@ static final class State implements Serializable {
final String version;
final String style;
final boolean reflowLongStrings;
final boolean reorderImports;

State(String stepName, String version, Provisioner provisioner) throws Exception {
this(stepName, version, DEFAULT_STYLE, provisioner);
Expand All @@ -111,24 +121,25 @@ static final class State implements Serializable {
}

State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
}

State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
JVM_SUPPORT.assertFormatterSupported(version);
ModuleHelper.doOpenInternalPackagesIfRequired();
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
this.stepName = stepName;
this.version = version;
this.style = style;
this.reflowLongStrings = reflowLongStrings;
this.reorderImports = reorderImports;
}

FormatterFunc createFormat() throws Exception {
final ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterFunc = classLoader.loadClass("com.diffplug.spotless.glue.java.GoogleJavaFormatFormatterFunc");
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings);
Constructor<?> constructor = formatterFunc.getConstructor(String.class, String.class, boolean.class, boolean.class);
FormatterFunc googleJavaFormatFormatterFunc = (FormatterFunc) constructor.newInstance(version, style, reflowLongStrings, reorderImports);

return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
}
Expand Down
26 changes: 15 additions & 11 deletions lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -61,13 +62,12 @@ public FormatterStep create() {
return FormatterStep.createLazy(name(), this::createState, State::toFunc);
}

private State createState() throws IOException, InterruptedException {
private State createState() {
String instructions = "https://docs.buf.build/installation";
String exeAbsPath = ForeignExe.nameAndVersion("buf", version)
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version)
.pathToExe(pathToExe)
.versionRegex(Pattern.compile("(\\S*)"))
.fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}")
.confirmVersionAndGetAbsolutePath();
.fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}");
return new State(this, exeAbsPath);
}

Expand All @@ -76,19 +76,23 @@ static class State implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
final transient ForeignExe exe;
// used for executing
final transient List<String> args;
private transient @Nullable List<String> args;

State(BufStep step, String exeAbsPath) {
State(BufStep step, ForeignExe exeAbsPath) {
this.version = step.version;
this.args = Arrays.asList(exeAbsPath, "format");
this.exe = Objects.requireNonNull(exeAbsPath);
}

String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException {
String[] processArgs = args.toArray(new String[args.size() + 1]);
// add an argument to the end
processArgs[args.size()] = file.getAbsolutePath();
return runner.exec(input.getBytes(StandardCharsets.UTF_8), processArgs).assertExitZero(StandardCharsets.UTF_8);
if (args == null) {
args = Arrays.asList(
exe.confirmVersionAndGetAbsolutePath(),
"format",
file.getAbsolutePath());
}
return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
}

FormatterFunc.Closeable toFunc() {
Expand Down
10 changes: 7 additions & 3 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Add a `jsonPatch` step to `json` formatter configurations. This allows patching of JSON documents using [JSON Patches](https://jsonpatch.com). ([#1753](https://github.com/diffplug/spotless/pull/1753))
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
### Fixed
* Add support for `prettier` version `3.0.0` and newer. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
* Fix npm install calls when npm cache is not up-to-date. ([#1760]https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))
* Add support for `prettier` version `3.0.0` and newer. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1751](https://github.com/diffplug/spotless/issues/1751))
* Fix npm install calls when npm cache is not up-to-date. ([#1760](https://github.com/diffplug/spotless/pull/1760), [#1750](https://github.com/diffplug/spotless/issues/1750))
* Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644))
* Fix configuration cache failure when formatting proto files with Buf. ([#1779](https://github.com/diffplug/spotless/pull/1779))
* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788))
### Changes
* Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761))
* Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))

## [6.20.0] - 2023-07-17
### Added
Expand Down
2 changes: 1 addition & 1 deletion plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ spotless {
// optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style
// and/or reflow long strings
// and/or use custom group artifact (you probably don't need this)
googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
```

### palantir-java-format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.Directory;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.tasks.TaskProvider;
Expand Down Expand Up @@ -925,7 +926,8 @@ protected void setupTask(SpotlessTask task) {
steps.replaceAll(formatterStep -> formatterStep.filterByContent(OnMatch.EXCLUDE, targetExcludeContentPattern));
}
task.setSteps(steps);
task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> totalTarget));
Directory projectDir = getProject().getLayout().getProjectDirectory();
task.setLineEndingsPolicy(getProject().provider(() -> getLineEndings().createPolicy(projectDir.getAsFile(), () -> totalTarget)));
spotless.getRegisterDependenciesTask().hookSubprojectTask(task);
task.setupRatchet(getRatchetFrom() != null ? getRatchetFrom() : "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public class GoogleJavaFormatConfig {
String groupArtifact;
String style;
boolean reflowLongStrings;
boolean reorderImports;

GoogleJavaFormatConfig(String version) {
this.version = Objects.requireNonNull(version);
Expand Down Expand Up @@ -207,13 +208,19 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
return this;
}

public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
this.reorderImports = reorderImports;
return this;
}

private FormatterStep createStep() {
return GoogleJavaFormatStep.create(
groupArtifact,
version,
style,
provisioner(),
reflowLongStrings);
reflowLongStrings,
reorderImports);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ public class KotlinFormatExtension {
addStep(createStep());
}

public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException {
if (editorConfigFile == null) {
public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException {
if (editorConfigPath == null) {
this.editorConfigPath = null;
} else {
this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile));
File editorConfigFile = getProject().file(editorConfigPath);
if (!editorConfigFile.exists()) {
throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile);
}
this.editorConfigPath = FileSignature.signAsList(editorConfigFile);
}
replaceStep(createStep());
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
Expand Down Expand Up @@ -64,14 +65,14 @@ public void setEncoding(String encoding) {
this.encoding = Objects.requireNonNull(encoding);
}

protected final LiveCache<LineEnding.Policy> lineEndingsPolicy = createLive("lineEndingsPolicy");
protected final LiveCache<Provider<LineEnding.Policy>> lineEndingsPolicy = createLive("lineEndingsPolicy");

@Input
public LineEnding.Policy getLineEndingsPolicy() {
public Provider<LineEnding.Policy> getLineEndingsPolicy() {
return lineEndingsPolicy.get();
}

public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
public void setLineEndingsPolicy(Provider<LineEnding.Policy> lineEndingsPolicy) {
this.lineEndingsPolicy.set(lineEndingsPolicy);
}

Expand Down Expand Up @@ -185,7 +186,7 @@ String formatName() {
Formatter buildFormatter() {
return Formatter.builder()
.name(formatName())
.lineEndingsPolicy(lineEndingsPolicy.get())
.lineEndingsPolicy(lineEndingsPolicy.get().get())
.encoding(Charset.forName(encoding))
.rootDir(getProjectDir().get().getAsFile().toPath())
.steps(steps.get())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 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 Down Expand Up @@ -62,7 +62,7 @@ public BuildServiceParameters.None getParameters() {
private SpotlessTaskImpl createFormatTask(String name) {
SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class);
task.init(taskService);
task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
task.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy));
task.setTarget(Collections.singletonList(file));
return task;
}
Expand Down Expand Up @@ -100,7 +100,7 @@ private Bundle create(File... files) throws IOException {

private Bundle create(List<File> files) throws IOException {
Bundle bundle = new Bundle("underTest");
bundle.task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
bundle.task.setLineEndingsPolicy(bundle.project.provider(LineEnding.UNIX::createPolicy));
bundle.task.setTarget(files);
return bundle;
}
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 Down Expand Up @@ -35,7 +35,7 @@ class FormatTaskTest extends ResourceHarness {
void createTask() {
Project project = TestProvisioner.gradleProject(rootFolder());
spotlessTask = project.getTasks().create("spotlessTaskUnderTest", SpotlessTaskImpl.class);
spotlessTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
spotlessTask.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy));
spotlessTask.init(GradleIntegrationHarness.providerOf(new SpotlessTaskService() {
@Override
public BuildServiceParameters.None getParameters() {
Expand Down

0 comments on commit 130402a

Please sign in to comment.