Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of Spotless Maven plugin #188

Merged
merged 93 commits into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
5ff5581
Initial version of Spotless Maven plugin
lutovich Jan 14, 2018
9956473
Switched maven plugin over to Formatter.
nedtwigg Jan 17, 2018
c219ef1
Improved config handling in maven plugin
lutovich Jan 17, 2018
a5a6cc9
Removed dependency on maven-core
lutovich Jan 17, 2018
1d63aa2
Simplified maven plugin build process
lutovich Jan 17, 2018
af677ab
Set maven plugin version from gradle props
lutovich Jan 17, 2018
b059d53
Added "aspiring" docs for the maven plugin.
nedtwigg Jan 19, 2018
c0a2fa6
Merge branch 'master' into maven-plugin
nedtwigg Jan 19, 2018
a431b71
Switch indent from tab to 2-space for better GitHub render.
nedtwigg Jan 19, 2018
3639269
Update feature matrix for current maven ambitions.
nedtwigg Jan 19, 2018
240f87c
Fixed some typos in README.
nedtwigg Jan 19, 2018
8570064
Made maven plugin build use template for pom
lutovich Jan 19, 2018
75f0204
Fixed code formatting and build dir reference
lutovich Jan 19, 2018
40da7f5
Improve maven plugin configuration
lutovich Jan 21, 2018
3696310
Changed maven artifactId to spotless-maven-plugin, but kept project f…
nedtwigg Jan 21, 2018
49c2753
Added an explicit steps container inside each "java" configuration.
nedtwigg Jan 21, 2018
35da59a
Change task names from "spotless-{check|apply}" to just "check|apply".
nedtwigg Jan 21, 2018
fd38755
First cut at an integration test system for the maven plugin.
nedtwigg Jan 21, 2018
3864178
Use ByteStreams.copy to reduce boilerplate.
nedtwigg Jan 22, 2018
d54b183
Made maven plugin ITs runnable
lutovich Jan 22, 2018
ddbf0eb
Better package name for maven plugin
lutovich Jan 22, 2018
4754a8f
Include stdout and stderr in maven plugin test failures
lutovich Jan 22, 2018
88bcd19
Assert on exit code, not stderr
lutovich Jan 22, 2018
c84becf
Refactored DiffMessageFormatter out of gradle and into lib-extra.
nedtwigg Jan 23, 2018
012b118
Added "check" to the maven plugin, but not properly integrated.
nedtwigg Jan 23, 2018
0c01e64
Made maven plugin use all dependencies from local repo
lutovich Jan 23, 2018
c43ed45
Consistent naming of mustache variables
lutovich Jan 23, 2018
66bb67e
Use correct root dir for DiffMessageFormatter
lutovich Jan 24, 2018
31d5357
Fix maven provisioner to resolve transitive dependencies
lutovich Jan 24, 2018
4b032eb
Support RemoveUnusedImports in maven plugin
lutovich Jan 24, 2018
4cf839a
Use correct artifactId for maven plugin in tests
lutovich Jan 24, 2018
622d2df
Extracted check MOJO
lutovich Jan 24, 2018
e759a0b
Removed DiffMessageFormatter::rootDir() because Formatter has it alre…
nedtwigg Jan 27, 2018
0185af6
The local dependencies repo now contains only our local deps, and it …
nedtwigg Jan 27, 2018
0dba10c
Forced buildMavenPlugin to act like the standard jar task, so that pu…
nedtwigg Jan 27, 2018
9bb5bbc
Added a GoogleJavaFormat step.
nedtwigg Jan 27, 2018
3189d7d
Added an ImportOrder step.
nedtwigg Jan 27, 2018
6568440
Reorganized packages.
nedtwigg Jan 27, 2018
21f8d00
Make the parent FormatterFactory available to the FormatterStepFactory.
nedtwigg Jan 27, 2018
8ebdbfc
Refactored non-java stuff out of FormatterFactory.
nedtwigg Jan 27, 2018
24f7096
Added a License step.
nedtwigg Jan 27, 2018
5e9fce6
Refactor formatter and step configuration
lutovich Jan 27, 2018
d95f76d
Allow multiple file extension for formatters
lutovich Jan 27, 2018
4f2952c
Made spotless mojo scan for files starting at basedir
lutovich Jan 28, 2018
1d82ba9
Added scalafmt step
lutovich Jan 28, 2018
4b34991
Fixed license header tests
lutovich Jan 28, 2018
7db3759
Mark check and apply MOJOs as thread safe
lutovich Jan 28, 2018
d8258bd
Removed need for "<steps>" config element
lutovich Jan 30, 2018
a8de57d
Added property to skip 'spotless:check'
lutovich Jan 30, 2018
3ca2bf7
Added javadoc to check and apply MOJOs
lutovich Jan 30, 2018
807291e
Bind 'spotless:check' to verify phase by default
lutovich Jan 30, 2018
48cb4c9
Run maven plugin tests with full stacktrace logging
lutovich Jan 30, 2018
08f90d2
Use mvnw to build maven plugin
lutovich Jan 31, 2018
5ccbd09
Cache the .m2 directory on Travis.
nedtwigg Feb 1, 2018
7df6191
Use mvnw to run tests.
nedtwigg Feb 1, 2018
a676bc4
Moved mvnw out of src/main/resources into the project route.
nedtwigg Feb 1, 2018
d4cddc0
Revert "Moved mvnw out of src/main/resources into the project route."
nedtwigg Feb 1, 2018
b9a4fa4
Added task graph vis, which shows problem in build.
nedtwigg Feb 1, 2018
bafc30c
Fixed the problem in the build.
nedtwigg Feb 1, 2018
9d3ea05
Moved mvnw and the template poms into src/test/resources.
nedtwigg Feb 1, 2018
e438881
Only run the maven tests on CI, for faster debugging. Also format fix.
nedtwigg Feb 1, 2018
f8bf47d
mvnw now works on Windows.
nedtwigg Feb 2, 2018
d2170fe
Disable the apache-snapshots repo.
nedtwigg Feb 2, 2018
da36b36
Another attempt at disabling the apache-snapshots repo.
nedtwigg Feb 2, 2018
07b6d68
Update docs for skipping spotless:check.
nedtwigg Feb 2, 2018
243035e
Try to figure out what travis is using for $HOME/.m2/settings.xml
nedtwigg Feb 2, 2018
ea6ebb5
Attempt #2...
nedtwigg Feb 2, 2018
6d660c6
Instead of snapshot-repo-whackamole, I'm just setting a restrictive s…
nedtwigg Feb 2, 2018
aef8b32
Another shot at a settings.xml - none at all.
nedtwigg Feb 2, 2018
21f163b
Force maven tests to run in "offline" mode.
nedtwigg Feb 2, 2018
e7cea18
Run Maven with debug logging and use "special" version
lutovich Feb 4, 2018
da77c39
Set version back to normal
lutovich Feb 4, 2018
34ff0e9
Local repo as repository, not only pluginRepository
lutovich Feb 4, 2018
7de4e1a
Do not use offline mode for maven plugin tests
lutovich Feb 4, 2018
8e77cf7
Force snapshot updates in maven plugin tests
lutovich Feb 4, 2018
fcebff7
Install maven plugin to local repo during build
lutovich Feb 4, 2018
53d8013
Turn off debug logging and cleanup local repo cmd
lutovich Feb 4, 2018
d52a448
Run all tests on CI
lutovich Feb 4, 2018
079e9a2
Removed repository declaration from POMs
lutovich Feb 4, 2018
3fdc785
Less verbose download logging when building maven plugin
lutovich Feb 4, 2018
95c7fc9
Cache the localMavenRepository on Travis, but delete the com.diffplug…
nedtwigg Feb 5, 2018
bd8a3b9
Merge branch 'master' into maven-plugin
nedtwigg Feb 5, 2018
d215905
Added support for google-java-format's style parameter.
nedtwigg Feb 5, 2018
2345cbd
Added `~/.m2` as a cached directory, because stuff gets downloaded fo…
nedtwigg Feb 5, 2018
0f9ff8e
Added a test for GoogleJavaFormat's style parameter.
nedtwigg Feb 5, 2018
9cee266
Gah! Fumble-fingers, I forgot the build.
nedtwigg Feb 5, 2018
15e65a0
Allow custom file includes and excludes
lutovich Feb 10, 2018
740317c
Added docs about includes & excludes configuration
lutovich Feb 10, 2018
a154f59
Fix serialization warning.
nedtwigg Feb 10, 2018
49987d7
Update README because maven-plugin is integrated with scala.
nedtwigg Feb 10, 2018
81477a6
Add @lutovich as first author of the maven plugin for the mavencentra…
nedtwigg Feb 10, 2018
a934a73
Fixed LicenseHeader when configured globally
lutovich Feb 11, 2018
4162569
Moved license header config handling to FormatterFactory
lutovich Feb 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
language: java
jdk:
- oraclejdk8
script: "./.ci/ci.sh"
install: true
script:
- ./.ci/ci.sh
before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
- rm -fr plugin-maven/build/localMavenRepository/com/diffplug/spotless/
cache:
directories:
- $HOME/.gradle/caches/
- $HOME/.gradle/wrapper/
- $HOME/.m2/
- plugin-maven/build/localMavenRepository/
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ output = [
'| --------------------------------------------- | ------------- | ------------ | --------|',
lib('generic.EndWithNewlineStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.IndentStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.LicenseHeaderStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('generic.ReplaceRegexStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.ReplaceStep') +'{{yes}} | {{no}} | {{no}} |',
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{no}} | {{no}} |',
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.ImportOrderStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{no}} | {{no}} |',
extra('java.EclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{no}} |',
extra('java.EclipseFormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('kotlin.KtLintStep') +'{{yes}} | {{no}} | {{no}} |',
lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |',
lib('scala.ScalaFmtStep') +'{{yes}} | {{no}} | {{no}} |',
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |',
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
'| [(Your FormatterStep here)](https://github.com/nedtwigg/spotless/blob/markdown-preview-temp/CONTRIBUTING.md#how-to-add-a-new-formatterstep) | {{no}} | {{no}} | {{no}} |',
'| Fast up-to-date checking | {{yes}} | {{no}} | {{no}} |',
Expand All @@ -57,18 +57,18 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}
| --------------------------------------------- | ------------- | ------------ | --------|
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :white_large_square: |
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :white_large_square: |
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: |
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :white_large_square: |
| [`java.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java) | :+1: | :+1: | :white_large_square: |
| [`kotlin.KtLintStep`](lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: |
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
| [(Your FormatterStep here)](https://github.com/nedtwigg/spotless/blob/markdown-preview-temp/CONTRIBUTING.md#how-to-add-a-new-formatterstep) | :white_large_square: | :white_large_square: | :white_large_square: |
| Fast up-to-date checking | :+1: | :white_large_square: | :white_large_square: |
Expand All @@ -77,6 +77,7 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}

## Acknowledgements

* Thanks to [Konstantin Lutovich](https://github.com/lutovich) for [implementing the maven plugin](https://github.com/diffplug/spotless/pull/188).
* Thanks to [Baptiste Mesta](https://github.com/baptistemesta) for
+ porting the DBeaver formatter to Spotless, and thanks to [DBeaver](https://dbeaver.jkiss.org/) and [its authors](https://github.com/serge-rider/dbeaver/graphs/contributors) for their excellent SQL formatter.
+ making license headers date-aware [#180](https://github.com/diffplug/spotless/pull/180)
Expand Down
9 changes: 8 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ artifactIdTestLib=spotless-testlib

versionMaven=1.0.0-SNAPSHOT
stableMaven=unreleased
artifactIdMaven=spotless-plugin-maven
# naming convention '-maven-plugin' allows mvn 'spotless:check' instead of 'spotless-plugin-maven:check'
artifactIdMaven=spotless-maven-plugin

versionGradle=3.9.0-SNAPSHOT
stableGradle=3.8.0
Expand All @@ -31,3 +32,9 @@ VER_PEGDOWN_DOCLET=1.3
VER_DURIAN=1.2.0
VER_JUNIT=4.12
VER_ASSERTJ=3.5.2
VER_MOCKITO=2.13.0

# Used for Maven Plugin
VER_MAVEN_API=3.0
VER_ECLIPSE_AETHER=1.1.0
VER_MUSTACHE=0.9.5
7 changes: 7 additions & 0 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ model {
}
}
developers {
if (project.ext.artifactId == 'spotless-plugin-maven') {
developer {
id 'lutovich'
name 'Konstantin Lutovich'
email 'konstantin.lutovich@neotechnology.com'
}
}
developer {
id 'nedtwigg'
name 'Ned Twigg'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;
package com.diffplug.spotless.extra.integration;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;

import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.EditList;
Expand All @@ -31,48 +31,89 @@
import org.eclipse.jgit.diff.RawTextComparator;

import com.diffplug.common.base.CharMatcher;
import com.diffplug.common.base.Errors;
import com.diffplug.common.base.Preconditions;
import com.diffplug.common.base.Splitter;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.PaddedCell;

/** Formats the messages of failed spotlessCheck invocations with a nice diff message. */
final class DiffMessageFormatter {
private static final int MAX_CHECK_MESSAGE_LINES = 50;
static final int MAX_FILES_TO_LIST = 10;

static String messageFor(SpotlessTask task, Formatter formatter, List<File> problemFiles) throws IOException {
DiffMessageFormatter diffFormater = new DiffMessageFormatter(task, formatter, problemFiles);
return "The following files had format violations:\n"
+ diffFormater.buffer
+ "Run 'gradlew "
+ SpotlessPlugin.EXTENSION
+ SpotlessPlugin.APPLY
+ "' to fix these violations.";
public final class DiffMessageFormatter {
public static Builder builder() {
return new Builder();
}

public static class Builder {
private Builder() {}

private String runToFix;
private boolean isPaddedCell;
private Formatter formatter;
private List<File> problemFiles;

/** "Run 'gradlew spotlessApply' to fix these violations." */
public Builder runToFix(String runToFix) {
this.runToFix = Objects.requireNonNull(runToFix);
return this;
}

public Builder isPaddedCell(boolean isPaddedCell) {
this.isPaddedCell = isPaddedCell;
return this;
}

public Builder formatter(Formatter formatter) {
this.formatter = Objects.requireNonNull(formatter);
return this;
}

public Builder problemFiles(List<File> problemFiles) {
this.problemFiles = Objects.requireNonNull(problemFiles);
Preconditions.checkArgument(!problemFiles.isEmpty(), "cannot be empty");
return this;
}

/** Returns the error message. */
public String getMessage() {
try {
Objects.requireNonNull(runToFix, "runToFix");
Objects.requireNonNull(formatter, "formatter");
Objects.requireNonNull(problemFiles, "problemFiles");
DiffMessageFormatter diffFormater = new DiffMessageFormatter(this);
return "The following files had format violations:\n"
+ diffFormater.buffer
+ runToFix;
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

String relativePath(File file) {
return formatter.getRootDir().relativize(file.toPath()).toString();
}
}

private static final int MAX_CHECK_MESSAGE_LINES = 50;
public static final int MAX_FILES_TO_LIST = 10;

private final StringBuilder buffer = new StringBuilder(MAX_CHECK_MESSAGE_LINES * 64);
private int numLines = 0;

private DiffMessageFormatter(SpotlessTask task, Formatter formatter, List<File> problemFiles) throws IOException {
Preconditions.checkArgument(!problemFiles.isEmpty(), "Problem files must not be empty");

Path rootDir = task.getProject().getRootDir().toPath();
ListIterator<File> problemIter = problemFiles.listIterator();
private DiffMessageFormatter(Builder builder) throws IOException {
ListIterator<File> problemIter = builder.problemFiles.listIterator();
while (problemIter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) {
File file = problemIter.next();
addFile(rootDir.relativize(file.toPath()) + "\n" +
DiffMessageFormatter.diff(task, formatter, file));
addFile(builder.relativePath(file) + "\n" + DiffMessageFormatter.diff(builder, file));
}
if (problemIter.hasNext()) {
int remainingFiles = problemFiles.size() - problemIter.nextIndex();
int remainingFiles = builder.problemFiles.size() - problemIter.nextIndex();
if (remainingFiles >= MAX_FILES_TO_LIST) {
buffer.append("Violations also present in ").append(remainingFiles).append(" other files.\n");
} else {
buffer.append("Violations also present in:\n");
while (problemIter.hasNext()) {
addIntendedLine(NORMAL_INDENT, rootDir.relativize(problemIter.next().toPath()).toString());
addIntendedLine(NORMAL_INDENT, builder.relativePath(problemIter.next()));
}
}
}
Expand Down Expand Up @@ -126,19 +167,19 @@ private void addIntendedLine(String indent, String line) {
* look like if formatted using the given formatter. Does not end with any newline
* sequence (\n, \r, \r\n).
*/
private static String diff(SpotlessTask task, Formatter formatter, File file) throws IOException {
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
private static String diff(Builder builder, File file) throws IOException {
String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding());
String rawUnix = LineEnding.toUnix(raw);
String formattedUnix;
if (task.isPaddedCell()) {
formattedUnix = PaddedCell.check(formatter, file, rawUnix).canonical();
if (builder.isPaddedCell) {
formattedUnix = PaddedCell.check(builder.formatter, file, rawUnix).canonical();
} else {
formattedUnix = formatter.compute(rawUnix, file);
formattedUnix = builder.formatter.compute(rawUnix, file);
}

if (rawUnix.equals(formattedUnix)) {
// the formatting is fine, so it's a line-ending issue
String formatted = formatter.computeLineEndings(formattedUnix, file);
String formatted = builder.formatter.computeLineEndings(formattedUnix, file);
return diffWhitespaceLineEndings(raw, formatted, false, true);
} else {
return diffWhitespaceLineEndings(rawUnix, formattedUnix, true, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Integration code for plugins.
*/
@ParametersAreNonnullByDefault
@ReturnValuesAreNonnullByDefault
package com.diffplug.spotless.extra.integration;

import javax.annotation.ParametersAreNonnullByDefault;

import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static com.diffplug.gradle.spotless.PluginGradlePreconditions.requireElementsNonNull;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.nio.file.Files;
Expand All @@ -45,6 +44,7 @@
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.PaddedCell;
import com.diffplug.spotless.PaddedCellBulk;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class SpotlessTask extends DefaultTask {
// set by SpotlessExtension, but possibly overridden by FormatExtension
Expand Down Expand Up @@ -164,7 +164,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
Formatter formatter = Formatter.builder()
.lineEndingsPolicy(lineEndingsPolicy)
.encoding(Charset.forName(encoding))
.rootDir(getProject().getProjectDir().toPath())
.rootDir(getProject().getRootDir().toPath())
.steps(steps)
.exceptionPolicy(exceptionPolicy)
.build();
Expand Down Expand Up @@ -279,7 +279,12 @@ private void check(Formatter formatter, List<File> outOfDate) throws Exception {
}

/** Returns an exception which indicates problem files nicely. */
GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) throws IOException {
return new GradleException(DiffMessageFormatter.messageFor(this, formatter, problemFiles));
GradleException formatViolationsFor(Formatter formatter, List<File> problemFiles) {
return new GradleException(DiffMessageFormatter.builder()
.runToFix("Run 'gradlew spotlessApply' to fix these violations.")
.isPaddedCell(paddedCell)
.formatter(formatter)
.problemFiles(problemFiles)
.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.ResourceHarness;
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class DiffMessageFormatterTest extends ResourceHarness {
private SpotlessTask create(File... files) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@
import com.diffplug.spotless.ResourceHarness;

public class GradleIntegrationTest extends ResourceHarness {
/**
* Each test gets its own temp folder, and we create a gradle
* build there and run it.
*
* Because those test folders don't have a .gitattributes file,
* git (on windows) will default to \r\n. So now if you read a
* test file from the spotless test resources, and compare it
* to a build result, the line endings won't match.
*
* By sticking this .gitattributes file into the test directory,
* we ensure that the default Spotless line endings policy of
* GIT_ATTRIBUTES will use \n, so that tests match the test
* resources on win and linux.
*/
@Before
public void gitAttributes() throws IOException {
write(".gitattributes", "* text eol=lf");
Expand Down
Loading