Skip to content

Commit

Permalink
Support lazy evaluation of YearMode, which is required for the gradle…
Browse files Browse the repository at this point in the history
… plugin to respond to `ratchetFrom` in the most natural way.
  • Loading branch information
nedtwigg committed Jun 30, 2020
1 parent 80670c8 commit e957de8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -49,15 +50,15 @@ public static LicenseHeaderStep headerDelimiter(String header, String delimiter)
}

public static LicenseHeaderStep headerDelimiter(ThrowingEx.Supplier<String> headerLazy, String delimiter) {
return new LicenseHeaderStep(headerLazy, delimiter, DEFAULT_YEAR_DELIMITER, YearMode.PRESERVE);
return new LicenseHeaderStep(headerLazy, delimiter, DEFAULT_YEAR_DELIMITER, () -> YearMode.PRESERVE);
}

final ThrowingEx.Supplier<String> headerLazy;
final String delimiter;
final String yearSeparator;
final YearMode yearMode;
final Supplier<YearMode> yearMode;

private LicenseHeaderStep(ThrowingEx.Supplier<String> headerLazy, String delimiter, String yearSeparator, YearMode yearMode) {
private LicenseHeaderStep(ThrowingEx.Supplier<String> headerLazy, String delimiter, String yearSeparator, Supplier<YearMode> yearMode) {
this.headerLazy = Objects.requireNonNull(headerLazy);
this.delimiter = Objects.requireNonNull(delimiter);
this.yearSeparator = Objects.requireNonNull(yearSeparator);
Expand All @@ -81,6 +82,10 @@ public LicenseHeaderStep withYearSeparator(String yearSeparator) {
}

public LicenseHeaderStep withYearMode(YearMode yearMode) {
return withYearModeLazy(() -> yearMode);
}

public LicenseHeaderStep withYearModeLazy(Supplier<YearMode> yearMode) {
return new LicenseHeaderStep(headerLazy, delimiter, yearSeparator, yearMode);
}

Expand All @@ -103,7 +108,7 @@ public String apply(String input) throws Exception {
}

public FormatterStep build() {
if (yearMode == YearMode.SET_FROM_GIT) {
if (yearMode.get() == YearMode.SET_FROM_GIT) {
return FormatterStep.createNeverUpToDateLazy(LicenseHeaderStep.name(), () -> {
boolean updateYear = false; // doesn't matter
Runtime step = new Runtime(headerLazy.get(), delimiter, yearSeparator, updateYear);
Expand All @@ -113,7 +118,7 @@ public FormatterStep build() {
return FormatterStep.createLazy(LicenseHeaderStep.name(), () -> {
// by default, we should update the year if the user is using ratchetFrom
boolean updateYear;
switch (yearMode) {
switch (yearMode.get()) {
case PRESERVE:
updateYear = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ public LicenseHeaderConfig updateYearWithLatest(boolean updateYearWithLatest) {
}

FormatterStep createStep() {
YearMode yearMode;
if ("true".equals(spotless.project.findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
yearMode = YearMode.SET_FROM_GIT;
} else {
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;
yearMode = updateYear ? YearMode.UPDATE_TO_TODAY : YearMode.PRESERVE;
}
return builder.withYearMode(yearMode).build();
return builder.withYearModeLazy(() -> {
if ("true".equals(spotless.project.findProperty(LicenseHeaderStep.FLAG_SET_LICENSE_HEADER_YEARS_FROM_GIT_HISTORY()))) {
return YearMode.SET_FROM_GIT;
} else {
boolean updateYear = updateYearWithLatest == null ? getRatchetFrom() != null : updateYearWithLatest;
return updateYear ? YearMode.UPDATE_TO_TODAY : YearMode.PRESERVE;
}
}).build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void assertUnchanged(String year) throws IOException {

private void assertTransform(String yearBefore, String yearAfter) throws IOException {
setFile(TEST_JAVA).toContent("/** " + yearBefore + " */\n" + CONTENT);
gradleRunner().withArguments("spotlessApply", "--stacktrace").build();
gradleRunner().withArguments("spotlessApply", "--stacktrace").forwardOutput().build();
assertFile(TEST_JAVA).hasContent("/** " + yearAfter + " */\n" + CONTENT);
}

Expand Down

0 comments on commit e957de8

Please sign in to comment.