Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270))
### Fixed
- Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private static final class Runtime implements Serializable {
private final @Nullable String yearToday;
private final @Nullable String beforeYear;
private final @Nullable String afterYear;
private final @Nullable String afterYearUntilNextYear;
private final boolean updateYearWithLatest;
private final boolean licenseHeaderWithRange;
private final boolean hasFileToken;
Expand Down Expand Up @@ -253,6 +254,8 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
int yearTokenIndex = licenseHeader.indexOf(yearToken.orElseThrow());
this.beforeYear = licenseHeader.substring(0, yearTokenIndex);
this.afterYear = licenseHeader.substring(yearTokenIndex + yearToken.orElseThrow().length());
// later year tokens get the same year, so an existing year is followed by the text up to the next token
this.afterYearUntilNextYear = getYearToken(afterYear).map(token -> afterYear.substring(0, afterYear.indexOf(token))).orElse(afterYear);
this.yearSepOrFull = yearSeparator;
this.updateYearWithLatest = updateYearWithLatest;

Expand All @@ -268,6 +271,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
this.yearToday = null;
this.beforeYear = null;
this.afterYear = null;
this.afterYearUntilNextYear = null;
this.yearSepOrFull = licenseHeader;
this.updateYearWithLatest = false;
this.licenseHeaderWithRange = false;
Expand Down Expand Up @@ -343,29 +347,39 @@ private String replaceYear(String raw) {
} else {
// the yes year case is a bit harder
int beforeYearIdx = raw.indexOf(beforeYear);
int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1);
int afterYearIdx = raw.indexOf(afterYearUntilNextYear, beforeYearIdx + beforeYear.length() + 1);

if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) {
if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYearUntilNextYear.length() <= contentMatcher.start()) {
// and also ends with exactly the right header, so it's easy to parse the existing year
String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx);
String newYear = formatYearStr(calculateYearExact(existingYear));
String afterNewYear = afterYearWith(newYear);
if (existingYear.equals(newYear)) {
// fastpath where we don't need to make any changes at all
boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw
boolean noPadding = beforeYearIdx == 0 && raw.startsWith(afterNewYear, afterYearIdx) && afterYearIdx + afterNewYear.length() == contentMatcher.start(); // allows fastpath return raw
if (noPadding) {
return raw;
}
}
return beforeYear + newYear + afterYear + content;
return beforeYear + newYear + afterNewYear + content;
} else {
String newYear = formatYearStr(calculateYearBySearching(raw.substring(0, contentMatcher.start())));
// at worst, we just say that it was made today
return beforeYear + newYear + afterYear + content;
return beforeYear + newYear + afterYearWith(newYear) + content;
}
}
}
}

/** The header after the first year token, with any later year tokens set to the same year. */
private String afterYearWith(String year) {
String result = afterYear;
for (String yearToken : YEAR_TOKENS) {
result = result.replace(yearToken, year);
}
return result;
}

private static final Pattern YYYY = Pattern.compile("\\d{4}");

/** Calculates the year to inject. */
Expand Down Expand Up @@ -465,7 +479,8 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws
} else {
yearRange = oldYear + yearSepOrFull + newYear;
}
return beforeYear + formatYearStr(yearRange) + afterYear + raw.substring(contentMatcher.start());
String formattedYearRange = formatYearStr(yearRange);
return beforeYear + formattedYearRange + afterYearWith(formattedYearRange) + raw.substring(contentMatcher.start());
}

private String replaceFileName(String raw, File file) {
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Added
- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270))
### Fixed
- Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190))
- Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877))
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Added
- License header: a `$YEAR` (or `$today.year`) token can now appear more than once in the header; every occurrence gets the same year. ([#1270](https://github.com/diffplug/spotless/issues/1270))
### Fixed
- Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190))
### Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ void updateYearWithLatest() throws Throwable {
.test(hasHeaderYear("1990-2015"), hasHeaderYear("1990-" + currentYear()));
}

@Test
void should_apply_license_containing_YEAR_token_multiple_times() throws Throwable {
String license = "This is a fake license, $YEAR. ACME corp.\n * Parts copyright $YEAR Other corp.";
StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_).build())
.test(getTestResource(FILE_NO_LICENSE), hasHeaderYear(license, currentYear()))
.testUnaffected(hasHeaderYear(license, currentYear()))
.testUnaffected(hasHeaderYear(license, "2003"))
.testUnaffected(hasHeaderYear(license, "1990-2015"))
.test(hasHeader("This is a fake license, 2003. ACME corp.\n * Parts copyright 2007 Other corp."), hasHeaderYear(license, "2003"));
StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_).withYearMode(YearMode.UPDATE_TO_TODAY).build())
.testUnaffected(hasHeaderYear(license, currentYear()))
.test(hasHeaderYear(license, "2003"), hasHeaderYear(license, "2003-" + currentYear()));
}

@Test
void should_apply_license_containing_YEAR_token_with_non_default_year_separator() throws Throwable {
StepHarness.forStep(LicenseHeaderStep.headerDelimiter(header(HEADER_WITH_$YEAR), PACKAGE_).withYearSeparator(", ").build())
Expand Down Expand Up @@ -336,4 +350,29 @@ void should_setFromGit_year_when_singleCommit() throws Exception {
StepHarnessWithFile.forStep(this, step)
.test("Foo.java", getTestResource(FILE_NO_LICENSE), hasHeaderYear(currentYear()));
}

@Test
@ClearGitConfig
void should_setFromGit_year_for_every_YEAR_token() throws Exception {
try (Git git = Git.init().setDirectory(rootFolder()).call()) {
setFile("Foo.java").toContent("package foo; // v1\n");
git.add()
.addFilepattern("Foo.java")
.call();
git.commit()
.setMessage("add")
.setAuthor("Test User", "test@example.com")
.setCommitter("Test User", "test@example.com")
.setSign(false)
.call();
}

String license = "This is a fake license, $YEAR. ACME corp.\n * Parts copyright $YEAR Other corp.";
FormatterStep step = LicenseHeaderStep.headerDelimiter(header(license), PACKAGE_)
.withYearMode(YearMode.SET_FROM_GIT)
.build();

StepHarnessWithFile.forStep(this, step)
.test("Foo.java", getTestResource(FILE_NO_LICENSE), hasHeaderYear(license, currentYear()));
}
}
Loading