18
18
import static com .diffplug .gradle .spotless .PluginGradlePreconditions .requireElementsNonNull ;
19
19
20
20
import java .io .File ;
21
- import java .io .IOException ;
22
21
import java .io .Serializable ;
23
22
import java .nio .charset .Charset ;
24
23
import java .nio .file .Files ;
47
46
import com .diffplug .spotless .generic .EndWithNewlineStep ;
48
47
import com .diffplug .spotless .generic .IndentStep ;
49
48
import com .diffplug .spotless .generic .LicenseHeaderStep ;
49
+ import com .diffplug .spotless .generic .LicenseHeaderStep .YearMode ;
50
50
import com .diffplug .spotless .generic .ReplaceRegexStep ;
51
51
import com .diffplug .spotless .generic .ReplaceStep ;
52
52
import com .diffplug .spotless .generic .TrimTrailingWhitespaceStep ;
@@ -443,21 +443,20 @@ public void indentWithTabs() {
443
443
* For most language-specific formats (e.g. java, scala, etc.) you can omit the second `delimiter` argument, because it is supplied
444
444
* automatically ({@link HasBuiltinDelimiterForLicense}).
445
445
*/
446
- public abstract class LicenseHeaderConfig {
447
- String delimiter ;
448
- String yearSeparator = LicenseHeaderStep .defaultYearDelimiter ();
446
+ public class LicenseHeaderConfig {
447
+ LicenseHeaderStep builder ;
449
448
Boolean updateYearWithLatest = null ;
450
449
451
- public LicenseHeaderConfig (String delimiter ) {
452
- this .delimiter = Objects . requireNonNull ( delimiter , "delimiter" ) ;
450
+ public LicenseHeaderConfig (LicenseHeaderStep builder ) {
451
+ this .builder = builder ;
453
452
}
454
453
455
454
/**
456
455
* @param delimiter
457
456
* Spotless will look for a line that starts with this regular expression pattern to know what the "top" is.
458
457
*/
459
458
public LicenseHeaderConfig delimiter (String delimiter ) {
460
- this . delimiter = Objects . requireNonNull (delimiter , "delimiter" );
459
+ builder = builder . withDelimiter (delimiter );
461
460
replaceStep (createStep ());
462
461
return this ;
463
462
}
@@ -467,7 +466,7 @@ public LicenseHeaderConfig delimiter(String delimiter) {
467
466
* The characters used to separate the first and last years in multi years patterns.
468
467
*/
469
468
public LicenseHeaderConfig yearSeparator (String yearSeparator ) {
470
- this . yearSeparator = Objects . requireNonNull (yearSeparator , "yearSeparator" );
469
+ builder = builder . withYearSeparator (yearSeparator );
471
470
replaceStep (createStep ());
472
471
return this ;
473
472
}
@@ -483,61 +482,15 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
483
482
return this ;
484
483
}
485
484
486
- protected abstract String licenseHeader () throws IOException ;
487
-
488
485
FormatterStep createStep () {
489
- if ("true" .equals (spotless .project .findProperty (LicenseHeaderStep .FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY ()))) {
490
- return FormatterStep .createNeverUpToDateLazy (LicenseHeaderStep .name (), () -> {
491
- boolean updateYear = false ; // doesn't matter
492
- LicenseHeaderStep step = new LicenseHeaderStep (licenseHeader (), delimiter , yearSeparator , updateYear );
493
- return new FormatterFunc () {
494
- @ Override
495
- public String apply (String input , File source ) throws Exception {
496
- return step .setLicenseHeaderYearsFromGitHistory (input , source );
497
- }
498
-
499
- @ Override
500
- public String apply (String input ) throws Exception {
501
- throw new UnsupportedOperationException ();
502
- }
503
- };
504
- });
505
- } else {
506
- return FormatterStep .createLazy (LicenseHeaderStep .name (), () -> {
507
- // by default, we should update the year if the user is using ratchetFrom
486
+ return builder .withYearModeLazy (() -> {
487
+ if ("true" .equals (spotless .project .findProperty (LicenseHeaderStep .FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY ()))) {
488
+ return YearMode .SET_FROM_GIT ;
489
+ } else {
508
490
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom () != null : updateYearWithLatest ;
509
- return new LicenseHeaderStep (licenseHeader (), delimiter , yearSeparator , updateYear );
510
- }, step -> step ::format );
511
- }
512
- }
513
- }
514
-
515
- private class LicenseStringHeaderConfig extends LicenseHeaderConfig {
516
- private String header ;
517
-
518
- LicenseStringHeaderConfig (String delimiter , String header ) {
519
- super (delimiter );
520
- this .header = Objects .requireNonNull (header , "header" );
521
- }
522
-
523
- @ Override
524
- protected String licenseHeader () {
525
- return header ;
526
- }
527
- }
528
-
529
- private class LicenseFileHeaderConfig extends LicenseHeaderConfig {
530
- private Object headerFile ;
531
-
532
- LicenseFileHeaderConfig (String delimiter , Object headerFile ) {
533
- super (delimiter );
534
- this .headerFile = Objects .requireNonNull (headerFile , "headerFile" );
535
- }
536
-
537
- @ Override
538
- protected String licenseHeader () throws IOException {
539
- byte [] content = Files .readAllBytes (getProject ().file (headerFile ).toPath ());
540
- return new String (content , getEncoding ());
491
+ return updateYear ? YearMode .UPDATE_TO_TODAY : YearMode .PRESERVE ;
492
+ }
493
+ }).build ();
541
494
}
542
495
}
543
496
@@ -548,7 +501,7 @@ protected String licenseHeader() throws IOException {
548
501
* Spotless will look for a line that starts with this regular expression pattern to know what the "top" is.
549
502
*/
550
503
public LicenseHeaderConfig licenseHeader (String licenseHeader , String delimiter ) {
551
- LicenseHeaderConfig config = new LicenseStringHeaderConfig ( delimiter , licenseHeader );
504
+ LicenseHeaderConfig config = new LicenseHeaderConfig ( LicenseHeaderStep . headerDelimiter ( licenseHeader , delimiter ) );
552
505
addStep (config .createStep ());
553
506
return config ;
554
507
}
@@ -560,7 +513,11 @@ public LicenseHeaderConfig licenseHeader(String licenseHeader, String delimiter)
560
513
* Spotless will look for a line that starts with this regular expression pattern to know what the "top" is.
561
514
*/
562
515
public LicenseHeaderConfig licenseHeaderFile (Object licenseHeaderFile , String delimiter ) {
563
- LicenseHeaderConfig config = new LicenseFileHeaderConfig (delimiter , licenseHeaderFile );
516
+ LicenseHeaderConfig config = new LicenseHeaderConfig (LicenseHeaderStep .headerDelimiter (() -> {
517
+ File file = getProject ().file (licenseHeaderFile );
518
+ byte [] data = Files .readAllBytes (file .toPath ());
519
+ return new String (data , getEncoding ());
520
+ }, delimiter ));
564
521
addStep (config .createStep ());
565
522
return config ;
566
523
}
0 commit comments