@@ -142,7 +142,7 @@ public FormatterStep build() {
142
142
throw new IllegalStateException (yearMode .toString ());
143
143
}
144
144
return new Runtime (headerLazy .get (), delimiter , yearSeparator , updateYear , skipLinesMatching );
145
- }, step -> step ::format );
145
+ }, step -> FormatterFunc . needsFile ( step ::format ) );
146
146
}
147
147
148
148
if (contentPattern == null ) {
@@ -214,6 +214,8 @@ private static class Runtime implements Serializable {
214
214
private final boolean updateYearWithLatest ;
215
215
private final boolean licenseHeaderWithRange ;
216
216
217
+ private static final Pattern FILENAME_PATTERN = Pattern .compile ("\\ $FILE" );
218
+
217
219
/** The license that we'd like enforced. */
218
220
private Runtime (String licenseHeader , String delimiter , String yearSeparator , boolean updateYearWithLatest , @ Nullable String skipLinesMatching ) {
219
221
if (delimiter .contains ("\n " )) {
@@ -266,9 +268,9 @@ private static Optional<String> getYearToken(String licenseHeader) {
266
268
}
267
269
268
270
/** Formats the given string. */
269
- private String format (String raw ) {
271
+ private String format (String raw , File file ) {
270
272
if (skipLinesMatching == null ) {
271
- return addOrUpdateLicenseHeader (raw );
273
+ return addOrUpdateLicenseHeader (raw , file );
272
274
} else {
273
275
String [] lines = raw .split ("\n " );
274
276
StringBuilder skippedLinesBuilder = new StringBuilder ();
@@ -287,11 +289,11 @@ private String format(String raw) {
287
289
remainingLinesBuilder .append (line ).append ('\n' );
288
290
}
289
291
}
290
- return skippedLinesBuilder + addOrUpdateLicenseHeader (remainingLinesBuilder .toString ());
292
+ return skippedLinesBuilder + addOrUpdateLicenseHeader (remainingLinesBuilder .toString (), file );
291
293
}
292
294
}
293
295
294
- private String addOrUpdateLicenseHeader (String raw ) {
296
+ private String addOrUpdateLicenseHeader (String raw , File file ) {
295
297
Matcher contentMatcher = delimiterPattern .matcher (raw );
296
298
if (!contentMatcher .find ()) {
297
299
throw new IllegalArgumentException ("Unable to find delimiter regex " + delimiterPattern );
@@ -305,13 +307,14 @@ private String addOrUpdateLicenseHeader(String raw) {
305
307
return raw ;
306
308
} else {
307
309
// otherwise we'll have to add the header
308
- return yearSepOrFull + content ;
310
+ return replaceFileName ( yearSepOrFull , file ) + content ;
309
311
}
310
312
} else {
311
313
// the yes year case is a bit harder
312
314
int beforeYearIdx = raw .indexOf (beforeYear );
313
315
int afterYearIdx = raw .indexOf (afterYear , beforeYearIdx + beforeYear .length () + 1 );
314
316
317
+ String header ;
315
318
if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear .length () <= contentMatcher .start ()) {
316
319
// and also ends with exactly the right header, so it's easy to parse the existing year
317
320
String existingYear = raw .substring (beforeYearIdx + beforeYear .length (), afterYearIdx );
@@ -323,12 +326,13 @@ private String addOrUpdateLicenseHeader(String raw) {
323
326
return raw ;
324
327
}
325
328
}
326
- return beforeYear + newYear + afterYear + content ;
329
+ header = beforeYear + newYear + afterYear ;
327
330
} else {
328
331
String newYear = calculateYearBySearching (raw .substring (0 , contentMatcher .start ()));
329
332
// at worst, we just say that it was made today
330
- return beforeYear + newYear + afterYear + content ;
333
+ header = beforeYear + newYear + afterYear ;
331
334
}
335
+ return replaceFileName (header , file ) + content ;
332
336
}
333
337
}
334
338
}
@@ -421,6 +425,10 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws
421
425
return beforeYear + yearRange + afterYear + raw .substring (contentMatcher .start ());
422
426
}
423
427
428
+ private String replaceFileName (String header , File file ) {
429
+ return FILENAME_PATTERN .matcher (header ).replaceAll (file .getName ());
430
+ }
431
+
424
432
private static String parseYear (String cmd , File file ) throws IOException {
425
433
String fullCmd = cmd + " -- " + file .getAbsolutePath ();
426
434
ProcessBuilder builder = new ProcessBuilder ().directory (file .getParentFile ());
0 commit comments