Skip to content

allow "spotlessFiles" project property in gradle plugin to only target specific files #322

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

Merged
merged 6 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Version 3.17.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))

* Updated default eclipse-jdt from 4.7.3a to 4.9.0 ([#316](https://github.com/diffplug/spotless/pull/316)). New version addresses enum-tab formatting bug in 4.8 ([#314](https://github.com/diffplug/spotless/issues/314)).
* Added `-files` switch to allow targeting specific files ([#322](https://github.com/diffplug/spotless/pull/322))

### Version 3.16.0 - October 30th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.16.0/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.16.0))

Expand Down
10 changes: 10 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ Note that `enforceCheck` is a global property which affects all formats (outside

<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> gradlew spotlessApply -PspotlessFiles=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)

Spotless is hosted on jcenter and at plugins.gradle.org. [Go here](https://plugins.gradle.org/plugin/com.diffplug.gradle.spotless) if you're not sure how to import the plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Predicate;

import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -47,6 +50,8 @@
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;

public class SpotlessTask extends DefaultTask {
private static final String FILES_PROPERTY = "spotlessFiles";

// set by SpotlessExtension, but possibly overridden by FormatExtension
protected String encoding = "UTF-8";

Expand Down Expand Up @@ -168,11 +173,23 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
.steps(steps)
.exceptionPolicy(exceptionPolicy)
.build()) {
// determine if a list of files has been passed in
Predicate<File> shouldInclude;
Project project = getProject();
if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) {
Object rawIncludePatterns = project.property(FILES_PROPERTY);
assert rawIncludePatterns != null;
final String[] includePatterns = ((String) rawIncludePatterns).split(",");
shouldInclude = file -> Arrays.stream(includePatterns)
.anyMatch(filePattern -> file.getAbsolutePath().matches(filePattern));
} else {
shouldInclude = file -> true;
}
// find the outOfDate files
List<File> outOfDate = new ArrayList<>();
inputs.outOfDate(inputDetails -> {
File file = inputDetails.getFile();
if (file.isFile() && !file.equals(getCacheFile())) {
if (file.isFile() && !file.equals(getCacheFile()) && shouldInclude.test(file)) {
outOfDate.add(file);
}
});
Expand All @@ -183,7 +200,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
if (getCacheFile().exists()) {
LastApply lastApply = SerializableMisc.fromFile(LastApply.class, getCacheFile());
for (File file : lastApply.changedFiles) {
if (!outOfDate.contains(file) && file.exists() && Iterables.contains(target, file)) {
if (!outOfDate.contains(file) && file.exists() && Iterables.contains(target, file) && shouldInclude.test(file)) {
outOfDate.add(file);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.gradle.spotless;

import java.io.IOException;

import org.junit.Test;

public class SpecificFilesTest extends GradleIntegrationTest {
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 {

setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'com.diffplug.gradle.spotless'",
"}",
"apply plugin: 'java'",
"spotless {",
" java {",
" googleJavaFormat('1.2')",
" }",
"}");

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

gradleRunner()
.withArguments("spotlessApply", "-PspotlessFiles=" + patterns)
.build();

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

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

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

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