Skip to content

Commit

Permalink
Added targetExclude to fix several issues related to excluding files …
Browse files Browse the repository at this point in the history
…from processing. Fixes #153, #194, #324.
  • Loading branch information
nedtwigg committed Feb 10, 2019
1 parent b7032ee commit b2b27cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
6 changes: 6 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
* Updated default google-java-format from 1.5 to 1.7 ([#335](https://github.com/diffplug/spotless/issues/335)).
* Replacing a step no longer triggers early evaluation ([#219](https://github.com/diffplug/spotless/issues/219)).
* `importOrderFile(Object file)` for java and groovy is now lazy ([#218](https://github.com/diffplug/spotless/issues/218)).
* added `targetExclude(Object...)` which excludes the given files from processing.
* This resolves several related issues:
* [#153](https://github.com/diffplug/spotless/issues/153) using a `PatternFilterable` to determine source processed by `JavaExtension` and `KotlinExtension`
* [#194](https://github.com/diffplug/spotless/issues/194) ignoreErrorForPath does not work
* [#324](https://github.com/diffplug/spotless/issues/324) better support for excluding files from processing
* Our answer for a long time had been "just use `target(Object...)` to fix this" but there is clearly sufficient demand to justify `targetExclude`.

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

Expand Down
14 changes: 11 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ spotless {
// - if you pass anything else, it will be sent to project.files(yourArg)
target '**/*.gradle', '**/*.md', '**/.gitignore'
targetExclude 'src/main/codegen/**', 'src/test/codegen/**'
// the files to be formatted = (target - targetExclude)
// NOTE: if target or targetExclude is called multiple times, only the
// last call is effective
// spotless has built-in rules for the most basic formatting tasks
trimTrailingWhitespace()
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
Expand Down Expand Up @@ -628,7 +633,12 @@ spotless {
}
```

If a formatter throws an exception, possibly because of malformed code or a bug in a formatter step, Spotless will report a build failure. You can suppress these specific failures as such:
When a misformatted file throws an exception, it will be for one of two reasons:

1) Spotless calculated the properly formatted version, and it is different than the current contents.
+ Fix this by excluding the file from formatted using the `targetExclude` method, see the [custom rules](#custom) section for details.
2) One of the formatters threw an exception while attempting to calculate the properly formatted version.
+ You can turn these exceptions into warnings like this:

```gradle
spotless {
Expand All @@ -642,8 +652,6 @@ spotless {
}
```

Note that `enforceCheck` is a global property which affects all formats (outside the java block), while `ignoreErrorForStep/Path` are local to a single format (inside the java block).

<a name="preview"></a>

## How do I preview what `spotlessApply` will do?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,34 +122,62 @@ public void encoding(String charset) {
setEncoding(charset);
}

/** The files that need to be formatted. */
protected FileCollection target;
/** The files to be formatted = (target - targetExclude). */
protected FileCollection target, targetExclude;

/**
* Sets which files should be formatted. Files to be formatted = (target - targetExclude).
*
* When this method is called multiple times, only the last call has any effect.
*
* FileCollections pass through raw.
* Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir.
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
* Anything else gets passed to getProject().files().
*/
public void target(Object... targets) {
this.target = parseTargets(targets);
}

/**
* Sets which files will be excluded from formatting. Files to be formatted = (target - targetExclude).
*
* When this method is called multiple times, only the last call has any effect.
*
* FileCollections pass through raw.
* Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir.
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
* Anything else gets passed to getProject().files().
*/
public void targetExclude(Object... targets) {
this.targetExclude = parseTargets(targets);
}

private FileCollection parseTargets(Object[] targets) {
requireElementsNonNull(targets);
if (targets.length == 0) {
this.target = getProject().files();
return getProject().files();
} else if (targets.length == 1) {
this.target = parseTarget(targets[0]);
return parseTarget(targets[0]);
} else {
if (Stream.of(targets).allMatch(o -> o instanceof String)) {
this.target = parseTarget(Arrays.asList(targets));
return parseTarget(Arrays.asList(targets));
} else {
FileCollection union = getProject().files();
for (Object target : targets) {
union = union.plus(parseTarget(target));
}
this.target = union;
return union;
}
}
}

/**
* FileCollections pass through raw.
* Strings are treated as the 'include' arg to fileTree, with project.rootDir as the dir.
* List<String> are treated as the 'includes' arg to fileTree, with project.rootDir as the dir.
* Anything else gets passed to getProject().files().
*/
@SuppressWarnings("unchecked")
protected FileCollection parseTarget(Object target) {
if (target instanceof FileCollection) {
Expand Down Expand Up @@ -525,7 +553,11 @@ protected void setupTask(SpotlessTask task) {
task.setPaddedCell(paddedCell);
task.setEncoding(getEncoding().name());
task.setExceptionPolicy(exceptionPolicy);
task.setTarget(target);
if (targetExclude == null) {
task.setTarget(target);
} else {
task.setTarget(target.minus(targetExclude));
}
task.setSteps(steps);
task.setLineEndingsPolicy(getLineEndings().createPolicy(getProject().getProjectDir(), () -> task.target));
}
Expand Down

0 comments on commit b2b27cc

Please sign in to comment.