@@ -213,6 +213,7 @@ private static class Runtime implements Serializable {
213
213
private final @ Nullable String afterYear ;
214
214
private final boolean updateYearWithLatest ;
215
215
private final boolean licenseHeaderWithRange ;
216
+ private final boolean hasFileToken ;
216
217
217
218
private static final Pattern FILENAME_PATTERN = Pattern .compile ("\\ $FILE" );
218
219
@@ -228,6 +229,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
228
229
}
229
230
this .delimiterPattern = Pattern .compile ('^' + delimiter , Pattern .UNIX_LINES | Pattern .MULTILINE );
230
231
this .skipLinesMatching = skipLinesMatching == null ? null : Pattern .compile (skipLinesMatching );
232
+ this .hasFileToken = FILENAME_PATTERN .matcher (licenseHeader ).find ();
231
233
232
234
Optional <String > yearToken = getYearToken (licenseHeader );
233
235
if (yearToken .isPresent ()) {
@@ -294,6 +296,12 @@ private String format(String raw, File file) {
294
296
}
295
297
296
298
private String addOrUpdateLicenseHeader (String raw , File file ) {
299
+ raw = replaceYear (raw );
300
+ raw = replaceFileName (raw , file );
301
+ return raw ;
302
+ }
303
+
304
+ private String replaceYear (String raw ) {
297
305
Matcher contentMatcher = delimiterPattern .matcher (raw );
298
306
if (!contentMatcher .find ()) {
299
307
throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
@@ -307,14 +315,13 @@ private String addOrUpdateLicenseHeader(String raw, File file) {
307
315
return raw ;
308
316
} else {
309
317
// otherwise we'll have to add the header
310
- return replaceFileName ( yearSepOrFull , file ) + content ;
318
+ return yearSepOrFull + content ;
311
319
}
312
320
} else {
313
321
// the yes year case is a bit harder
314
322
int beforeYearIdx = raw .indexOf (beforeYear );
315
323
int afterYearIdx = raw .indexOf (afterYear , beforeYearIdx + beforeYear .length () + 1 );
316
324
317
- String header ;
318
325
if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear .length () <= contentMatcher .start ()) {
319
326
// and also ends with exactly the right header, so it's easy to parse the existing year
320
327
String existingYear = raw .substring (beforeYearIdx + beforeYear .length (), afterYearIdx );
@@ -323,16 +330,15 @@ private String addOrUpdateLicenseHeader(String raw, File file) {
323
330
// fastpath where we don't need to make any changes at all
324
331
boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear .length () == contentMatcher .start (); // allows fastpath return raw
325
332
if (noPadding ) {
326
- return replaceFileName ( raw . substring ( 0 , contentMatcher . start ()), file ) + content ;
333
+ return raw ;
327
334
}
328
335
}
329
- header = beforeYear + newYear + afterYear ;
336
+ return beforeYear + newYear + afterYear + content ;
330
337
} else {
331
338
String newYear = calculateYearBySearching (raw .substring (0 , contentMatcher .start ()));
332
339
// at worst, we just say that it was made today
333
- header = beforeYear + newYear + afterYear ;
340
+ return beforeYear + newYear + afterYear + content ;
334
341
}
335
- return replaceFileName (header , file ) + content ;
336
342
}
337
343
}
338
344
}
@@ -425,8 +431,17 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws
425
431
return beforeYear + yearRange + afterYear + raw .substring (contentMatcher .start ());
426
432
}
427
433
428
- private String replaceFileName (String header , File file ) {
429
- return FILENAME_PATTERN .matcher (header ).replaceAll (file .getName ());
434
+ private String replaceFileName (String raw , File file ) {
435
+ if (!hasFileToken ) {
436
+ return raw ;
437
+ }
438
+ Matcher contentMatcher = delimiterPattern .matcher (raw );
439
+ if (!contentMatcher .find ()) {
440
+ throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
441
+ }
442
+ String header = raw .substring (0 , contentMatcher .start ());
443
+ String content = raw .substring (contentMatcher .start ());
444
+ return FILENAME_PATTERN .matcher (header ).replaceAll (file .getName ()) + content ;
430
445
}
431
446
432
447
private static String parseYear (String cmd , File file ) throws IOException {
0 commit comments