Skip to content

Commit 130402a

Browse files
committed
Merge branch 'main' into patch-2
2 parents 14e4e62 + 4b1a75b commit 130402a

File tree

16 files changed

+114
-50
lines changed

16 files changed

+114
-50
lines changed

CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* 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))
15-
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
15+
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
1616
### Fixed
1717
* 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))
18-
* 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))
19-
* 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))
18+
* 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))
19+
* 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))
2020
### Changes
2121
* Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761))
2222
* Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))
23+
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
2324

2425
## [2.40.0] - 2023-07-17
2526
### Added

lib/src/googleJavaFormat/java/com/diffplug/spotless/glue/java/GoogleJavaFormatFormatterFunc.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import com.diffplug.spotless.FormatterFunc;
3131

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

3436
@Nonnull
@@ -42,10 +44,13 @@ public class GoogleJavaFormatFormatterFunc implements FormatterFunc {
4244

4345
private final boolean reflowStrings;
4446

45-
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings) {
47+
private final boolean reorderImports;
48+
49+
public GoogleJavaFormatFormatterFunc(@Nonnull String version, @Nonnull String style, boolean reflowStrings, boolean reorderImports) {
4650
this.version = Objects.requireNonNull(version);
4751
this.formatterStyle = Style.valueOf(Objects.requireNonNull(style));
4852
this.reflowStrings = reflowStrings;
53+
this.reorderImports = reorderImports;
4954

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

lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private GoogleJavaFormatStep() {}
3232

3333
private static final String DEFAULT_STYLE = "GOOGLE";
3434
private static final boolean DEFAULT_REFLOW_LONG_STRINGS = false;
35+
private static final boolean DEFAULT_REORDER_IMPORTS = false;
3536
static final String NAME = "google-java-format";
3637
static final String MAVEN_COORDINATE = "com.google.googlejavaformat:google-java-format";
3738

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

58-
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
5959
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) {
60+
return create(groupArtifact, version, style, provisioner, reflowLongStrings, false);
61+
}
62+
63+
/** Creates a step which formats everything - groupArtifact, code, import order, and unused imports - and optionally reflows long strings. */
64+
public static FormatterStep create(String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) {
6065
Objects.requireNonNull(groupArtifact, "groupArtifact");
6166
if (groupArtifact.chars().filter(ch -> ch == ':').count() != 1) {
6267
throw new IllegalArgumentException("groupArtifact must be in the form 'groupId:artifactId'");
@@ -65,7 +70,7 @@ public static FormatterStep create(String groupArtifact, String version, String
6570
Objects.requireNonNull(style, "style");
6671
Objects.requireNonNull(provisioner, "provisioner");
6772
return FormatterStep.createLazy(NAME,
68-
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings),
73+
() -> new State(NAME, groupArtifact, version, style, provisioner, reflowLongStrings, reorderImports),
6974
State::createFormat);
7075
}
7176

@@ -92,6 +97,10 @@ public static boolean defaultReflowLongStrings() {
9297
return DEFAULT_REFLOW_LONG_STRINGS;
9398
}
9499

100+
public static boolean defaultReorderImports() {
101+
return DEFAULT_REORDER_IMPORTS;
102+
}
103+
95104
static final class State implements Serializable {
96105
private static final long serialVersionUID = 1L;
97106

@@ -101,6 +110,7 @@ static final class State implements Serializable {
101110
final String version;
102111
final String style;
103112
final boolean reflowLongStrings;
113+
final boolean reorderImports;
104114

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

113123
State(String stepName, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
114-
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings);
124+
this(stepName, MAVEN_COORDINATE, version, style, provisioner, reflowLongStrings, DEFAULT_REORDER_IMPORTS);
115125
}
116126

117-
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings) throws Exception {
127+
State(String stepName, String groupArtifact, String version, String style, Provisioner provisioner, boolean reflowLongStrings, boolean reorderImports) throws Exception {
118128
JVM_SUPPORT.assertFormatterSupported(version);
119129
ModuleHelper.doOpenInternalPackagesIfRequired();
120130
this.jarState = JarState.from(groupArtifact + ":" + version, provisioner);
121131
this.stepName = stepName;
122132
this.version = version;
123133
this.style = style;
124134
this.reflowLongStrings = reflowLongStrings;
135+
this.reorderImports = reorderImports;
125136
}
126137

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

133144
return JVM_SUPPORT.suggestLaterVersionOnError(version, googleJavaFormatFormatterFunc);
134145
}

lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.Arrays;
2323
import java.util.List;
24+
import java.util.Objects;
2425
import java.util.regex.Pattern;
2526

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

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

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

82-
State(BufStep step, String exeAbsPath) {
83+
State(BufStep step, ForeignExe exeAbsPath) {
8384
this.version = step.version;
84-
this.args = Arrays.asList(exeAbsPath, "format");
85+
this.exe = Objects.requireNonNull(exeAbsPath);
8586
}
8687

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

9498
FormatterFunc.Closeable toFunc() {

plugin-gradle/CHANGES.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* 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))
8-
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
8+
* Support GJF own import order. ([#1780](https://github.com/diffplug/spotless/pull/1780))
99
### Fixed
10-
* 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))
11-
* 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))
10+
* 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))
11+
* 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))
12+
* Fix configuration cache failure when using LineEnding.GIT_ATTRIBUTES ([#1644](https://github.com/diffplug/spotless/issues/1644))
13+
* Fix configuration cache failure when formatting proto files with Buf. ([#1779](https://github.com/diffplug/spotless/pull/1779))
14+
* Check if EditorConfig file exist for Ktlint. ([#1788](https://github.com/diffplug/spotless/pull/1788))
1215
### Changes
1316
* Bump default `eslint` version to latest `8.31.0` -> `8.45.0` ([#1761](https://github.com/diffplug/spotless/pull/1761))
1417
* Bump default `prettier` version to latest (v2) `2.8.1` -> `2.8.8`. ([#1760](https://github.com/diffplug/spotless/pull/1760))
18+
* Add support for Eclipse 4.28 to the Groovy formatter. ([#1775](https://github.com/diffplug/spotless/pull/1775))
1519

1620
## [6.20.0] - 2023-07-17
1721
### Added

plugin-gradle/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ spotless {
206206
// optional: you can specify a specific version (>= 1.8) and/or switch to AOSP style
207207
// and/or reflow long strings
208208
// and/or use custom group artifact (you probably don't need this)
209-
googleJavaFormat('1.8').aosp().reflowLongStrings().groupArtifact('com.google.googlejavaformat:google-java-format')
209+
googleJavaFormat('1.8').aosp().reflowLongStrings().reorderImports(false).groupArtifact('com.google.googlejavaformat:google-java-format')
210210
```
211211

212212
### palantir-java-format

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.gradle.api.GradleException;
4141
import org.gradle.api.Project;
4242
import org.gradle.api.file.ConfigurableFileTree;
43+
import org.gradle.api.file.Directory;
4344
import org.gradle.api.file.FileCollection;
4445
import org.gradle.api.plugins.BasePlugin;
4546
import org.gradle.api.tasks.TaskProvider;
@@ -925,7 +926,8 @@ protected void setupTask(SpotlessTask task) {
925926
steps.replaceAll(formatterStep -> formatterStep.filterByContent(OnMatch.EXCLUDE, targetExcludeContentPattern));
926927
}
927928
task.setSteps(steps);
928-
task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> totalTarget));
929+
Directory projectDir = getProject().getLayout().getProjectDirectory();
930+
task.setLineEndingsPolicy(getProject().provider(() -> getLineEndings().createPolicy(projectDir.getAsFile(), () -> totalTarget)));
929931
spotless.getRegisterDependenciesTask().hookSubprojectTask(task);
930932
task.setupRatchet(getRatchetFrom() != null ? getRatchetFrom() : "");
931933
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public class GoogleJavaFormatConfig {
173173
String groupArtifact;
174174
String style;
175175
boolean reflowLongStrings;
176+
boolean reorderImports;
176177

177178
GoogleJavaFormatConfig(String version) {
178179
this.version = Objects.requireNonNull(version);
@@ -207,13 +208,19 @@ public GoogleJavaFormatConfig reflowLongStrings(boolean reflowLongStrings) {
207208
return this;
208209
}
209210

211+
public GoogleJavaFormatConfig reorderImports(boolean reorderImports) {
212+
this.reorderImports = reorderImports;
213+
return this;
214+
}
215+
210216
private FormatterStep createStep() {
211217
return GoogleJavaFormatStep.create(
212218
groupArtifact,
213219
version,
214220
style,
215221
provisioner(),
216-
reflowLongStrings);
222+
reflowLongStrings,
223+
reorderImports);
217224
}
218225
}
219226

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/KotlinExtension.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ public class KotlinFormatExtension {
8585
addStep(createStep());
8686
}
8787

88-
public KotlinFormatExtension setEditorConfigPath(Object editorConfigFile) throws IOException {
89-
if (editorConfigFile == null) {
88+
public KotlinFormatExtension setEditorConfigPath(Object editorConfigPath) throws IOException {
89+
if (editorConfigPath == null) {
9090
this.editorConfigPath = null;
9191
} else {
92-
this.editorConfigPath = FileSignature.signAsList(getProject().file(editorConfigFile));
92+
File editorConfigFile = getProject().file(editorConfigPath);
93+
if (!editorConfigFile.exists()) {
94+
throw new IllegalArgumentException("EditorConfig file does not exist: " + editorConfigFile);
95+
}
96+
this.editorConfigPath = FileSignature.signAsList(editorConfigFile);
9397
}
9498
replaceStep(createStep());
9599
return this;

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.gradle.api.file.DirectoryProperty;
2929
import org.gradle.api.file.FileCollection;
3030
import org.gradle.api.provider.Property;
31+
import org.gradle.api.provider.Provider;
3132
import org.gradle.api.tasks.Input;
3233
import org.gradle.api.tasks.InputFiles;
3334
import org.gradle.api.tasks.Internal;
@@ -64,14 +65,14 @@ public void setEncoding(String encoding) {
6465
this.encoding = Objects.requireNonNull(encoding);
6566
}
6667

67-
protected final LiveCache<LineEnding.Policy> lineEndingsPolicy = createLive("lineEndingsPolicy");
68+
protected final LiveCache<Provider<LineEnding.Policy>> lineEndingsPolicy = createLive("lineEndingsPolicy");
6869

6970
@Input
70-
public LineEnding.Policy getLineEndingsPolicy() {
71+
public Provider<LineEnding.Policy> getLineEndingsPolicy() {
7172
return lineEndingsPolicy.get();
7273
}
7374

74-
public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
75+
public void setLineEndingsPolicy(Provider<LineEnding.Policy> lineEndingsPolicy) {
7576
this.lineEndingsPolicy.set(lineEndingsPolicy);
7677
}
7778

@@ -185,7 +186,7 @@ String formatName() {
185186
Formatter buildFormatter() {
186187
return Formatter.builder()
187188
.name(formatName())
188-
.lineEndingsPolicy(lineEndingsPolicy.get())
189+
.lineEndingsPolicy(lineEndingsPolicy.get().get())
189190
.encoding(Charset.forName(encoding))
190191
.rootDir(getProjectDir().get().getAsFile().toPath())
191192
.steps(steps.get())

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/DiffMessageFormatterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 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.
@@ -62,7 +62,7 @@ public BuildServiceParameters.None getParameters() {
6262
private SpotlessTaskImpl createFormatTask(String name) {
6363
SpotlessTaskImpl task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name), SpotlessTaskImpl.class);
6464
task.init(taskService);
65-
task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
65+
task.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy));
6666
task.setTarget(Collections.singletonList(file));
6767
return task;
6868
}
@@ -100,7 +100,7 @@ private Bundle create(File... files) throws IOException {
100100

101101
private Bundle create(List<File> files) throws IOException {
102102
Bundle bundle = new Bundle("underTest");
103-
bundle.task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
103+
bundle.task.setLineEndingsPolicy(bundle.project.provider(LineEnding.UNIX::createPolicy));
104104
bundle.task.setTarget(files);
105105
return bundle;
106106
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

Lines changed: 2 additions & 2 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.
@@ -35,7 +35,7 @@ class FormatTaskTest extends ResourceHarness {
3535
void createTask() {
3636
Project project = TestProvisioner.gradleProject(rootFolder());
3737
spotlessTask = project.getTasks().create("spotlessTaskUnderTest", SpotlessTaskImpl.class);
38-
spotlessTask.setLineEndingsPolicy(LineEnding.UNIX.createPolicy());
38+
spotlessTask.setLineEndingsPolicy(project.provider(LineEnding.UNIX::createPolicy));
3939
spotlessTask.init(GradleIntegrationHarness.providerOf(new SpotlessTaskService() {
4040
@Override
4141
public BuildServiceParameters.None getParameters() {

0 commit comments

Comments
 (0)