20
20
import java .io .Serializable ;
21
21
import java .nio .charset .Charset ;
22
22
import java .nio .file .Files ;
23
+ import java .time .YearMonth ;
23
24
import java .util .Objects ;
24
25
import java .util .regex .Matcher ;
25
26
import java .util .regex .Pattern ;
@@ -35,6 +36,11 @@ public final class LicenseHeaderStep implements Serializable {
35
36
36
37
private final String licenseHeader ;
37
38
private final Pattern delimiterPattern ;
39
+ private Pattern yearMatcherPattern ;
40
+ private boolean hasYearToken ;
41
+ private String licenseHeaderBeforeYEARToken ;
42
+ private String licenseHeaderAfterYEARToken ;
43
+ private String licenseHeaderWithYEARTokenReplaced ;
38
44
39
45
/** Creates a FormatterStep which forces the start of each file to match a license header. */
40
46
public static FormatterStep createFromHeader (String licenseHeader , String delimiter ) {
@@ -74,6 +80,14 @@ private LicenseHeaderStep(String licenseHeader, String delimiter) {
74
80
}
75
81
this .licenseHeader = licenseHeader ;
76
82
this .delimiterPattern = Pattern .compile ('^' + delimiter , Pattern .UNIX_LINES | Pattern .MULTILINE );
83
+ hasYearToken = licenseHeader .contains ("$YEAR" );
84
+ if (hasYearToken ) {
85
+ int yearTokenIndex = licenseHeader .indexOf ("$YEAR" );
86
+ licenseHeaderBeforeYEARToken = licenseHeader .substring (0 , yearTokenIndex );
87
+ licenseHeaderAfterYEARToken = licenseHeader .substring (yearTokenIndex + 5 , licenseHeader .length ());
88
+ licenseHeaderWithYEARTokenReplaced = licenseHeader .replace ("$YEAR" , String .valueOf (YearMonth .now ().getYear ()));
89
+ this .yearMatcherPattern = Pattern .compile ("[0-9]{4}(-[0-9]{4})?" );
90
+ }
77
91
}
78
92
79
93
/** Reads the license file from the given file. */
@@ -87,7 +101,14 @@ public String format(String raw) {
87
101
if (!matcher .find ()) {
88
102
throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
89
103
} else {
90
- if (matcher .start () == licenseHeader .length () && raw .startsWith (licenseHeader )) {
104
+ if (hasYearToken ) {
105
+ if (matchesLicenseWithYearToken (raw , matcher )) {
106
+ //that means we have the license like `licenseHeaderBeforeYEARToken 1990-2015 licenseHeaderAfterYEARToken`
107
+ return raw ;
108
+ } else {
109
+ return licenseHeaderWithYEARTokenReplaced + raw .substring (matcher .start ());
110
+ }
111
+ } else if (matcher .start () == licenseHeader .length () && raw .startsWith (licenseHeader )) {
91
112
// if no change is required, return the raw string without
92
113
// creating any other new strings for maximum performance
93
114
return raw ;
@@ -97,4 +118,10 @@ public String format(String raw) {
97
118
}
98
119
}
99
120
}
121
+
122
+ private boolean matchesLicenseWithYearToken (String raw , Matcher matcher ) {
123
+ int startOfTheSecondPart = raw .indexOf (licenseHeaderAfterYEARToken );
124
+ return (raw .startsWith (licenseHeaderBeforeYEARToken ) && startOfTheSecondPart + licenseHeaderAfterYEARToken .length () == matcher .start ())
125
+ && yearMatcherPattern .matcher (raw .substring (licenseHeaderBeforeYEARToken .length (), startOfTheSecondPart )).matches ();
126
+ }
100
127
}
0 commit comments