Skip to content

Commit 07d58e5

Browse files
authored
Merge pull request #542 from sameer-b/supportIntelliJTokens
Enable support for intelliJ placeholder for year in license header
2 parents 2204946 + 6ccc16d commit 07d58e5

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Enable IntelliJ-compatible token `$today.year` for specifying the year in license header files. ([#542](https://github.com/diffplug/spotless/pull/542))
1315
### Build
1416
* All `CHANGES.md` are now in keepachangelog format. ([#507](https://github.com/diffplug/spotless/pull/507))
1517
* We now use [javadoc.io](https://javadoc.io/) instead of github pages. ([#508](https://github.com/diffplug/spotless/pull/508))

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import java.nio.charset.Charset;
2222
import java.nio.file.Files;
2323
import java.time.YearMonth;
24+
import java.util.Arrays;
25+
import java.util.List;
2426
import java.util.Objects;
27+
import java.util.Optional;
2528
import java.util.regex.Matcher;
2629
import java.util.regex.Pattern;
2730

@@ -35,17 +38,18 @@ public final class LicenseHeaderStep implements Serializable {
3538

3639
private static final String NAME = "licenseHeader";
3740
private static final String DEFAULT_YEAR_DELIMITER = "-";
41+
private static final List<String> YEAR_TOKENS = Arrays.asList("$YEAR", "$today.year");
3842

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

4246
private final String licenseHeader;
47+
private final boolean hasYearToken;
4348
private final Pattern delimiterPattern;
44-
private Pattern yearMatcherPattern;
45-
private boolean hasYearToken;
46-
private String licenseHeaderBeforeYearToken;
47-
private String licenseHeaderAfterYearToken;
48-
private String licenseHeaderWithYearTokenReplaced;
49+
private final Pattern yearMatcherPattern;
50+
private final String licenseHeaderBeforeYearToken;
51+
private final String licenseHeaderAfterYearToken;
52+
private final String licenseHeaderWithYearTokenReplaced;
4953

5054
/** Creates a FormatterStep which forces the start of each file to match a license header. */
5155
public static FormatterStep createFromHeader(String licenseHeader, String delimiter) {
@@ -107,16 +111,34 @@ private LicenseHeaderStep(String licenseHeader, String delimiter, String yearSep
107111
}
108112
this.licenseHeader = licenseHeader;
109113
this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE);
110-
this.hasYearToken = licenseHeader.contains("$YEAR");
111-
if (this.hasYearToken) {
112-
int yearTokenIndex = licenseHeader.indexOf("$YEAR");
113-
this.licenseHeaderBeforeYearToken = licenseHeader.substring(0, yearTokenIndex);
114-
this.licenseHeaderAfterYearToken = licenseHeader.substring(yearTokenIndex + 5, licenseHeader.length());
115-
this.licenseHeaderWithYearTokenReplaced = licenseHeader.replace("$YEAR", String.valueOf(YearMonth.now().getYear()));
116-
this.yearMatcherPattern = Pattern.compile("[0-9]{4}(" + Pattern.quote(yearSeparator) + "[0-9]{4})?");
114+
115+
Optional<String> yearToken = getYearToken(licenseHeader);
116+
this.hasYearToken = yearToken.isPresent();
117+
if (hasYearToken) {
118+
int yearTokenIndex = licenseHeader.indexOf(yearToken.get());
119+
licenseHeaderBeforeYearToken = licenseHeader.substring(0, yearTokenIndex);
120+
licenseHeaderAfterYearToken = licenseHeader.substring(yearTokenIndex + 5);
121+
licenseHeaderWithYearTokenReplaced = licenseHeader.replace(yearToken.get(), String.valueOf(YearMonth.now().getYear()));
122+
yearMatcherPattern = Pattern.compile("[0-9]{4}(" + Pattern.quote(yearSeparator) + "[0-9]{4})?");
123+
} else {
124+
licenseHeaderBeforeYearToken = null;
125+
licenseHeaderAfterYearToken = null;
126+
licenseHeaderWithYearTokenReplaced = null;
127+
yearMatcherPattern = null;
117128
}
118129
}
119130

131+
/**
132+
* Get the first place holder token being used in the
133+
* license header for specifying the year
134+
*
135+
* @param licenseHeader String representation of the license header
136+
* @return Matching value from YEAR_TOKENS or null if none exist
137+
*/
138+
private static Optional<String> getYearToken(String licenseHeader) {
139+
return YEAR_TOKENS.stream().filter(licenseHeader::contains).findFirst();
140+
}
141+
120142
/** Reads the license file from the given file. */
121143
private LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter, String yearSeparator) throws IOException {
122144
this(new String(Files.readAllBytes(licenseFile.toPath()), encoding), delimiter, yearSeparator);

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Enable IntelliJ-compatible token `$today.year` for specifying the year in license header files. ([#542](https://github.com/diffplug/spotless/pull/542))
68
### Fixed
79
* Eclipse-WTP formatter (web tools platform, not java) could encounter errors in parallel multiproject builds [#492](https://github.com/diffplug/spotless/issues/492). Fixed for Eclipse-WTP formatter Eclipse version 4.13.0 (default version).
810

911
## [3.27.2] - 2020-03-05
12+
### Fixed
1013
* Add tests to `SpecificFilesTest` to fix [#529](https://github.com/diffplug/spotless/issues/529)
1114
* If you applied spotless to a subproject, but not to the root project, then on Gradle 6+ you would get the deprecation warning `Using method Project#afterEvaluate(Action) when the project is already evaluated has been deprecated.` This has now been fixed. ([#506](https://github.com/diffplug/spotless/issues/506))
1215

plugin-gradle/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,7 @@ to true.
523523

524524
## License header options
525525

526-
If the string contents of a licenseHeader step or the file contents of a licenseHeaderFile step contains a $YEAR token,
527-
then in the end-result generated license headers which use this license header as a template, $YEAR will be replaced with the current year.
528-
526+
If the license header (specified with `licenseHeader` or `licenseHeaderFile`) contains `$YEAR` or `$today.year`, then that token will be replaced with the current 4-digit year.
529527

530528
For example:
531529
```

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Enable IntelliJ-compatible token `$today.year` for specifying the year in license header files. ([#542](https://github.com/diffplug/spotless/pull/542))
68
### Fixed
79
* Fix scala and kotlin maven config documentation.
810
* Eclipse-WTP formatter (web tools platform, not java) could encounter errors in parallel multiproject builds [#492](https://github.com/diffplug/spotless/issues/492). Fixed for Eclipse-WTP formatter Eclipse version 4.13.0 (default version).

testlib/src/test/java/com/diffplug/spotless/generic/LicenseHeaderStepTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class LicenseHeaderStepTest extends ResourceHarness {
3939
private static final String KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER = "license/FileWithLicenseHeaderAndPlaceholder.test";
4040
// Licenses to test $YEAR token replacement
4141
private static final String LICENSE_HEADER_YEAR = "This is a fake license, $YEAR. ACME corp.";
42+
// License to test $today.year token replacement
43+
private static final String LICENSE_HEADER_YEAR_INTELLIJ_TOKEN = "This is a fake license, $today.year. ACME corp.";
4244
// Special case where the characters immediately before and after the year token are the same,
4345
// start position of the second part might overlap the end position of the first part.
4446
private static final String LICENSE_HEADER_YEAR_VARIANT = "This is a fake license. Copyright $YEAR ACME corp.";
@@ -82,6 +84,12 @@ public void should_apply_license_containing_YEAR_token() throws Throwable {
8284
.test(fileWithLicenseContaining(" ACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()))
8385
.test(fileWithLicenseContaining("This is a fake license. Copyright ACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()))
8486
.test(fileWithLicenseContaining("This is a fake license. CopyrightACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()));
87+
88+
//Check when token is of the format $today.year
89+
step = LicenseHeaderStep.createFromFile(createLicenseWith(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER);
90+
91+
StepHarness.forStep(step)
92+
.test(fileWithLicenseContaining(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN), fileWithLicenseContaining(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN, currentYear(), "$today.year"));
8593
}
8694

8795
@Test
@@ -126,6 +134,10 @@ private String fileWithLicenseContaining(String license, String yearContent) thr
126134
return getTestResource(KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER).replace("__LICENSE_PLACEHOLDER__", license).replace("$YEAR", yearContent);
127135
}
128136

137+
private String fileWithLicenseContaining(String license, String yearContent, String token) throws IOException {
138+
return getTestResource(KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER).replace("__LICENSE_PLACEHOLDER__", license).replace(token, yearContent);
139+
}
140+
129141
private String currentYear() {
130142
return String.valueOf(YearMonth.now().getYear());
131143
}

0 commit comments

Comments
 (0)