Skip to content

Commit

Permalink
Enable support for intelliJ placeholder for year in license header
Browse files Browse the repository at this point in the history
  • Loading branch information
sameer-b authored and Sameer Balasubrahmanyam committed Mar 20, 2020
1 parent 2204946 commit 373b6f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -35,6 +38,7 @@ public final class LicenseHeaderStep implements Serializable {

private static final String NAME = "licenseHeader";
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");
Expand Down Expand Up @@ -107,16 +111,29 @@ private LicenseHeaderStep(String licenseHeader, String delimiter, String yearSep
}
this.licenseHeader = licenseHeader;
this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE);
this.hasYearToken = licenseHeader.contains("$YEAR");
if (this.hasYearToken) {
int yearTokenIndex = licenseHeader.indexOf("$YEAR");

if (getToken(licenseHeader).isPresent()) {
String token = getToken(licenseHeader).get();
this.hasYearToken = true;
int yearTokenIndex = licenseHeader.indexOf(token);
this.licenseHeaderBeforeYearToken = licenseHeader.substring(0, yearTokenIndex);
this.licenseHeaderAfterYearToken = licenseHeader.substring(yearTokenIndex + 5, licenseHeader.length());
this.licenseHeaderWithYearTokenReplaced = licenseHeader.replace("$YEAR", String.valueOf(YearMonth.now().getYear()));
this.licenseHeaderAfterYearToken = licenseHeader.substring(yearTokenIndex + 5);
this.licenseHeaderWithYearTokenReplaced = licenseHeader.replace(token, String.valueOf(YearMonth.now().getYear()));
this.yearMatcherPattern = Pattern.compile("[0-9]{4}(" + Pattern.quote(yearSeparator) + "[0-9]{4})?");
}
}

/**
* Get the first place holder token being used in the
* license header for specifying the year
*
* @param licenseHeader String representation of the license header
* @return Matching value from YEAR_TOKENS or null if none exist
*/
private Optional<String> getToken(String licenseHeader) {
return YEAR_TOKENS.stream().filter(licenseHeader::contains).findFirst();
}

/** Reads the license file from the given file. */
private LicenseHeaderStep(File licenseFile, Charset encoding, String delimiter, String yearSeparator) throws IOException {
this(new String(Files.readAllBytes(licenseFile.toPath()), encoding), delimiter, yearSeparator);
Expand Down
4 changes: 2 additions & 2 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ to true.

## License header options

If the string contents of a licenseHeader step or the file contents of a licenseHeaderFile step contains a $YEAR token,
then in the end-result generated license headers which use this license header as a template, $YEAR will be replaced with the current year.
If the string contents of a licenseHeader step or the file contents of a licenseHeaderFile step contains a $YEAR OR $today.year token,
then in the end-result generated license headers which use this license header as a template, $YEAR or $today.year will be replaced with the current year.


For example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class LicenseHeaderStepTest extends ResourceHarness {
private static final String KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER = "license/FileWithLicenseHeaderAndPlaceholder.test";
// Licenses to test $YEAR token replacement
private static final String LICENSE_HEADER_YEAR = "This is a fake license, $YEAR. ACME corp.";
// License to test $today.year token replacement
private static final String LICENSE_HEADER_YEAR_INTELLIJ_TOKEN = "This is a fake license, $today.year. ACME corp.";
// Special case where the characters immediately before and after the year token are the same,
// start position of the second part might overlap the end position of the first part.
private static final String LICENSE_HEADER_YEAR_VARIANT = "This is a fake license. Copyright $YEAR ACME corp.";
Expand Down Expand Up @@ -82,6 +84,12 @@ public void should_apply_license_containing_YEAR_token() throws Throwable {
.test(fileWithLicenseContaining(" ACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()))
.test(fileWithLicenseContaining("This is a fake license. Copyright ACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()))
.test(fileWithLicenseContaining("This is a fake license. CopyrightACME corp."), fileWithLicenseContaining(LICENSE_HEADER_YEAR_VARIANT, currentYear()));

//Check when token is of the format $today.year
step = LicenseHeaderStep.createFromFile(createLicenseWith(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN), StandardCharsets.UTF_8, LICENSE_HEADER_DELIMITER);

StepHarness.forStep(step)
.test(fileWithLicenseContaining(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN), fileWithLicenseContaining(LICENSE_HEADER_YEAR_INTELLIJ_TOKEN, currentYear(), "$today.year"));
}

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

private String fileWithLicenseContaining(String license, String yearContent, String token) throws IOException {
return getTestResource(KEY_FILE_WITH_LICENSE_AND_PLACEHOLDER).replace("__LICENSE_PLACEHOLDER__", license).replace(token, yearContent);
}

private String currentYear() {
return String.valueOf(YearMonth.now().getYear());
}
Expand Down

0 comments on commit 373b6f4

Please sign in to comment.