From b2b27ccbad2aacafeaa1eb8f19e75cb65661d07f Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 9 Feb 2019 23:34:02 -0800 Subject: [PATCH 1/2] Added targetExclude to fix several issues related to excluding files from processing. Fixes #153, #194, #324. --- plugin-gradle/CHANGES.md | 6 +++ plugin-gradle/README.md | 14 ++++-- .../gradle/spotless/FormatExtension.java | 46 ++++++++++++++++--- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index cb9c701792..2c28199f65 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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)) diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index f4eff9d65f..6e58ed0f5c 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -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 @@ -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 { @@ -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). - ## How do I preview what `spotlessApply` will do? diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 5e558fdc16..a109557a04 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -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 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 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 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) { @@ -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)); } From a03e4c71b84c068c3b600de63aec365e37d30a14 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sat, 9 Feb 2019 23:43:37 -0800 Subject: [PATCH 2/2] Improved changelog and docs. --- plugin-gradle/CHANGES.md | 2 +- plugin-gradle/README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2c28199f65..88dcdc96f0 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -7,7 +7,7 @@ * 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. +* added `targetExclude(Object...)` which excludes the given files from processing ([#353](https://github.com/diffplug/spotless/pull/353)). * 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 diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index 6e58ed0f5c..07ee20ae17 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -636,9 +636,9 @@ spotless { 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: + +You can fix (1) by excluding the file from formatting using the `targetExclude` method, see the [custom rules](#custom) section for details. You can fix (2) and turn these exceptions into warnings like this: ```gradle spotless {