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

#1132 - Add magic value to disable ratchetFrom #1134

Merged
merged 3 commits into from
Feb 19, 2022
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
3 changes: 3 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
* Magic value 'NONE' for disabling ratchet functionality ([#1134](https://github.com/diffplug/spotless/issues/1134))

### Changed
* Use SLF4J for logging ([#1116](https://github.com/diffplug/spotless/issues/1116))

Expand Down
8 changes: 8 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,14 @@ However, we strongly recommend that you use a non-local branch, such as a tag or

This is especially helpful for injecting accurate copyright dates using the [license step](#license-header).

You can explicitly disable ratchet functionality by providing the value 'NONE':
```xml
<configuration>
<ratchetFrom>NONE</ratchetFrom>
</configuration>
```
This is useful for disabling the ratchet functionality in child projects where the parent defines a ratchetFrom value.

## `spotless:off` and `spotless:on`

Sometimes there is a chunk of code which you have carefully handcrafted, and you would like to exclude just this one little part from getting clobbered by the autoformat. Some formatters have a way to do this, many don't, but who cares. If you setup your spotless like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
private static final String DEFAULT_INDEX_FILE_NAME = "spotless-index";
private static final String DEFAULT_ENCODING = "UTF-8";
private static final String DEFAULT_LINE_ENDINGS = "GIT_ATTRIBUTES";

/** Value to allow unsetting the ratchet inherited from parent pom configuration. */
static final String RATCHETFROM_NONE = "NONE";

static final String GOAL_CHECK = "check";
static final String GOAL_APPLY = "apply";

Expand Down Expand Up @@ -302,7 +306,9 @@ private FormatterConfig getFormatterConfig() {
Provisioner provisioner = MavenProvisioner.create(resolver);
List<FormatterStepFactory> formatterStepFactories = getFormatterStepFactories();
FileLocator fileLocator = getFileLocator();
return new FormatterConfig(baseDir, encoding, lineEndings, Optional.ofNullable(ratchetFrom), provisioner, fileLocator, formatterStepFactories, Optional.ofNullable(setLicenseHeaderYearsFromGitHistory));
final Optional<String> optionalRatchetFrom = Optional.ofNullable(this.ratchetFrom)
.filter(ratchet -> !RATCHETFROM_NONE.equals(ratchet));
return new FormatterConfig(baseDir, encoding, lineEndings, optionalRatchetFrom, provisioner, fileLocator, formatterStepFactories, Optional.ofNullable(setLicenseHeaderYearsFromGitHistory));
}

private FileLocator getFileLocator() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package com.diffplug.spotless.maven;

import static com.diffplug.spotless.maven.AbstractSpotlessMojo.RATCHETFROM_NONE;
import static java.util.Collections.emptySet;

import java.io.File;
Expand All @@ -35,7 +36,17 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.generic.PipeStepPair;
import com.diffplug.spotless.maven.generic.*;
import com.diffplug.spotless.maven.generic.EclipseWtp;
import com.diffplug.spotless.maven.generic.EndWithNewline;
import com.diffplug.spotless.maven.generic.Indent;
import com.diffplug.spotless.maven.generic.Jsr223;
import com.diffplug.spotless.maven.generic.LicenseHeader;
import com.diffplug.spotless.maven.generic.NativeCmd;
import com.diffplug.spotless.maven.generic.Prettier;
import com.diffplug.spotless.maven.generic.Replace;
import com.diffplug.spotless.maven.generic.ReplaceRegex;
import com.diffplug.spotless.maven.generic.ToggleOffOn;
import com.diffplug.spotless.maven.generic.TrimTrailingWhitespace;

public abstract class FormatterFactory {
@Parameter
Expand Down Expand Up @@ -159,6 +170,8 @@ private LineEnding lineEndings(FormatterConfig config) {
Optional<String> ratchetFrom(FormatterConfig config) {
if (RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL.equals(ratchetFrom)) {
return config.getRatchetFrom();
} else if (RATCHETFROM_NONE.equals(ratchetFrom)) {
return Optional.empty();
} else {
return Optional.ofNullable(ratchetFrom);
}
Expand Down