Skip to content

Refactor the public API of LicenseHeaderStep #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support lazy evaluation of YearMode, which is required for the gradle…
… plugin to respond to `ratchetFrom` in the most natural way.
  • Loading branch information
nedtwigg committed Jun 30, 2020
commit e957de88d8d649b3a51de54f4d4b398500c17429
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