diff --git a/CHANGES.md b/CHANGES.md index f4c2adf4ec..ed78ff047c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ You might be looking for: ### Version 1.11.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/)) +* Added generic format support for maven-plugin ([#209](https://github.com/diffplug/spotless/pull/209)) + ### Version 1.10.0 - February 15th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.10.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.10.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra))) * LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199)) diff --git a/README.md b/README.md index 38aa599cfc..b230f876b8 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,12 @@ function extra(className) { return '| [`' + className + '`](lib-extra/src/main/j output = [ '| Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) |', '| --------------------------------------------- | ------------- | ------------ | --------|', -lib('generic.EndWithNewlineStep') +'{{yes}} | {{no}} | {{no}} |', -lib('generic.IndentStep') +'{{yes}} | {{no}} | {{no}} |', +lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{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}} |', +lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} |', +lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}} | {{no}} |', extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |', lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} |', lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |', @@ -55,12 +55,12 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} --> | Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) | | --------------------------------------------- | ------------- | ------------ | --------| -| [`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.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :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: | +| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :+1: | :white_large_square: | +| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :+1: | :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: | :+1: | :white_large_square: | | [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: | diff --git a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java b/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java index 2ccdccefc5..e50a45b82e 100644 --- a/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java +++ b/lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java @@ -23,6 +23,9 @@ /** Simple step which checks for consistent indentation characters. */ public final class IndentStep { + + private static final int DEFAULT_NUM_SPACES_PER_TAB = 4; + // prevent direct instantiation private IndentStep() {} @@ -33,6 +36,11 @@ private T tabSpace(T tab, T space) { return this == TAB ? tab : space; } + /** Creates a step which will indent with the given type of whitespace, converting between tabs and spaces at the default ratio. */ + public FormatterStep create() { + return IndentStep.create(this, defaultNumSpacesPerTab()); + } + /** Synonym for {@link IndentStep#create(Type, int)}. */ public FormatterStep create(int numSpacesPerTab) { return IndentStep.create(this, numSpacesPerTab); @@ -128,4 +136,8 @@ String format(String raw) { private static boolean isSpaceOrTab(char c) { return c == ' ' || c == '\t'; } + + public static int defaultNumSpacesPerTab() { + return DEFAULT_NUM_SPACES_PER_TAB; + } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 77d5259881..7071dbd6ee 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -332,7 +332,7 @@ public void indentWithSpaces(int numSpacesPerTab) { /** Ensures that the files are indented using spaces. */ public void indentWithSpaces() { - indentWithSpaces(4); + addStep(IndentStep.Type.SPACE.create()); } /** Ensures that the files are indented using tabs. */ @@ -342,7 +342,7 @@ public void indentWithTabs(int tabToSpaces) { /** Ensures that the files are indented using tabs. */ public void indentWithTabs() { - indentWithTabs(4); + addStep(IndentStep.Type.TAB.create()); } abstract class LicenseHeaderConfig { diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 342b878115..4223d6c091 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -115,6 +115,60 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/* ``` + + +## Applying to custom sources + +By default, no Ant-Style include patterns are defined. Each element under `` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified. + +```xml + + + + + src/**/resources/**/*.properties + + + + + /* Licensed under Apache-2.0 */ + ${basedir}/license-header + + # + + + + + + + + + true + true + + 4 + + + + + + + + Say Hello to Mars + World + Mars + + + + + Say Hello to Mars from Regex + (Hello) W[a-z]{3}d + $1 Mars + + + +``` + ## Line endings and encodings (invisible stuff) diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 04fb5001cf..33bf1a7061 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -27,7 +27,6 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.FileUtils; @@ -38,6 +37,7 @@ import com.diffplug.spotless.Formatter; import com.diffplug.spotless.LineEnding; import com.diffplug.spotless.Provisioner; +import com.diffplug.spotless.maven.generic.Format; import com.diffplug.spotless.maven.generic.LicenseHeader; import com.diffplug.spotless.maven.java.Java; import com.diffplug.spotless.maven.scala.Scala; @@ -71,6 +71,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private LicenseHeader licenseHeader; + @Parameter + private Format format; + @Parameter private Java java; @@ -80,7 +83,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { protected abstract void process(List files, Formatter formatter) throws MojoExecutionException; @Override - public final void execute() throws MojoExecutionException, MojoFailureException { + public final void execute() throws MojoExecutionException { List formatterFactories = getFormatterFactories(); for (FormatterFactory formatterFactory : formatterFactories) { @@ -127,7 +130,7 @@ private FormatterConfig getFormatterConfig() { } private List getFormatterFactories() { - return Stream.of(java, scala) + return Stream.of(format, java, scala) .filter(Objects::nonNull) .collect(toList()); } diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java new file mode 100644 index 0000000000..4f042962a4 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EndWithNewline.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.EndWithNewlineStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class EndWithNewline implements FormatterStepFactory { + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + return EndWithNewlineStep.create(); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java new file mode 100644 index 0000000000..10f1ba505c --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Format.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import java.util.Collections; +import java.util.Set; + +import com.diffplug.spotless.maven.FormatterFactory; + +public class Format extends FormatterFactory { + + @Override + public Set defaultIncludes() { + return Collections.emptySet(); + } + + @Override + public String licenseHeaderDelimiter() { + // do not specify a default delimiter + return null; + } + + public void addEndWithNewline(EndWithNewline endWithNewline) { + addStepFactory(endWithNewline); + } + + public void addIndent(Indent indent) { + addStepFactory(indent); + } + + public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) { + addStepFactory(trimTrailingWhitespace); + } + + public void addReplace(Replace replace) { + addStepFactory(replace); + } + + public void addReplaceRegex(ReplaceRegex replaceRegex) { + addStepFactory(replaceRegex); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java new file mode 100644 index 0000000000..1d1b6925e8 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.IndentStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class Indent implements FormatterStepFactory { + + @Parameter + private boolean tabs; + + @Parameter + private boolean spaces; + + @Parameter + private Integer spacesPerTab; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + + if (spacesPerTab == null) { + spacesPerTab = IndentStep.defaultNumSpacesPerTab(); + } + + if (spaces ^ tabs) { + if (spaces) { + return IndentStep.create(IndentStep.Type.SPACE, spacesPerTab); + } else { + return IndentStep.create(IndentStep.Type.TAB, spacesPerTab); + } + } else { + throw new IllegalArgumentException("Must specify exactly one of 'spaces' or 'tabs'."); + } + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java new file mode 100644 index 0000000000..947ceee8e2 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java @@ -0,0 +1,44 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.ReplaceStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class Replace implements FormatterStepFactory { + + @Parameter + private String name; + + @Parameter + private String search; + + @Parameter + private String replacement; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + if (name == null || search == null || replacement == null) { + throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'."); + } + + return ReplaceStep.create(name, search, replacement); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java new file mode 100644 index 0000000000..243f14220e --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java @@ -0,0 +1,44 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.ReplaceRegexStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class ReplaceRegex implements FormatterStepFactory { + + @Parameter + private String name; + + @Parameter + private String searchRegex; + + @Parameter + private String replacement; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + if (name == null || searchRegex == null || replacement == null) { + throw new IllegalArgumentException("Must specify 'name', 'searchRegex' and 'replacement'."); + } + + return ReplaceRegexStep.create(name, searchRegex, replacement); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java new file mode 100644 index 0000000000..3993f29705 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java @@ -0,0 +1,29 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.generic.TrimTrailingWhitespaceStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class TrimTrailingWhitespace implements FormatterStepFactory { + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig config) { + return TrimTrailingWhitespaceStep.create(); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java index f15444f75f..a8c0078fb9 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java @@ -20,9 +20,9 @@ import java.util.Set; -import com.diffplug.spotless.maven.FormatterFactory; +import com.diffplug.spotless.maven.generic.Format; -public class Java extends FormatterFactory { +public class Java extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/java/**/*.java", "src/test/java/**/*.java")); private static final String LICENSE_HEADER_DELIMITER = "package "; diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java index 0f2034e883..dbaada1cad 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/scala/Scala.java @@ -20,9 +20,9 @@ import java.util.Set; -import com.diffplug.spotless.maven.FormatterFactory; +import com.diffplug.spotless.maven.generic.Format; -public class Scala extends FormatterFactory { +public class Scala extends Format { private static final Set DEFAULT_INCLUDES = unmodifiableSet(newHashSet("src/main/scala/**/*.scala", "src/test/scala/**/*.scala", "src/main/scala/**/*.sc", "src/test/scala/**/*.sc")); private static final String LICENSE_HEADER_DELIMITER = "package "; diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java index 524c03c29a..9547113794 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationTest.java @@ -79,6 +79,10 @@ private File copy(String path) throws IOException { return target.toFile(); } + protected void writePomWithFormatSteps(String... steps) throws IOException { + writePom(groupWithSteps("format", including("src/**/java/**/*.java"), steps)); + } + protected void writePomWithJavaSteps(String... steps) throws IOException { writePom(groupWithSteps("java", steps)); } @@ -137,11 +141,24 @@ private static String getSystemProperty(String name) { return value; } + private static String[] groupWithSteps(String group, String[] includes, String... steps) { + String[] result = new String[steps.length + includes.length + 2]; + result[0] = "<" + group + ">"; + System.arraycopy(includes, 0, result, 1, includes.length); + System.arraycopy(steps, 0, result, includes.length + 1, steps.length); + result[result.length - 1] = ""; + return result; + } + private static String[] groupWithSteps(String group, String... steps) { - String[] result = new String[steps.length + 2]; - result[0] = '<' + group + '>'; - System.arraycopy(steps, 0, result, 1, steps.length); - result[result.length - 1] = "'; + return groupWithSteps(group, new String[]{}, steps); + } + + private static String[] including(String... include) { + String[] result = new String[include.length + 2]; + result[0] = ""; + System.arraycopy(include, 0, result, 1, include.length); + result[result.length - 1] = ""; return result; } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java new file mode 100644 index 0000000000..2215b8899f --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/EndWithNewlineTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class EndWithNewlineTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + ""); + runTest(); + } + + @Test + public void fromContentWithSelfclosingTag() throws Exception { + writePomWithFormatSteps( + ""); + runTest(); + } + + private void runTest() throws Exception { + String noTrailingNewline = "public class Java {}"; + String hasTrailingNewline = noTrailingNewline + "\n"; + setFile("src/main/java/test.java").toContent(noTrailingNewline); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("src/main/java/test.java").hasContent(hasTrailingNewline); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java new file mode 100644 index 0000000000..915c66c59f --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/IndentTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class IndentTest extends MavenIntegrationTest { + + @Test + public void fromContentToTabs() throws Exception { + writePomWithFormatSteps( + "", + " true", + ""); + runToTabTest(); + } + + @Test + public void fromContentToSpaces() throws Exception { + writePomWithFormatSteps( + "", + " true", + ""); + runToSpacesTest(); + } + + private void runToTabTest() throws Exception { + runTest("indent/IndentedWithSpace.test", "indent/IndentedWithTab.test"); + } + + private void runToSpacesTest() throws Exception { + runTest("indent/IndentedWithTab.test", "indent/IndentedWithSpace.test"); + } + + private void runTest(String source, String target) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toResource(source); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource(target); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java index c0e7f784d2..957bbd2e5c 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java @@ -15,8 +15,6 @@ */ package com.diffplug.spotless.maven.generic; -import static org.assertj.core.api.Assertions.assertThat; - import org.junit.Test; import com.diffplug.spotless.maven.MavenIntegrationTest; @@ -25,7 +23,7 @@ public class LicenseHeaderTest extends MavenIntegrationTest { private static final String KEY_LICENSE = "license/TestLicense"; @Test - public void fromFile() throws Exception { + public void fromFileJava() throws Exception { setFile("license.txt").toResource(KEY_LICENSE); writePomWithJavaSteps( "", @@ -35,7 +33,7 @@ public void fromFile() throws Exception { } @Test - public void fromContent() throws Exception { + public void fromContentJava() throws Exception { writePomWithJavaSteps( "", " ", @@ -57,10 +55,34 @@ public void fromFileGlobal() throws Exception { runTest(); } + @Test + public void fromFileFormat() throws Exception { + setFile("license.txt").toResource(KEY_LICENSE); + writePomWithFormatSteps( + "", + " ${basedir}/license.txt", + " package", + ""); + runTest(); + } + + @Test + public void fromContentFormat() throws Exception { + writePomWithFormatSteps( + "", + " ", + "// If you can't trust a man's word", + "// Does it help to have it in writing?", + " ", + " package", + ""); + runTest(); + } + private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("license/MissingLicense.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("license/MissingLicense.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - String actual = read("src/main/java/test.java"); - assertThat(actual).isEqualTo(getTestResource("license/HasLicense.test")); + assertFile(path).sameAsResource("license/HasLicense.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java new file mode 100644 index 0000000000..8d23527975 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LineEndingsTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class LineEndingsTest extends MavenIntegrationTest { + + @Test + public void fromContentToWindows() throws Exception { + writePomWithFormatSteps( + "WINDOWS"); + runToWindowsTest(); + } + + @Test + public void fromContentToUnix() throws Exception { + writePomWithFormatSteps( + "UNIX"); + runToUnixTest(); + } + + private void runToWindowsTest() throws Exception { + runTest(getClassWithLineEndings("\n"), getClassWithLineEndings("\r\n")); + } + + private void runToUnixTest() throws Exception { + runTest(getClassWithLineEndings("\r\n"), getClassWithLineEndings("\n")); + } + + private void runTest(String sourceContent, String targetContent) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toContent(sourceContent); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).hasContent(targetContent); + } + + private String getClassWithLineEndings(String lineEnding) { + return "public class Java {" + lineEnding + + " public static void main(String[] args) {" + lineEnding + + " System.out.println(\"hello\");" + lineEnding + + " }" + lineEnding + + "}"; + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java new file mode 100644 index 0000000000..204b0e57a4 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceRegexTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class ReplaceRegexTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + " Greetings to Mars", + " (hello) w[a-z]{3}d", + " $1 mars", + ""); + + runTest("hello world", "hello mars"); + } + + private void runTest(String sourceContent, String targetContent) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toContent(sourceContent); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).hasContent(targetContent); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java new file mode 100644 index 0000000000..04ae5ae6d0 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/ReplaceTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class ReplaceTest extends MavenIntegrationTest { + + @Test + public void fromContent() throws Exception { + writePomWithFormatSteps( + "", + " Greetings to Mars", + " World", + " Mars", + ""); + runTest("Hello World", "Hello Mars"); + } + + private void runTest(String sourceContent, String targetContent) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toContent(sourceContent); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).hasContent(targetContent); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java new file mode 100644 index 0000000000..b8f930cf12 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespaceTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2016 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.generic; + +import org.junit.Test; + +import com.diffplug.spotless.maven.MavenIntegrationTest; + +public class TrimTrailingWhitespaceTest extends MavenIntegrationTest { + + @Test + public void fromContentToTabs() throws Exception { + writePomWithFormatSteps( + ""); + + String target = "This line ends with whitespaces"; + String source = target + " "; + runTest(source, target); + } + + private void runTest(String sourceContent, String targetContent) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toContent(sourceContent); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).hasContent(targetContent); + } + +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java index f97dc2d664..af9f06e15d 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/EclipseFormatStepTest.java @@ -30,8 +30,9 @@ public void testEclipse() throws Exception { ""); setFile("formatter.xml").toResource("java/eclipse/formatter.xml"); - setFile("src/main/java/test.java").toResource("java/eclipse/JavaCodeUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/eclipse/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/eclipse/JavaCodeFormatted.test"); + assertFile(path).sameAsResource("java/eclipse/JavaCodeFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java index e32fde7e00..137cf4c7fc 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/GoogleJavaFormatTest.java @@ -27,9 +27,7 @@ public void specificVersionDefaultStyle() throws Exception { " 1.2", ""); - setFile("src/main/java/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/googlejavaformat/JavaCodeFormatted.test"); + runTest("java/googlejavaformat/JavaCodeFormatted.test"); } @Test @@ -40,8 +38,13 @@ public void specificVersionSpecificStyle() throws Exception { " ", ""); - setFile("src/main/java/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test"); + runTest("java/googlejavaformat/JavaCodeFormattedAOSP.test"); + } + + private void runTest(String targetResource) throws Exception { + String path = "src/main/java/test.java"; + setFile(path).toResource("java/googlejavaformat/JavaCodeUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/googlejavaformat/JavaCodeFormattedAOSP.test"); + assertFile(path).sameAsResource(targetResource); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java index 99a5bb219d..aa9db93793 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/ImportOrderTest.java @@ -40,8 +40,9 @@ public void order() throws Exception { } private void runTest() throws Exception { - setFile("src/main/java/test.java").toResource("java/importsorter/JavaCodeUnsortedImports.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/importsorter/JavaCodeUnsortedImports.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/importsorter/JavaCodeSortedImports.test"); + assertFile(path).sameAsResource("java/importsorter/JavaCodeSortedImports.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java index 9ef2134582..4cad34ef90 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java @@ -25,8 +25,9 @@ public class RemoveUnusedImportsStepTest extends MavenIntegrationTest { public void testRemoveUnusedInports() throws Exception { writePomWithJavaSteps(""); - setFile("src/main/java/test.java").toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); + String path = "src/main/java/test.java"; + setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/java/test.java").sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); + assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java index d070a92f0f..50be37addb 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/scala/ScalafmtTest.java @@ -24,21 +24,25 @@ public class ScalafmtTest extends MavenIntegrationTest { public void testScalafmtWithDefaultConfig() throws Exception { writePomWithScalaSteps(""); - setFile("src/main/scala/test.scala").toResource("scala/scalafmt/basic.dirty"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/scala/test.scala").sameAsResource("scala/scalafmt/basic.clean"); + runTest("scala/scalafmt/basic.clean"); } @Test public void testScalafmtWithCustomConfig() throws Exception { + setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); + writePomWithScalaSteps( "", " ${project.basedir}/scalafmt.conf", ""); - setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); - setFile("src/main/scala/test.scala").toResource("scala/scalafmt/basic.dirty"); + runTest("scala/scalafmt/basic.cleanWithCustomConf"); + } + + private void runTest(String s) throws Exception { + String path = "src/main/scala/test.scala"; + setFile(path).toResource("scala/scalafmt/basic.dirty"); mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile("src/main/scala/test.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + assertFile(path).sameAsResource(s); } }