Skip to content

Commit d4d3814

Browse files
committed
Refactor LicenseHeaderStep
1 parent 318f453 commit d4d3814

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private static class Runtime implements Serializable {
213213
private final @Nullable String afterYear;
214214
private final boolean updateYearWithLatest;
215215
private final boolean licenseHeaderWithRange;
216+
private final boolean hasFileToken;
216217

217218
private static final Pattern FILENAME_PATTERN = Pattern.compile("\\$FILE");
218219

@@ -228,6 +229,7 @@ private Runtime(String licenseHeader, String delimiter, String yearSeparator, bo
228229
}
229230
this.delimiterPattern = Pattern.compile('^' + delimiter, Pattern.UNIX_LINES | Pattern.MULTILINE);
230231
this.skipLinesMatching = skipLinesMatching == null ? null : Pattern.compile(skipLinesMatching);
232+
this.hasFileToken = FILENAME_PATTERN.matcher(licenseHeader).find();
231233

232234
Optional<String> yearToken = getYearToken(licenseHeader);
233235
if (yearToken.isPresent()) {
@@ -294,6 +296,12 @@ private String format(String raw, File file) {
294296
}
295297

296298
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) {
297305
Matcher contentMatcher = delimiterPattern.matcher(raw);
298306
if (!contentMatcher.find()) {
299307
throw new IllegalArgumentException("Unable to find delimiter regex " + delimiterPattern);
@@ -307,14 +315,13 @@ private String addOrUpdateLicenseHeader(String raw, File file) {
307315
return raw;
308316
} else {
309317
// otherwise we'll have to add the header
310-
return replaceFileName(yearSepOrFull, file) + content;
318+
return yearSepOrFull + content;
311319
}
312320
} else {
313321
// the yes year case is a bit harder
314322
int beforeYearIdx = raw.indexOf(beforeYear);
315323
int afterYearIdx = raw.indexOf(afterYear, beforeYearIdx + beforeYear.length() + 1);
316324

317-
String header;
318325
if (beforeYearIdx >= 0 && afterYearIdx >= 0 && afterYearIdx + afterYear.length() <= contentMatcher.start()) {
319326
// and also ends with exactly the right header, so it's easy to parse the existing year
320327
String existingYear = raw.substring(beforeYearIdx + beforeYear.length(), afterYearIdx);
@@ -323,16 +330,15 @@ private String addOrUpdateLicenseHeader(String raw, File file) {
323330
// fastpath where we don't need to make any changes at all
324331
boolean noPadding = beforeYearIdx == 0 && afterYearIdx + afterYear.length() == contentMatcher.start(); // allows fastpath return raw
325332
if (noPadding) {
326-
return replaceFileName(raw.substring(0, contentMatcher.start()), file) + content;
333+
return raw;
327334
}
328335
}
329-
header = beforeYear + newYear + afterYear;
336+
return beforeYear + newYear + afterYear + content;
330337
} else {
331338
String newYear = calculateYearBySearching(raw.substring(0, contentMatcher.start()));
332339
// at worst, we just say that it was made today
333-
header = beforeYear + newYear + afterYear;
340+
return beforeYear + newYear + afterYear + content;
334341
}
335-
return replaceFileName(header, file) + content;
336342
}
337343
}
338344
}
@@ -425,8 +431,17 @@ private String setLicenseHeaderYearsFromGitHistory(String raw, File file) throws
425431
return beforeYear + yearRange + afterYear + raw.substring(contentMatcher.start());
426432
}
427433

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;
430445
}
431446

432447
private static String parseYear(String cmd, File file) throws IOException {

0 commit comments

Comments
 (0)