close
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
PR review
  • Loading branch information
baptistemesta committed Jan 2, 2018
commit d71cfd7190aa88c7601683c37e7c1e6b593e8f4a
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public final class LicenseHeaderStep implements Serializable {
private final Pattern delimiterPattern;
private Pattern yearMatcherPattern;
private boolean hasYearToken;
private String licenseHeaderBeforeYEARToken;
private String licenseHeaderAfterYEARToken;
private String licenseHeaderWithYEARTokenReplaced;
private String licenseHeaderBeforeYearToken;
private String licenseHeaderAfterYearToken;
private String licenseHeaderWithYearTokenReplaced;

/** Creates a FormatterStep which forces the start of each file to match a license header. */
public static FormatterStep createFromHeader(String licenseHeader, String delimiter) {
Expand Down Expand Up @@ -80,12 +80,12 @@ private LicenseHeaderStep(String licenseHeader, String delimiter) {
}
this.licenseHeader = licenseHeader;
this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE);
hasYearToken = licenseHeader.contains("$YEAR");
if (hasYearToken) {
this.hasYearToken = licenseHeader.contains("$YEAR");
if (this.hasYearToken) {
int yearTokenIndex = licenseHeader.indexOf("$YEAR");
licenseHeaderBeforeYEARToken = licenseHeader.substring(0, yearTokenIndex);
licenseHeaderAfterYEARToken = licenseHeader.substring(yearTokenIndex + 5, licenseHeader.length());
licenseHeaderWithYEARTokenReplaced = licenseHeader.replace("$YEAR", String.valueOf(YearMonth.now().getYear()));
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.yearMatcherPattern = Pattern.compile("[0-9]{4}(-[0-9]{4})?");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor niggle: I believe it would read a bit better if all field usages in this constructor were prepended with this..

For example, hasYearToken = ... refers to a field, but the hasYearToken part lacks a this., which makes it a bit inconsistent with the rest of the code in the constructor.

}
}
Expand All @@ -103,10 +103,10 @@ public String format(String raw) {
} else {
if (hasYearToken) {
if (matchesLicenseWithYearToken(raw, matcher)) {
//that means we have the license like `licenseHeaderBeforeYEARToken 1990-2015 licenseHeaderAfterYEARToken`
// that means we have the license like `licenseHeaderBeforeYearToken 1990-2015 licenseHeaderAfterYearToken`
return raw;
} else {
return licenseHeaderWithYEARTokenReplaced + raw.substring(matcher.start());
return licenseHeaderWithYearTokenReplaced + raw.substring(matcher.start());
}
} else if (matcher.start() == licenseHeader.length() && raw.startsWith(licenseHeader)) {
// if no change is required, return the raw string without
Expand All @@ -120,8 +120,8 @@ public String format(String raw) {
}

private boolean matchesLicenseWithYearToken(String raw, Matcher matcher) {
int startOfTheSecondPart = raw.indexOf(licenseHeaderAfterYEARToken);
return (raw.startsWith(licenseHeaderBeforeYEARToken) && startOfTheSecondPart + licenseHeaderAfterYEARToken.length() == matcher.start())
&& yearMatcherPattern.matcher(raw.substring(licenseHeaderBeforeYEARToken.length(), startOfTheSecondPart)).matches();
int startOfTheSecondPart = raw.indexOf(licenseHeaderAfterYearToken);
return (raw.startsWith(licenseHeaderBeforeYearToken) && startOfTheSecondPart + licenseHeaderAfterYearToken.length() == matcher.start())
&& yearMatcherPattern.matcher(raw.substring(licenseHeaderBeforeYearToken.length(), startOfTheSecondPart)).matches();
}
}
27 changes: 16 additions & 11 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ spotless {

## License header options

The license header can contains a `$YEAR` variable that will be replaced by the current year.
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.


For example:
```
Expand All @@ -287,16 +289,19 @@ will produce
```
/* Licensed under Apache-2.0 2017. */
```
if build is launched in 2017


The step will change the license according to the following rules
* It replace the license using the current year when
* The license is missing
* The license is not formatted correctly
* It will *not* replace the license when
* The year variable is already present and is a single year, e.g. `/* Licensed under Apache-2.0 1990. */`
* The year variable is already present and is a year span, e.g. `/* Licensed under Apache-2.0 1990-2003. */`
if Spotless is launched in 2017


The `licenseHeader` and `licenseHeaderFile` steps will generate license headers with automatic years from the base license header according to the following rules:
* A generated license header will be updated with the current year when
* the generated license header is missing
* the generated license header is not formatted correctly
* A generated license header will _not_ be updated when
* a single year is already present, e.g.
`/* Licensed under Apache-2.0 1990. */`
* a hyphen-separated year range is already present, e.g.
`/* Licensed under Apache-2.0 1990-2003. */`
* the `$YEAR` token is otherwise missing


<a name="custom"></a>
Expand Down