Skip to content
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

Convert SpotlessTaskModern to use InputChanges API #607

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
import java.util.stream.Collectors;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.incremental.IncrementalTaskInputs;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
import org.gradle.work.Incremental;
import org.gradle.work.InputChanges;

import com.diffplug.common.base.Errors;
import com.diffplug.common.base.Preconditions;
Expand All @@ -35,10 +42,17 @@

@CacheableTask
public class SpotlessTaskModern extends SpotlessTask {
@TaskAction
public void performAction(IncrementalTaskInputs inputs) throws Exception {
// TODO: implement using the InputChanges api

@PathSensitive(PathSensitivity.RELATIVE)
@Incremental
@InputFiles
@Override
public FileCollection getTarget() {
return super.getTarget();
}

@TaskAction
public void performAction(InputChanges inputs) throws Exception {
if (target == null) {
throw new GradleException("You must specify 'Iterable<File> target'");
}
Expand Down Expand Up @@ -68,27 +82,27 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
}

try (Formatter formatter = buildFormatter()) {
inputs.outOfDate(inputDetails -> {
File input = inputDetails.getFile();
try {
if (shouldInclude.test(input) && input.isFile()) {
processInputFile(formatter, input);
for (FileChange fileChange : inputs.getFileChanges(target)) {
File input = fileChange.getFile();
if (fileChange.getChangeType() == ChangeType.REMOVED) {
try {
if (shouldInclude.test(input)) {
deletePreviousResult(input);
}
} catch (IOException e) {
throw Errors.asRuntime(e);
}
} catch (IOException e) {
throw Errors.asRuntime(e);
}
});
}

inputs.removed(removedDetails -> {
File input = removedDetails.getFile();
try {
if (shouldInclude.test(input)) {
deletePreviousResult(input);
} else {
try {
if (shouldInclude.test(input) && input.isFile()) {
processInputFile(formatter, input);
}
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}
} catch (IOException e) {
throw Errors.asRuntime(e);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class ConfigAvoidanceTest extends GradleIntegrationHarness {
protected final GradleRunner gradleRunnerConfigAvoidance() throws IOException {
return gradleRunner().withGradleVersion(SpotlessPluginPreConfigAvoidance.CONFIG_AVOIDANCE_INTRODUCED.getVersion());
return gradleRunner().withGradleVersion(GradleVersionSupport.CONFIG_AVOIDANCE.version);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ private void runWithSuccess(String... messages) throws Exception {
if (JreVersion.thisVm() != JreVersion._8) {
return;
}
BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").build();
BuildResult result = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version).withArguments("check").build();
assertResultAndMessages(result, TaskOutcome.SUCCESS, messages);
}

private void runWithFailure(String... messages) throws Exception {
if (JreVersion.thisVm() != JreVersion._8) {
return;
}
BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").buildAndFail();
BuildResult result = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version).withArguments("check").buildAndFail();
assertResultAndMessages(result, TaskOutcome.FAILED, messages);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@
import com.diffplug.spotless.ResourceHarness;

public class GradleIntegrationHarness extends ResourceHarness {
public enum GradleVersionSupport {
LEGACY("2.14"), KOTLIN("4.0"), CONFIG_AVOIDANCE("4.9"), MODERN(SpotlessPluginModern.MINIMUM_GRADLE), SETTINGS_PLUGINS("6.0");

final String version;

GradleVersionSupport(String version) {
this.version = adaptGradleVersionForJdk(adaptGradleVersionForModern(version));
}
}

protected static String adaptGradleVersionForModern(String ver) {
if ("true".equals(System.getProperty(SpotlessPluginModern.SPOTLESS_MODERN))) {
return Double.parseDouble(ver) < Double.parseDouble(SpotlessPluginModern.MINIMUM_GRADLE) ? SpotlessPluginModern.MINIMUM_GRADLE : ver;
}
return ver;
}

/**
* For Java 11+, Gradle 5 is the minimum.
* So if you ask for less than Gradle 5, you get it on Java 8, but on Java 11 you get promoted to Gradle 5.
* If you ask for more than Gradle 5, you'll definitely get it.
*/
protected static String adaptGradleVersionForJdk(String ver) {
JreVersion jre = JreVersion.thisVm();
// @formatter:off
switch (jre) {
case _8: return ver;
case _11: return Double.parseDouble(ver) < 5.0 ? "5.0" : ver;
default: throw new IllegalStateException("Spotless build is only supported on Java 8 and Java 11");
}
// @formatter:on
}

/**
* Each test gets its own temp folder, and we create a gradle
* build there and run it.
Expand All @@ -58,30 +91,14 @@ public void gitAttributes() throws IOException {
setFile(".gitattributes").toContent("* text eol=lf");
}

/**
* For Java 11+, Gradle 5 is the minimum.
* So if you ask for less than Gradle 5, you get it on Java 8, but on Java 11 you get promoted to Gradle 5.
* If you ask for more than Gradle 5, you'll definitely get it.
*/
protected static String requestGradleForJre8and11(String ver) {
JreVersion jre = JreVersion.thisVm();
// @formatter:off
switch (jre) {
case _8: return ver;
case _11: return Double.parseDouble(ver) < 5.0 ? "5.0" : ver;
default: throw new IllegalStateException("Spotless build is only supported on Java 8 and Java 11");
}
// @formatter:on
}

protected final GradleRunner gradleRunner() throws IOException {
GradleRunner runner = GradleRunner.create()
.withGradleVersion(requestGradleForJre8and11("2.14"))
.withGradleVersion(GradleVersionSupport.LEGACY.version)
.withProjectDir(rootFolder())
.withPluginClasspath();
if ("true".equals(System.getProperty(SpotlessPluginModern.SPOTLESS_MODERN))) {
runner.withEnvironment(ImmutableMap.of("ORG_GRADLE_PROJECT_" + SpotlessPluginModern.SPOTLESS_MODERN, "true"));
runner.withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE);
runner.withGradleVersion(GradleVersionSupport.MODERN.version);
}
return runner;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void runWith(String... arguments) throws IOException {
// gradle 4.7 -> 5.1 don't work in tooling API because of https://github.com/gradle/gradle/issues/7617
// gradle 5.1 -> current confirmed to work
gradleRunner()
.withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE)
.withGradleVersion(GradleVersionSupport.MODERN.version)
.withProjectDir(rootFolder())
.withPluginClasspath()
.withArguments(arguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void failureDoesntTriggerAll() throws IOException {
}

private final GradleRunner gradleRunner6() throws IOException {
return gradleRunner().withGradleVersion("6.0");
return gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ private void assertDirty() throws Exception {
}

private BuildResultAssertion assertPass(String... tasks) throws Exception {
return new BuildResultAssertion(gradleRunner().withGradleVersion("6.0").withArguments(tasks).build());
return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).build());
}

private BuildResultAssertion assertFail(String... tasks) throws Exception {
return new BuildResultAssertion(gradleRunner().withGradleVersion("6.0").withArguments(tasks).buildAndFail());
return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).buildAndFail());
}

private static final String BASELINE_ROOT = "ebb03d6940ee0254010e71917735efa203c27e16";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void registerDependencies() throws IOException {
"}");

setFile("gradle.properties").toLines();
String newestSupported = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE)
String newestSupported = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version)
.withArguments("spotlessCheck").build().getOutput();
Assertions.assertThat(newestSupported.replace("\r", ""))
.startsWith("> Task :spotlessCheck UP-TO-DATE\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void integration(String patterns,
GradleRunner runner = gradleRunner()
.withArguments("spotlessApply", "-PspotlessFiles=" + patterns);
if (isKotlin) {
runner.withGradleVersion(requestGradleForJre8and11("4.0"));
runner.withGradleVersion(GradleVersionSupport.KOTLIN.version);
}
runner.build();

Expand Down