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

Specify files maven #392

Merged
merged 6 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Version 1.22.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))

* Updated default eclipse-cdt from 4.7.3a to 4.11.0 ([#390](https://github.com/diffplug/spotless/pull/390)).
* Added `-DspotlessFiles` switch to allow targeting specific files ([#392](https://github.com/diffplug/spotless/pull/392))

### Version 1.21.1 - March 29th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.21.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.21.1))

Expand Down
10 changes: 10 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ By default, `spotless:check` is bound to the `verify` phase. You might want to

<a name="examples"></a>

## Can I apply Spotless to specific files?

You can target specific files by setting the `spotlessFiles` project property to a comma-separated list of file patterns:

```
cmd> mvn spotless:apply -DspotlessFiles=my/file/pattern.java,more/generic/.*-pattern.java
```

The patterns are matched using `String#matches(String)` against the absolute file path.

## Example configurations (from real-world projects)

- [Apache Avro](https://github.com/apache/avro/blob/8026c8ffe4ef67ab419dba73910636bf2c1a691c/lang/java/pom.xml#L307-L334)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -100,6 +103,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Deprecated
private com.diffplug.spotless.maven.css.Css css;

@Parameter(property = "spotlessFiles")
private String filePatterns;

protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;

@Override
Expand Down Expand Up @@ -132,7 +138,22 @@ private List<File> collectFiles(FormatterFactory formatterFactory) throws MojoEx
String excludesString = String.join(",", excludes);

try {
return FileUtils.getFiles(baseDir, includesString, excludesString);
final List<File> files = FileUtils.getFiles(baseDir, includesString, excludesString);
if (filePatterns == null || filePatterns.isEmpty()) {
return files;
}
final String[] includePatterns = this.filePatterns.split(",");
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
.map(Pattern::compile)
.collect(Collectors.toList());
final Predicate<File> shouldInclude = file -> compiledIncludePatterns
.stream()
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
.matches());
return files
.stream()
.filter(shouldInclude)
.collect(toList());
} catch (IOException e) {
throw new MojoExecutionException("Unable to scan file tree rooted at " + baseDir, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public File locateFile(String path) {
} catch (ResourceNotFoundException e) {
throw new RuntimeException("Unable to locate file with path: " + path, e);
} catch (FileResourceCreationException e) {
throw new RuntimeException("Unable to create temporaty file '" + outputFile + "' in the output directory", e);
throw new RuntimeException("Unable to create temporary file '" + outputFile + "' in the output directory", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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;

import java.io.IOException;

import org.junit.Test;

public class SpecificFilesTest extends MavenIntegrationTest {
private String testFile(int number, boolean absolute) throws IOException {
String rel = "src/main/java/test" + number + ".java";
if (absolute) {
return rootFolder() + "/" + rel;
} else {
return rel;
}
}

private String testFile(int number) throws IOException {
return testFile(number, false);
}

private String fixture(boolean formatted) {
return "java/googlejavaformat/JavaCode" + (formatted ? "F" : "Unf") + "ormatted.test";
}

private void integration(String patterns, boolean firstFormatted, boolean secondFormatted, boolean thirdFormatted)
throws IOException, InterruptedException {

writePomWithJavaSteps(
"<includes>",
" <include>src/**/java/**/*.java</include>",
"</includes>",
"<googleJavaFormat>",
" <version>1.2</version>",
"</googleJavaFormat>");

setFile(testFile(1)).toResource(fixture(false));
setFile(testFile(2)).toResource(fixture(false));
setFile(testFile(3)).toResource(fixture(false));

mavenRunner()
.withArguments("spotless:apply", "-DspotlessFiles=" + patterns)
.runNoError();

assertFile(testFile(1)).sameAsResource(fixture(firstFormatted));
assertFile(testFile(2)).sameAsResource(fixture(secondFormatted));
assertFile(testFile(3)).sameAsResource(fixture(thirdFormatted));
}

@Test
public void singleFile() throws IOException, InterruptedException {
integration(testFile(2, true), false, true, false);
}

@Test
public void multiFile() throws IOException, InterruptedException {
integration(testFile(1, true) + "," + testFile(3, true), true, false, true);
}

@Test
public void regexp() throws IOException, InterruptedException {
integration(".*/src/main/java/test\\(1\\|3\\).java", true, false, true);
}
}