Skip to content

Commit a7c74de

Browse files
authored
Merge pull request #457 from diffplug/feature/exclude-build-dir
Improve behavior of `target` and `targetExclude` for gradle plugin
2 parents c551ad8 + fbdaad5 commit a7c74de

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

Diff for: plugin-gradle/CHANGES.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
* Fixes [#410](https://github.com/diffplug/spotless/issues/410) AccessDeniedException in MinGW/GitBash.
77
* Also fixes occasional [hang on NFS due to filesystem timers](https://github.com/diffplug/spotless/pull/407#issuecomment-514824364).
88
* Eclipse-based formatters used to leave temporary files around ([#447](https://github.com/diffplug/spotless/issues/447)). This is now fixed, but only for eclipse 4.12+, no back-port to older Eclipse formatter versions is planned. ([#451](https://github.com/diffplug/spotless/issues/451))
9-
* Fixed a bad but simple bug in `paddedCell()` (https://github.com/diffplug/spotless/pull/455)
9+
* Fixed a bad but simple bug in `paddedCell()` ([#455](https://github.com/diffplug/spotless/pull/455))
1010
- if a formatter was behaving correctly on a given file (was idempotent)
1111
- but the file was not properly formatted
1212
- `spotlessCheck` would improperly say "all good" even though `spotlessApply` would properly change them
1313
- combined with up-to-date checking, could lead to even more confusing results,
14-
(https://github.com/diffplug/spotless/issues/338)
14+
([#338](https://github.com/diffplug/spotless/issues/338))
1515
- Fixed now!
16+
* When you specify `targetExclude()`, spotless no longer silently removes `build` directories from the exclusion ([#457](https://github.com/diffplug/spotless/pull/457)).
1617

1718
### Version 3.24.2 - August 19th 2019 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-plugin-gradle/3.24.1/), [jcenter](https://bintray.com/diffplug/opensource/spotless-plugin-gradle/3.24.1))
1819

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+47-24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.gradle.api.GradleException;
2929
import org.gradle.api.Project;
3030
import org.gradle.api.file.FileCollection;
31+
import org.gradle.api.tasks.util.PatternFilterable;
3132

3233
import com.diffplug.spotless.FormatExceptionPolicyStrict;
3334
import com.diffplug.spotless.FormatterFunc;
@@ -134,9 +135,12 @@ public void encoding(String charset) {
134135
* Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir.
135136
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
136137
* Anything else gets passed to getProject().files().
138+
*
139+
* If you pass any strings that start with "**\/*", this method will automatically filter out
140+
* "build", ".gradle", and ".git" folders.
137141
*/
138142
public void target(Object... targets) {
139-
this.target = parseTargets(targets);
143+
this.target = parseTargetsIsExclude(targets, false);
140144
}
141145

142146
/**
@@ -150,22 +154,22 @@ public void target(Object... targets) {
150154
* Anything else gets passed to getProject().files().
151155
*/
152156
public void targetExclude(Object... targets) {
153-
this.targetExclude = parseTargets(targets);
157+
this.targetExclude = parseTargetsIsExclude(targets, true);
154158
}
155159

156-
private FileCollection parseTargets(Object[] targets) {
160+
private FileCollection parseTargetsIsExclude(Object[] targets, boolean isExclude) {
157161
requireElementsNonNull(targets);
158162
if (targets.length == 0) {
159163
return getProject().files();
160164
} else if (targets.length == 1) {
161-
return parseTarget(targets[0]);
165+
return parseTargetIsExclude(targets[0], isExclude);
162166
} else {
163167
if (Stream.of(targets).allMatch(o -> o instanceof String)) {
164-
return parseTarget(Arrays.asList(targets));
168+
return parseTargetIsExclude(Arrays.asList(targets), isExclude);
165169
} else {
166170
FileCollection union = getProject().files();
167171
for (Object target : targets) {
168-
union = union.plus(parseTarget(target));
172+
union = union.plus(parseTargetIsExclude(target, isExclude));
169173
}
170174
return union;
171175
}
@@ -178,33 +182,52 @@ private FileCollection parseTargets(Object[] targets) {
178182
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
179183
* Anything else gets passed to getProject().files().
180184
*/
181-
@SuppressWarnings("unchecked")
182-
protected FileCollection parseTarget(Object target) {
185+
protected final FileCollection parseTarget(Object target) {
186+
return parseTargetIsExclude(target, false);
187+
}
188+
189+
private final FileCollection parseTargetIsExclude(Object target, boolean isExclude) {
183190
if (target instanceof FileCollection) {
184191
return (FileCollection) target;
185192
} else if (target instanceof String ||
186193
(target instanceof List && ((List<?>) target).stream().allMatch(o -> o instanceof String))) {
187-
// since people are likely to do '**/*.md', we want to make sure to exclude folders
188-
// they don't want to format which will slow down the operation greatly
189194
File dir = getProject().getProjectDir();
190-
List<String> excludes = new ArrayList<>();
191-
// no git
192-
excludes.add(".git");
193-
// no .gradle
194-
if (getProject() == getProject().getRootProject()) {
195-
excludes.add(".gradle");
196-
}
197-
// no build folders (flatInclude means that subproject might not be subfolders, see https://github.com/diffplug/spotless/issues/121)
198-
relativizeIfSubdir(excludes, dir, getProject().getBuildDir());
199-
for (Project subproject : getProject().getSubprojects()) {
200-
relativizeIfSubdir(excludes, dir, subproject.getBuildDir());
201-
}
195+
PatternFilterable userExact; // exactly the collection that the user specified
202196
if (target instanceof String) {
203-
return (FileCollection) getProject().fileTree(dir).include((String) target).exclude(excludes);
197+
userExact = getProject().fileTree(dir).include((String) target);
204198
} else {
205199
// target can only be a List<String> at this point
206-
return (FileCollection) getProject().fileTree(dir).include((List<String>) target).exclude(excludes);
200+
@SuppressWarnings("unchecked")
201+
List<String> targetList = (List<String>) target;
202+
userExact = getProject().fileTree(dir).include(targetList);
203+
}
204+
boolean filterOutGitAndGradle;
205+
// since people are likely to do '**/*.md', we want to make sure to exclude folders
206+
// they don't want to format which will slow down the operation greatly
207+
// but we only want to do that if they are *including* - if they are specifying
208+
// what they want to exclude, we shouldn't filter at all
209+
if (target instanceof String && !isExclude) {
210+
String str = (String) target;
211+
filterOutGitAndGradle = str.startsWith("**/*") || str.startsWith("**\\*");
212+
} else {
213+
filterOutGitAndGradle = false;
214+
}
215+
if (filterOutGitAndGradle) {
216+
List<String> excludes = new ArrayList<>();
217+
// no git
218+
excludes.add(".git");
219+
// no .gradle
220+
if (getProject() == getProject().getRootProject()) {
221+
excludes.add(".gradle");
222+
}
223+
// no build folders (flatInclude means that subproject might not be subfolders, see https://github.com/diffplug/spotless/issues/121)
224+
relativizeIfSubdir(excludes, dir, getProject().getBuildDir());
225+
for (Project subproject : getProject().getSubprojects()) {
226+
relativizeIfSubdir(excludes, dir, subproject.getBuildDir());
227+
}
228+
userExact = userExact.exclude(excludes);
207229
}
230+
return (FileCollection) userExact;
208231
} else {
209232
return getProject().files(target);
210233
}

0 commit comments

Comments
 (0)