Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/gradle/com.diffplug.spotless-6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Dec 2, 2021
2 parents f9903a6 + 1ddf895 commit bb22471
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 43 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

## [2.20.1] - 2021-12-01

### Changed
* Added `named` option to `licenseHeader` to support alternate license header within same format (like java) ([872](https://github.com/diffplug/spotless/issues/872)).
* Added `onlyIfContentMatches` option to `licenseHeader` to skip license header application based on source file content pattern ([#650](https://github.com/diffplug/spotless/issues/650)).
* Bump jgit version ([#992](https://github.com/diffplug/spotless/pull/992)).
* jgit `5.10.0.202012080955-r` -> `5.13.0.202109080827-r`

Expand Down
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Contributing to Spotless

Pull requests are welcome, preferably against `main`. Feel free to develop spotless any way you like, but the easiest way to look at the code is to clone the repo and run `gradlew ide`, which will download, setup, and start an Eclipse IDE for you.
Pull requests are welcome, preferably against `main`. Feel free to develop spotless any way you like.

## How Spotless works

Expand Down Expand Up @@ -36,7 +36,6 @@ For the folders below in monospace text, they are published on maven central at
| `lib-extra` | Contains the optional parts of Spotless which require external dependencies. `LineEnding.GIT_ATTRIBUTES` won't work unless `lib-extra` is available. |
| `plugin-gradle` | Integrates spotless and all of its formatters into Gradle. |
| `plugin-maven` | Integrates spotless and all of its formatters into Maven. |
| ide | Generates and launches an IDE for developing spotless. |
| _ext | Folder for generating glue jars (specifically packaging Eclipse jars from p2 for consumption using maven).

## How to add a new FormatterStep
Expand Down
5 changes: 5 additions & 0 deletions gradle/java-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,8 @@ if (!version.endsWith('-SNAPSHOT')) {
}
}
}

tasks.withType(AbstractArchiveTask).configureEach {
preserveFileTimestamps = false
reproducibleFileOrder = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2016-2021 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.spotless;

import java.io.File;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nullable;

final class FilterByContentPatternFormatterStep implements FormatterStep {
private final FormatterStep delegateStep;
final Pattern contentPattern;

FilterByContentPatternFormatterStep(FormatterStep delegateStep, String contentPattern) {
this.delegateStep = Objects.requireNonNull(delegateStep);
this.contentPattern = Pattern.compile(Objects.requireNonNull(contentPattern));
}

@Override
public String getName() {
return delegateStep.getName();
}

@Override
public @Nullable String format(String raw, File file) throws Exception {
Objects.requireNonNull(raw, "raw");
Objects.requireNonNull(file, "file");

Matcher matcher = contentPattern.matcher(raw);

if (matcher.find()) {
return delegateStep.format(raw, file);
} else {
return raw;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FilterByContentPatternFormatterStep that = (FilterByContentPatternFormatterStep) o;
return Objects.equals(delegateStep, that.delegateStep) &&
Objects.equals(contentPattern.pattern(), that.contentPattern.pattern());
}

@Override
public int hashCode() {
return Objects.hash(delegateStep, contentPattern.pattern());
}

private static final long serialVersionUID = 1L;
}
12 changes: 12 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/FormatterStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public interface FormatterStep extends Serializable {
*/
public @Nullable String format(String rawUnix, File file) throws Exception;

/**
* Returns a new FormatterStep which will only apply its changes
* to files which pass the given filter.
*
* @param contentPattern
* java regular expression used to filter out files which content doesn't contain pattern
* @return FormatterStep
*/
public default FormatterStep filterByContentPattern(String contentPattern) {
return new FilterByContentPatternFormatterStep(this, contentPattern);
}

/**
* Returns a new FormatterStep which will only apply its changes
* to files which pass the given filter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,54 +51,72 @@ public static LicenseHeaderStep headerDelimiter(String header, String delimiter)
}

public static LicenseHeaderStep headerDelimiter(ThrowingEx.Supplier<String> headerLazy, String delimiter) {
return new LicenseHeaderStep(headerLazy, delimiter, DEFAULT_YEAR_DELIMITER, () -> YearMode.PRESERVE);
return new LicenseHeaderStep(null, null, headerLazy, delimiter, DEFAULT_YEAR_DELIMITER, () -> YearMode.PRESERVE);
}

final String name;
final @Nullable String contentPattern;
final ThrowingEx.Supplier<String> headerLazy;
final String delimiter;
final String yearSeparator;
final Supplier<YearMode> yearMode;

private LicenseHeaderStep(ThrowingEx.Supplier<String> headerLazy, String delimiter, String yearSeparator, Supplier<YearMode> yearMode) {
private LicenseHeaderStep(String name, String contentPattern, ThrowingEx.Supplier<String> headerLazy, String delimiter, String yearSeparator, Supplier<YearMode> yearMode) {
this.name = sanitizeName(name);
this.contentPattern = sanitizeContentPattern(contentPattern);
this.headerLazy = Objects.requireNonNull(headerLazy);
this.delimiter = Objects.requireNonNull(delimiter);
this.yearSeparator = Objects.requireNonNull(yearSeparator);
this.yearMode = Objects.requireNonNull(yearMode);
}

public String getName() {
return name;
}

public LicenseHeaderStep withName(String name) {
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public LicenseHeaderStep withContentPattern(String contentPattern) {
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public LicenseHeaderStep withHeaderString(String header) {
return withHeaderLazy(() -> header);
}

public LicenseHeaderStep withHeaderLazy(ThrowingEx.Supplier<String> headerLazy) {
return new LicenseHeaderStep(headerLazy, delimiter, yearSeparator, yearMode);
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public LicenseHeaderStep withDelimiter(String delimiter) {
return new LicenseHeaderStep(headerLazy, delimiter, yearSeparator, yearMode);
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public LicenseHeaderStep withYearSeparator(String yearSeparator) {
return new LicenseHeaderStep(headerLazy, delimiter, yearSeparator, yearMode);
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public LicenseHeaderStep withYearMode(YearMode yearMode) {
return withYearModeLazy(() -> yearMode);
}

public LicenseHeaderStep withYearModeLazy(Supplier<YearMode> yearMode) {
return new LicenseHeaderStep(headerLazy, delimiter, yearSeparator, yearMode);
return new LicenseHeaderStep(name, contentPattern, headerLazy, delimiter, yearSeparator, yearMode);
}

public FormatterStep build() {
FormatterStep formatterStep = null;

if (yearMode.get() == YearMode.SET_FROM_GIT) {
return FormatterStep.createNeverUpToDateLazy(LicenseHeaderStep.name(), () -> {
formatterStep = FormatterStep.createNeverUpToDateLazy(name, () -> {
boolean updateYear = false; // doesn't matter
Runtime runtime = new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear);
return FormatterFunc.needsFile(runtime::setLicenseHeaderYearsFromGitHistory);
});
} else {
return FormatterStep.createLazy(LicenseHeaderStep.name(), () -> {
formatterStep = FormatterStep.createLazy(name, () -> {
// by default, we should update the year if the user is using ratchetFrom
boolean updateYear;
switch (yearMode.get()) {
Expand All @@ -115,19 +133,50 @@ public FormatterStep build() {
return new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear);
}, step -> step::format);
}

if (contentPattern == null) {
return formatterStep;
}

return formatterStep.filterByContentPattern(contentPattern);
}

private String sanitizeName(String name) {
if (name == null) {
return DEFAULT_NAME_PREFIX;
}

name = name.trim();

if (Objects.equals(DEFAULT_NAME_PREFIX, name) || name.startsWith(DEFAULT_NAME_PREFIX)) {
return name;
}

return DEFAULT_NAME_PREFIX + "-" + name;
}

private static final String NAME = "licenseHeader";
@Nullable
private String sanitizeContentPattern(String contentPattern) {
if (contentPattern == null) {
return contentPattern;
}

contentPattern = contentPattern.trim();

if (contentPattern.isEmpty()) {
return null;
}

return contentPattern;
}

private static final String DEFAULT_NAME_PREFIX = LicenseHeaderStep.class.getName();
private static final String DEFAULT_YEAR_DELIMITER = "-";
private static final List<String> YEAR_TOKENS = Arrays.asList("$YEAR", "$today.year");

private static final SerializableFileFilter UNSUPPORTED_JVM_FILES_FILTER = SerializableFileFilter.skipFilesNamed(
"package-info.java", "package-info.groovy", "module-info.java");

public static String name() {
return NAME;
}

public static String defaultYearDelimiter() {
return DEFAULT_YEAR_DELIMITER;
}
Expand Down
4 changes: 4 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]

## [6.0.1] - 2021-12-01

### Changed
* Added `named` option to `licenseHeader` to support alternate license header within same format (like java) ([872](https://github.com/diffplug/spotless/issues/872)).
* Added `onlyIfContentMatches` option to `licenseHeader` to skip license header application based on source file content pattern ([#650](https://github.com/diffplug/spotless/issues/650)).
* Bump jgit version ([#992](https://github.com/diffplug/spotless/pull/992)).
* jgit `5.10.0.202012080955-r` -> `5.13.0.202109080827-r`

Expand Down
Loading

0 comments on commit bb22471

Please sign in to comment.