1
1
/*
2
- * Copyright 2016-2021 DiffPlug
2
+ * Copyright 2016-2023 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public static LicenseHeaderStep headerDelimiter(String header, String delimiter)
51
51
}
52
52
53
53
public static LicenseHeaderStep headerDelimiter (ThrowingEx .Supplier <String > headerLazy , String delimiter ) {
54
- return new LicenseHeaderStep (null , null , headerLazy , delimiter , DEFAULT_YEAR_DELIMITER , () -> YearMode .PRESERVE );
54
+ return new LicenseHeaderStep (null , null , headerLazy , delimiter , DEFAULT_YEAR_DELIMITER , () -> YearMode .PRESERVE , null );
55
55
}
56
56
57
57
final String name ;
@@ -60,50 +60,56 @@ public static LicenseHeaderStep headerDelimiter(ThrowingEx.Supplier<String> head
60
60
final String delimiter ;
61
61
final String yearSeparator ;
62
62
final Supplier <YearMode > yearMode ;
63
+ final @ Nullable String skipLinesMatching ;
63
64
64
- private LicenseHeaderStep (@ Nullable String name , @ Nullable String contentPattern , ThrowingEx .Supplier <String > headerLazy , String delimiter , String yearSeparator , Supplier <YearMode > yearMode ) {
65
+ private LicenseHeaderStep (@ Nullable String name , @ Nullable String contentPattern , ThrowingEx .Supplier <String > headerLazy , String delimiter , String yearSeparator , Supplier <YearMode > yearMode , @ Nullable String skipLinesMatching ) {
65
66
this .name = sanitizeName (name );
66
- this .contentPattern = sanitizeContentPattern (contentPattern );
67
+ this .contentPattern = sanitizePattern (contentPattern );
67
68
this .headerLazy = Objects .requireNonNull (headerLazy );
68
69
this .delimiter = Objects .requireNonNull (delimiter );
69
70
this .yearSeparator = Objects .requireNonNull (yearSeparator );
70
71
this .yearMode = Objects .requireNonNull (yearMode );
72
+ this .skipLinesMatching = sanitizePattern (skipLinesMatching );
71
73
}
72
74
73
75
public String getName () {
74
76
return name ;
75
77
}
76
78
77
79
public LicenseHeaderStep withName (String name ) {
78
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
80
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
79
81
}
80
82
81
83
public LicenseHeaderStep withContentPattern (String contentPattern ) {
82
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
84
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
83
85
}
84
86
85
87
public LicenseHeaderStep withHeaderString (String header ) {
86
88
return withHeaderLazy (() -> header );
87
89
}
88
90
89
91
public LicenseHeaderStep withHeaderLazy (ThrowingEx .Supplier <String > headerLazy ) {
90
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
92
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
91
93
}
92
94
93
95
public LicenseHeaderStep withDelimiter (String delimiter ) {
94
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
96
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
95
97
}
96
98
97
99
public LicenseHeaderStep withYearSeparator (String yearSeparator ) {
98
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
100
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
99
101
}
100
102
101
103
public LicenseHeaderStep withYearMode (YearMode yearMode ) {
102
104
return withYearModeLazy (() -> yearMode );
103
105
}
104
106
105
107
public LicenseHeaderStep withYearModeLazy (Supplier <YearMode > yearMode ) {
106
- return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode );
108
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
109
+ }
110
+
111
+ public LicenseHeaderStep withSkipLinesMatching (@ Nullable String skipLinesMatching ) {
112
+ return new LicenseHeaderStep (name , contentPattern , headerLazy , delimiter , yearSeparator , yearMode , skipLinesMatching );
107
113
}
108
114
109
115
public FormatterStep build () {
@@ -112,7 +118,7 @@ public FormatterStep build() {
112
118
if (yearMode .get () == YearMode .SET_FROM_GIT ) {
113
119
formatterStep = FormatterStep .createNeverUpToDateLazy (name , () -> {
114
120
boolean updateYear = false ; // doesn't matter
115
- Runtime runtime = new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear );
121
+ Runtime runtime = new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear , skipLinesMatching );
116
122
return FormatterFunc .needsFile (runtime ::setLicenseHeaderYearsFromGitHistory );
117
123
});
118
124
} else {
@@ -130,7 +136,7 @@ public FormatterStep build() {
130
136
default :
131
137
throw new IllegalStateException (yearMode .toString ());
132
138
}
133
- return new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear );
139
+ return new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear , skipLinesMatching );
134
140
}, step -> step ::format );
135
141
}
136
142
@@ -156,18 +162,18 @@ private String sanitizeName(@Nullable String name) {
156
162
}
157
163
158
164
@ Nullable
159
- private String sanitizeContentPattern (@ Nullable String contentPattern ) {
160
- if (contentPattern == null ) {
161
- return contentPattern ;
165
+ private String sanitizePattern (@ Nullable String pattern ) {
166
+ if (pattern == null ) {
167
+ return pattern ;
162
168
}
163
169
164
- contentPattern = contentPattern .trim ();
170
+ pattern = pattern .trim ();
165
171
166
- if (contentPattern .isEmpty ()) {
172
+ if (pattern .isEmpty ()) {
167
173
return null ;
168
174
}
169
175
170
- return contentPattern ;
176
+ return pattern ;
171
177
}
172
178
173
179
private static final String DEFAULT_NAME_PREFIX = LicenseHeaderStep .class .getName ();
@@ -195,6 +201,7 @@ private static class Runtime implements Serializable {
195
201
private static final long serialVersionUID = 1475199492829130965L ;
196
202
197
203
private final Pattern delimiterPattern ;
204
+ private final @ Nullable Pattern skipLinesMatching ;
198
205
private final String yearSepOrFull ;
199
206
private final @ Nullable String yearToday ;
200
207
private final @ Nullable String beforeYear ;
@@ -203,7 +210,7 @@ private static class Runtime implements Serializable {
203
210
private final boolean licenseHeaderWithRange ;
204
211
205
212
/** The license that we'd like enforced. */
206
- private Runtime (String licenseHeader , String delimiter , String yearSeparator , boolean updateYearWithLatest ) {
213
+ private Runtime (String licenseHeader , String delimiter , String yearSeparator , boolean updateYearWithLatest , @ Nullable String skipLinesMatching ) {
207
214
if (delimiter .contains ("\n " )) {
208
215
throw new IllegalArgumentException ("The delimiter must not contain any newlines." );
209
216
}
@@ -213,6 +220,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
213
220
licenseHeader = licenseHeader + "\n " ;
214
221
}
215
222
this .delimiterPattern = Pattern .compile ('^' + delimiter , Pattern .UNIX_LINES | Pattern .MULTILINE );
223
+ this .skipLinesMatching = skipLinesMatching == null ? null : Pattern .compile (skipLinesMatching );
216
224
217
225
Optional <String > yearToken = getYearToken (licenseHeader );
218
226
if (yearToken .isPresent ()) {
@@ -254,6 +262,31 @@ private static Optional<String> getYearToken(String licenseHeader) {
254
262
255
263
/** Formats the given string. */
256
264
private String format (String raw ) {
265
+ if (skipLinesMatching == null ) {
266
+ return addOrUpdateLicenseHeader (raw );
267
+ } else {
268
+ String [] lines = raw .split ("\n " );
269
+ StringBuilder skippedLinesBuilder = new StringBuilder ();
270
+ StringBuilder remainingLinesBuilder = new StringBuilder ();
271
+ boolean lastMatched = true ;
272
+ for (String line : lines ) {
273
+ if (lastMatched ) {
274
+ Matcher matcher = skipLinesMatching .matcher (line );
275
+ if (matcher .find ()) {
276
+ skippedLinesBuilder .append (line ).append ('\n' );
277
+ } else {
278
+ remainingLinesBuilder .append (line ).append ('\n' );
279
+ lastMatched = false ;
280
+ }
281
+ } else {
282
+ remainingLinesBuilder .append (line ).append ('\n' );
283
+ }
284
+ }
285
+ return skippedLinesBuilder + addOrUpdateLicenseHeader (remainingLinesBuilder .toString ());
286
+ }
287
+ }
288
+
289
+ private String addOrUpdateLicenseHeader (String raw ) {
257
290
Matcher contentMatcher = delimiterPattern .matcher (raw );
258
291
if (!contentMatcher .find ()) {
259
292
throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
0 commit comments