-
Notifications
You must be signed in to change notification settings - Fork 459
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
add support for missing generic steps (closes #207) #209
Changes from 5 commits
0a3d542
98bfe1e
0ab7ca0
23a1d1b
2256c73
c53962a
b2eee6f
9047612
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,60 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/* | |
</configuration> | ||
``` | ||
|
||
<a name="format"></a> | ||
|
||
## Applying to custom sources | ||
|
||
By default, no Ant-Style include patterns are defined. Each element under `<format>` 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 | ||
<configuration> | ||
<format> | ||
<includes> | ||
<!-- include all property files in "resource" folders under "src" --> | ||
<include>src/**/resources/**/*.properties</include> | ||
</includes> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this section define default includes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No the |
||
|
||
<licenseHeader> | ||
<!-- Specify either content or file, but not both --> | ||
<content>/* Licensed under Apache-2.0 */</content> | ||
<file>${basedir}/license-header</file> | ||
<!-- conent until first occurrence of the delimiter regex will be interpreted as header section --> | ||
<delimiter>#</delimiter> | ||
</licenseHeader> | ||
|
||
<!-- Files must end with a newline --> | ||
<endWithNewline /> | ||
|
||
<!-- Specify whether to use tabs or spaces for indentation --> | ||
<indent> | ||
<!-- Specify either spaces or tabs --> | ||
<spaces>true</spaces> | ||
<tabs>true</tabs> | ||
<!-- Specify how many spaces are used to convert one tab and vice versa. Defaults to 4 --> | ||
<spacesPerTab>4</spacesPerTab> | ||
</indent> | ||
|
||
<!-- Trim trailing whitespaces --> | ||
<trimTrailingWhitespace /> | ||
|
||
<!-- Specify replacements using search and replace --> | ||
<replace> | ||
<name>Say Hello to Mars</name> | ||
<search>World</search> | ||
<replacement>Mars</replacement> | ||
</replace> | ||
|
||
<!-- Specify replacements using regex match and replace --> | ||
<replaceRegex> | ||
<name>Say Hello to Mars from Regex</name> | ||
<searchRegex>(Hello) W[a-z]{3}d</searchRegex> | ||
<replacement>$1 Mars</replacement> | ||
</replaceRegex> | ||
</format> | ||
</configuration> | ||
``` | ||
|
||
<a name="invisible"></a> | ||
|
||
## Line endings and encodings (invisible stuff) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Existence of both
which seems invalid because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the gradle plugin, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what you mean. It's 100% the same as with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please never mind all my comments about |
||
|
||
@Parameter | ||
private Java java; | ||
|
||
|
@@ -80,7 +83,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { | |
protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException; | ||
|
||
@Override | ||
public final void execute() throws MojoExecutionException, MojoFailureException { | ||
public final void execute() throws MojoExecutionException { | ||
List<FormatterFactory> formatterFactories = getFormatterFactories(); | ||
|
||
for (FormatterFactory formatterFactory : formatterFactories) { | ||
|
@@ -127,7 +130,7 @@ private FormatterConfig getFormatterConfig() { | |
} | ||
|
||
private List<FormatterFactory> getFormatterFactories() { | ||
return Stream.of(java, scala) | ||
return Stream.of(format, java, scala) | ||
.filter(Objects::nonNull) | ||
.collect(toList()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extending There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep them separate for now. Hopefully @lutovich will have a chance to take a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I right that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class Moreover IMHO the maven-plugin should support the same feature set as the gradle-plugin does. And the gradle-plugin supports it this way. So one could setup the formatting like this <configuration>
<!-- global defaults -->
<encoding>UTF-8</encoding>
<!-- custom format for properties -->
<format>
<inlcudes>
<include>src/**/resources/**/*.properties</include>
</includes>
<endWithNewline />
<encoding>ISO 8859-1</encoding>
</format>
<!-- custom format for java files -->
<java>
<endWithNewline />
<trimTrailingWhitespace />
</java>
</configuration> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used the annotation parameter in my first iteration, but that leads to validation messages which are hard to understand for users:
Where as a custom handling is shown like that:
Thats why I prefer custom validation for required parameters and would like to keep it. |
||
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about not having
<format>
element? Configuration could then look like:?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the purpose of the
<format>
element is to do specific formatting on specific kinds of files that aren't java or scala. e.g.:spotless/spotlessSelf.gradle
Lines 54 to 59 in 4d28488
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, got it. This explains a lot :)
Please dismiss all my previous comments about
<format>
.Maybe then we need an ability to configure multiple formats?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea of the new format section is to provide the ability to define custom formats for e.g. property and sql files. Currently only one format section is allowed, but the gradle plugin allows unlimited format sections. This way you can define a format per source set. This comes in quite handy on larger projects.
I'm planing to extend the maven-plugin to provide unlimited format sections too.